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
 #
7
 #
8
 
8
 
9
 opts = Options('build.conf')
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
         EnumOption('debug', 'build the debug code', 'no',
16
         EnumOption('debug', 'build the debug code', 'no',
23
             allowed_values=('yes', 'no', '1', '0'), map={},
17
             allowed_values=('yes', 'no', '1', '0'), map={},
24
             ignorecase=0),
18
             ignorecase=0),

+ 4
- 4
src/SConscript View File

151
 #	Build
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
 Default(ntkd, ntkresolv, ntkconsole)
159
 Default(ntkd, ntkresolv, ntkconsole)
160
 
160
 

+ 51
- 0
src/console.h View File

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
 /* Header files required for the console bindings
1
 /* Header files required for the console bindings
2
  * not included outside of this file. */
2
  * not included outside of this file. */
3
 
3
 
4
+
4
 #include <utmp.h>
5
 #include <utmp.h>
5
 #include <sys/un.h>
6
 #include <sys/un.h>
6
 #include <unistd.h>
7
 #include <unistd.h>
7
 
8
 
9
+#include "console.h"
8
 #include "netsukuku.h"
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
 /* Variable and structure defintions, serverfd refers to socket file descriptor
13
 /* Variable and structure defintions, serverfd refers to socket file descriptor
18
  * length refers to the required length of the requests that will be sent.
14
  * length refers to the required length of the requests that will be sent.
28
 /* Cleans up the console bindings for closing, Closes socket file descriptors,
24
 /* Cleans up the console bindings for closing, Closes socket file descriptors,
29
  * unlinks the server path, etc. */
25
  * unlinks the server path, etc. */
30
 
26
 
31
-void clean_up(void) {
32
-	
27
+static void
28
+clean_up(void)
29
+{
33
 	const int optVal = 1;
30
 	const int optVal = 1;
34
 	const socklen_t optLen = sizeof(optVal);
31
 	const socklen_t optLen = sizeof(optVal);
35
 	
32
 	
38
 	if (serverfd != -1)
35
 	if (serverfd != -1)
39
 		close(serverfd);
36
 		close(serverfd);
40
 
37
 
41
-   unlink(SERVER_PATH);
38
+   unlink(CONSOLE_SOCKET_PATH);
42
 	
39
 	
43
 }
40
 }
44
 
41
 
45
 /* Creates an AF_UNIX socket and binds it to a local address. */
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
 	int stop_trying;
47
 	int stop_trying;
50
 	
48
 	
51
 	serverfd = socket(AF_UNIX, SOCK_STREAM, 0);
49
 	serverfd = socket(AF_UNIX, SOCK_STREAM, 0);
56
 	
54
 	
57
 	memset(&serveraddr, 0, sizeof(serveraddr));
55
 	memset(&serveraddr, 0, sizeof(serveraddr));
58
 	serveraddr.sun_family = AF_UNIX;
56
 	serveraddr.sun_family = AF_UNIX;
59
-	strcpy(serveraddr.sun_path, SERVER_PATH);
57
+	strcpy(serveraddr.sun_path, CONSOLE_SOCKET_PATH);
60
 
58
 
61
 	rc = bind(serverfd, (struct sockaddr *)&serveraddr, SUN_LEN(&serveraddr));
59
 	rc = bind(serverfd, (struct sockaddr *)&serveraddr, SUN_LEN(&serveraddr));
62
 	if (rc < 0) {
60
 	if (rc < 0) {
73
 	}
71
 	}
74
 }
72
 }
75
 
73
 
74
+
76
 /* Sends a parsed response to the ntk console client. */
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
 	int response_length = (int)strlen(response);
80
 	int response_length = (int)strlen(response);
82
 	rc = send(session_fd, response, response_length, 0);
81
 	rc = send(session_fd, response, response_length, 0);
91
  * to data from ntkd structures such as: me 
90
  * to data from ntkd structures such as: me 
92
  * into a response for the ntk console client. */
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
 	if(strncmp(unprocessed_request,"uptime", (int)strlen(unprocessed_request))  == 0)
96
 	if(strncmp(unprocessed_request,"uptime", (int)strlen(unprocessed_request))  == 0)
98
 		send_response(session_fd, (char)time(0)-me.uptime);
97
 		send_response(session_fd, (char)time(0)-me.uptime);
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
 wait_session(int server_fd)
173
 wait_session(int server_fd)
158
 {
174
 {
159
 	rc = listen(serverfd, 10);
175
 	rc = listen(serverfd, 10);
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
 void
206
 void
208
 console_recv_send(void)
207
 console_recv_send(void)
209
 {
208
 {

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

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
 #include "ntk-console.h"
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
 #include <unistd.h>
27
 #include <unistd.h>
5
 
28
 
6
 
29
 
30
 command_t
53
 command_t
31
 command_parse(char *request)
54
 command_parse(char *request)
32
 {
55
 {
33
-	if (strlen(request) > BUFFER_LENGTH) {
56
+	if (strlen(request) > CONSOLE_BUFFER_LENGTH) {
34
 		printf("Error: Command longer than 250 bytes.\n");
57
 		printf("Error: Command longer than 250 bytes.\n");
35
 		return -1;
58
 		return -1;
36
 	}
59
 	}
92
 		exit(-1);
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
 	if (rc < 0) {
120
 	if (rc < 0) {
98
 		perror("recv() failed");
121
 		perror("recv() failed");
99
 		exit(-1);
122
 		exit(-1);
114
 	
137
 	
115
 	memset(&serveraddr, 0, sizeof(serveraddr));
138
 	memset(&serveraddr, 0, sizeof(serveraddr));
116
 	serveraddr.sun_family = AF_UNIX;
139
 	serveraddr.sun_family = AF_UNIX;
117
-	strcpy(serveraddr.sun_path, SERVER_PATH);
140
+	strcpy(serveraddr.sun_path, CONSOLE_SOCKET_PATH);
118
 
141
 
119
 	rc = connect(sockfd, (struct sockaddr *)&serveraddr, sizeof(serveraddr));
142
 	rc = connect(sockfd, (struct sockaddr *)&serveraddr, sizeof(serveraddr));
120
 	if (rc < 0) {
143
 	if (rc < 0) {
206
 			millisleep(200);
229
 			millisleep(200);
207
 			break;
230
 			break;
208
 		case COMMAND_VERSION:
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
 			ntkd_request(request);
234
 			ntkd_request(request);
211
 			break;
235
 			break;
212
 		case COMMAND_CONSUPTIME:
236
 		case COMMAND_CONSUPTIME:
241
 
265
 
242
 	printf("This is the Netsukuku Console. Please type 'help' for more information.\n");
266
 	printf("This is the Netsukuku Console. Please type 'help' for more information.\n");
243
 	for(;;) {
267
 	for(;;) {
244
-		char* request = (char*)malloc(BUFFER_LENGTH);
268
+		char* request = (char*)malloc(CONSOLE_BUFFER_LENGTH);
245
 		printf("\n> ");
269
 		printf("\n> ");
246
 		fgets(request, 16, stdin);
270
 		fgets(request, 16, stdin);
247
 		fflush(stdin);
271
 		fflush(stdin);

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

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
 #ifndef NETSUKUKUCONSOLE_H
18
 #ifndef NETSUKUKUCONSOLE_H
2
 #define NETSUKUKUCONSOLE_H
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
 int sockfd = -1, sockfd1 = -1;
27
 int sockfd = -1, sockfd1 = -1;

Loading…
Cancel
Save