diff --git a/src/Ntk-Console/Netsukuku-Console.c b/src/Ntk-Console/Netsukuku-Console.c index 6444af3..8a2a1aa 100644 --- a/src/Ntk-Console/Netsukuku-Console.c +++ b/src/Ntk-Console/Netsukuku-Console.c @@ -2,112 +2,199 @@ #include #include #include +#include +#include +#include #include #include "Netsukuku-Console.h" -int fd[2]; -pid_t ntkd_pid; +void usage(); + +int validity_check(char argv) { + + switch(argv) { + case 'help': + return 1; + break; + case 'uptime': + return 0; + break; + case 'kill': + return 2; + break; + case 'version': + return 3; + break; + case 'inet_connected': + return 0; + break; + case 'cur_ifs': + return 0; + break; + case 'cur_ifs_n': + return 0; + break; + case 'cur_qspn_id': + return 0; + break; + case 'cur_ip': + return 0; + break; + case 'cur_node': + return 0; + break; + case 'ifs': + return 0; + break; + case 'ifs_n': + return 0; + break; + case 'console_uptime': + return 0; + break; + default: + printf("Incorrect or unreadable command, Please correct it.\n"); + return -1; + break; + } + +} /* this function is run by the second thread */ -void *server_opt_pipe(void *args) { - - int i; +void *ntkd_request(void *argv) { - int* ServIter = (int*)&server_opt; - - for(i = 0; i<33; i++) { - - printf("%d\n",*(ServIter + i)); - - } - -} - -/* - * is_ntkd_already_running - * - * Returns 1 if there's already a ntkd running - */ -int is_ntkd_already_running(void) -{ - pid_t oldpid; - FILE *fd; - - if(!(fd=fopen(server_opt.pid_file, "r"))) { - if(errno != ENOENT) - printf("Cannot read pid file \"%s\": %s\n", - server_opt.pid_file, strerror(errno)); - return 0; - } - - fscanf(fd, "ntkd %d\n", &oldpid); - if(ferror(fd)) { - printf("error reading pid file \"%s\": %s\n", - server_opt.pid_file, strerror(errno)); - fclose(fd); - return 0; - } - fclose(fd); - - return !kill(oldpid, 0); -} - -int openpipe(void) { - - if(is_ntkd_already_running() == 1){ - char process_name[256] = {0}; - pid_t pid1; - printf("...Opening Pipe to ntkd...\n"); - FILE *fd1=fopen(server_opt.pid_file, "r"); - while(fscanf(fd1, "%s %d", process_name, &pid1)!=EOF) { - if(strcmp(process_name, "ntkd") == 0) { - ntkd_pid = pid1; - if (pipe(fd) == -1) { - printf("Error in Pipe Creation: %s\n", strerror(errno)); - exit(1); - } + while(sendrecv == 1) { + rc = sendto(sockfd1, request, strlen(request), 0, (struct sockaddr *)&serveraddr, strlen(serveraddr)); + if (rc < 0) { + perror("sendto() failed"); + exit(-1); } + + rc = recvfrom(sockfd1, response, strlen(response), MSG_WAITALL, (struct sockaddr *)&ntkdaddr, strlen(ntkdaddr)); + if (rc < 0) { + perror("recvfrom() failed"); + exit(-1); } - fclose(fd1); + + if(rc >= 0) { + printf("Sent and received Successfully!\n The Response was: %s", response); + + } + + } +} + +int opensocket(void) { + + sockfd = socket(AF_UNIX, SOCK_STREAM, 0); + if (sockfd < 0) { + perror("socket creation failed"); + exit(-1); } - else if(is_ntkd_already_running() == 0) { - printf("ntkd is not running\n ...Exiting...\n"); - exit(0); - - } + memset(&serveraddr, 0, sizeof(serveraddr)); + serveraddr.sun_family = AF_UNIX; + strcpy(serveraddr.sun_path, SERVER_PATH); + + rc = bind(sockfd, (struct sockaddr *)&serveraddr, SUN_LEN(&serveraddr)); + if (rc < 0) { + perror("bind() failed"); + exit(-1); + } +} + +int console(void *argv) { + int exit_now = 0; + + while(exit_now == 1) { + printf("\n>") + + request = scanf("%s"); + + if(validity_check(request) == -1) + usage(); + + if(strncmp(request, "quit", 4) == 0) + exit(0); + + if(validity_check(request) == 0) + sendrecv = 1; + + if(validity_check(request) == 1) + usage(); + + if(validity_check(request) == 2) + system("ntkd -k"); + + if(validity_check(request) == 3) { + printf("%s", VERSION_STR); + sendrecv = 1; + } + + sendrecv = 0; + } } int main(void) { - server_opt.pid_file="/var/run/ntkd.pid"; - - openpipe(); + opensocket(); printf("This is the Netsukuku Console, Please type: 'help' for more information.\n"); - server_opt_pipe(NULL); - /* This variable is our reference to the second thread */ - pthread_t NetsukukuServeroptPipe; + pthread_t NtkdRequest; -/* create a second thread which executes inc_x(&x) */ - if(pthread_create(&NetsukukuServeroptPipe, NULL, server_opt_pipe, NULL)) { +/* Create a second thread which executes ntkd_request() */ + if(pthread_create(&NtkdRequest, NULL, ntkd_request, NULL)) { + fprintf(stderr, "Error creating thread\n"); + return -1; + } - fprintf(stderr, "Error creating thread\n"); - return 1; +/* Detach the second thread */ + if(pthread_detach(NtkdRequest)) { + fprintf(stderr, "Error joining thread\n"); + return -2; + } + + pthread_t ConsoleThread; + + if(pthread_create(&ConsoleThread, NULL, console, NULL)) { + fprintf(stderr, "Error creating thread\n"); + return -1; + } - } - -/* wait for the second thread to finish */ - if(pthread_join(NetsukukuServeroptPipe, NULL)) { - - fprintf(stderr, "Error joining thread\n"); - return 2; - - } + if(pthread_detach(ConsoleThread)) { + fprintf(stderr, "Error joining thread\n"); + return -2; + } return 0; +} + +void usage(void) { + + printf("Usage:\n" + " uptime Returns the time when ntkd finished the hooking, + "to get the the actual uptime just do: " + "time(0)-me.uptime \n" + " help Shows this\n" + " kill Kills the running instance of netsukuku with SIGINT\n\n" + " version Shows the running version of ntkd and ntk-console\n" + " inet_connected If it is 1, Ntkd is connected to the Internet\n" + "\n" + " cur_ifs Lists all of the interfaces in cur_ifs\n" + " cur_ifs_n Lists the number of interfaces present in `cur_ifs'\n" + "\n" + " cur_qspn_id The current qspn_id we are processing. " + "It is cur_qspn_id[levels] big\n" + " cur_ip Current IP address\n" + "\n" + " cur_node Current Node\n" + " ifs Lists all of the interfaces in server_opt.ifs\n" + " ifs_n Lists the number of interfaces present in server_opt.ifs\n" + " quit Exits this program\n" + " console_uptime Gets the uptime of this console (Yet to be implemented)\n"); + } \ No newline at end of file diff --git a/src/Ntk-Console/Netsukuku-Console.h b/src/Ntk-Console/Netsukuku-Console.h index a29ffc2..9dd15ce 100644 --- a/src/Ntk-Console/Netsukuku-Console.h +++ b/src/Ntk-Console/Netsukuku-Console.h @@ -1,177 +1,14 @@ #ifndef NETSUKUKUCONSOLE_H #define NETSUKUKUCONSOLE_H -#define MAX_INTERFACES 16 /* The maximum number of network - interfaces, which can be used - by Netsukuku */ +#define SERVER_PATH "/tmp/server" +#define VERSION_STR "0.0.1" +#define FALSE 0 - -/* - * current_globals - * - * Here there are the main globals variables used among the code. - */ -/*struct current_globals -{ - /* - * Internal map - */ - //map_node *int_map; /*Internal Map*/ - - /* - * External map - */ - //map_gnode **ext_map; /*External Map. */ - //quadro_group cur_quadg; - - /* - * Border nodes maps.(bmap.h) - */ - //map_bnode **bnode_map; - //u_int *bmap_nodes; /* bnode counter for each map*/ - //u_int *bmap_nodes_closed; /* number of closed bnodes */ - //u_int *bmap_nodes_opened; /* " " opened " */ - - /* - * Myself - */ - //inet_prefix cur_ip; - //map_node *cur_node; - - /* - * external rnode cache list. (see gmap.h) - */ - //ext_rnode_cache *cur_erc; - //u_int cur_erc_counter; - - /* - * Current Qspn id and qspn time - */ - //int *cur_qspn_id; /*The current qspn_id we are processing. - // It is cur_qspn_id[levels] big*/ - //struct timeval *cur_qspn_time; /*When the last qspn round was received/sent - // (gettimeofday format)*/ - /* - * Internet gateways - */ - //inet_gw **igws; - //int *igws_counter; - /*inet_gw **my_igws; /* my_igws[level] points to our inet gateway - present at igws[level]. It's the same of using - igw_find_node(igws, me.cur_quadg.gnode[_EL(level)]); */ - //u_char my_bandwidth; /* The bandwidth of the Internet connection - // we are sharing*/ - //u_char inet_connected; /* If it is 1, we are connected to the Internet */ - - /* - * Network interfaces - */ - //interface cur_ifs[MAX_INTERFACES]; - //int cur_ifs_n; /* number of interfaces present - // in `cur_ifs' */ - - /*time_t uptime; /*The time when we finished the hooking, - to get the the actual uptime just do: - time(0)-me.uptime*/ -//}me; - -#define NTK_TCP_PORT 269 -#define NTK_UDP_RADAR_PORT 269 - -#define ANDNA_UDP_PORT 277 -#define ANDNA_TCP_PORT 277 - -const static u_short ntk_udp_radar_port = NTK_UDP_RADAR_PORT, - ntk_tcp_port = NTK_TCP_PORT; -const static u_short andna_udp_port = ANDNA_UDP_PORT, - andna_tcp_port = ANDNA_TCP_PORT; - -#define NTK_CONFIG_FILE CONF_DIR "/netsukuku.conf" -#define NTK_PID_FILE PID_DIR "/ntkd.pid" - - -#define INT_MAP_FILE DATA_DIR "/ntk_internal_map" -#define EXT_MAP_FILE DATA_DIR "/ntk_external_map" -#define BNODE_MAP_FILE DATA_DIR "/ntk_bnode_map" - -#define ANDNA_HNAMES_FILE CONF_DIR "/andna_hostnames" -#define SNSD_NODES_FILE CONF_DIR "/snsd_nodes" -#define ANDNA_CACHE_FILE DATA_DIR "/andna_cache" -#define LCLKEY_FILE DATA_DIR "/andna_lcl_keyring" -#define LCL_FILE DATA_DIR "/andna_lcl_cache" -#define RHC_FILE DATA_DIR "/andna_rh_cache" -#define COUNTER_C_FILE DATA_DIR "/andna_counter_cache" - -#define IPMASQ_SCRIPT_FILE CONF_DIR "/ip_masquerade.sh" -#define TCSHAPER_SCRIPT_FILE CONF_DIR "/tc_shaper.sh" - -/* - * ServOpt - * - * Options - */ -typedef struct -{ - char *config_file; - char *pid_file; - - int family; - - char *ifs[MAX_INTERFACES]; - int ifs_n; /* number of interfaces present in `ifs' */ - - char *int_map_file; - char *ext_map_file; - char *bnode_map_file; - - char *andna_hnames_file; - char *snsd_nodes_file; - char *andna_cache_file; - char *lclkey_file; - char *lcl_file; - char *rhc_file; - char *counter_c_file; - - char daemon; - - char restricted; - int restricted_class; - char inet_connection;/* If it's 1, we are connected - to the Internet */ - char share_internet; - char shape_internet; - char use_shared_inet; - //inet_prefix inet_gw; - char *inet_gw_dev; - char **inet_hosts; /* Hosts to be pinged in order to check - if the internet connection is up */ - int inet_hosts_counter; - char *ip_masq_script; - char *tc_shaper_script; - - /* The bandwidths of the Internet connection we are sharing. - * If we are just leeching they are all 0. */ - //u_int my_upload_bw; - //u_int my_dnload_bw; - - char disable_andna; - char disable_resolvconf; - - int max_connections; - int max_accepts_per_host; - int max_accepts_per_host_time; - - char dbg_lvl; -}ServOpt; -ServOpt server_opt; - -time_t sigterm_timestamp, sighup_timestamp, sigalrm_timestamp; - -#define MAX_CONNECTIONS 512 - -#define MAX_ACCEPTS 16 -#define FREE_ACCEPT_TIME 4 /*in seconds*/ - -#define setzero(_p, _sz) memset((_p), 0, (_sz)) +extern int sockfd, sockfd1, sendrecv = 0; +extern struct sockaddr_un serveraddr; +extern struct sockaddr ntkdaddr; +extern int rc, length; +extern char *request, *response; #endif /*NETSUKUKUCONSOLE_H*/ \ No newline at end of file