ntk-console: Cleanup commands, accept command packets

* console commands now read as standardized struct packets
* responses are still character arrays
This commit is contained in:
Alexander von Gluck IV 2014-09-14 15:38:40 -05:00
parent 84e377eff5
commit eb12395dcb
3 changed files with 60 additions and 70 deletions

View File

@ -22,6 +22,7 @@
#define CONSOLE_SOCKET_PATH "/tmp/ntk-console"
#define CONSOLE_VERSION_MAJOR 0
#define CONSOLE_VERSION_MINOR 3
#define CONSOLE_ARGV_LENGTH 250
#define CONSOLE_BUFFER_LENGTH 250
#ifndef TRUE
@ -48,4 +49,12 @@ typedef enum {
} command_t;
#pragma pack(1)
typedef struct {
command_t command;
char* argv[CONSOLE_ARGV_LENGTH];
} cmd_packet_t;
#pragma pack(0)
#endif /* CONSOLE_H */

View File

@ -91,82 +91,64 @@ send_response(int session_fd, char response[CONSOLE_BUFFER_LENGTH], ...)
* to data from ntkd structures such as: me
* into a response for the ntk console client. */
static int
request_processing(int session_fd, char unprocessed_request[CONSOLE_BUFFER_LENGTH])
static void
request_processing(int session_fd, cmd_packet_t packet)
{
if(strncmp(unprocessed_request,"uptime", (int)strlen(unprocessed_request)) == 0)
send_response(session_fd, (char)time(0)-me.uptime);
else if(strncmp(unprocessed_request,"version", (int)strlen(unprocessed_request)) == 0)
send_response(session_fd, VERSION_STR);
else if(strncmp(unprocessed_request,"inet_connected", (int)strlen(unprocessed_request)) == 0)
send_response(session_fd, (char)me.inet_connected);
else if(strncmp(unprocessed_request,"cur_ifs", (int)strlen(unprocessed_request)) == 0)
send_response(session_fd, (char)me.cur_ifs);
else if(strncmp(unprocessed_request,"cur_ifs_n", (int)strlen(unprocessed_request)) == 0)
send_response(session_fd, (char)me.cur_ifs_n);
else if(strncmp(unprocessed_request,"cur_qspn_id", (int)strlen(unprocessed_request)) == 0)
send_response(session_fd, (char)me.cur_qspn_id);
else if(strncmp(unprocessed_request,"cur_ip", (int)strlen(unprocessed_request)) == 0)
send_response(session_fd, (char)me.cur_ip.data);
/*else if(strncmp(unprocessed_request,"cur_node", (int)strlen(unprocessed_request)) == 0)
send_response(me.cur_node);
else if(strncmp(unprocessed_request,"ifs", (int)strlen(unprocessed_request)) == 0)
return 0;
else if(strncmp(unprocessed_request,"ifs_n", (int)strlen(unprocessed_request)) == 0)
return 0;*/
send_response(session_fd, unprocessed_request, " Is invalid or yet to be implemented.");
return -1;
}
static int
request_receive(int sock, char message[], int max)
{
int total = 0;
const int bsize = 1024;
char buffer[bsize+1];
int read = bsize;
message[0] = 0; // initialize for strcat()
while(read == bsize) {
read = recv(sock, buffer, bsize, 0);
if(read < 0)
return -1; // error, bail out
total += read;
if(total > max)
return -2; // overflow
buffer[read] = 0;
strcat(message, buffer);
switch (packet.command) {
case COMMAND_UPTIME:
send_response(session_fd, (char)time(0)-me.uptime);
break;
case COMMAND_VERSION:
send_response(session_fd, VERSION_STR);
break;
case COMMAND_CURIFS:
send_response(session_fd, (char)me.cur_ifs);
break;
case COMMAND_CURIFSCT:
send_response(session_fd, (char)me.cur_ifs_n);
break;
case COMMAND_INETCONN:
send_response(session_fd, (char)me.inet_connected);
break;
case COMMAND_CURQSPNID:
send_response(session_fd, (char)me.cur_qspn_id);
break;
case COMMAND_CURIP:
send_response(session_fd, (char)me.cur_ip.data);
break;
case COMMAND_CURNODE:
send_response(session_fd, (char)me.cur_node);
break;
case COMMAND_IFS:
send_response(session_fd, "IFS: TODO");
break;
case COMMAND_IFSCT:
send_response(session_fd, "IFS: TODO");
break;
default:
send_response(session_fd, "Provided command is invalid or yet to be implemented.");
break;
}
return total;
}
static void
handle_session(int session_fd)
{
char request[CONSOLE_BUFFER_LENGTH];
cmd_packet_t packetIn;
rc = request_receive(session_fd, request, CONSOLE_BUFFER_LENGTH);
if (rc < 0) {
rc = recv(session_fd, &packetIn, sizeof(packetIn), 0);
printf("%d bytes of data were received\n", rc);
if (rc < sizeof(packetIn)) {
perror("recv() failed");
exit(-1);
}
printf("%d bytes of data were received\n", rc);
printf("0x%x command received\n", packetIn.command);
request_processing(session_fd, request);
request_processing(session_fd, packetIn);
}

View File

@ -100,19 +100,18 @@ request_receive(int sock, char message[], int max)
/* Sends and receives to ntkd */
void
ntkd_request(char *request)
ntkd_request(command_t command)
{
if (sockfd <= 0) {
perror("ntkd connection closed unexpectedly!\n");
exit(-1);
}
int request_length = strlen(request) - 1;
request[request_length] = '\0';
cmd_packet_t packetOut;
packetOut.command = command;
printf("request: '%s'\n", request);
rc = send(sockfd, request, request_length, 0);
if (rc < 0) {
rc = send(sockfd, &packetOut, sizeof(packetOut), 0);
if (rc < sizeof(packetOut)) {
perror("send() failed");
exit(-1);
}
@ -228,13 +227,13 @@ console(char* request)
case COMMAND_CURNODE:
case COMMAND_IFS:
case COMMAND_IFSCT:
ntkd_request(request);
ntkd_request(commandID);
millisleep(200);
break;
case COMMAND_VERSION:
printf("ntk-console version: %d.%d\n",
CONSOLE_VERSION_MAJOR, CONSOLE_VERSION_MINOR);
ntkd_request(request);
ntkd_request(commandID);
break;
case COMMAND_CONSUPTIME:
console_uptime();