Console: Add common console header to keep API in sync

This commit is contained in:
Alexander von Gluck IV 2014-09-14 11:43:50 -05:00
parent 2627ba6350
commit bc054faf0c
6 changed files with 143 additions and 92 deletions

View File

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

51
src/console.h Normal file
View File

@ -0,0 +1,51 @@
/* This file is part of Netsukuku
*
* This source code is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as published
* by the Free Software Foundation; either version 2 of the License,
* or (at your option) any later version.
*
* This source code is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* Please refer to the GNU Public License for more details.
*
* You should have received a copy of the GNU Public License along with
* this source code; if not, write to:
* Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
#ifndef CONSOLE_H
#define CONSOLE_H
#define CONSOLE_SOCKET_PATH "/tmp/ntk-console"
#define CONSOLE_VERSION_MAJOR 0
#define CONSOLE_VERSION_MINOR 3
#define CONSOLE_BUFFER_LENGTH 250
#ifndef TRUE
#define FALSE 0
#define TRUE 1
#endif
typedef enum {
COMMAND_HELP = 0x100,
COMMAND_UPTIME,
COMMAND_KILL,
COMMAND_VERSION,
COMMAND_INETCONN,
COMMAND_CURIFS,
COMMAND_CURIFSCT,
COMMAND_CURQSPNID,
COMMAND_CURIP,
COMMAND_CURNODE,
COMMAND_IFS,
COMMAND_IFSCT,
COMMAND_QUIT,
COMMAND_CONSUPTIME,
} command_t;
#endif /* CONSOLE_H */

View File

@ -1,18 +1,14 @@
/* Header files required for the console bindings /* Header files required for the console bindings
* not included outside of this file. */ * not included outside of this file. */
#include <utmp.h> #include <utmp.h>
#include <sys/un.h> #include <sys/un.h>
#include <unistd.h> #include <unistd.h>
#include "console.h"
#include "netsukuku.h" #include "netsukuku.h"
/* Constants used for the console bindings. */
#define SERVER_PATH "/tmp/ntk-console"
#define REQUEST_LENGTH 250
#define FALSE 0
#define TRUE 1
/* Variable and structure defintions, serverfd refers to socket file descriptor /* Variable and structure defintions, serverfd refers to socket file descriptor
* length refers to the required length of the requests that will be sent. * length refers to the required length of the requests that will be sent.
@ -28,8 +24,9 @@ int rc, length;
/* Cleans up the console bindings for closing, Closes socket file descriptors, /* Cleans up the console bindings for closing, Closes socket file descriptors,
* unlinks the server path, etc. */ * unlinks the server path, etc. */
void clean_up(void) { static void
clean_up(void)
{
const int optVal = 1; const int optVal = 1;
const socklen_t optLen = sizeof(optVal); const socklen_t optLen = sizeof(optVal);
@ -38,14 +35,15 @@ void clean_up(void) {
if (serverfd != -1) if (serverfd != -1)
close(serverfd); close(serverfd);
unlink(SERVER_PATH); unlink(CONSOLE_SOCKET_PATH);
} }
/* Creates an AF_UNIX socket and binds it to a local address. */ /* Creates an AF_UNIX socket and binds it to a local address. */
void opensocket(void) { static void
opensocket(void)
{
int stop_trying; int stop_trying;
serverfd = socket(AF_UNIX, SOCK_STREAM, 0); serverfd = socket(AF_UNIX, SOCK_STREAM, 0);
@ -56,7 +54,7 @@ void opensocket(void) {
memset(&serveraddr, 0, sizeof(serveraddr)); memset(&serveraddr, 0, sizeof(serveraddr));
serveraddr.sun_family = AF_UNIX; serveraddr.sun_family = AF_UNIX;
strcpy(serveraddr.sun_path, SERVER_PATH); strcpy(serveraddr.sun_path, CONSOLE_SOCKET_PATH);
rc = bind(serverfd, (struct sockaddr *)&serveraddr, SUN_LEN(&serveraddr)); rc = bind(serverfd, (struct sockaddr *)&serveraddr, SUN_LEN(&serveraddr));
if (rc < 0) { if (rc < 0) {
@ -73,10 +71,11 @@ void opensocket(void) {
} }
} }
/* Sends a parsed response to the ntk console client. */ /* Sends a parsed response to the ntk console client. */
void static void
send_response(int session_fd, char response[REQUEST_LENGTH], ...) send_response(int session_fd, char response[CONSOLE_BUFFER_LENGTH], ...)
{ {
int response_length = (int)strlen(response); int response_length = (int)strlen(response);
rc = send(session_fd, response, response_length, 0); rc = send(session_fd, response, response_length, 0);
@ -91,8 +90,8 @@ send_response(int session_fd, char response[REQUEST_LENGTH], ...)
* to data from ntkd structures such as: me * to data from ntkd structures such as: me
* into a response for the ntk console client. */ * into a response for the ntk console client. */
int static int
request_processing(int session_fd, char unprocessed_request[REQUEST_LENGTH]) request_processing(int session_fd, char unprocessed_request[CONSOLE_BUFFER_LENGTH])
{ {
if(strncmp(unprocessed_request,"uptime", (int)strlen(unprocessed_request)) == 0) if(strncmp(unprocessed_request,"uptime", (int)strlen(unprocessed_request)) == 0)
send_response(session_fd, (char)time(0)-me.uptime); send_response(session_fd, (char)time(0)-me.uptime);
@ -153,7 +152,24 @@ request_receive(int sock, char message[], int max)
} }
void static void
handle_session(int session_fd)
{
char request[CONSOLE_BUFFER_LENGTH];
rc = request_receive(session_fd, request, CONSOLE_BUFFER_LENGTH);
if (rc < 0) {
perror("recv() failed");
exit(-1);
}
printf("%d bytes of data were received\n", rc);
request_processing(session_fd, request);
}
static void
wait_session(int server_fd) wait_session(int server_fd)
{ {
rc = listen(serverfd, 10); rc = listen(serverfd, 10);
@ -187,23 +203,6 @@ wait_session(int server_fd)
} }
void
handle_session(int session_fd)
{
char request[REQUEST_LENGTH];
rc = request_receive(session_fd, request, REQUEST_LENGTH);
if (rc < 0) {
perror("recv() failed");
exit(-1);
}
printf("%d bytes of data were received\n", rc);
request_processing(session_fd, request);
}
void void
console_recv_send(void) console_recv_send(void)
{ {

View File

@ -1,6 +1,29 @@
/* This file is part of Netsukuku
*
* This source code is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as published
* by the Free Software Foundation; either version 2 of the License,
* or (at your option) any later version.
*
* This source code is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* Please refer to the GNU Public License for more details.
*
* You should have received a copy of the GNU Public License along with
* this source code; if not, write to:
* Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
#include "ntk-console.h" #include "ntk-console.h"
#include "console.h"
#include <stdio.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <time.h>
#include <unistd.h> #include <unistd.h>
@ -30,7 +53,7 @@ const struct supported_commands {
command_t command_t
command_parse(char *request) command_parse(char *request)
{ {
if (strlen(request) > BUFFER_LENGTH) { if (strlen(request) > CONSOLE_BUFFER_LENGTH) {
printf("Error: Command longer than 250 bytes.\n"); printf("Error: Command longer than 250 bytes.\n");
return -1; return -1;
} }
@ -92,8 +115,8 @@ ntkd_request(char *request)
exit(-1); exit(-1);
} }
char response[BUFFER_LENGTH]; char response[CONSOLE_BUFFER_LENGTH];
request_receive(sockfd, response, BUFFER_LENGTH); request_receive(sockfd, response, CONSOLE_BUFFER_LENGTH);
if (rc < 0) { if (rc < 0) {
perror("recv() failed"); perror("recv() failed");
exit(-1); exit(-1);
@ -114,7 +137,7 @@ opensocket(void)
memset(&serveraddr, 0, sizeof(serveraddr)); memset(&serveraddr, 0, sizeof(serveraddr));
serveraddr.sun_family = AF_UNIX; serveraddr.sun_family = AF_UNIX;
strcpy(serveraddr.sun_path, SERVER_PATH); strcpy(serveraddr.sun_path, CONSOLE_SOCKET_PATH);
rc = connect(sockfd, (struct sockaddr *)&serveraddr, sizeof(serveraddr)); rc = connect(sockfd, (struct sockaddr *)&serveraddr, sizeof(serveraddr));
if (rc < 0) { if (rc < 0) {
@ -206,7 +229,8 @@ console(char* request)
millisleep(200); millisleep(200);
break; break;
case COMMAND_VERSION: case COMMAND_VERSION:
printf("Ntk-Console Version: %s\n", VERSION_STR); printf("ntk-console version: %d.%d\n",
CONSOLE_VERSION_MAJOR, CONSOLE_VERSION_MINOR);
ntkd_request(request); ntkd_request(request);
break; break;
case COMMAND_CONSUPTIME: case COMMAND_CONSUPTIME:
@ -241,7 +265,7 @@ main(void)
printf("This is the Netsukuku Console. Please type 'help' for more information.\n"); printf("This is the Netsukuku Console. Please type 'help' for more information.\n");
for(;;) { for(;;) {
char* request = (char*)malloc(BUFFER_LENGTH); char* request = (char*)malloc(CONSOLE_BUFFER_LENGTH);
printf("\n> "); printf("\n> ");
fgets(request, 16, stdin); fgets(request, 16, stdin);
fflush(stdin); fflush(stdin);

View File

@ -1,44 +1,27 @@
/* This file is part of Netsukuku
*
* This source code is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as published
* by the Free Software Foundation; either version 2 of the License,
* or (at your option) any later version.
*
* This source code is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* Please refer to the GNU Public License for more details.
*
* You should have received a copy of the GNU Public License along with
* this source code; if not, write to:
* Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
#ifndef NETSUKUKUCONSOLE_H #ifndef NETSUKUKUCONSOLE_H
#define NETSUKUKUCONSOLE_H #define NETSUKUKUCONSOLE_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <utmp.h>
#include <sys/un.h>
#include <unistd.h>
#include <time.h> #include <time.h>
#include <errno.h>
#include "console.h"
#define SERVER_PATH "/tmp/ntk-console"
#define BUFFER_LENGTH 250
#define VERSION_STR "0.0.2"
#ifndef TRUE
#define FALSE 0
#define TRUE 1
#endif
typedef enum {
COMMAND_HELP = 0x100,
COMMAND_UPTIME,
COMMAND_KILL,
COMMAND_VERSION,
COMMAND_INETCONN,
COMMAND_CURIFS,
COMMAND_CURIFSCT,
COMMAND_CURQSPNID,
COMMAND_CURIP,
COMMAND_CURNODE,
COMMAND_IFS,
COMMAND_IFSCT,
COMMAND_QUIT,
COMMAND_CONSUPTIME,
} command_t;
int sockfd = -1, sockfd1 = -1; int sockfd = -1, sockfd1 = -1;