Browse Source

Ntk-console: Clean up command line parsing

* Make console application function
* No more segfaults
* Apply KnR style
Alexander von Gluck IV 10 years ago
parent
commit
3a842c2d4c
2 changed files with 265 additions and 259 deletions
  1. 2
    2
      src/Ntk-Console/Makefile
  2. 263
    257
      src/Ntk-Console/Netsukuku-Console.c

+ 2
- 2
src/Ntk-Console/Makefile View File

@@ -1,12 +1,12 @@
1 1
 C=clang
2 2
 DEBUG=-g -fno-builtin -Wfatal-errors -pedantic -Wunreachable-code -Winline -Wredundant-decls -Wno-vla
3
-CFLAGS=-Wall ${DEBUG}
3
+CFLAGS=-Wall ${DEBUG} -std=c99
4 4
 SRCBIN=Netsukuku-Console
5 5
 
6 6
 all: ${SRCBIN}
7 7
 
8 8
 ${SRCBIN}: Netsukuku-Console.o
9
-	${C} ${LDFLAGS} ${LIBS} $^ -o $@
9
+	${C} ${LDFLAGS} ${CFLAGS} ${LIBS} $^ -o $@
10 10
 
11 11
 .PHONY: clean
12 12
 

+ 263
- 257
src/Ntk-Console/Netsukuku-Console.c View File

@@ -6,283 +6,289 @@ void usage();
6 6
 
7 7
 void clean_up();
8 8
 
9
-int validity_check(char *request) {
10
-    
11
-        if(strncmp(request,"help", (int)strlen(request))  == 0)
12
-            return 1;
13
-        
14
-        else if(strncmp(request,"uptime", (int)strlen(request))  == 0)
15
-            return 0;
16
-        
17
-        else if(strncmp(request,"kill", (int)strlen(request))  == 0)
18
-            return 2;
19
-        
20
-        else if(strncmp(request,"version", (int)strlen(request))  == 0)
21
-            return 3;
22
-        
23
-        else if(strncmp(request,"console_uptime", (int)strlen(request))  == 0)
24
-            return 4;
25
-        
26
-        else if(strlen(request) > BUFFER_LENGTH)
27
-            return 5;
28
-        
29
-        else if(strncmp(request,"inet_connected", (int)strlen(request))  == 0)
30
-            return 0;
31
-        
32
-        else if(strncmp(request,"cur_ifs", (int)strlen(request))  == 0)
33
-            return 0;
34
-        
35
-        else if(strncmp(request,"cur_ifs_n", (int)strlen(request))  == 0)
36
-            return 0;
37
-        
38
-        else if(strncmp(request,"cur_qspn_id", (int)strlen(request))  == 0)
39
-            return 0;
40
-        
41
-        else if(strncmp(request,"cur_ip", (int)strlen(request))  == 0)
42
-            return 0;
43
-        
44
-        else if(strncmp(request,"cur_node", (int)strlen(request))  == 0)
45
-            return 0;
46
-        
47
-        else if(strncmp(request,"ifs", (int)strlen(request))  == 0)
48
-            return 0;
49
-        
50
-        else if(strncmp(request,"ifs_n", (int)strlen(request))  == 0)
51
-            return 0;
52
-        
53
-        else {
54
-            printf("Incorrect or unreadable command, Please correct it.\n");
55
-            return -1;
56
-        }
57
-    
58
-    return -2;
59
-    
9
+
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;
26
+
27
+
28
+const struct supported_commands {
29
+	command_t 		id;
30
+	const char* 	command;
31
+	const char* 	help;
32
+	int 			arguments;
33
+} kSupportedCommands[] = {
34
+	{COMMAND_HELP, 		"help", 			"Shows console help", 0},
35
+	{COMMAND_UPTIME, 	"uptime", 			"Returns the time when ntkd finished the hooking", 0},
36
+	{COMMAND_KILL, 		"kill", 			"Kills the running instance of netsukuku with SIGINT", 0},
37
+	{COMMAND_VERSION, 	"version", 			"Shows the running version of the ntk-console, and ntkd.", 0},
38
+	{COMMAND_INETCONN, 	"inet_connected", 	"Query if Ntkd is connected to the internet", 0},
39
+	{COMMAND_CURIFS, 	"cur_ifs", 			"Lists all of the interfaces in cur_ifs", 0},
40
+	{COMMAND_CURIFSCT, 	"cur_ifs_n", 		"Lists the number of interfaces present in `cur_ifs`", 0},
41
+	{COMMAND_CURQSPNID, "cur_qspn_id", 		"The current qspn_id we are processing. It is cur_qspn_id[levels] big", 0},
42
+	{COMMAND_CURIP, 	"cur_ip", 			"Current IP address", 0},
43
+	{COMMAND_CURNODE,	"cur_node", 		"Current node", 0},
44
+	{COMMAND_IFS, 		"ifs", 				"List all the interfaces in server_opt.ifs", 0},
45
+	{COMMAND_IFSCT, 	"ifs_n", 			"List the number of interfaces present in server_opt.ifs", 0},
46
+	{COMMAND_QUIT, 		"quit", 			"Exit the console", 0},
47
+	{COMMAND_CONSUPTIME,"console_uptime", 	"Get the uptime of this console", 0},
48
+};
49
+
50
+
51
+command_t
52
+command_parse(char *request)
53
+{
54
+	if (strlen(request) > BUFFER_LENGTH) {
55
+		printf("Error: Command longer than 250 bytes.\n");
56
+		return -1;
57
+	}
58
+
59
+	for (int i = 0; i < sizeof(kSupportedCommands)
60
+		/ sizeof(kSupportedCommands[0]); i++) {
61
+		if (strncmp(request, kSupportedCommands[i].command,
62
+			(int)strlen(request) - 1) == 0) {
63
+			return kSupportedCommands[i].id;
64
+		}
65
+	}
66
+
67
+	printf("Incorrect or unreadable command, Please correct it.\n");
68
+	return -1;
60 69
 }
61 70
 
62
-void response_cleanup(char response[BUFFER_LENGTH]) {
63
-    
64
-    char remove = 'a';
65
-
66
-    char* c;
67
-    char* pPosition;
68
-    while((pPosition = strchr(response, 'a')) != NULL)  {
69
-        if ((c = index(response, remove)) != NULL) {
70
-        size_t len_left = sizeof(response) - (c+1-response);
71
-        memmove(c, c+1, len_left);
72
-        }
73
-    }
74
-    printf("Sent and received Successfully!\n The Response was: %s", response);
71
+
72
+void
73
+response_cleanup(char response[BUFFER_LENGTH])
74
+{
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
+		}
84
+	}
85
+
86
+	printf("Sent and received Successfully!\n The Response was: %s", response);
75 87
 }
76 88
 
89
+
77 90
 /* Sends and receives to ntkd */
78
-void ntkd_request(char *request) {
79
-
80
-    int request_length;
81
-    
82
-            rc = connect(sockfd, (struct sockaddr *)&serveraddr, SUN_LEN(&serveraddr));
83
-                if (rc < 0) {
84
-                    perror("connect() failed");
85
-                    exit(-1);
86
-                }
87
-    
88
-    
89
-            request_length = (int)strlen(request);
90
-            memset(request, 'a', BUFFER_LENGTH - request_length);
91
-            rc = send(sockfd, request, sizeof(request), 0);
92
-            if (rc < 0) {
93
-                perror("send() failed");
94
-                exit(-1);
95
-            }
96
-
97
-            bytesReceived = 0;
98
-            while (bytesReceived < BUFFER_LENGTH) {
99
-                rc = recv(sockfd, & response[bytesReceived],
100
-                   BUFFER_LENGTH - bytesReceived, 0);
101
-                if (rc < 0) {
102
-                    perror("recv() failed");
103
-                    exit(-1);
104
-                }
105
-            else if (rc == 0) {
106
-                printf("The server closed the connection\n");
107
-                exit(-1);
108
-            }
109
-
110
-            /* Increment the number of bytes that have been received so far  */
111
-            bytesReceived += rc;        
112
-            }
113
-            response_cleanup(response);
91
+void
92
+ntkd_request(char *request)
93
+{
94
+	rc = connect(sockfd, (struct sockaddr *)&serveraddr, sizeof(serveraddr));
95
+	if (rc < 0) {
96
+		perror("connect() failed");
97
+		exit(-1);
98
+	}
99
+
100
+	int request_length = strlen(request);
101
+	memset(request, 'a', BUFFER_LENGTH - request_length);
102
+	rc = send(sockfd, request, sizeof(request), 0);
103
+	if (rc < 0) {
104
+		perror("send() failed");
105
+		exit(-1);
106
+	}
107
+
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");
118
+		exit(-1);
119
+	}
120
+
121
+	/* Increment the number of bytes that have been received so far  */
122
+	bytesReceived += rc;		
123
+	}
124
+	response_cleanup(response);
114 125
 }
115 126
 
116
-void opensocket(void) {
117
-    
118
-    sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
119
-    if (sockfd < 0) {
120
-         perror("socket creation failed");
121
-         exit(-1);
122
-    }
123
-    
124
-    memset(&serveraddr, 0, sizeof(serveraddr));
125
-    serveraddr.sun_family = AF_UNIX;
126
-    strcpy(serveraddr.sun_path, SERVER_PATH);
127
+
128
+void
129
+opensocket(void)
130
+{
131
+	sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
132
+	if (sockfd < 0) {
133
+		 perror("socket creation failed");
134
+		 exit(-1);
135
+	}
136
+	
137
+	memset(&serveraddr, 0, sizeof(serveraddr));
138
+	serveraddr.sun_family = AF_UNIX;
139
+	strcpy(serveraddr.sun_path, SERVER_PATH);
127 140
 }
128 141
 
129
-void console_uptime(void) {
130
-    
131
-    int uptime_sec1;
132
-    int uptime_min1;
133
-    int uptime_hour1;
134
-    
135
-    int uptime_day1;
136
-    int uptime_month1;
137
-    int uptime_year1;
138
-    
139
-    time(&rawtime);
140
-    
141
-    timeinfo = localtime(&rawtime);
142
-    
143
-    uptime_sec1 = timeinfo->tm_sec;
144
-    uptime_min1 = timeinfo->tm_min;
145
-    uptime_hour1 = timeinfo->tm_hour;
146
-    
147
-    uptime_day1 = timeinfo->tm_mday;
148
-    uptime_month1 = timeinfo->tm_mon;
149
-    uptime_year1 = timeinfo->tm_year;
150
-    
151
-    uptime_sec1 -= uptime_sec;
152
-    uptime_min1 -= uptime_min;
153
-    uptime_hour1 -= uptime_hour;
154
-    
155
-    uptime_day1 -= uptime_day;
156
-    uptime_month1 -= uptime_month;
157
-    uptime_year1 -= uptime_year;
158
-    
159
-    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);
160
-    
142
+
143
+void
144
+console_uptime(void)
145
+{
146
+	int uptime_sec1;
147
+	int uptime_min1;
148
+	int uptime_hour1;
149
+	
150
+	int uptime_day1;
151
+	int uptime_month1;
152
+	int uptime_year1;
153
+	
154
+	time(&rawtime);
155
+	
156
+	timeinfo = localtime(&rawtime);
157
+	
158
+	uptime_sec1 = timeinfo->tm_sec;
159
+	uptime_min1 = timeinfo->tm_min;
160
+	uptime_hour1 = timeinfo->tm_hour;
161
+	
162
+	uptime_day1 = timeinfo->tm_mday;
163
+	uptime_month1 = timeinfo->tm_mon;
164
+	uptime_year1 = timeinfo->tm_year;
165
+	
166
+	uptime_sec1 -= uptime_sec;
167
+	uptime_min1 -= uptime_min;
168
+	uptime_hour1 -= uptime_hour;
169
+	
170
+	uptime_day1 -= uptime_day;
171
+	uptime_month1 -= uptime_month;
172
+	uptime_year1 -= uptime_year;
173
+	
174
+	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);
161 175
 }
162 176
 
163
-int millisleep(unsigned ms)
177
+
178
+int
179
+millisleep(unsigned ms)
164 180
 {
165 181
   return usleep(1000 * ms);
166 182
 }
167 183
 
168
-void console(char * request) {
169
-    
170
-    if(validity_check(request) == -2)
171
-            printf("Error: Command has not been processed!\n");
172
-        
173
-    if(validity_check(request) == -1)
174
-            usage();
175
-        
176
-    if(strncmp(request,"quit", (int)strlen(request)) == 0) {
177
-            clean_up();
178
-            exit(0);
179
-        }
180
-    
181
-    if(validity_check(request) == 0) {
182
-            ntkd_request(request);
183
-            millisleep(200);
184
-    }
185
-        
186
-    if(validity_check(request) == 1)
187
-            usage();
188
-        
189
-    if(validity_check(request) == 2)
190
-            system("ntkd -k");
191
-        
192
-    if(validity_check(request) == 3) {
193
-            printf("Ntk-Console Version: %s\n", VERSION_STR);
194
-            ntkd_request(request);
195
-        }
196
-
197
-    if(validity_check(request) == 4)
198
-            console_uptime();
199
-    
200
-    if(validity_check(request) == 5)
201
-        printf("Error: Command longer than 250 bytes.\n");
184
+
185
+void
186
+console(char* request)
187
+{
188
+	command_t commandID = command_parse(request);
189
+
190
+	switch (commandID) {
191
+		case COMMAND_QUIT:
192
+			clean_up();
193
+			exit(0);
194
+		case COMMAND_UPTIME:
195
+		case COMMAND_INETCONN:
196
+		case COMMAND_CURIFS:
197
+		case COMMAND_CURIFSCT:
198
+		case COMMAND_CURQSPNID:
199
+		case COMMAND_CURIP:
200
+		case COMMAND_CURNODE:
201
+		case COMMAND_IFS:
202
+		case COMMAND_IFSCT:
203
+			ntkd_request(request);
204
+			millisleep(200);
205
+			break;
206
+		case COMMAND_VERSION:
207
+			printf("Ntk-Console Version: %s\n", VERSION_STR);
208
+			ntkd_request(request);
209
+			break;
210
+		case COMMAND_CONSUPTIME:
211
+			console_uptime();
212
+			break;
213
+		case COMMAND_KILL:
214
+			system("ntkd -k");
215
+			break;
216
+		case COMMAND_HELP:
217
+		default:
218
+			usage();
219
+	}
202 220
 }
203 221
 
204
-int main(void) {
205
-    
206
-    time(&rawtime);
207
-    
208
-    timeinfo = localtime(&rawtime);
209
-    
210
-    uptime_sec = timeinfo->tm_sec;
211
-    uptime_min = timeinfo->tm_min;
212
-    uptime_hour = timeinfo->tm_hour;
213
-    uptime_day = timeinfo->tm_mday;
214
-    uptime_month = timeinfo->tm_mon;
215
-    uptime_year = timeinfo->tm_year;
216
-    
217
-    opensocket();
218
-    
219
-    printf("This is the Netsukuku Console, Please type 'help' for more information.\n");
220
-    
221
-    char * request;
222
-
223
-    char request1;
224
-    
225
-    request = (char *)malloc(BUFFER_LENGTH);
226
-    
227
-    request1 = (char)malloc(BUFFER_LENGTH);
228
-    
229
-    do {
230
-    
231
-    printf("\n>");
232
-        
233
-    fgets(request, 16, stdin);
234
-    
235
-    fflush(stdin);
236
-    
237
-    request = request1;
238
-    
239
-    console(request);
240
-    } while(FALSE);
241
-    
242
-clean_up(); 
243
-return 0;
222
+
223
+int
224
+main(void)
225
+{
226
+	time(&rawtime);
227
+	
228
+	timeinfo = localtime(&rawtime);
229
+	
230
+	uptime_sec = timeinfo->tm_sec;
231
+	uptime_min = timeinfo->tm_min;
232
+	uptime_hour = timeinfo->tm_hour;
233
+	uptime_day = timeinfo->tm_mday;
234
+	uptime_month = timeinfo->tm_mon;
235
+	uptime_year = timeinfo->tm_year;
236
+	
237
+	opensocket();
238
+	
239
+	printf("This is the Netsukuku Console, Please type 'help' for more information.\n");
240
+	
241
+	char* request = (char*)malloc(BUFFER_LENGTH);
242
+	
243
+	do {
244
+		printf("\n>");
245
+		fgets(request, 16, stdin);
246
+		fflush(stdin);
247
+		console(request);
248
+	} while(FALSE);
249
+	
250
+	clean_up(); 
251
+	return 0;
244 252
 }
245 253
 
246 254
 void usage(void) {
247
-    
248
-    	printf("Usage\n"
249
-		" uptime    Returns the time when ntkd finished the hooking," 
250
-					  "to get the the actual uptime just do) "
251
-					  "time(0)-me.uptime \n"
252
-		" help	Shows this\n"
253
-		" kill	Kills the running instance of netsukuku with SIGINT\n\n"
254
-		" version   Shows the running version of the ntk-console, and ntkd.\n"
255
-		" inet_connected    If it is 1, Ntkd is connected to the Internet\n"
256
-		"\n"
257
-		" cur_ifs   Lists all of the interfaces in cur_ifs\n"
258
-		" cur_ifs_n Lists the number of interfaces present in `cur_ifs'\n"
259
-		"\n");
260
-        printf(" cur_qspn_id   The current qspn_id we are processing. "
261
-                                "It is cur_qspn_id[levels] big\n"
262
-		" cur_ip    Current IP address\n"
263
-		"\n"
264
-		" cur_node  Current Node\n"
265
-		" ifs   Lists all of the interfaces in server_opt.ifs\n"
266
-                " ifs_n Lists the number of interfaces present in server_opt.ifs\n"
267
-                " quit Exits this program\n"
268
-		" console_uptime    Gets the uptime of this console\n");
269
-    
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
+	
270 277
 }
271 278
 
272
-void clean_up(void) {
273
-    
274
-    const int optVal = 1;
275
-    const socklen_t optLen = sizeof(optVal);
276
-    
277
-    setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (void*) &optVal, optLen);
278
-    setsockopt(sockfd1, SOL_SOCKET, SO_REUSEADDR, (void*) &optVal, optLen);
279
-    
280
-    if (sockfd != -1)
281
-        close(sockfd);
282
-
283
-    if (sockfd1 != -1)
284
-        close(sockfd1);
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);
285 292
    
286
-   unlink(SERVER_PATH);
287
-    
288
-}
293
+	unlink(SERVER_PATH);
294
+}

Loading…
Cancel
Save