You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ntk-console.c 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /* This file is part of Netsukuku
  2. *
  3. * This source code is free software; you can redistribute it and/or
  4. * modify it under the terms of the GNU General Public License as published
  5. * by the Free Software Foundation; either version 2 of the License,
  6. * or (at your option) any later version.
  7. *
  8. * This source code is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. * Please refer to the GNU Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Public License along with
  14. * this source code; if not, write to:
  15. * Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. *
  17. */
  18. #include "ntk-console.h"
  19. #include "console.h"
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <sys/socket.h>
  24. #include <sys/un.h>
  25. #include <time.h>
  26. #include <unistd.h>
  27. const struct supported_commands {
  28. command_t id;
  29. const char *command;
  30. const char *help;
  31. int arguments;
  32. } kSupportedCommands[] = {
  33. {
  34. COMMAND_HELP, "help", "Shows console help", 0}, {
  35. COMMAND_UPTIME, "uptime",
  36. "Returns the time when ntkd finished the hooking", 0}, {
  37. COMMAND_KILL, "kill",
  38. "Kills the running instance of netsukuku with SIGINT", 0}, {
  39. COMMAND_VERSION, "version",
  40. "Shows the running version of the ntk-console, and ntkd.", 0},
  41. {
  42. COMMAND_INETCONN, "inet_connected",
  43. "Query if Ntkd is connected to the internet", 0}, {
  44. COMMAND_CURIFS, "cur_ifs",
  45. "Lists all of the interfaces in cur_ifs", 0}, {
  46. COMMAND_CURIFSCT, "cur_ifs_n",
  47. "Lists the number of interfaces present in `cur_ifs`", 0}, {
  48. COMMAND_CURQSPNID, "cur_qspn_id",
  49. "The current qspn_id we are processing. It is cur_qspn_id[levels] big",
  50. 0}, {
  51. COMMAND_CURIP, "cur_ip", "Current IP address", 0}, {
  52. COMMAND_CURNODE, "cur_node", "Current node", 0}, {
  53. COMMAND_IFS, "ifs", "List all the interfaces in server_opt.ifs", 0}, {
  54. COMMAND_IFSCT, "ifs_n",
  55. "List the number of interfaces present in server_opt.ifs", 0},
  56. {
  57. COMMAND_QUIT, "quit", "Exit the console", 0}, {
  58. COMMAND_CONSUPTIME, "console_uptime",
  59. "Get the uptime of this console", 0},};
  60. command_t
  61. command_parse(char *request)
  62. {
  63. if (strlen(request) > CONSOLE_BUFFER_LENGTH) {
  64. printf("Error: Command longer than 250 bytes.\n");
  65. return -1;
  66. }
  67. for (int i = 0; i < sizeof(kSupportedCommands)
  68. / sizeof(kSupportedCommands[0]); i++) {
  69. if (strncmp(request, kSupportedCommands[i].command,
  70. (int) strlen(request) - 1) == 0) {
  71. return kSupportedCommands[i].id;
  72. }
  73. }
  74. printf("Incorrect or unreadable command, Please correct it.\n");
  75. return -1;
  76. }
  77. static int
  78. request_receive(int sock, char message[], int max)
  79. {
  80. int total = 0;
  81. const int bsize = 1024;
  82. char buffer[bsize + 1];
  83. int read = bsize;
  84. message[0] = 0; // initialize for strcat()
  85. while (read == bsize) {
  86. read = recv(sock, buffer, bsize, 0);
  87. if (read < 0)
  88. return -1; // error, bail out
  89. total += read;
  90. if (total > max)
  91. return -2; // overflow
  92. buffer[read] = 0;
  93. strcat(message, buffer);
  94. }
  95. return total;
  96. }
  97. int
  98. opensocket(void)
  99. {
  100. sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
  101. if (sockfd < 0) {
  102. perror("socket creation failed");
  103. return -1;
  104. }
  105. memset(&serveraddr, 0, sizeof(serveraddr));
  106. serveraddr.sun_family = AF_UNIX;
  107. strcpy(serveraddr.sun_path, CONSOLE_SOCKET_PATH);
  108. rc = connect(sockfd, (struct sockaddr *) &serveraddr,
  109. sizeof(serveraddr));
  110. if (rc < 0) {
  111. perror("connect() failed");
  112. return -1;
  113. }
  114. return 0;
  115. }
  116. void
  117. closesocket(void)
  118. {
  119. const int optVal = 1;
  120. const socklen_t optLen = sizeof(optVal);
  121. setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (void *) &optVal, optLen);
  122. if (sockfd >= 0)
  123. close(sockfd);
  124. }
  125. /* Sends and receives to ntkd */
  126. void
  127. ntkd_request(command_t command)
  128. {
  129. if (opensocket() < 0) {
  130. printf("Unable to connect to ntk daemon console.\n");
  131. return;
  132. }
  133. cmd_packet_t packetOut;
  134. packetOut.command = command;
  135. rc = send(sockfd, &packetOut, sizeof(packetOut), 0);
  136. if (rc < sizeof(packetOut)) {
  137. perror("send() failed");
  138. exit(-1);
  139. }
  140. char *response = (char *) malloc(CONSOLE_BUFFER_LENGTH);
  141. request_receive(sockfd, response, CONSOLE_BUFFER_LENGTH);
  142. if (rc < 0) {
  143. perror("recv() failed");
  144. exit(-1);
  145. }
  146. printf("%s\n", response);
  147. free(response);
  148. closesocket();
  149. }
  150. void console_uptime(void) {
  151. unsigned int uptime_sec1;
  152. unsigned int uptime_min1;
  153. unsigned int uptime_hour1;
  154. unsigned int uptime_day1;
  155. unsigned int uptime_month1;
  156. unsigned int uptime_year1;
  157. time(&rawtime);
  158. timeinfo = localtime(&rawtime);
  159. uptime_sec1 = timeinfo->tm_sec;
  160. uptime_min1 = timeinfo->tm_min;
  161. uptime_hour1 = timeinfo->tm_hour;
  162. uptime_day1 = timeinfo->tm_mday;
  163. uptime_month1 = timeinfo->tm_mon;
  164. uptime_year1 = timeinfo->tm_year;
  165. uptime_sec1 -= uptime_sec;
  166. uptime_min1 -= uptime_min;
  167. uptime_hour1 -= uptime_hour;
  168. uptime_day1 -= uptime_day;
  169. uptime_month1 -= uptime_month;
  170. uptime_year1 -= uptime_year;
  171. /* Checks if the date/time is negative,
  172. * And makes it positive.
  173. * e.g -11 is 1 because this is a signed variable
  174. * at a modulus of 12. Thus after 12, It is -12
  175. * which is zero, Every number after this is counting up
  176. * to 12 again, Which is going to be 0 in this instance.
  177. * -12 to 12 is 24 actual months.
  178. * So -11 + 12 is 1, As it should be, And -12 + 12 is zero
  179. * as it should be. The only difference is the modulus number,
  180. * i.e: 12, 24, etc. */
  181. if(uptime_month1 < 0)
  182. uptime_month1 + 12;
  183. if(uptime_day1 < 0)
  184. uptime_day1 + 365;
  185. if(uptime_hour1 < 0)
  186. uptime_hour1 + 24;
  187. if(uptime_min1 < 0)
  188. uptime_min1 + 60;
  189. if(uptime_sec1 < 0)
  190. uptime_sec1 + 60;
  191. /* Checks if the date/time is the modulus, And resets it to zero.
  192. * e.g: 12 months is a year, Listing 12 months and one year
  193. * is confusing,
  194. * And implies that it has been two years. */
  195. if(uptime_month1 == 12)
  196. uptime_month1 = 0;
  197. if(uptime_day1 == 365)
  198. uptime_day1 = 0;
  199. if(uptime_hour1 == 24)
  200. uptime_hour1 = 0;
  201. if(uptime_min1 == 60)
  202. uptime_min1 = 0;
  203. if(uptime_sec1 == 60)
  204. uptime_sec1 = 0;
  205. printf("Total Uptime is: %i Year(s), %i Month(s), %i Day(s), %i Hour(s), %i Minute(s), %i Second(s)\n",uptime_year1, uptime_month1, uptime_day1, uptime_hour1, uptime_min1, uptime_sec1);
  206. }
  207. static void
  208. millisleep(unsigned ms)
  209. {
  210. usleep(1000 * ms);
  211. }
  212. void
  213. console(char *request)
  214. {
  215. command_t commandID = command_parse(request);
  216. switch (commandID) {
  217. case COMMAND_QUIT:
  218. closesocket();
  219. exit(0);
  220. break;
  221. case COMMAND_UPTIME:
  222. case COMMAND_INETCONN:
  223. case COMMAND_CURIFS:
  224. case COMMAND_CURIFSCT:
  225. case COMMAND_CURQSPNID:
  226. case COMMAND_CURIP:
  227. case COMMAND_CURNODE:
  228. case COMMAND_IFS:
  229. case COMMAND_IFSCT:
  230. ntkd_request(commandID);
  231. millisleep(200);
  232. break;
  233. case COMMAND_VERSION:
  234. printf("ntk-console version: %d.%d\n",
  235. CONSOLE_VERSION_MAJOR, CONSOLE_VERSION_MINOR);
  236. ntkd_request(commandID);
  237. break;
  238. case COMMAND_CONSUPTIME:
  239. console_uptime();
  240. break;
  241. case COMMAND_KILL:
  242. closesocket();
  243. system("ntkd -k");
  244. break;
  245. case COMMAND_HELP:
  246. default:
  247. usage();
  248. }
  249. }
  250. int
  251. main(void)
  252. {
  253. time(&rawtime);
  254. timeinfo = localtime(&rawtime);
  255. uptime_sec = timeinfo->tm_sec;
  256. uptime_min = timeinfo->tm_min;
  257. uptime_hour = timeinfo->tm_hour;
  258. uptime_day = timeinfo->tm_mday;
  259. uptime_month = timeinfo->tm_mon;
  260. uptime_year = timeinfo->tm_year;
  261. printf
  262. ("This is the Netsukuku Console. Please type 'help' for more information.\n");
  263. for (;;) {
  264. char *request = (char *) malloc(CONSOLE_BUFFER_LENGTH);
  265. printf("\n> ");
  266. fgets(request, 16, stdin);
  267. fflush(stdin);
  268. console(request);
  269. free(request);
  270. closesocket();
  271. }
  272. return 0;
  273. }
  274. void
  275. usage(void)
  276. {
  277. printf("Usage:\n");
  278. for (int i = 0; i < sizeof(kSupportedCommands)
  279. / sizeof(kSupportedCommands[0]); i++) {
  280. printf(" %16s - %s\n", kSupportedCommands[i].command,
  281. kSupportedCommands[i].help);
  282. }
  283. }