From e542a51345a555d98e58927c5eec1b256f6ca69c Mon Sep 17 00:00:00 2001 From: MissValeska Date: Wed, 4 Jun 2014 03:47:49 -0700 Subject: [PATCH] Wow! I've done A LOT of work on the netsukuku console recently! I still haven't set up any way for ntkd to receive the commands, Or done much of anything in netsukuku itself recently except change some stuff in the exclude function to prevent possible errors if an interface had the same name up until the end of the one specified. I.E if it had tunl012, It wouldn't work because it had tunl0 in the beginning. Now, It will read from the entire length of the variable, And if it doesn't match it entirely, Then it is okay. Anyway! The netsukuku console is almost ready! I just keep getting this weird issue around fgets() not working at all in console(). It causes the program to spam the > symbol. It functions in main()! However, After that, It has a segmentation fault. I've been trying everything I can think of, One thing made it not do a segmentation fault, However! The fgets() inside of console() still didn't work, So, It just spammed endlessly. Anyone who can, Please help! I've been working on this for a while! :/ --- src/Ntk-Console/Makefile | 2 +- src/Ntk-Console/Netsukuku-Console.c | 148 +++++++++++++++++-------- src/Ntk-Console/Ntk-Console-Compile.sh | 3 +- src/netsukuku.c | 2 +- 4 files changed, 107 insertions(+), 48 deletions(-) diff --git a/src/Ntk-Console/Makefile b/src/Ntk-Console/Makefile index c0ce2fd..8e30407 100644 --- a/src/Ntk-Console/Makefile +++ b/src/Ntk-Console/Makefile @@ -1,7 +1,7 @@ LDLIBS=-pthread LIBS=-pthread C=clang -DEBUG=-g -Wfatal-errors -pedantic -Weffc++ -Wunreachable-code -Winline -Wredundant-decls -Wno-vla +DEBUG=-g -Wfatal-errors -pedantic -Wunreachable-code -Winline -Wredundant-decls -Wno-vla CFLAGS=-Wall ${DEBUG} SRCBIN=Netsukuku-Console diff --git a/src/Ntk-Console/Netsukuku-Console.c b/src/Ntk-Console/Netsukuku-Console.c index cc79725..6d513e5 100644 --- a/src/Ntk-Console/Netsukuku-Console.c +++ b/src/Ntk-Console/Netsukuku-Console.c @@ -4,8 +4,10 @@ #include #include #include +#include #include #include +#include #include #include "Netsukuku-Console.h" @@ -15,83 +17,98 @@ struct sockaddr ntkdaddr; int rc, length, exit_now; char *request, *response; +time_t rawtime; +struct tm *timeinfo; + +int uptime_sec; +int uptime_min; +int uptime_hr; + void usage(); void clean_up(); int validity_check(void) { - if(strncmp(request,"help", 4) == 0) + if(request && strncmp(request, "null", (int)strlen(request)) != 0) { + + if(strncmp(request,"help", (int)strlen(request)) == 0) return 1; - else if(strncmp(request,"uptime", 6) == 0) + else if(strncmp(request,"uptime", (int)strlen(request)) == 0) return 0; - else if(strncmp(request,"kill", 4) == 0) + else if(strncmp(request,"kill", (int)strlen(request)) == 0) return 2; - else if(strncmp(request,"version", 7) == 0) + else if(strncmp(request,"version", (int)strlen(request)) == 0) return 3; - else if(strncmp(request,"console_uptime", 14) == 0) + else if(strncmp(request,"console_uptime", (int)strlen(request)) == 0) return 4; - else if(strncmp(request,"inet_connected", 14) == 0) + else if(strncmp(request,"inet_connected", (int)strlen(request)) == 0) return 0; - else if(strncmp(request,"cur_ifs", 7) == 0) + else if(strncmp(request,"cur_ifs", (int)strlen(request)) == 0) return 0; - else if(strncmp(request,"cur_ifs_n", 9) == 0) + else if(strncmp(request,"cur_ifs_n", (int)strlen(request)) == 0) return 0; - else if(strncmp(request,"cur_qspn_id", 11) == 0) + else if(strncmp(request,"cur_qspn_id", (int)strlen(request)) == 0) return 0; - else if(strncmp(request,"cur_ip", 6) == 0) + else if(strncmp(request,"cur_ip", (int)strlen(request)) == 0) return 0; - else if(strncmp(request,"cur_node", 8) == 0) + else if(strncmp(request,"cur_node", (int)strlen(request)) == 0) return 0; - else if(strncmp(request,"ifs", 3) == 0) + else if(strncmp(request,"ifs", (int)strlen(request)) == 0) return 0; - else if(strncmp(request,"ifs_n", 5) == 0) + else if(strncmp(request,"ifs_n", (int)strlen(request)) == 0) return 0; else { printf("Incorrect or unreadable command, Please correct it.\n"); return -1; } + } + + return -2; } /* this function is run by the second thread */ void *ntkd_request(void *argv) { - while(sendrecv == 1) { - rc = sendto(sockfd1, request, strlen(request), 0, (struct sockaddr *)&serveraddr, (socklen_t *)strlen(&serveraddr)); + while(usleep(200)) { + while(request && sendrecv == 1 && strncmp(request, "null", (int)strlen(request) != 0)) { + rc = sendto(sockfd1, request, strlen(request), 0, (struct sockaddr *)&serveraddr, (socklen_t)sizeof(&serveraddr)); + if (rc < 0) { + perror("sendto() failed"); + exit(-1); + } + + rc = recvfrom(sockfd1, response, strlen(response), MSG_WAITALL, (struct sockaddr *)&ntkdaddr, (socklen_t *__restrict)sizeof(&ntkdaddr)); if (rc < 0) { - perror("sendto() failed"); + perror("recvfrom() failed"); exit(-1); - } - - rc = recvfrom(sockfd1, response, strlen(response), MSG_WAITALL, (struct sockaddr *)&ntkdaddr, (socklen_t *)strlen(&ntkdaddr)); - if (rc < 0) { - perror("recvfrom() failed"); - exit(-1); - } - - if(rc >= 0) { - printf("Sent and received Successfully!\n The Response was) %s", response); - - } + } + + if(rc >= 0) { + printf("Sent and received Successfully!\n The Response was) %s", response); + + } } + } +return 0; } -int opensocket(void) { +void opensocket(void) { sockfd = socket(AF_UNIX, SOCK_STREAM, 0); if (sockfd < 0) { @@ -107,28 +124,52 @@ int opensocket(void) { if (rc < 0) { perror("bind() failed"); clean_up(); - exit(-1); + opensocket(); } } +void console_uptime(void) { + + int uptime_sec1; + int uptime_min1; + int uptime_hr1; + + time(&rawtime); + + timeinfo = localtime(&rawtime); + + uptime_sec1 = timeinfo->tm_sec; + uptime_min1 = timeinfo->tm_min; + uptime_hr1 = timeinfo->tm_hour; + + uptime_sec1 -= uptime_sec; + uptime_min1 -= uptime_min; + uptime_hr1 -= uptime_hr; + + printf("Total Uptime is: %i, %i, %i\n", uptime_hr1, uptime_min1, uptime_sec1); + +} + void console(void) { + char request_buf[16]; + exit_now = 1; while(exit_now == 1) { printf("\n>"); - - //request = scanf("%s"); - fgets(request, 15, stdin); - int len = strlen(request); - if (len > 0 && request[len-1] == '\n') - request[len-1] = '\0'; + fgets(request_buf, 16, stdin); + + strcpy(request, request_buf); + + if(validity_check() == -2) + printf("Error: Command has not been processed!"); if(validity_check() == -1) usage(); - if(strncmp(request,"quit", 4) == 0) { + if(strncmp(request,"quit", (int)strlen(request)) == 0) { exit_now = 2; clean_up(); exit(0); @@ -149,44 +190,61 @@ void console(void) { } if(validity_check() == 4) - sizeof(char); // Place holder text + console_uptime(); sendrecv = 0; } } int main(void) { + + char request_buf[16]; + + printf("\n>"); + + fgets(request_buf, 16, stdin); + + printf("uhm %s", request_buf); sendrecv = 0; + /*request = "null";*/ + + time(&rawtime); + + timeinfo = localtime(&rawtime); + + uptime_sec = timeinfo->tm_sec; + uptime_min = timeinfo->tm_min; + uptime_hr = timeinfo->tm_hour; + opensocket(); printf("This is the Netsukuku Console, Please type 'help' for more information.\n"); - console(); - /* This variable is our reference to the second thread */ pthread_t NtkdRequest; /* Create a second thread which executes ntkd_request() */ - if(pthread_create(&NtkdRequest, NULL, ntkd_request, NULL)) { + if(pthread_create(&NtkdRequest, NULL, ntkd_request, 0)) { fprintf(stderr, "Error creating thread\n"); return -1; } /* Detach the second thread */ - if(exit_now == 2) { if(pthread_detach(NtkdRequest)) { fprintf(stderr, "Error detaching thread\n"); return -2; } - } + + console(); + return 0; } void usage(void) { - printf("Usage)\n" + printf("Usage\n" " uptime Returns the time when ntkd finished the hooking," "to get the the actual uptime just do) " "time(0)-me.uptime \n" @@ -206,7 +264,7 @@ void usage(void) { " ifs Lists all of the interfaces in server_opt.ifs\n" " ifs_n Lists the number of interfaces present in server_opt.ifs\n" " quit Exits this program\n" - " console_uptime Gets the uptime of this console (Yet to be implemented)\n"); + " console_uptime Gets the uptime of this console\n"); } diff --git a/src/Ntk-Console/Ntk-Console-Compile.sh b/src/Ntk-Console/Ntk-Console-Compile.sh index ca11c36..ae4d5d9 100755 --- a/src/Ntk-Console/Ntk-Console-Compile.sh +++ b/src/Ntk-Console/Ntk-Console-Compile.sh @@ -1,2 +1,3 @@ #!/bin/bash -clang Netsukuku-Console.c -o Netsukuku-Console -pthread -g +make clean +make \ No newline at end of file diff --git a/src/netsukuku.c b/src/netsukuku.c index 704841e..533c509 100644 --- a/src/netsukuku.c +++ b/src/netsukuku.c @@ -372,7 +372,7 @@ int exclude_interface(int prevent_doubles) { old_tmp = ifs; - if(tmp && tmp->ifa_addr && tmp->ifa_addr->sa_family == AF_PACKET && tmp->ifa_flags & IFF_UP && !(tmp->ifa_flags & IFF_LOOPBACK && strncmp(tmp->ifa_name, "tunl0", 5) != 0 && strncmp(tmp->ifa_name, "tunl1", 5) != 0 && strcmp(optarg, tmp->ifa_name) != 0)) { + if(tmp && tmp->ifa_addr && tmp->ifa_addr->sa_family == AF_PACKET && tmp->ifa_flags & IFF_UP && !(tmp->ifa_flags & IFF_LOOPBACK && strncmp(tmp->ifa_name, "tunl0", (int)strlen(tmp->ifa_name)) != 0 && strncmp(tmp->ifa_name, "tunl1", (int)strlen(tmp->ifa_name)) != 0 && strcmp(optarg, tmp->ifa_name) != 0)) { ifs = tmp->ifa_name; ifs_n++; }