Browse Source

ntk-console: Cleanup commands, accept command packets

* console commands now read as standardized struct packets
* responses are still character arrays
Alexander von Gluck IV 10 years ago
parent
commit
eb12395dcb
3 changed files with 60 additions and 70 deletions
  1. 9
    0
      src/console.h
  2. 44
    62
      src/ntk-console-server.c
  3. 7
    8
      src/ntk-console.c

+ 9
- 0
src/console.h View File

@@ -22,6 +22,7 @@
22 22
 #define CONSOLE_SOCKET_PATH 	"/tmp/ntk-console"
23 23
 #define CONSOLE_VERSION_MAJOR	0
24 24
 #define CONSOLE_VERSION_MINOR 	3
25
+#define CONSOLE_ARGV_LENGTH 	250
25 26
 #define CONSOLE_BUFFER_LENGTH 	250
26 27
 
27 28
 #ifndef TRUE
@@ -48,4 +49,12 @@ typedef enum {
48 49
 } command_t;
49 50
 
50 51
 
52
+#pragma pack(1)
53
+typedef struct {
54
+	command_t command;
55
+	char* argv[CONSOLE_ARGV_LENGTH];
56
+} cmd_packet_t;
57
+#pragma pack(0)
58
+
59
+
51 60
 #endif /* CONSOLE_H */

+ 44
- 62
src/ntk-console-server.c View File

@@ -91,82 +91,64 @@ send_response(int session_fd, char response[CONSOLE_BUFFER_LENGTH], ...)
91 91
  * to data from ntkd structures such as: me 
92 92
  * into a response for the ntk console client. */
93 93
 
94
-static int
95
-request_processing(int session_fd, char unprocessed_request[CONSOLE_BUFFER_LENGTH])
96
-{
97
-	if(strncmp(unprocessed_request,"uptime", (int)strlen(unprocessed_request))  == 0)
98
-		send_response(session_fd, (char)time(0)-me.uptime);
99
-	
100
-	else if(strncmp(unprocessed_request,"version", (int)strlen(unprocessed_request))  == 0)
101
-		send_response(session_fd, VERSION_STR);
102
-	
103
-	else if(strncmp(unprocessed_request,"inet_connected", (int)strlen(unprocessed_request))  == 0)
104
-		send_response(session_fd, (char)me.inet_connected);
105
-	
106
-	else if(strncmp(unprocessed_request,"cur_ifs", (int)strlen(unprocessed_request))  == 0)
107
-		send_response(session_fd, (char)me.cur_ifs);
108
-		
109
-	else if(strncmp(unprocessed_request,"cur_ifs_n", (int)strlen(unprocessed_request))  == 0)
110
-		send_response(session_fd, (char)me.cur_ifs_n);
111
-		
112
-	else if(strncmp(unprocessed_request,"cur_qspn_id", (int)strlen(unprocessed_request))  == 0)
113
-		send_response(session_fd, (char)me.cur_qspn_id);
114
-		
115
-	else if(strncmp(unprocessed_request,"cur_ip", (int)strlen(unprocessed_request))  == 0)
116
-		send_response(session_fd, (char)me.cur_ip.data);
117
-		
118
-	/*else if(strncmp(unprocessed_request,"cur_node", (int)strlen(unprocessed_request))  == 0)
119
-		send_response(me.cur_node);
120
-		
121
-	else if(strncmp(unprocessed_request,"ifs", (int)strlen(unprocessed_request))  == 0)
122
-		return 0;
123
-		
124
-	else if(strncmp(unprocessed_request,"ifs_n", (int)strlen(unprocessed_request))  == 0)
125
-		return 0;*/
126
-	send_response(session_fd, unprocessed_request, " Is invalid or yet to be implemented.");
127
-	return -1;
128
-}
129
-
130
-
131
-static int
132
-request_receive(int sock, char message[], int max)
94
+static void
95
+request_processing(int session_fd, cmd_packet_t packet)
133 96
 {
134
-	int total = 0;
135
-	const int bsize = 1024;
136
-	char buffer[bsize+1];
137
-	int read = bsize;
138
-	 
139
-	message[0] = 0; // initialize for strcat()
140
-	
141
-	while(read == bsize) {
142
-		read = recv(sock, buffer, bsize, 0);
143
-		if(read < 0)
144
-			return -1; // error, bail out
145
-		total += read;
146
-		if(total > max)
147
-			return -2; // overflow
148
-		buffer[read] = 0;
149
-		strcat(message, buffer);
97
+	switch (packet.command) {
98
+		case COMMAND_UPTIME:
99
+			send_response(session_fd, (char)time(0)-me.uptime);
100
+			break;
101
+		case COMMAND_VERSION:
102
+			send_response(session_fd, VERSION_STR);
103
+			break;
104
+		case COMMAND_CURIFS:
105
+			send_response(session_fd, (char)me.cur_ifs);
106
+			break;
107
+		case COMMAND_CURIFSCT:
108
+			send_response(session_fd, (char)me.cur_ifs_n);
109
+			break;
110
+		case COMMAND_INETCONN:
111
+			send_response(session_fd, (char)me.inet_connected);
112
+			break;
113
+		case COMMAND_CURQSPNID:
114
+			send_response(session_fd, (char)me.cur_qspn_id);
115
+			break;
116
+		case COMMAND_CURIP:
117
+			send_response(session_fd, (char)me.cur_ip.data);
118
+			break;
119
+		case COMMAND_CURNODE:
120
+			send_response(session_fd, (char)me.cur_node);
121
+			break;
122
+		case COMMAND_IFS:
123
+			send_response(session_fd, "IFS: TODO");
124
+			break;
125
+		case COMMAND_IFSCT:
126
+			send_response(session_fd, "IFS: TODO");
127
+			break;
128
+		default:
129
+			send_response(session_fd, "Provided command is invalid or yet to be implemented.");
130
+			break;
150 131
 	}
151
-	     
152
-	return total;
153 132
 }
154 133
 
155 134
 
135
+
156 136
 static void
157 137
 handle_session(int session_fd)
158 138
 {
159
-	char request[CONSOLE_BUFFER_LENGTH];
139
+	cmd_packet_t packetIn;
160 140
 
161
-	rc = request_receive(session_fd, request, CONSOLE_BUFFER_LENGTH);
162
-	if (rc < 0) {
141
+	rc = recv(session_fd, &packetIn, sizeof(packetIn), 0);
142
+
143
+	printf("%d bytes of data were received\n", rc);
144
+	if (rc < sizeof(packetIn)) {
163 145
 		perror("recv() failed");
164 146
 		exit(-1);
165 147
 	}
166 148
 	
167
-	printf("%d bytes of data were received\n", rc);
149
+	printf("0x%x command received\n", packetIn.command);
168 150
 	
169
-	request_processing(session_fd, request);
151
+	request_processing(session_fd, packetIn);
170 152
 }
171 153
 
172 154
 

+ 7
- 8
src/ntk-console.c View File

@@ -100,19 +100,18 @@ request_receive(int sock, char message[], int max)
100 100
 
101 101
 /* Sends and receives to ntkd */
102 102
 void
103
-ntkd_request(char *request)
103
+ntkd_request(command_t command)
104 104
 {
105 105
 	if (sockfd <= 0) {
106 106
 		perror("ntkd connection closed unexpectedly!\n");
107 107
 		exit(-1);
108 108
 	}
109 109
 
110
-	int request_length = strlen(request) - 1;
111
-	request[request_length] = '\0';
110
+	cmd_packet_t packetOut;
111
+	packetOut.command = command;
112 112
 
113
-	printf("request: '%s'\n", request);
114
-	rc = send(sockfd, request, request_length, 0);
115
-	if (rc < 0) {
113
+	rc = send(sockfd, &packetOut, sizeof(packetOut), 0);
114
+	if (rc < sizeof(packetOut)) {
116 115
 		perror("send() failed");
117 116
 		exit(-1);
118 117
 	}
@@ -228,13 +227,13 @@ console(char* request)
228 227
 		case COMMAND_CURNODE:
229 228
 		case COMMAND_IFS:
230 229
 		case COMMAND_IFSCT:
231
-			ntkd_request(request);
230
+			ntkd_request(commandID);
232 231
 			millisleep(200);
233 232
 			break;
234 233
 		case COMMAND_VERSION:
235 234
 			printf("ntk-console version: %d.%d\n",
236 235
 				CONSOLE_VERSION_MAJOR, CONSOLE_VERSION_MINOR);
237
-			ntkd_request(request);
236
+			ntkd_request(commandID);
238 237
 			break;
239 238
 		case COMMAND_CONSUPTIME:
240 239
 			console_uptime();

Loading…
Cancel
Save