Ntk-console: Clean up command line parsing

* Make console application function
* No more segfaults
* Apply KnR style
This commit is contained in:
Alexander von Gluck IV 2014-09-13 20:13:49 -05:00
parent 5d49293e08
commit 3a842c2d4c
2 changed files with 258 additions and 252 deletions

View File

@ -1,12 +1,12 @@
C=clang C=clang
DEBUG=-g -fno-builtin -Wfatal-errors -pedantic -Wunreachable-code -Winline -Wredundant-decls -Wno-vla DEBUG=-g -fno-builtin -Wfatal-errors -pedantic -Wunreachable-code -Winline -Wredundant-decls -Wno-vla
CFLAGS=-Wall ${DEBUG} CFLAGS=-Wall ${DEBUG} -std=c99
SRCBIN=Netsukuku-Console SRCBIN=Netsukuku-Console
all: ${SRCBIN} all: ${SRCBIN}
${SRCBIN}: Netsukuku-Console.o ${SRCBIN}: Netsukuku-Console.o
${C} ${LDFLAGS} ${LIBS} $^ -o $@ ${C} ${LDFLAGS} ${CFLAGS} ${LIBS} $^ -o $@
.PHONY: clean .PHONY: clean

View File

@ -6,61 +6,72 @@ void usage();
void clean_up(); void clean_up();
int validity_check(char *request) {
if(strncmp(request,"help", (int)strlen(request)) == 0) typedef enum {
return 1; COMMAND_HELP = 0x100,
COMMAND_UPTIME,
COMMAND_KILL,
COMMAND_VERSION,
COMMAND_INETCONN,
COMMAND_CURIFS,
COMMAND_CURIFSCT,
COMMAND_CURQSPNID,
COMMAND_CURIP,
COMMAND_CURNODE,
COMMAND_IFS,
COMMAND_IFSCT,
COMMAND_QUIT,
COMMAND_CONSUPTIME,
} command_t;
else if(strncmp(request,"uptime", (int)strlen(request)) == 0)
return 0;
else if(strncmp(request,"kill", (int)strlen(request)) == 0) const struct supported_commands {
return 2; command_t id;
const char* command;
const char* help;
int arguments;
} kSupportedCommands[] = {
{COMMAND_HELP, "help", "Shows console help", 0},
{COMMAND_UPTIME, "uptime", "Returns the time when ntkd finished the hooking", 0},
{COMMAND_KILL, "kill", "Kills the running instance of netsukuku with SIGINT", 0},
{COMMAND_VERSION, "version", "Shows the running version of the ntk-console, and ntkd.", 0},
{COMMAND_INETCONN, "inet_connected", "Query if Ntkd is connected to the internet", 0},
{COMMAND_CURIFS, "cur_ifs", "Lists all of the interfaces in cur_ifs", 0},
{COMMAND_CURIFSCT, "cur_ifs_n", "Lists the number of interfaces present in `cur_ifs`", 0},
{COMMAND_CURQSPNID, "cur_qspn_id", "The current qspn_id we are processing. It is cur_qspn_id[levels] big", 0},
{COMMAND_CURIP, "cur_ip", "Current IP address", 0},
{COMMAND_CURNODE, "cur_node", "Current node", 0},
{COMMAND_IFS, "ifs", "List all the interfaces in server_opt.ifs", 0},
{COMMAND_IFSCT, "ifs_n", "List the number of interfaces present in server_opt.ifs", 0},
{COMMAND_QUIT, "quit", "Exit the console", 0},
{COMMAND_CONSUPTIME,"console_uptime", "Get the uptime of this console", 0},
};
else if(strncmp(request,"version", (int)strlen(request)) == 0)
return 3;
else if(strncmp(request,"console_uptime", (int)strlen(request)) == 0) command_t
return 4; command_parse(char *request)
{
else if(strlen(request) > BUFFER_LENGTH) if (strlen(request) > BUFFER_LENGTH) {
return 5; printf("Error: Command longer than 250 bytes.\n");
else if(strncmp(request,"inet_connected", (int)strlen(request)) == 0)
return 0;
else if(strncmp(request,"cur_ifs", (int)strlen(request)) == 0)
return 0;
else if(strncmp(request,"cur_ifs_n", (int)strlen(request)) == 0)
return 0;
else if(strncmp(request,"cur_qspn_id", (int)strlen(request)) == 0)
return 0;
else if(strncmp(request,"cur_ip", (int)strlen(request)) == 0)
return 0;
else if(strncmp(request,"cur_node", (int)strlen(request)) == 0)
return 0;
else if(strncmp(request,"ifs", (int)strlen(request)) == 0)
return 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 -1;
} }
return -2; for (int i = 0; i < sizeof(kSupportedCommands)
/ sizeof(kSupportedCommands[0]); i++) {
if (strncmp(request, kSupportedCommands[i].command,
(int)strlen(request) - 1) == 0) {
return kSupportedCommands[i].id;
}
}
printf("Incorrect or unreadable command, Please correct it.\n");
return -1;
} }
void response_cleanup(char response[BUFFER_LENGTH]) {
void
response_cleanup(char response[BUFFER_LENGTH])
{
char remove = 'a'; char remove = 'a';
char* c; char* c;
@ -71,22 +82,22 @@ void response_cleanup(char response[BUFFER_LENGTH]) {
memmove(c, c+1, len_left); memmove(c, c+1, len_left);
} }
} }
printf("Sent and received Successfully!\n The Response was: %s", response); printf("Sent and received Successfully!\n The Response was: %s", response);
} }
/* Sends and receives to ntkd */ /* Sends and receives to ntkd */
void ntkd_request(char *request) { void
ntkd_request(char *request)
int request_length; {
rc = connect(sockfd, (struct sockaddr *)&serveraddr, sizeof(serveraddr));
rc = connect(sockfd, (struct sockaddr *)&serveraddr, SUN_LEN(&serveraddr));
if (rc < 0) { if (rc < 0) {
perror("connect() failed"); perror("connect() failed");
exit(-1); exit(-1);
} }
int request_length = strlen(request);
request_length = (int)strlen(request);
memset(request, 'a', BUFFER_LENGTH - request_length); memset(request, 'a', BUFFER_LENGTH - request_length);
rc = send(sockfd, request, sizeof(request), 0); rc = send(sockfd, request, sizeof(request), 0);
if (rc < 0) { if (rc < 0) {
@ -113,8 +124,10 @@ void ntkd_request(char *request) {
response_cleanup(response); response_cleanup(response);
} }
void opensocket(void) {
void
opensocket(void)
{
sockfd = socket(AF_UNIX, SOCK_STREAM, 0); sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
if (sockfd < 0) { if (sockfd < 0) {
perror("socket creation failed"); perror("socket creation failed");
@ -126,8 +139,10 @@ void opensocket(void) {
strcpy(serveraddr.sun_path, SERVER_PATH); strcpy(serveraddr.sun_path, SERVER_PATH);
} }
void console_uptime(void) {
void
console_uptime(void)
{
int uptime_sec1; int uptime_sec1;
int uptime_min1; int uptime_min1;
int uptime_hour1; int uptime_hour1;
@ -157,52 +172,57 @@ void console_uptime(void) {
uptime_year1 -= uptime_year; uptime_year1 -= uptime_year;
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); 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);
} }
int millisleep(unsigned ms)
int
millisleep(unsigned ms)
{ {
return usleep(1000 * ms); return usleep(1000 * ms);
} }
void console(char * request) {
if(validity_check(request) == -2) void
printf("Error: Command has not been processed!\n"); console(char* request)
{
command_t commandID = command_parse(request);
if(validity_check(request) == -1) switch (commandID) {
usage(); case COMMAND_QUIT:
if(strncmp(request,"quit", (int)strlen(request)) == 0) {
clean_up(); clean_up();
exit(0); exit(0);
} case COMMAND_UPTIME:
case COMMAND_INETCONN:
if(validity_check(request) == 0) { case COMMAND_CURIFS:
case COMMAND_CURIFSCT:
case COMMAND_CURQSPNID:
case COMMAND_CURIP:
case COMMAND_CURNODE:
case COMMAND_IFS:
case COMMAND_IFSCT:
ntkd_request(request); ntkd_request(request);
millisleep(200); millisleep(200);
} break;
case COMMAND_VERSION:
if(validity_check(request) == 1)
usage();
if(validity_check(request) == 2)
system("ntkd -k");
if(validity_check(request) == 3) {
printf("Ntk-Console Version: %s\n", VERSION_STR); printf("Ntk-Console Version: %s\n", VERSION_STR);
ntkd_request(request); ntkd_request(request);
} break;
case COMMAND_CONSUPTIME:
if(validity_check(request) == 4)
console_uptime(); console_uptime();
break;
if(validity_check(request) == 5) case COMMAND_KILL:
printf("Error: Command longer than 250 bytes.\n"); system("ntkd -k");
break;
case COMMAND_HELP:
default:
usage();
}
} }
int main(void) {
int
main(void)
{
time(&rawtime); time(&rawtime);
timeinfo = localtime(&rawtime); timeinfo = localtime(&rawtime);
@ -218,49 +238,36 @@ int main(void) {
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; char* request = (char*)malloc(BUFFER_LENGTH);
char request1;
request = (char *)malloc(BUFFER_LENGTH);
request1 = (char)malloc(BUFFER_LENGTH);
do { do {
printf("\n>"); printf("\n>");
fgets(request, 16, stdin); fgets(request, 16, stdin);
fflush(stdin); fflush(stdin);
request = request1;
console(request); console(request);
} while(FALSE); } while(FALSE);
clean_up(); clean_up();
return 0; return 0;
} }
void usage(void) { void usage(void) {
printf("Usage\n" printf("Usage\n"
" uptime Returns the time when ntkd finished the hooking," " uptime Returns the time when ntkd finished the hooking,\n"
"to get the the actual uptime just do) " " to get the the actual uptime just do)\n"
"time(0)-me.uptime \n" " time(0)-me.uptime \n"
" help Shows this\n" " help Shows this\n"
" kill Kills the running instance of netsukuku with SIGINT\n\n" " kill Kills the running instance of netsukuku with SIGINT\n\n"
" version Shows the running version of the ntk-console, and ntkd.\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" " inet_connected If it is 1, Ntkd is connected to the Internet\n"
"\n" " \n"
" cur_ifs Lists all of the interfaces in cur_ifs\n" " cur_ifs Lists all of the interfaces in cur_ifs\n"
" cur_ifs_n Lists the number of interfaces present in `cur_ifs'\n" " cur_ifs_n Lists the number of interfaces present in `cur_ifs'\n"
"\n"); " cur_qspn_id The current qspn_id we are processing. "
printf(" cur_qspn_id The current qspn_id we are processing. " " It is cur_qspn_id[levels] big\n"
"It is cur_qspn_id[levels] big\n"
" cur_ip Current IP address\n" " cur_ip Current IP address\n"
"\n" " \n"
" cur_node Current Node\n" " cur_node Current Node\n"
" ifs Lists all of the interfaces in server_opt.ifs\n" " ifs Lists all of the interfaces in server_opt.ifs\n"
" ifs_n Lists the number of interfaces present in server_opt.ifs\n" " ifs_n Lists the number of interfaces present in server_opt.ifs\n"
@ -269,8 +276,8 @@ void usage(void) {
} }
void clean_up(void) { void clean_up(void)
{
const int optVal = 1; const int optVal = 1;
const socklen_t optLen = sizeof(optVal); const socklen_t optLen = sizeof(optVal);
@ -284,5 +291,4 @@ void clean_up(void) {
close(sockfd1); close(sockfd1);
unlink(SERVER_PATH); unlink(SERVER_PATH);
} }