diff --git a/src/Ntk-Console/Netsukuku-Console.c b/src/Ntk-Console/Netsukuku-Console.c index c8f1f16..31a83da 100644 --- a/src/Ntk-Console/Netsukuku-Console.c +++ b/src/Ntk-Console/Netsukuku-Console.c @@ -56,7 +56,7 @@ int validity_check(char *request) { } -/* this function is run by the second thread */ +/* Sends and receives to ntkd */ void ntkd_request(char *request) { rc = sendto(sockfd1, request, strlen(request), 0, (struct sockaddr *)&serveraddr, (socklen_t)sizeof(&serveraddr)); @@ -79,6 +79,8 @@ void ntkd_request(char *request) { void opensocket(void) { + int stop_trying; + sockfd = socket(AF_UNIX, SOCK_STREAM, 0); if (sockfd < 0) { perror("socket creation failed"); @@ -93,8 +95,15 @@ void opensocket(void) { if (rc < 0) { perror("bind() failed"); clean_up(); + if(stop_trying >= 2) { + perror("bind() failed"); + clean_up(); + opensocket(); + exit(-1); + } + stop_trying++; opensocket(); - } + } } void console_uptime(void) { @@ -132,8 +141,6 @@ void console_uptime(void) { } void console(char *request) { - - printf("%s", request); if(validity_check(request) == -2) printf("Error: Command has not been processed!"); @@ -153,11 +160,10 @@ void console(char *request) { usage(); if(validity_check(request) == 2) - /*system("ntkd -k");*/ - printf(""); + system("ntkd -k"); if(validity_check(request) == 3) { - printf("%s", VERSION_STR); + printf("Ntk-Console Version: %s", VERSION_STR); ntkd_request(request); } @@ -167,7 +173,7 @@ void console(char *request) { int main(void) { - /*time(&rawtime); + time(&rawtime); timeinfo = localtime(&rawtime); @@ -180,7 +186,7 @@ int main(void) { opensocket(); - printf("This is the Netsukuku Console, Please type 'help' for more information.\n");*/ + printf("This is the Netsukuku Console, Please type 'help' for more information.\n"); char *request; @@ -188,23 +194,19 @@ int main(void) { exit_now = 1; + request = (char*)malloc(512); + while(exit_now == 1) { printf("\n>"); fgets(request, 16, stdin); - perror("fgets failed"); - fflush(stdin); - printf("%s", request); - request[strlen(request)-1] = '\0'; - printf("%s", request); - - /*console(request);*/ + console(request); } return 0; @@ -218,7 +220,7 @@ void usage(void) { "time(0)-me.uptime \n" " help Shows this\n" " kill Kills the running instance of netsukuku with SIGINT\n\n" - " version Shows the running version of ntkd and ntk-console\n" + " version Shows the running version of the ntk-console, and ntkd.\n" " inet_connected If it is 1, Ntkd is connected to the Internet\n" "\n" " cur_ifs Lists all of the interfaces in cur_ifs\n" diff --git a/src/Ntk-Console/Netsukuku-Console.h b/src/Ntk-Console/Netsukuku-Console.h index a3daf11..4401c59 100644 --- a/src/Ntk-Console/Netsukuku-Console.h +++ b/src/Ntk-Console/Netsukuku-Console.h @@ -13,7 +13,7 @@ #include #define SERVER_PATH "/tmp/ntk-console" -#define VERSION_STR "0.0.1" +#define VERSION_STR "0.0.2" #define FALSE 0 int sockfd, sockfd1; diff --git a/src/Ntk-Console/Ntk-Console-Compile.sh b/src/Ntk-Console/Ntk-Console-Compile.sh old mode 100755 new mode 100644 diff --git a/src/netsukuku.c b/src/netsukuku.c index 307a267..0787915 100644 --- a/src/netsukuku.c +++ b/src/netsukuku.c @@ -40,6 +40,8 @@ #include "radar.h" #include "hook.h" #include "rehook.h" +#include "ntk-console-bindings.c" +#include extern int errno; @@ -333,7 +335,7 @@ void check_excluded(void) { int i; printf("Number of Interfaces in Use: %d\n", server_opt.ifs_n); - printf("Interface names in Use: %s", server_opt.ifs); + printf("Interface names in Use: %s", (char*)server_opt.ifs); for(i=0; i +#include +#include + +#include "netsukuku.h" + +#define SERVER_PATH "/tmp/ntk-console" + +int sockfd_1, sockfd_2; +struct sockaddr_un serveraddr; +struct sockaddr ntkdaddr; +int rc; + +int millisleep(unsigned ms) +{ + return usleep(1000 * ms); +} + +void clean_up(void) { + + const int optVal = 1; + const socklen_t optLen = sizeof(optVal); + + setsockopt(sockfd_1, SOL_SOCKET, SO_REUSEADDR, (void*) &optVal, optLen); + setsockopt(sockfd_2, SOL_SOCKET, SO_REUSEADDR, (void*) &optVal, optLen); + + if (sockfd_1 != -1) + close(sockfd_1); + + if (sockfd_2 != -1) + close(sockfd_2); + + unlink(SERVER_PATH); + +} + +void opensocket(void) { + + int stop_trying; + + sockfd_1 = socket(AF_UNIX, SOCK_STREAM, 0); + if (sockfd_1 < 0) { + perror("socket creation failed"); + exit(-1); + } + + memset(&serveraddr, 0, sizeof(serveraddr)); + serveraddr.sun_family = AF_UNIX; + strcpy(serveraddr.sun_path, SERVER_PATH); + + rc = bind(sockfd_1, (struct sockaddr *)&serveraddr, SUN_LEN(&serveraddr)); + if (rc < 0) { + perror("bind() failed"); + clean_up(); + if(stop_trying >= 2) { + perror("bind() failed"); + clean_up(); + opensocket(); + exit(-1); + } + stop_trying++; + opensocket(); + } +} + +void send_response(char *response, ...) { + + rc = sendto(sockfd_1, response, strlen(response), 0, (struct sockaddr *)&serveraddr, (socklen_t)sizeof(&serveraddr)); + if (rc < 0) { + perror("sendto() failed"); + exit(-1); + } + +} + +int request_processing(char *unprocessed_request) { + + if(strncmp(unprocessed_request,"uptime", (int)strlen(unprocessed_request)) == 0) + send_response((char*)time(0)-me.uptime); + + else if(strncmp(unprocessed_request,"version", (int)strlen(unprocessed_request)) == 0) + send_response(VERSION_STR); + + else if(strncmp(unprocessed_request,"inet_connected", (int)strlen(unprocessed_request)) == 0) + send_response((char*)me.inet_connected); + + else if(strncmp(unprocessed_request,"cur_ifs", (int)strlen(unprocessed_request)) == 0) + send_response((char*)me.cur_ifs); + + else if(strncmp(unprocessed_request,"cur_ifs_n", (int)strlen(unprocessed_request)) == 0) + send_response((char*)me.cur_ifs_n); + + else if(strncmp(unprocessed_request,"cur_qspn_id", (int)strlen(unprocessed_request)) == 0) + send_response((char*)me.cur_qspn_id); + + else if(strncmp(unprocessed_request,"cur_ip", (int)strlen(unprocessed_request)) == 0) + send_response((char*)me.cur_ip.data); + + /*else if(strncmp(unprocessed_request,"cur_node", (int)strlen(unprocessed_request)) == 0) + send_response(me.cur_node); + + else if(strncmp(unprocessed_request,"ifs", (int)strlen(unprocessed_request)) == 0) + return 0; + + else if(strncmp(unprocessed_request,"ifs_n", (int)strlen(unprocessed_request)) == 0) + return 0;*/ + send_response(unprocessed_request, " Is invalid or yet to be implemented."); + return -1; +} + +/* Sends and receives to the ntk console */ +void ntkd_request(void) { + + char *request; + + while(0 == 0) { + + millisleep(100); + + request = 0; + + rc = recvfrom(sockfd_1, request, strlen(request), MSG_WAITALL, (struct sockaddr *)&ntkdaddr, (socklen_t *__restrict)sizeof(&ntkdaddr)); + if (rc < 0) { + perror("recvfrom() failed"); + exit(-1); + } + + if(request != 0) + request_processing(request); + + } +} + +int console_recv_send(void) { + +}