From b13c3bf5de6b2eedf39405601391426a8681084a Mon Sep 17 00:00:00 2001 From: MissValeska Date: Fri, 15 Aug 2014 22:51:54 -0700 Subject: [PATCH] I have done a LOT of work! I have changed the whole paradigm of the ntk console client and console bindings! I have changed it from peer to peer to server-client because there was no point doing it the other way, And that way was hard and I didn't really know what I was doing. I followed some IBM examples, And didn't try to extrapolate. I have done a little bit of work trying to resolve some compilation errors, However, I cannot check if there are any compilation errors now because my root partition is full, again. My compiler needs to do something with /tmp so I can only see a portion of the error logs, All of which are warnings up until the no space error. I will do some more testing and debugging on my other machine, Help is appreciated, Enjoy! --- src/Ntk-Console/Netsukuku-Console.c | 97 +++++++++++-------- src/Ntk-Console/Netsukuku-Console.h | 6 +- src/netsukuku.c | 1 + src/ntk-console-bindings.c | 141 ++++++++++++++++++++-------- 4 files changed, 167 insertions(+), 78 deletions(-) diff --git a/src/Ntk-Console/Netsukuku-Console.c b/src/Ntk-Console/Netsukuku-Console.c index 31a83da..85da3dc 100644 --- a/src/Ntk-Console/Netsukuku-Console.c +++ b/src/Ntk-Console/Netsukuku-Console.c @@ -1,12 +1,12 @@ #include "Netsukuku-Console.h" -char *response; +char response[250]; void usage(); void clean_up(); -int validity_check(char *request) { +int validity_check(char request) { if(strncmp(request,"help", (int)strlen(request)) == 0) return 1; @@ -56,31 +56,63 @@ int validity_check(char *request) { } -/* Sends and receives to ntkd */ -void ntkd_request(char *request) { +void response_cleanup(char response[250]) { + + char remove = 'a'; - rc = sendto(sockfd1, request, strlen(request), 0, (struct sockaddr *)&serveraddr, (socklen_t)sizeof(&serveraddr)); + char* c; + int x; + char* pPosition; + while(pPosition = strchr(response, 'a') != NULL) { + if ((c = index(response, remove)) != NULL) { + size_t len_left = sizeof(response) - (c+1-response); + memmove(c, c+1, len_left); + } + } + printf("Sent and received Successfully!\n The Response was: %s", response); +} + +/* Sends and receives to ntkd */ +void ntkd_request(char request) { + + int request_length; + + rc = connect(sockfd, (struct sockaddr *)&serveraddr, SUN_LEN(&serveraddr)); if (rc < 0) { - perror("sendto() failed"); + perror("connect() failed"); exit(-1); } - - rc = recvfrom(sockfd1, response, strlen(response), MSG_WAITALL, (struct sockaddr *)&ntkdaddr, (socklen_t *__restrict)sizeof(&ntkdaddr)); + + + request_length = (int)strlen(request); + memset(request, 'a', 250 - request_length); + rc = send(sockfd, request, sizeof(request), 0); if (rc < 0) { - perror("recvfrom() failed"); + perror("send() failed"); exit(-1); - } + } - if(rc >= 0) { - printf("Sent and received Successfully!\n The Response was) %s", response); + bytesReceived = 0; + while (bytesReceived < BUFFER_LENGTH) { + rc = recv(sockfd, & response[bytesReceived], + BUFFER_LENGTH - bytesReceived, 0); + if (rc < 0) { + perror("recv() failed"); + exit(-1); + } + else if (rc == 0) { + printf("The server closed the connection\n"); + exit(-1); + } - } + /* Increment the number of bytes that have been received so far */ + bytesReceived += rc; + } + response_cleanup(response); } void opensocket(void) { - int stop_trying; - sockfd = socket(AF_UNIX, SOCK_STREAM, 0); if (sockfd < 0) { perror("socket creation failed"); @@ -90,20 +122,6 @@ void opensocket(void) { memset(&serveraddr, 0, sizeof(serveraddr)); serveraddr.sun_family = AF_UNIX; strcpy(serveraddr.sun_path, SERVER_PATH); - - rc = bind(sockfd, (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 console_uptime(void) { @@ -140,7 +158,7 @@ void console_uptime(void) { } -void console(char *request) { +void console(char request) { if(validity_check(request) == -2) printf("Error: Command has not been processed!"); @@ -188,15 +206,15 @@ int main(void) { printf("This is the Netsukuku Console, Please type 'help' for more information.\n"); - char *request; + char request; - int exit_now; + request = (char)malloc(512); - exit_now = 1; + char *request1; - request = (char*)malloc(512); + request1 = (char*)malloc(512); - while(exit_now == 1) { + do { printf("\n>"); @@ -204,12 +222,15 @@ int main(void) { fflush(stdin); - request[strlen(request)-1] = '\0'; + request1[strlen(request)-1] = '\0'; + + strcpy(request, request1); console(request); - } + } while(FALSE); - return 0; +clean_up(); +return 0; } void usage(void) { diff --git a/src/Ntk-Console/Netsukuku-Console.h b/src/Ntk-Console/Netsukuku-Console.h index 4401c59..e81142d 100644 --- a/src/Ntk-Console/Netsukuku-Console.h +++ b/src/Ntk-Console/Netsukuku-Console.h @@ -13,13 +13,13 @@ #include #define SERVER_PATH "/tmp/ntk-console" +#define BUFFER_LENGTH 250 #define VERSION_STR "0.0.2" #define FALSE 0 -int sockfd, sockfd1; +int sockfd = -1, sockfd1 = -1; struct sockaddr_un serveraddr; -struct sockaddr ntkdaddr; -int rc; +int rc, bytesReceived; time_t rawtime; struct tm *timeinfo; diff --git a/src/netsukuku.c b/src/netsukuku.c index 0787915..59975c6 100644 --- a/src/netsukuku.c +++ b/src/netsukuku.c @@ -177,6 +177,7 @@ void usage(void) " -h Shows this help\n" " -v Shows the version you are using\n" " -k Kills the running instance of ntkd\n" + " -C Runs the console server for Ntk-Console to connect to\n" " -e Excludes an interface from usage I.E all interfaces except this one\n"); } diff --git a/src/ntk-console-bindings.c b/src/ntk-console-bindings.c index eb80363..c581638 100644 --- a/src/ntk-console-bindings.c +++ b/src/ntk-console-bindings.c @@ -1,20 +1,31 @@ +/* Header files required for the console bindings + * not included outside of this file. */ + #include #include #include #include "netsukuku.h" +/* Constants used for the console bindings. */ + #define SERVER_PATH "/tmp/ntk-console" +#define REQUEST_LENGTH 250 +#define FALSE 0 -int sockfd_1, sockfd_2; +/* Variable and structure defintions, sockfd refers to socket file descriptor + * length refers to the required length of the requests that will be sent. + * recv won't wake up until length is received. + * rc is used for error checking in socket operations. + * serveraddr is a structure describing the address of + * an AF_LOCAL (aka AF_UNIX) socket. */ + +int sockfd_1 = -1, sockfd_2 = -1; struct sockaddr_un serveraddr; -struct sockaddr ntkdaddr; -int rc; +int rc, length; -int millisleep(unsigned ms) -{ - return usleep(1000 * ms); -} +/* Cleans up the console bindings for closing, Closes socket file descriptors, + * unlinks the server path, etc. */ void clean_up(void) { @@ -34,6 +45,8 @@ void clean_up(void) { } +/* Creates an AF_UNIX socket and binds it to a local address. */ + void opensocket(void) { int stop_trying; @@ -63,38 +76,66 @@ void opensocket(void) { } } -void send_response(char *response, ...) { +/* Sends a parsed response to the ntk console client. */ + +void send_response(char response) { + int response_length; - rc = sendto(sockfd_1, response, strlen(response), 0, (struct sockaddr *)&serveraddr, (socklen_t)sizeof(&serveraddr)); - if (rc < 0) { - perror("sendto() failed"); - exit(-1); + response_length = (int)strlen(response); + memset(response, 'a', 250 - response_length); + rc = send(sockfd_2, response, sizeof(response), 0); + if (rc < 0){ + perror("send() failed"); + exit(-1); } } -int request_processing(char *unprocessed_request) { +void request_cleanup(char unprocessed_request) { + + char remove = 'a'; + + char* c; + int x; + char* pPosition; + while(pPosition = strchr(unprocessed_request, 'a') != NULL) { + if ((c = index(unprocessed_request, remove)) != NULL) { + size_t len_left = sizeof(unprocessed_request) - (c+1-unprocessed_request); + memmove(c, c+1, len_left); + } + } + printf("Cleaned Request is: %s", unprocessed_request); + + request_processing(unprocessed_request); + +} + +/* Parses the received request from the ntk console client + * to data from ntkd structures such as: me + * into a response for the ntk console client. */ + +int request_processing(char unprocessed_request) { if(strncmp(unprocessed_request,"uptime", (int)strlen(unprocessed_request)) == 0) - send_response((char*)time(0)-me.uptime); + 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); + send_response((char)me.inet_connected); else if(strncmp(unprocessed_request,"cur_ifs", (int)strlen(unprocessed_request)) == 0) - send_response((char*)me.cur_ifs); + 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); + 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); + 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); + send_response((char)me.cur_ip.data); /*else if(strncmp(unprocessed_request,"cur_node", (int)strlen(unprocessed_request)) == 0) send_response(me.cur_node); @@ -108,29 +149,55 @@ int request_processing(char *unprocessed_request) { return -1; } -/* Sends and receives to the ntk console */ +/* Receives a request of 250 bytes from the ntk console client. + * listen and accept are also in a while loop to allow for different + * start times between the ntk console bindings and the ntk console client, + * As well as restarting of the ntk console client. */ void ntkd_request(void) { - char *request; + char request[REQUEST_LENGTH]; - while(0 == 0) { + do { + + rc = listen(sockfd_1, 10); + if (rc< 0) { + perror("listen() failed"); + exit(-1); + } + + printf("Ready for client connect().\n"); + + sockfd_2 = accept(sockfd_1, NULL, NULL); + if (sd2 < 0) { + perror("accept() failed"); + exit(-1); + } - millisleep(100); + length = REQUEST_LENGTH; + rc = setsockopt(sockfd_2, SOL_SOCKET, SO_RCVLOWAT, + (char *)&length, sizeof(length)); + if (rc < 0) { + perror("setsockopt(SO_RCVLOWAT) failed"); + exit(-1); + } - request = 0; + rc = recv(sockfd_2, request, sizeof(request), 0); + if (rc < 0) { + perror("recv() failed"); + exit(-1); + } - rc = recvfrom(sockfd_1, request, strlen(request), MSG_WAITALL, (struct sockaddr *)&ntkdaddr, (socklen_t *__restrict)sizeof(&ntkdaddr)); - if (rc < 0) { - perror("recvfrom() failed"); + printf("%d bytes of data were received\n", rc); + + if (rc == 0 || rc < sizeof(request)) { + printf("The console client closed the connection before all of the\n"); + printf("data was sent\n"); exit(-1); } - - if(request != 0) - request_processing(request); - - } -} - -int console_recv_send(void) { - -} + + request_cleanup(request); + } while(FALSE); + + clean_up(); + +} \ No newline at end of file