Browse Source

Console: Add common console header to keep API in sync

Alexander von Gluck IV 10 years ago
parent
commit
bc054faf0c
6 changed files with 143 additions and 92 deletions
  1. 6
    12
      SConstruct
  2. 4
    4
      src/SConscript
  3. 51
    0
      src/console.h
  4. 33
    34
      src/ntk-console-bindings.c
  5. 30
    6
      src/ntk-console.c
  6. 19
    36
      src/ntk-console.h

+ 6
- 12
SConstruct View File

@@ -7,18 +7,12 @@ import platform as _platform
7 7
 #
8 8
 
9 9
 opts = Options('build.conf')
10
-opts.AddOptions(('CONF_DIR', """Directory where the Netsukuku configuration files will be installed""",
11
-                '/etc/netsukuku'),
12
-        ('DATA_DIR', 'Directory to install data files',
13
-                '/usr/share/netsukuku'),
14
-        ('MAN_DIR',  'Where the manuals will be installed',
15
-                '/usr/man'),
16
-        ('BIN_DIR' , 'Directory to install the binaries',
17
-                '/usr/bin'),
18
-        ('PID_DIR',  'Specify location of ntkd.pid file',
19
-                '/var/run'),
20
-        ('destdir', 'SCons will copy all the files under destdir during installation',
21
-                '/'),
10
+opts.AddOptions(('CONF_DIR', """Directory where the Netsukuku configuration files will be installed""", '/etc/netsukuku'),
11
+        ('DATA_DIR', 'Directory to install data files', '/usr/share/netsukuku'),
12
+        ('MAN_DIR',  'Where the manuals will be installed', '/usr/man'),
13
+        ('BIN_DIR' , 'Directory to install the binaries', '/usr/bin'),
14
+        ('PID_DIR',  'Specify location of ntkd.pid file', '/var/run'),
15
+        ('destdir', 'SCons will copy all the files under destdir during installation', '/'),
22 16
         EnumOption('debug', 'build the debug code', 'no',
23 17
             allowed_values=('yes', 'no', '1', '0'), map={},
24 18
             ignorecase=0),

+ 4
- 4
src/SConscript View File

@@ -151,10 +151,10 @@ build_config_files(env = env)
151 151
 #	Build
152 152
 #
153 153
 
154
-ntkd 	= env.Program('ntkd', sources_netsukuku, LIBS = libs, CPPPATH = '.')
155
-qspn    = env.Program('qspn-empiric', sources_qspn, LIBS = libs, CPPPATH = '.')
156
-ntkresolv = env.Program('ntk-resolv', sources_ntkresolv, LIBS = libs, CPPPATH = '.')
157
-ntkconsole = env.Program('ntk-console', sources_ntkconsole, LIBS = libs, CPPPATH = '.', CFLAGS = '-std=c99')
154
+ntkd 		= env.Program('ntkd', sources_netsukuku, LIBS = libs, CPPPATH = '.')
155
+qspn  		= env.Program('qspn-empiric', sources_qspn, LIBS = libs, CPPPATH = '.')
156
+ntkresolv 	= env.Program('ntk-resolv', sources_ntkresolv, LIBS = libs, CPPPATH = '.')
157
+ntkconsole 	= env.Program('ntk-console', sources_ntkconsole, LIBS = libs, CPPPATH = '.', CFLAGS = '-std=c99')
158 158
 
159 159
 Default(ntkd, ntkresolv, ntkconsole)
160 160
 

+ 51
- 0
src/console.h View File

@@ -0,0 +1,51 @@
1
+/* This file is part of Netsukuku
2
+ *
3
+ * This source code is free software; you can redistribute it and/or
4
+ * modify it under the terms of the GNU General Public License as published
5
+ * by the Free Software Foundation; either version 2 of the License,
6
+ * or (at your option) any later version.
7
+ *
8
+ * This source code is distributed in the hope that it will be useful,
9
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11
+ * Please refer to the GNU Public License for more details.
12
+ *
13
+ * You should have received a copy of the GNU Public License along with
14
+ * this source code; if not, write to:
15
+ * Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16
+ *
17
+ */
18
+#ifndef CONSOLE_H
19
+#define CONSOLE_H
20
+
21
+
22
+#define CONSOLE_SOCKET_PATH 	"/tmp/ntk-console"
23
+#define CONSOLE_VERSION_MAJOR	0
24
+#define CONSOLE_VERSION_MINOR 	3
25
+#define CONSOLE_BUFFER_LENGTH 	250
26
+
27
+#ifndef TRUE
28
+#define FALSE               0
29
+#define TRUE                1
30
+#endif
31
+
32
+
33
+typedef enum {
34
+    COMMAND_HELP = 0x100,
35
+    COMMAND_UPTIME,
36
+    COMMAND_KILL,
37
+    COMMAND_VERSION,
38
+    COMMAND_INETCONN,
39
+    COMMAND_CURIFS,
40
+    COMMAND_CURIFSCT,
41
+    COMMAND_CURQSPNID,
42
+    COMMAND_CURIP,
43
+    COMMAND_CURNODE,
44
+    COMMAND_IFS,
45
+    COMMAND_IFSCT,
46
+    COMMAND_QUIT,
47
+    COMMAND_CONSUPTIME,
48
+} command_t;
49
+
50
+
51
+#endif /* CONSOLE_H */

+ 33
- 34
src/ntk-console-bindings.c View File

@@ -1,18 +1,14 @@
1 1
 /* Header files required for the console bindings
2 2
  * not included outside of this file. */
3 3
 
4
+
4 5
 #include <utmp.h>
5 6
 #include <sys/un.h>
6 7
 #include <unistd.h>
7 8
 
9
+#include "console.h"
8 10
 #include "netsukuku.h"
9 11
 
10
-/* Constants used for the console bindings. */
11
-
12
-#define SERVER_PATH	 "/tmp/ntk-console"
13
-#define REQUEST_LENGTH   250
14
-#define FALSE 			0
15
-#define TRUE 			1
16 12
 
17 13
 /* Variable and structure defintions, serverfd refers to socket file descriptor
18 14
  * length refers to the required length of the requests that will be sent.
@@ -28,8 +24,9 @@ int rc, length;
28 24
 /* Cleans up the console bindings for closing, Closes socket file descriptors,
29 25
  * unlinks the server path, etc. */
30 26
 
31
-void clean_up(void) {
32
-	
27
+static void
28
+clean_up(void)
29
+{
33 30
 	const int optVal = 1;
34 31
 	const socklen_t optLen = sizeof(optVal);
35 32
 	
@@ -38,14 +35,15 @@ void clean_up(void) {
38 35
 	if (serverfd != -1)
39 36
 		close(serverfd);
40 37
 
41
-   unlink(SERVER_PATH);
38
+   unlink(CONSOLE_SOCKET_PATH);
42 39
 	
43 40
 }
44 41
 
45 42
 /* Creates an AF_UNIX socket and binds it to a local address. */
46 43
 
47
-void opensocket(void) {
48
-	
44
+static void
45
+opensocket(void)
46
+{
49 47
 	int stop_trying;
50 48
 	
51 49
 	serverfd = socket(AF_UNIX, SOCK_STREAM, 0);
@@ -56,7 +54,7 @@ void opensocket(void) {
56 54
 	
57 55
 	memset(&serveraddr, 0, sizeof(serveraddr));
58 56
 	serveraddr.sun_family = AF_UNIX;
59
-	strcpy(serveraddr.sun_path, SERVER_PATH);
57
+	strcpy(serveraddr.sun_path, CONSOLE_SOCKET_PATH);
60 58
 
61 59
 	rc = bind(serverfd, (struct sockaddr *)&serveraddr, SUN_LEN(&serveraddr));
62 60
 	if (rc < 0) {
@@ -73,10 +71,11 @@ void opensocket(void) {
73 71
 	}
74 72
 }
75 73
 
74
+
76 75
 /* Sends a parsed response to the ntk console client. */
77 76
 
78
-void
79
-send_response(int session_fd, char response[REQUEST_LENGTH], ...)
77
+static void
78
+send_response(int session_fd, char response[CONSOLE_BUFFER_LENGTH], ...)
80 79
 {
81 80
 	int response_length = (int)strlen(response);
82 81
 	rc = send(session_fd, response, response_length, 0);
@@ -91,8 +90,8 @@ send_response(int session_fd, char response[REQUEST_LENGTH], ...)
91 90
  * to data from ntkd structures such as: me 
92 91
  * into a response for the ntk console client. */
93 92
 
94
-int
95
-request_processing(int session_fd, char unprocessed_request[REQUEST_LENGTH])
93
+static int
94
+request_processing(int session_fd, char unprocessed_request[CONSOLE_BUFFER_LENGTH])
96 95
 {
97 96
 	if(strncmp(unprocessed_request,"uptime", (int)strlen(unprocessed_request))  == 0)
98 97
 		send_response(session_fd, (char)time(0)-me.uptime);
@@ -153,7 +152,24 @@ request_receive(int sock, char message[], int max)
153 152
 }
154 153
 
155 154
 
156
-void
155
+static void
156
+handle_session(int session_fd)
157
+{
158
+	char request[CONSOLE_BUFFER_LENGTH];
159
+
160
+	rc = request_receive(session_fd, request, CONSOLE_BUFFER_LENGTH);
161
+	if (rc < 0) {
162
+		perror("recv() failed");
163
+		exit(-1);
164
+	}
165
+	
166
+	printf("%d bytes of data were received\n", rc);
167
+	
168
+	request_processing(session_fd, request);
169
+}
170
+
171
+
172
+static void
157 173
 wait_session(int server_fd)
158 174
 {
159 175
 	rc = listen(serverfd, 10);
@@ -187,23 +203,6 @@ wait_session(int server_fd)
187 203
 }
188 204
 
189 205
 
190
-void
191
-handle_session(int session_fd)
192
-{
193
-	char request[REQUEST_LENGTH];
194
-
195
-	rc = request_receive(session_fd, request, REQUEST_LENGTH);
196
-	if (rc < 0) {
197
-		perror("recv() failed");
198
-		exit(-1);
199
-	}
200
-	
201
-	printf("%d bytes of data were received\n", rc);
202
-	
203
-	request_processing(session_fd, request);
204
-}
205
-
206
-
207 206
 void
208 207
 console_recv_send(void)
209 208
 {

+ 30
- 6
src/ntk-console.c View File

@@ -1,6 +1,29 @@
1
+/* This file is part of Netsukuku
2
+ *
3
+ * This source code is free software; you can redistribute it and/or
4
+ * modify it under the terms of the GNU General Public License as published
5
+ * by the Free Software Foundation; either version 2 of the License,
6
+ * or (at your option) any later version.
7
+ *
8
+ * This source code is distributed in the hope that it will be useful,
9
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11
+ * Please refer to the GNU Public License for more details.
12
+ *
13
+ * You should have received a copy of the GNU Public License along with
14
+ * this source code; if not, write to:
15
+ * Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16
+ *
17
+ */
18
+
1 19
 
2 20
 #include "ntk-console.h"
21
+#include "console.h"
3 22
 
23
+#include <stdio.h>
24
+#include <sys/socket.h>
25
+#include <sys/un.h>
26
+#include <time.h>
4 27
 #include <unistd.h>
5 28
 
6 29
 
@@ -30,7 +53,7 @@ const struct supported_commands {
30 53
 command_t
31 54
 command_parse(char *request)
32 55
 {
33
-	if (strlen(request) > BUFFER_LENGTH) {
56
+	if (strlen(request) > CONSOLE_BUFFER_LENGTH) {
34 57
 		printf("Error: Command longer than 250 bytes.\n");
35 58
 		return -1;
36 59
 	}
@@ -92,8 +115,8 @@ ntkd_request(char *request)
92 115
 		exit(-1);
93 116
 	}
94 117
 
95
-	char response[BUFFER_LENGTH];
96
-	request_receive(sockfd, response, BUFFER_LENGTH);
118
+	char response[CONSOLE_BUFFER_LENGTH];
119
+	request_receive(sockfd, response, CONSOLE_BUFFER_LENGTH);
97 120
 	if (rc < 0) {
98 121
 		perror("recv() failed");
99 122
 		exit(-1);
@@ -114,7 +137,7 @@ opensocket(void)
114 137
 	
115 138
 	memset(&serveraddr, 0, sizeof(serveraddr));
116 139
 	serveraddr.sun_family = AF_UNIX;
117
-	strcpy(serveraddr.sun_path, SERVER_PATH);
140
+	strcpy(serveraddr.sun_path, CONSOLE_SOCKET_PATH);
118 141
 
119 142
 	rc = connect(sockfd, (struct sockaddr *)&serveraddr, sizeof(serveraddr));
120 143
 	if (rc < 0) {
@@ -206,7 +229,8 @@ console(char* request)
206 229
 			millisleep(200);
207 230
 			break;
208 231
 		case COMMAND_VERSION:
209
-			printf("Ntk-Console Version: %s\n", VERSION_STR);
232
+			printf("ntk-console version: %d.%d\n",
233
+				CONSOLE_VERSION_MAJOR, CONSOLE_VERSION_MINOR);
210 234
 			ntkd_request(request);
211 235
 			break;
212 236
 		case COMMAND_CONSUPTIME:
@@ -241,7 +265,7 @@ main(void)
241 265
 
242 266
 	printf("This is the Netsukuku Console. Please type 'help' for more information.\n");
243 267
 	for(;;) {
244
-		char* request = (char*)malloc(BUFFER_LENGTH);
268
+		char* request = (char*)malloc(CONSOLE_BUFFER_LENGTH);
245 269
 		printf("\n> ");
246 270
 		fgets(request, 16, stdin);
247 271
 		fflush(stdin);

+ 19
- 36
src/ntk-console.h View File

@@ -1,44 +1,27 @@
1
+/* This file is part of Netsukuku
2
+ *
3
+ * This source code is free software; you can redistribute it and/or
4
+ * modify it under the terms of the GNU General Public License as published
5
+ * by the Free Software Foundation; either version 2 of the License,
6
+ * or (at your option) any later version.
7
+ *
8
+ * This source code is distributed in the hope that it will be useful,
9
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11
+ * Please refer to the GNU Public License for more details.
12
+ *
13
+ * You should have received a copy of the GNU Public License along with
14
+ * this source code; if not, write to:
15
+ * Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16
+ *
17
+ */
1 18
 #ifndef NETSUKUKUCONSOLE_H
2 19
 #define NETSUKUKUCONSOLE_H
3 20
 
4
-#include <stdio.h>
5
-#include <stdlib.h>
6
-#include <string.h>
7
-#include <sys/types.h> 
8
-#include <sys/socket.h>
9
-#include <utmp.h>
10
-#include <sys/un.h>
11
-#include <unistd.h>
12
-#include <time.h>
13
-#include <errno.h>
14
-
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 21
 
22
+#include <time.h>
25 23
 
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;
24
+#include "console.h"
42 25
 
43 26
 
44 27
 int sockfd = -1, sockfd1 = -1;

Loading…
Cancel
Save