mirror of
https://github.com/ChronosX88/netsukuku.git
synced 2024-11-22 18:22:18 +00:00
Ntk-console: Clean up command line parsing
* Make console application function * No more segfaults * Apply KnR style
This commit is contained in:
parent
5d49293e08
commit
3a842c2d4c
@ -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
|
||||||
|
|
||||||
|
@ -6,283 +6,289 @@ void usage();
|
|||||||
|
|
||||||
void clean_up();
|
void clean_up();
|
||||||
|
|
||||||
int validity_check(char *request) {
|
|
||||||
|
typedef enum {
|
||||||
if(strncmp(request,"help", (int)strlen(request)) == 0)
|
COMMAND_HELP = 0x100,
|
||||||
return 1;
|
COMMAND_UPTIME,
|
||||||
|
COMMAND_KILL,
|
||||||
else if(strncmp(request,"uptime", (int)strlen(request)) == 0)
|
COMMAND_VERSION,
|
||||||
return 0;
|
COMMAND_INETCONN,
|
||||||
|
COMMAND_CURIFS,
|
||||||
else if(strncmp(request,"kill", (int)strlen(request)) == 0)
|
COMMAND_CURIFSCT,
|
||||||
return 2;
|
COMMAND_CURQSPNID,
|
||||||
|
COMMAND_CURIP,
|
||||||
else if(strncmp(request,"version", (int)strlen(request)) == 0)
|
COMMAND_CURNODE,
|
||||||
return 3;
|
COMMAND_IFS,
|
||||||
|
COMMAND_IFSCT,
|
||||||
else if(strncmp(request,"console_uptime", (int)strlen(request)) == 0)
|
COMMAND_QUIT,
|
||||||
return 4;
|
COMMAND_CONSUPTIME,
|
||||||
|
} command_t;
|
||||||
else if(strlen(request) > BUFFER_LENGTH)
|
|
||||||
return 5;
|
|
||||||
|
const struct supported_commands {
|
||||||
else if(strncmp(request,"inet_connected", (int)strlen(request)) == 0)
|
command_t id;
|
||||||
return 0;
|
const char* command;
|
||||||
|
const char* help;
|
||||||
else if(strncmp(request,"cur_ifs", (int)strlen(request)) == 0)
|
int arguments;
|
||||||
return 0;
|
} kSupportedCommands[] = {
|
||||||
|
{COMMAND_HELP, "help", "Shows console help", 0},
|
||||||
else if(strncmp(request,"cur_ifs_n", (int)strlen(request)) == 0)
|
{COMMAND_UPTIME, "uptime", "Returns the time when ntkd finished the hooking", 0},
|
||||||
return 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},
|
||||||
else if(strncmp(request,"cur_qspn_id", (int)strlen(request)) == 0)
|
{COMMAND_INETCONN, "inet_connected", "Query if Ntkd is connected to the internet", 0},
|
||||||
return 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},
|
||||||
else if(strncmp(request,"cur_ip", (int)strlen(request)) == 0)
|
{COMMAND_CURQSPNID, "cur_qspn_id", "The current qspn_id we are processing. It is cur_qspn_id[levels] big", 0},
|
||||||
return 0;
|
{COMMAND_CURIP, "cur_ip", "Current IP address", 0},
|
||||||
|
{COMMAND_CURNODE, "cur_node", "Current node", 0},
|
||||||
else if(strncmp(request,"cur_node", (int)strlen(request)) == 0)
|
{COMMAND_IFS, "ifs", "List all the interfaces in server_opt.ifs", 0},
|
||||||
return 0;
|
{COMMAND_IFSCT, "ifs_n", "List the number of interfaces present in server_opt.ifs", 0},
|
||||||
|
{COMMAND_QUIT, "quit", "Exit the console", 0},
|
||||||
else if(strncmp(request,"ifs", (int)strlen(request)) == 0)
|
{COMMAND_CONSUPTIME,"console_uptime", "Get the uptime of this console", 0},
|
||||||
return 0;
|
};
|
||||||
|
|
||||||
else if(strncmp(request,"ifs_n", (int)strlen(request)) == 0)
|
|
||||||
return 0;
|
command_t
|
||||||
|
command_parse(char *request)
|
||||||
else {
|
{
|
||||||
printf("Incorrect or unreadable command, Please correct it.\n");
|
if (strlen(request) > BUFFER_LENGTH) {
|
||||||
return -1;
|
printf("Error: Command longer than 250 bytes.\n");
|
||||||
}
|
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]) {
|
|
||||||
|
|
||||||
char remove = 'a';
|
|
||||||
|
|
||||||
char* c;
|
void
|
||||||
char* pPosition;
|
response_cleanup(char response[BUFFER_LENGTH])
|
||||||
while((pPosition = strchr(response, 'a')) != NULL) {
|
{
|
||||||
if ((c = index(response, remove)) != NULL) {
|
char remove = 'a';
|
||||||
size_t len_left = sizeof(response) - (c+1-response);
|
|
||||||
memmove(c, c+1, len_left);
|
char* c;
|
||||||
}
|
char* pPosition;
|
||||||
}
|
while((pPosition = strchr(response, 'a')) != NULL) {
|
||||||
printf("Sent and received Successfully!\n The Response was: %s", response);
|
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 */
|
/* Sends and receives to ntkd */
|
||||||
void ntkd_request(char *request) {
|
void
|
||||||
|
ntkd_request(char *request)
|
||||||
|
{
|
||||||
|
rc = connect(sockfd, (struct sockaddr *)&serveraddr, sizeof(serveraddr));
|
||||||
|
if (rc < 0) {
|
||||||
|
perror("connect() failed");
|
||||||
|
exit(-1);
|
||||||
|
}
|
||||||
|
|
||||||
int request_length;
|
int request_length = strlen(request);
|
||||||
|
memset(request, 'a', BUFFER_LENGTH - request_length);
|
||||||
rc = connect(sockfd, (struct sockaddr *)&serveraddr, SUN_LEN(&serveraddr));
|
rc = send(sockfd, request, sizeof(request), 0);
|
||||||
if (rc < 0) {
|
if (rc < 0) {
|
||||||
perror("connect() failed");
|
perror("send() failed");
|
||||||
exit(-1);
|
exit(-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
request_length = (int)strlen(request);
|
|
||||||
memset(request, 'a', BUFFER_LENGTH - request_length);
|
|
||||||
rc = send(sockfd, request, sizeof(request), 0);
|
|
||||||
if (rc < 0) {
|
|
||||||
perror("send() failed");
|
|
||||||
exit(-1);
|
|
||||||
}
|
|
||||||
|
|
||||||
bytesReceived = 0;
|
bytesReceived = 0;
|
||||||
while (bytesReceived < BUFFER_LENGTH) {
|
while (bytesReceived < BUFFER_LENGTH) {
|
||||||
rc = recv(sockfd, & response[bytesReceived],
|
rc = recv(sockfd, & response[bytesReceived],
|
||||||
BUFFER_LENGTH - bytesReceived, 0);
|
BUFFER_LENGTH - bytesReceived, 0);
|
||||||
if (rc < 0) {
|
if (rc < 0) {
|
||||||
perror("recv() failed");
|
perror("recv() failed");
|
||||||
exit(-1);
|
exit(-1);
|
||||||
}
|
}
|
||||||
else if (rc == 0) {
|
else if (rc == 0) {
|
||||||
printf("The server closed the connection\n");
|
printf("The server closed the connection\n");
|
||||||
exit(-1);
|
exit(-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Increment the number of bytes that have been received so far */
|
/* Increment the number of bytes that have been received so far */
|
||||||
bytesReceived += rc;
|
bytesReceived += rc;
|
||||||
}
|
}
|
||||||
response_cleanup(response);
|
response_cleanup(response);
|
||||||
}
|
}
|
||||||
|
|
||||||
void opensocket(void) {
|
|
||||||
|
void
|
||||||
sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
|
opensocket(void)
|
||||||
if (sockfd < 0) {
|
{
|
||||||
perror("socket creation failed");
|
sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
|
||||||
exit(-1);
|
if (sockfd < 0) {
|
||||||
}
|
perror("socket creation failed");
|
||||||
|
exit(-1);
|
||||||
memset(&serveraddr, 0, sizeof(serveraddr));
|
}
|
||||||
serveraddr.sun_family = AF_UNIX;
|
|
||||||
strcpy(serveraddr.sun_path, SERVER_PATH);
|
memset(&serveraddr, 0, sizeof(serveraddr));
|
||||||
|
serveraddr.sun_family = AF_UNIX;
|
||||||
|
strcpy(serveraddr.sun_path, SERVER_PATH);
|
||||||
}
|
}
|
||||||
|
|
||||||
void console_uptime(void) {
|
|
||||||
|
void
|
||||||
int uptime_sec1;
|
console_uptime(void)
|
||||||
int uptime_min1;
|
{
|
||||||
int uptime_hour1;
|
int uptime_sec1;
|
||||||
|
int uptime_min1;
|
||||||
int uptime_day1;
|
int uptime_hour1;
|
||||||
int uptime_month1;
|
|
||||||
int uptime_year1;
|
int uptime_day1;
|
||||||
|
int uptime_month1;
|
||||||
time(&rawtime);
|
int uptime_year1;
|
||||||
|
|
||||||
timeinfo = localtime(&rawtime);
|
time(&rawtime);
|
||||||
|
|
||||||
uptime_sec1 = timeinfo->tm_sec;
|
timeinfo = localtime(&rawtime);
|
||||||
uptime_min1 = timeinfo->tm_min;
|
|
||||||
uptime_hour1 = timeinfo->tm_hour;
|
uptime_sec1 = timeinfo->tm_sec;
|
||||||
|
uptime_min1 = timeinfo->tm_min;
|
||||||
uptime_day1 = timeinfo->tm_mday;
|
uptime_hour1 = timeinfo->tm_hour;
|
||||||
uptime_month1 = timeinfo->tm_mon;
|
|
||||||
uptime_year1 = timeinfo->tm_year;
|
uptime_day1 = timeinfo->tm_mday;
|
||||||
|
uptime_month1 = timeinfo->tm_mon;
|
||||||
uptime_sec1 -= uptime_sec;
|
uptime_year1 = timeinfo->tm_year;
|
||||||
uptime_min1 -= uptime_min;
|
|
||||||
uptime_hour1 -= uptime_hour;
|
uptime_sec1 -= uptime_sec;
|
||||||
|
uptime_min1 -= uptime_min;
|
||||||
uptime_day1 -= uptime_day;
|
uptime_hour1 -= uptime_hour;
|
||||||
uptime_month1 -= uptime_month;
|
|
||||||
uptime_year1 -= uptime_year;
|
uptime_day1 -= uptime_day;
|
||||||
|
uptime_month1 -= uptime_month;
|
||||||
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);
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
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)
|
|
||||||
printf("Error: Command has not been processed!\n");
|
|
||||||
|
|
||||||
if(validity_check(request) == -1)
|
|
||||||
usage();
|
|
||||||
|
|
||||||
if(strncmp(request,"quit", (int)strlen(request)) == 0) {
|
|
||||||
clean_up();
|
|
||||||
exit(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
if(validity_check(request) == 0) {
|
|
||||||
ntkd_request(request);
|
|
||||||
millisleep(200);
|
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
|
||||||
ntkd_request(request);
|
|
||||||
}
|
|
||||||
|
|
||||||
if(validity_check(request) == 4)
|
void
|
||||||
console_uptime();
|
console(char* request)
|
||||||
|
{
|
||||||
if(validity_check(request) == 5)
|
command_t commandID = command_parse(request);
|
||||||
printf("Error: Command longer than 250 bytes.\n");
|
|
||||||
|
switch (commandID) {
|
||||||
|
case COMMAND_QUIT:
|
||||||
|
clean_up();
|
||||||
|
exit(0);
|
||||||
|
case COMMAND_UPTIME:
|
||||||
|
case COMMAND_INETCONN:
|
||||||
|
case COMMAND_CURIFS:
|
||||||
|
case COMMAND_CURIFSCT:
|
||||||
|
case COMMAND_CURQSPNID:
|
||||||
|
case COMMAND_CURIP:
|
||||||
|
case COMMAND_CURNODE:
|
||||||
|
case COMMAND_IFS:
|
||||||
|
case COMMAND_IFSCT:
|
||||||
|
ntkd_request(request);
|
||||||
|
millisleep(200);
|
||||||
|
break;
|
||||||
|
case COMMAND_VERSION:
|
||||||
|
printf("Ntk-Console Version: %s\n", VERSION_STR);
|
||||||
|
ntkd_request(request);
|
||||||
|
break;
|
||||||
|
case COMMAND_CONSUPTIME:
|
||||||
|
console_uptime();
|
||||||
|
break;
|
||||||
|
case COMMAND_KILL:
|
||||||
|
system("ntkd -k");
|
||||||
|
break;
|
||||||
|
case COMMAND_HELP:
|
||||||
|
default:
|
||||||
|
usage();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(void) {
|
|
||||||
|
|
||||||
time(&rawtime);
|
|
||||||
|
|
||||||
timeinfo = localtime(&rawtime);
|
|
||||||
|
|
||||||
uptime_sec = timeinfo->tm_sec;
|
|
||||||
uptime_min = timeinfo->tm_min;
|
|
||||||
uptime_hour = timeinfo->tm_hour;
|
|
||||||
uptime_day = timeinfo->tm_mday;
|
|
||||||
uptime_month = timeinfo->tm_mon;
|
|
||||||
uptime_year = timeinfo->tm_year;
|
|
||||||
|
|
||||||
opensocket();
|
|
||||||
|
|
||||||
printf("This is the Netsukuku Console, Please type 'help' for more information.\n");
|
|
||||||
|
|
||||||
char * request;
|
|
||||||
|
|
||||||
char request1;
|
int
|
||||||
|
main(void)
|
||||||
request = (char *)malloc(BUFFER_LENGTH);
|
{
|
||||||
|
time(&rawtime);
|
||||||
request1 = (char)malloc(BUFFER_LENGTH);
|
|
||||||
|
timeinfo = localtime(&rawtime);
|
||||||
do {
|
|
||||||
|
uptime_sec = timeinfo->tm_sec;
|
||||||
printf("\n>");
|
uptime_min = timeinfo->tm_min;
|
||||||
|
uptime_hour = timeinfo->tm_hour;
|
||||||
fgets(request, 16, stdin);
|
uptime_day = timeinfo->tm_mday;
|
||||||
|
uptime_month = timeinfo->tm_mon;
|
||||||
fflush(stdin);
|
uptime_year = timeinfo->tm_year;
|
||||||
|
|
||||||
request = request1;
|
opensocket();
|
||||||
|
|
||||||
console(request);
|
printf("This is the Netsukuku Console, Please type 'help' for more information.\n");
|
||||||
} while(FALSE);
|
|
||||||
|
char* request = (char*)malloc(BUFFER_LENGTH);
|
||||||
clean_up();
|
|
||||||
return 0;
|
do {
|
||||||
|
printf("\n>");
|
||||||
|
fgets(request, 16, stdin);
|
||||||
|
fflush(stdin);
|
||||||
|
console(request);
|
||||||
|
} while(FALSE);
|
||||||
|
|
||||||
|
clean_up();
|
||||||
|
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"
|
" quit Exits this program\n"
|
||||||
" quit Exits this program\n"
|
" console_uptime Gets the uptime of this console\n");
|
||||||
" console_uptime Gets the uptime of this console\n");
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
||||||
|
|
||||||
setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (void*) &optVal, optLen);
|
setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (void*) &optVal, optLen);
|
||||||
setsockopt(sockfd1, SOL_SOCKET, SO_REUSEADDR, (void*) &optVal, optLen);
|
setsockopt(sockfd1, SOL_SOCKET, SO_REUSEADDR, (void*) &optVal, optLen);
|
||||||
|
|
||||||
if (sockfd != -1)
|
if (sockfd != -1)
|
||||||
close(sockfd);
|
close(sockfd);
|
||||||
|
|
||||||
if (sockfd1 != -1)
|
if (sockfd1 != -1)
|
||||||
close(sockfd1);
|
close(sockfd1);
|
||||||
|
|
||||||
unlink(SERVER_PATH);
|
unlink(SERVER_PATH);
|
||||||
|
}
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user