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!
This commit is contained in:
MissValeska 2014-08-15 22:51:54 -07:00
parent bd9a10620f
commit b13c3bf5de
4 changed files with 167 additions and 78 deletions

View File

@ -1,12 +1,12 @@
#include "Netsukuku-Console.h" #include "Netsukuku-Console.h"
char *response; char response[250];
void usage(); void usage();
void clean_up(); void clean_up();
int validity_check(char *request) { int validity_check(char request) {
if(strncmp(request,"help", (int)strlen(request)) == 0) if(strncmp(request,"help", (int)strlen(request)) == 0)
return 1; return 1;
@ -56,31 +56,63 @@ int validity_check(char *request) {
} }
void response_cleanup(char response[250]) {
char remove = 'a';
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 */ /* Sends and receives to ntkd */
void ntkd_request(char *request) { void ntkd_request(char request) {
rc = sendto(sockfd1, request, strlen(request), 0, (struct sockaddr *)&serveraddr, (socklen_t)sizeof(&serveraddr)); int request_length;
rc = connect(sockfd, (struct sockaddr *)&serveraddr, SUN_LEN(&serveraddr));
if (rc < 0) { if (rc < 0) {
perror("sendto() failed"); perror("connect() failed");
exit(-1); 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) { if (rc < 0) {
perror("recvfrom() failed"); perror("send() failed");
exit(-1); exit(-1);
} }
if(rc >= 0) { bytesReceived = 0;
printf("Sent and received Successfully!\n The Response was) %s", response); 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) { void opensocket(void) {
int stop_trying;
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");
@ -90,20 +122,6 @@ void opensocket(void) {
memset(&serveraddr, 0, sizeof(serveraddr)); memset(&serveraddr, 0, sizeof(serveraddr));
serveraddr.sun_family = AF_UNIX; serveraddr.sun_family = AF_UNIX;
strcpy(serveraddr.sun_path, SERVER_PATH); 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) { 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) if(validity_check(request) == -2)
printf("Error: Command has not been processed!"); 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"); 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>"); printf("\n>");
@ -204,12 +222,15 @@ int main(void) {
fflush(stdin); fflush(stdin);
request[strlen(request)-1] = '\0'; request1[strlen(request)-1] = '\0';
strcpy(request, request1);
console(request); console(request);
} } while(FALSE);
return 0; clean_up();
return 0;
} }
void usage(void) { void usage(void) {

View File

@ -13,13 +13,13 @@
#include <errno.h> #include <errno.h>
#define SERVER_PATH "/tmp/ntk-console" #define SERVER_PATH "/tmp/ntk-console"
#define BUFFER_LENGTH 250
#define VERSION_STR "0.0.2" #define VERSION_STR "0.0.2"
#define FALSE 0 #define FALSE 0
int sockfd, sockfd1; int sockfd = -1, sockfd1 = -1;
struct sockaddr_un serveraddr; struct sockaddr_un serveraddr;
struct sockaddr ntkdaddr; int rc, bytesReceived;
int rc;
time_t rawtime; time_t rawtime;
struct tm *timeinfo; struct tm *timeinfo;

View File

@ -177,6 +177,7 @@ void usage(void)
" -h Shows this help\n" " -h Shows this help\n"
" -v Shows the version you are using\n" " -v Shows the version you are using\n"
" -k Kills the running instance of ntkd\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"); " -e Excludes an interface from usage I.E all interfaces except this one\n");
} }

View File

@ -1,20 +1,31 @@
/* Header files required for the console bindings
* not included outside of this file. */
#include <utmp.h> #include <utmp.h>
#include <sys/un.h> #include <sys/un.h>
#include <unistd.h> #include <unistd.h>
#include "netsukuku.h" #include "netsukuku.h"
/* Constants used for the console bindings. */
#define SERVER_PATH "/tmp/ntk-console" #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_un serveraddr;
struct sockaddr ntkdaddr; int rc, length;
int rc;
int millisleep(unsigned ms) /* Cleans up the console bindings for closing, Closes socket file descriptors,
{ * unlinks the server path, etc. */
return usleep(1000 * ms);
}
void clean_up(void) { 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) { void opensocket(void) {
int stop_trying; int stop_trying;
@ -63,38 +76,66 @@ void opensocket(void) {
} }
} }
void send_response(char *response, ...) { /* Sends a parsed response to the ntk console client. */
rc = sendto(sockfd_1, response, strlen(response), 0, (struct sockaddr *)&serveraddr, (socklen_t)sizeof(&serveraddr)); void send_response(char response) {
if (rc < 0) { int response_length;
perror("sendto() failed");
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); 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) 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) else if(strncmp(unprocessed_request,"version", (int)strlen(unprocessed_request)) == 0)
send_response(VERSION_STR); send_response(VERSION_STR);
else if(strncmp(unprocessed_request,"inet_connected", (int)strlen(unprocessed_request)) == 0) 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) 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) 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) 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) 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) /*else if(strncmp(unprocessed_request,"cur_node", (int)strlen(unprocessed_request)) == 0)
send_response(me.cur_node); send_response(me.cur_node);
@ -108,29 +149,55 @@ int request_processing(char *unprocessed_request) {
return -1; 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) { void ntkd_request(void) {
char *request; char request[REQUEST_LENGTH];
while(0 == 0) { do {
millisleep(100); rc = listen(sockfd_1, 10);
if (rc< 0) {
request = 0; perror("listen() failed");
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); exit(-1);
} }
if(request != 0) printf("Ready for client connect().\n");
request_processing(request);
sockfd_2 = accept(sockfd_1, NULL, NULL);
if (sd2 < 0) {
perror("accept() failed");
exit(-1);
} }
}
int console_recv_send(void) { 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);
}
rc = recv(sockfd_2, request, sizeof(request), 0);
if (rc < 0) {
perror("recv() failed");
exit(-1);
}
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);
}
request_cleanup(request);
} while(FALSE);
clean_up();
} }