Browse Source

Ntk-console: Stop using fixed buffer width.

* Detect end of string in console and act on that.
* Open console connection on startup
* Terminate console connection on exit
Alexander von Gluck IV 10 years ago
parent
commit
bc3f496788
3 changed files with 248 additions and 260 deletions
  1. 69
    99
      src/Ntk-Console/Netsukuku-Console.c
  2. 36
    5
      src/Ntk-Console/Netsukuku-Console.h
  3. 143
    156
      src/ntk-console-bindings.c

+ 69
- 99
src/Ntk-Console/Netsukuku-Console.c View File

@@ -1,28 +1,10 @@
1
-#include "Netsukuku-Console.h"
2
-
3
-char response[BUFFER_LENGTH];
4 1
 
5
-void usage();
2
+#include "Netsukuku-Console.h"
6 3
 
7
-void clean_up();
4
+#include <unistd.h>
8 5
 
9 6
 
10
-typedef enum {
11
-	COMMAND_HELP = 0x100,
12
-	COMMAND_UPTIME,
13
-	COMMAND_KILL,
14
-	COMMAND_VERSION,
15
-	COMMAND_INETCONN,
16
-	COMMAND_CURIFS,
17
-	COMMAND_CURIFSCT,
18
-	COMMAND_CURQSPNID,
19
-	COMMAND_CURIP,
20
-	COMMAND_CURNODE,
21
-	COMMAND_IFS,
22
-	COMMAND_IFSCT,
23
-	COMMAND_QUIT,
24
-	COMMAND_CONSUPTIME,
25
-} command_t;
7
+char response[BUFFER_LENGTH];
26 8
 
27 9
 
28 10
 const struct supported_commands {
@@ -69,21 +51,28 @@ command_parse(char *request)
69 51
 }
70 52
 
71 53
 
72
-void
73
-response_cleanup(char response[BUFFER_LENGTH])
54
+static int
55
+request_receive(int sock, char message[], int max)
74 56
 {
75
-	char remove = 'a';
76
-
77
-	char* c;
78
-	char* pPosition;
79
-	while((pPosition = strchr(response, 'a')) != NULL)  {
80
-		if ((c = index(response, remove)) != NULL) {
81
-		size_t len_left = sizeof(response) - (c+1-response);
82
-		memmove(c, c+1, len_left);
83
-		}
57
+	int total = 0;
58
+	const int bsize = 1024;
59
+	char buffer[bsize+1];
60
+	int read = bsize;
61
+	
62
+	message[0] = 0; // initialize for strcat()
63
+	
64
+	while(read == bsize) {
65
+		read = recv(sock, buffer, bsize, 0);
66
+		if(read < 0)
67
+		    return -1; // error, bail out
68
+		total += read;
69
+		if(total > max)
70
+		    return -2; // overflow
71
+		buffer[read] = 0;
72
+		strcat(message, buffer);
84 73
 	}
85 74
 
86
-	printf("Sent and received Successfully!\n The Response was: %s", response);
75
+    return total;
87 76
 }
88 77
 
89 78
 
@@ -91,37 +80,28 @@ response_cleanup(char response[BUFFER_LENGTH])
91 80
 void
92 81
 ntkd_request(char *request)
93 82
 {
94
-	rc = connect(sockfd, (struct sockaddr *)&serveraddr, sizeof(serveraddr));
95
-	if (rc < 0) {
96
-		perror("connect() failed");
83
+	if (sockfd <= 0) {
84
+		perror("ntkd connection closed unexpectedly!\n");
97 85
 		exit(-1);
98 86
 	}
99 87
 
100
-	int request_length = strlen(request);
101
-	memset(request, 'a', BUFFER_LENGTH - request_length);
102
-	rc = send(sockfd, request, sizeof(request), 0);
88
+	int request_length = strlen(request) - 1;
89
+	request[request_length] = '\0';
90
+
91
+	printf("request: '%s'\n", request);
92
+	rc = send(sockfd, request, request_length, 0);
103 93
 	if (rc < 0) {
104 94
 		perror("send() failed");
105 95
 		exit(-1);
106 96
 	}
107 97
 
108
-	bytesReceived = 0;
109
-	while (bytesReceived < BUFFER_LENGTH) {
110
-		rc = recv(sockfd, & response[bytesReceived],
111
-		   BUFFER_LENGTH - bytesReceived, 0);
112
-		if (rc < 0) {
113
-			perror("recv() failed");
114
-			exit(-1);
115
-		}
116
-	else if (rc == 0) {
117
-		printf("The server closed the connection\n");
98
+	request_receive(sockfd, response, BUFFER_LENGTH);
99
+	if (rc < 0) {
100
+		perror("recv() failed");
118 101
 		exit(-1);
119 102
 	}
120 103
 
121
-	/* Increment the number of bytes that have been received so far  */
122
-	bytesReceived += rc;		
123
-	}
124
-	response_cleanup(response);
104
+	printf("%s\n", response);
125 105
 }
126 106
 
127 107
 
@@ -137,6 +117,28 @@ opensocket(void)
137 117
 	memset(&serveraddr, 0, sizeof(serveraddr));
138 118
 	serveraddr.sun_family = AF_UNIX;
139 119
 	strcpy(serveraddr.sun_path, SERVER_PATH);
120
+
121
+	rc = connect(sockfd, (struct sockaddr *)&serveraddr, sizeof(serveraddr));
122
+	if (rc < 0) {
123
+		perror("connect() failed");
124
+		exit(-1);
125
+	}
126
+	printf("ntkd console connection opened successfully.\n");
127
+}
128
+
129
+
130
+void
131
+closesocket(void)
132
+{
133
+    const int optVal = 1;
134
+    const socklen_t optLen = sizeof(optVal);
135
+
136
+    setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (void*) &optVal, optLen);
137
+
138
+	if (sockfd >= 0)
139
+		close(sockfd);
140
+
141
+	printf("ntkd console connection closed.\n");
140 142
 }
141 143
 
142 144
 
@@ -175,10 +177,10 @@ console_uptime(void)
175 177
 }
176 178
 
177 179
 
178
-int
180
+static void
179 181
 millisleep(unsigned ms)
180 182
 {
181
-  return usleep(1000 * ms);
183
+	usleep(1000 * ms);
182 184
 }
183 185
 
184 186
 
@@ -189,7 +191,7 @@ console(char* request)
189 191
 
190 192
 	switch (commandID) {
191 193
 		case COMMAND_QUIT:
192
-			clean_up();
194
+			closesocket();
193 195
 			exit(0);
194 196
 		case COMMAND_UPTIME:
195 197
 		case COMMAND_INETCONN:
@@ -211,6 +213,7 @@ console(char* request)
211 213
 			console_uptime();
212 214
 			break;
213 215
 		case COMMAND_KILL:
216
+			closesocket();
214 217
 			system("ntkd -k");
215 218
 			break;
216 219
 		case COMMAND_HELP:
@@ -234,61 +237,28 @@ main(void)
234 237
 	uptime_month = timeinfo->tm_mon;
235 238
 	uptime_year = timeinfo->tm_year;
236 239
 	
237
-	opensocket();
238
-	
239 240
 	printf("This is the Netsukuku Console, Please type 'help' for more information.\n");
240 241
 	
241 242
 	char* request = (char*)malloc(BUFFER_LENGTH);
242 243
 	
244
+	opensocket();
243 245
 	do {
244
-		printf("\n>");
246
+		printf("\n> ");
245 247
 		fgets(request, 16, stdin);
246 248
 		fflush(stdin);
247 249
 		console(request);
248
-	} while(FALSE);
250
+		memset(request, 0, BUFFER_LENGTH);
251
+	} while(TRUE);
252
+	closesocket();
249 253
 	
250 254
 	clean_up(); 
251 255
 	return 0;
252 256
 }
253 257
 
254 258
 void usage(void) {
255
-	
256
-		printf("Usage\n"
257
-		"  uptime         Returns the time when ntkd finished the hooking,\n" 
258
-		"                 to get the the actual uptime just do)\n"
259
-		"                 time(0)-me.uptime \n"
260
-		"  help           Shows this\n"
261
-		"  kill           Kills the running instance of netsukuku with SIGINT\n\n"
262
-		"  version        Shows the running version of the ntk-console, and ntkd.\n"
263
-		"  inet_connected If it is 1, Ntkd is connected to the Internet\n"
264
-		" \n"
265
-		"  cur_ifs        Lists all of the interfaces in cur_ifs\n"
266
-		"  cur_ifs_n      Lists the number of interfaces present in `cur_ifs'\n"
267
-		"  cur_qspn_id    The current qspn_id we are processing. "
268
-		"                 It is cur_qspn_id[levels] big\n"
269
-		"  cur_ip         Current IP address\n"
270
-		" \n"
271
-		"  cur_node       Current Node\n"
272
-		"  ifs            Lists all of the interfaces in server_opt.ifs\n"
273
-		"  ifs_n          Lists the number of interfaces present in server_opt.ifs\n"
274
-		"  quit           Exits this program\n"
275
-		"  console_uptime Gets the uptime of this console\n");
276
-	
277
-}
278
-
279
-void clean_up(void)
280
-{
281
-	const int optVal = 1;
282
-	const socklen_t optLen = sizeof(optVal);
283
-	
284
-	setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (void*) &optVal, optLen);
285
-	setsockopt(sockfd1, SOL_SOCKET, SO_REUSEADDR, (void*) &optVal, optLen);
286
-	
287
-	if (sockfd != -1)
288
-		close(sockfd);
289
-
290
-	if (sockfd1 != -1)
291
-		close(sockfd1);
292
-   
293
-	unlink(SERVER_PATH);
259
+	printf("Usage:\n");
260
+	for (int i = 0; i < sizeof(kSupportedCommands)
261
+		/ sizeof(kSupportedCommands[0]); i++) {
262
+		printf("  %16s - %s\n", kSupportedCommands[i].command, kSupportedCommands[i].help);
263
+	}
294 264
 }

+ 36
- 5
src/Ntk-Console/Netsukuku-Console.h View File

@@ -12,10 +12,34 @@
12 12
 #include <time.h>
13 13
 #include <errno.h>
14 14
 
15
-#define SERVER_PATH     "/tmp/ntk-console"
16
-#define BUFFER_LENGTH    250
17
-#define VERSION_STR     "0.0.2"
18
-#define FALSE              0
15
+
16
+#define SERVER_PATH 		"/tmp/ntk-console"
17
+#define BUFFER_LENGTH 		250
18
+#define VERSION_STR 		"0.0.2"
19
+
20
+#ifndef TRUE
21
+#define FALSE 				0
22
+#define TRUE 				1
23
+#endif
24
+
25
+
26
+typedef enum {
27
+	COMMAND_HELP = 0x100,
28
+	COMMAND_UPTIME,
29
+	COMMAND_KILL,
30
+	COMMAND_VERSION,
31
+	COMMAND_INETCONN,
32
+	COMMAND_CURIFS,
33
+	COMMAND_CURIFSCT,
34
+	COMMAND_CURQSPNID,
35
+	COMMAND_CURIP,
36
+	COMMAND_CURNODE,
37
+	COMMAND_IFS,
38
+	COMMAND_IFSCT,
39
+	COMMAND_QUIT,
40
+	COMMAND_CONSUPTIME,
41
+} command_t;
42
+
19 43
 
20 44
 int sockfd = -1, sockfd1 = -1;
21 45
 struct sockaddr_un serveraddr;
@@ -33,4 +57,11 @@ int uptime_year;
33 57
 
34 58
 int i;
35 59
 
36
-#endif /*NETSUKUKUCONSOLE_H*/
60
+void usage();
61
+void clean_up();
62
+
63
+void opensocket();
64
+void closesocket();
65
+
66
+
67
+#endif /*NETSUKUKUCONSOLE_H*/

+ 143
- 156
src/ntk-console-bindings.c View File

@@ -9,9 +9,10 @@
9 9
 
10 10
 /* Constants used for the console bindings. */
11 11
 
12
-#define SERVER_PATH     "/tmp/ntk-console"
12
+#define SERVER_PATH	 "/tmp/ntk-console"
13 13
 #define REQUEST_LENGTH   250
14
-#define FALSE           0
14
+#define FALSE 			0
15
+#define TRUE 			1
15 16
 
16 17
 /* Variable and structure defintions, sockfd refers to socket file descriptor
17 18
  * length refers to the required length of the requests that will be sent.
@@ -28,181 +29,167 @@ int rc, length;
28 29
  * unlinks the server path, etc. */
29 30
 
30 31
 void clean_up(void) {
31
-    
32
-    const int optVal = 1;
33
-    const socklen_t optLen = sizeof(optVal);
34
-    
35
-    setsockopt(sockfd_1, SOL_SOCKET, SO_REUSEADDR, (void*) &optVal, optLen);
36
-    setsockopt(sockfd_2, SOL_SOCKET, SO_REUSEADDR, (void*) &optVal, optLen);
37
-    
38
-    if (sockfd_1 != -1)
39
-        close(sockfd_1);
40
-
41
-    if (sockfd_2 != -1)
42
-        close(sockfd_2);
32
+	
33
+	const int optVal = 1;
34
+	const socklen_t optLen = sizeof(optVal);
35
+	
36
+	setsockopt(sockfd_1, SOL_SOCKET, SO_REUSEADDR, (void*) &optVal, optLen);
37
+	setsockopt(sockfd_2, SOL_SOCKET, SO_REUSEADDR, (void*) &optVal, optLen);
38
+	
39
+	if (sockfd_1 != -1)
40
+		close(sockfd_1);
41
+
42
+	if (sockfd_2 != -1)
43
+		close(sockfd_2);
43 44
    
44 45
    unlink(SERVER_PATH);
45
-    
46
+	
46 47
 }
47 48
 
48 49
 /* Creates an AF_UNIX socket and binds it to a local address. */
49 50
 
50 51
 void opensocket(void) {
51
-    
52
-    int stop_trying;
53
-    
54
-    sockfd_1 = socket(AF_UNIX, SOCK_STREAM, 0);
55
-    if (sockfd_1 < 0) {
56
-         perror("socket creation failed");
57
-         exit(-1);
58
-    }
59
-    
60
-    memset(&serveraddr, 0, sizeof(serveraddr));
61
-    serveraddr.sun_family = AF_UNIX;
62
-    strcpy(serveraddr.sun_path, SERVER_PATH);
63
-
64
-    rc = bind(sockfd_1, (struct sockaddr *)&serveraddr, SUN_LEN(&serveraddr));
65
-    if (rc < 0) {
66
-        perror("bind() failed");
67
-        clean_up();
68
-        if(stop_trying >= 2) {
69
-            perror("bind() failed");
70
-            clean_up();
71
-            opensocket();
72
-            exit(-1);
73
-        }
74
-        stop_trying++;
75
-        opensocket();
76
-    }
52
+	
53
+	int stop_trying;
54
+	
55
+	sockfd_1 = socket(AF_UNIX, SOCK_STREAM, 0);
56
+	if (sockfd_1 < 0) {
57
+		 perror("socket creation failed");
58
+		 exit(-1);
59
+	}
60
+	
61
+	memset(&serveraddr, 0, sizeof(serveraddr));
62
+	serveraddr.sun_family = AF_UNIX;
63
+	strcpy(serveraddr.sun_path, SERVER_PATH);
64
+
65
+	rc = bind(sockfd_1, (struct sockaddr *)&serveraddr, SUN_LEN(&serveraddr));
66
+	if (rc < 0) {
67
+		perror("bind() failed");
68
+		clean_up();
69
+		if(stop_trying >= 2) {
70
+			perror("bind() failed");
71
+			clean_up();
72
+			opensocket();
73
+			exit(-1);
74
+		}
75
+		stop_trying++;
76
+		opensocket();
77
+	}
77 78
 }
78 79
 
79 80
 /* Sends a parsed response to the ntk console client. */
80 81
 
81
-void send_response(char response[REQUEST_LENGTH], ...) {
82
-    int response_length;
83
-    
84
-    response_length = (int)strlen(response);
85
-    memset(response, 'a', REQUEST_LENGTH - response_length);
86
-    rc = send(sockfd_2, response, sizeof(response), 0);
87
-        if (rc < 0){
88
-            perror("send() failed");
89
-            exit(-1);
90
-        }
91
-    
82
+void
83
+send_response(char response[REQUEST_LENGTH], ...)
84
+{
85
+	int response_length = (int)strlen(response);
86
+	rc = send(sockfd_2, response, sizeof(response), 0);
87
+	if (rc < 0){
88
+		perror("send() failed");
89
+		exit(-1);
90
+	}
92 91
 }
93 92
 
94
-void request_cleanup(char unprocessed_request[REQUEST_LENGTH]) {
95
-    
96
-    char remove = 'a';
97
-
98
-    char* c;
99
-    int x;
100
-    char* pPosition;
101
-    while(pPosition = strchr(unprocessed_request, 'a') != NULL)  {
102
-        if ((c = index(unprocessed_request, remove)) != NULL) {
103
-        size_t len_left = sizeof(unprocessed_request) - (c+1-unprocessed_request);
104
-        memmove(c, c+1, len_left);
105
-        }
106
-    }
107
-    printf("Cleaned Request is: %s", unprocessed_request);
108
-    
109
-    request_processing(unprocessed_request);
110
-    
111
-}
112 93
 
113 94
 /* Parses the received request from the ntk console client
114 95
  * to data from ntkd structures such as: me 
115 96
  * into a response for the ntk console client. */
116 97
 
117
-int request_processing(char unprocessed_request[REQUEST_LENGTH]) {
118
-    
119
-        if(strncmp(unprocessed_request,"uptime", (int)strlen(unprocessed_request))  == 0)
120
-            send_response((char)time(0)-me.uptime);
121
-        
122
-        else if(strncmp(unprocessed_request,"version", (int)strlen(unprocessed_request))  == 0)
123
-            send_response(VERSION_STR);
124
-        
125
-        else if(strncmp(unprocessed_request,"inet_connected", (int)strlen(unprocessed_request))  == 0)
126
-            send_response((char)me.inet_connected);
127
-        
128
-        else if(strncmp(unprocessed_request,"cur_ifs", (int)strlen(unprocessed_request))  == 0)
129
-            send_response((char)me.cur_ifs);
130
-        
131
-        else if(strncmp(unprocessed_request,"cur_ifs_n", (int)strlen(unprocessed_request))  == 0)
132
-            send_response((char)me.cur_ifs_n);
133
-        
134
-        else if(strncmp(unprocessed_request,"cur_qspn_id", (int)strlen(unprocessed_request))  == 0)
135
-            send_response((char)me.cur_qspn_id);
136
-        
137
-        else if(strncmp(unprocessed_request,"cur_ip", (int)strlen(unprocessed_request))  == 0)
138
-            send_response((char)me.cur_ip.data);
139
-        
140
-        /*else if(strncmp(unprocessed_request,"cur_node", (int)strlen(unprocessed_request))  == 0)
141
-            send_response(me.cur_node);
142
-        
143
-        else if(strncmp(unprocessed_request,"ifs", (int)strlen(unprocessed_request))  == 0)
144
-            return 0;
145
-        
146
-        else if(strncmp(unprocessed_request,"ifs_n", (int)strlen(unprocessed_request))  == 0)
147
-            return 0;*/
148
-    send_response(unprocessed_request, " Is invalid or yet to be implemented.");
149
-    return -1;
98
+int
99
+request_processing(char unprocessed_request[REQUEST_LENGTH])
100
+{
101
+	if(strncmp(unprocessed_request,"uptime", (int)strlen(unprocessed_request))  == 0)
102
+		send_response((char)time(0)-me.uptime);
103
+	
104
+	else if(strncmp(unprocessed_request,"version", (int)strlen(unprocessed_request))  == 0)
105
+		send_response(VERSION_STR);
106
+	
107
+	else if(strncmp(unprocessed_request,"inet_connected", (int)strlen(unprocessed_request))  == 0)
108
+		send_response((char)me.inet_connected);
109
+	
110
+	else if(strncmp(unprocessed_request,"cur_ifs", (int)strlen(unprocessed_request))  == 0)
111
+		send_response((char)me.cur_ifs);
112
+		
113
+	else if(strncmp(unprocessed_request,"cur_ifs_n", (int)strlen(unprocessed_request))  == 0)
114
+		send_response((char)me.cur_ifs_n);
115
+		
116
+	else if(strncmp(unprocessed_request,"cur_qspn_id", (int)strlen(unprocessed_request))  == 0)
117
+		send_response((char)me.cur_qspn_id);
118
+		
119
+	else if(strncmp(unprocessed_request,"cur_ip", (int)strlen(unprocessed_request))  == 0)
120
+		send_response((char)me.cur_ip.data);
121
+		
122
+	/*else if(strncmp(unprocessed_request,"cur_node", (int)strlen(unprocessed_request))  == 0)
123
+		send_response(me.cur_node);
124
+		
125
+	else if(strncmp(unprocessed_request,"ifs", (int)strlen(unprocessed_request))  == 0)
126
+		return 0;
127
+		
128
+	else if(strncmp(unprocessed_request,"ifs_n", (int)strlen(unprocessed_request))  == 0)
129
+		return 0;*/
130
+	send_response(unprocessed_request, " Is invalid or yet to be implemented.");
131
+	return -1;
132
+}
133
+
134
+
135
+static int
136
+request_receive(int sock, char message[], int max)
137
+{
138
+	int total = 0;
139
+	const int bsize = 1024;
140
+	char buffer[bsize+1];
141
+	int read = bsize;
142
+	 
143
+	message[0] = 0; // initialize for strcat()
144
+	
145
+	while(read == bsize) {
146
+		read = recv(sock, buffer, bsize, 0);
147
+		if(read < 0)
148
+			return -1; // error, bail out
149
+		total += read;
150
+		if(total > max)
151
+			return -2; // overflow
152
+		buffer[read] = 0;
153
+		strcat(message, buffer);
154
+	}
155
+	     
156
+	return total;
150 157
 }
151 158
 
152
-/* Receives a request of 250 bytes from the ntk console client.
153
- * listen and accept are also in a while loop to allow for different
154
- * start times between the ntk console bindings and the ntk console client,
155
- * As well as restarting of the ntk console client. */
159
+
156 160
 void ntkd_request(void) {
157
-    
158
-    char request[REQUEST_LENGTH];
159
-    
160
-    do {
161
-    
162
-        rc = listen(sockfd_1, 10);
163
-            if (rc< 0) {
164
-                perror("listen() failed");
165
-                exit(-1);
166
-            }
167
-
168
-        printf("Ready for client connect().\n");
169
-      
170
-        sockfd_2 = accept(sockfd_1, NULL, NULL);
171
-            if (sockfd_2 < 0) {
172
-                perror("accept() failed");
173
-                exit(-1);
174
-            }
175
-            
176
-        length = REQUEST_LENGTH;
177
-        rc = setsockopt(sockfd_2, SOL_SOCKET, SO_RCVLOWAT,
178
-                                          (char *)&length, sizeof(length));
179
-            if (rc < 0) {
180
-                perror("setsockopt(SO_RCVLOWAT) failed");
181
-                exit(-1);
182
-            }
183
-            
184
-        rc = recv(sockfd_2, request, sizeof(request), 0);
185
-            if (rc < 0) {
186
-                perror("recv() failed");
187
-                exit(-1);
188
-            }
189
-            
190
-            printf("%d bytes of data were received\n", rc);
191
-            
192
-                if (rc == 0 || rc < sizeof(request)) {
193
-                    printf("The console client closed the connection before all of the\n");
194
-                    printf("data was sent\n");
195
-                    exit(-1);
196
-                }
197
-        
198
-        request_cleanup(request);  
199
-        } while(FALSE);
200
-        
201
-        clean_up();
202
-        
161
+	char request[REQUEST_LENGTH];
162
+	
163
+	rc = listen(sockfd_1, 10);
164
+	if (rc< 0) {
165
+		perror("listen() failed");
166
+		exit(-1);
167
+	}
168
+
169
+	printf("Ready for client connect().\n");
170
+
171
+	do {
172
+		sockfd_2 = accept(sockfd_1, NULL, NULL);
173
+		if (sockfd_2 < 0) {
174
+			perror("accept() failed");
175
+			exit(-1);
176
+		}
177
+
178
+		rc = request_receive(sockfd_2, request, REQUEST_LENGTH);
179
+		if (rc < 0) {
180
+			perror("recv() failed");
181
+			exit(-1);
182
+		}
183
+		
184
+		printf("%d bytes of data were received\n", rc);
185
+		
186
+		request_processing(request);
187
+	} while(TRUE);
188
+		
189
+	clean_up();
203 190
 }
204 191
 
205 192
 void console_recv_send(void) {
206
-    opensocket();
207
-    ntkd_request();
208
-}
193
+	opensocket();
194
+	ntkd_request();
195
+}

Loading…
Cancel
Save