mirror of
https://github.com/ChronosX88/netsukuku.git
synced 2024-11-22 10:12:18 +00:00
Wow! That was a lot of work! But, Netsukuku now has (or should have) a functioni
ng console with the bindings in ntkd to receive, process, and respond to command s! Now, I just need to test stuff, And figure out how to deal with server_opt be cause that is a type and not just a struct. Anyway, Enjoy!
This commit is contained in:
parent
f47aae4605
commit
bd9a10620f
@ -56,7 +56,7 @@ int validity_check(char *request) {
|
||||
|
||||
}
|
||||
|
||||
/* this function is run by the second thread */
|
||||
/* Sends and receives to ntkd */
|
||||
void ntkd_request(char *request) {
|
||||
|
||||
rc = sendto(sockfd1, request, strlen(request), 0, (struct sockaddr *)&serveraddr, (socklen_t)sizeof(&serveraddr));
|
||||
@ -79,6 +79,8 @@ void ntkd_request(char *request) {
|
||||
|
||||
void opensocket(void) {
|
||||
|
||||
int stop_trying;
|
||||
|
||||
sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
|
||||
if (sockfd < 0) {
|
||||
perror("socket creation failed");
|
||||
@ -93,8 +95,15 @@ void opensocket(void) {
|
||||
if (rc < 0) {
|
||||
perror("bind() failed");
|
||||
clean_up();
|
||||
if(stop_trying >= 2) {
|
||||
perror("bind() failed");
|
||||
clean_up();
|
||||
opensocket();
|
||||
exit(-1);
|
||||
}
|
||||
stop_trying++;
|
||||
opensocket();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void console_uptime(void) {
|
||||
@ -132,8 +141,6 @@ void console_uptime(void) {
|
||||
}
|
||||
|
||||
void console(char *request) {
|
||||
|
||||
printf("%s", request);
|
||||
|
||||
if(validity_check(request) == -2)
|
||||
printf("Error: Command has not been processed!");
|
||||
@ -153,11 +160,10 @@ void console(char *request) {
|
||||
usage();
|
||||
|
||||
if(validity_check(request) == 2)
|
||||
/*system("ntkd -k");*/
|
||||
printf("");
|
||||
system("ntkd -k");
|
||||
|
||||
if(validity_check(request) == 3) {
|
||||
printf("%s", VERSION_STR);
|
||||
printf("Ntk-Console Version: %s", VERSION_STR);
|
||||
ntkd_request(request);
|
||||
}
|
||||
|
||||
@ -167,7 +173,7 @@ void console(char *request) {
|
||||
|
||||
int main(void) {
|
||||
|
||||
/*time(&rawtime);
|
||||
time(&rawtime);
|
||||
|
||||
timeinfo = localtime(&rawtime);
|
||||
|
||||
@ -180,7 +186,7 @@ int main(void) {
|
||||
|
||||
opensocket();
|
||||
|
||||
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");
|
||||
|
||||
char *request;
|
||||
|
||||
@ -188,23 +194,19 @@ int main(void) {
|
||||
|
||||
exit_now = 1;
|
||||
|
||||
request = (char*)malloc(512);
|
||||
|
||||
while(exit_now == 1) {
|
||||
|
||||
printf("\n>");
|
||||
|
||||
fgets(request, 16, stdin);
|
||||
|
||||
perror("fgets failed");
|
||||
|
||||
fflush(stdin);
|
||||
|
||||
printf("%s", request);
|
||||
|
||||
request[strlen(request)-1] = '\0';
|
||||
|
||||
printf("%s", request);
|
||||
|
||||
/*console(request);*/
|
||||
console(request);
|
||||
}
|
||||
|
||||
return 0;
|
||||
@ -218,7 +220,7 @@ void usage(void) {
|
||||
"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"
|
||||
" version Shows the running version of the ntk-console, and ntkd.\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"
|
||||
|
@ -13,7 +13,7 @@
|
||||
#include <errno.h>
|
||||
|
||||
#define SERVER_PATH "/tmp/ntk-console"
|
||||
#define VERSION_STR "0.0.1"
|
||||
#define VERSION_STR "0.0.2"
|
||||
#define FALSE 0
|
||||
|
||||
int sockfd, sockfd1;
|
||||
|
0
src/Ntk-Console/Ntk-Console-Compile.sh
Executable file → Normal file
0
src/Ntk-Console/Ntk-Console-Compile.sh
Executable file → Normal file
@ -40,6 +40,8 @@
|
||||
#include "radar.h"
|
||||
#include "hook.h"
|
||||
#include "rehook.h"
|
||||
#include "ntk-console-bindings.c"
|
||||
#include <pthread.h>
|
||||
|
||||
|
||||
extern int errno;
|
||||
@ -333,7 +335,7 @@ void check_excluded(void) {
|
||||
int i;
|
||||
|
||||
printf("Number of Interfaces in Use: %d\n", server_opt.ifs_n);
|
||||
printf("Interface names in Use: %s", server_opt.ifs);
|
||||
printf("Interface names in Use: %s", (char*)server_opt.ifs);
|
||||
|
||||
for(i=0; i<server_opt.ifs_n; i++) {
|
||||
if(strcmp(server_opt.ifs[i], optarg) == 0) {
|
||||
@ -417,6 +419,19 @@ check_excluded();
|
||||
freeifaddrs(addrs);
|
||||
}
|
||||
|
||||
void *console_recv_send1(void *void_ptr){
|
||||
console_recv_send();
|
||||
}
|
||||
|
||||
int ntk_thread_creatation(void) {
|
||||
int x;
|
||||
pthread_t console_recv_send_thread;
|
||||
if(pthread_create(&console_recv_send_thread, NULL, console_recv_send1, &x)) {
|
||||
fprintf(stderr, "Error creating thread\n");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
void parse_options(int argc, char **argv)
|
||||
{
|
||||
int c, saved_argc=argc;
|
||||
@ -444,17 +459,22 @@ void parse_options(int argc, char **argv)
|
||||
{"version", 0, 0, 'v'},
|
||||
{"kill", 0, 0, 'k'},
|
||||
{"exclude", 1, 0, 'e'},
|
||||
{"console", 0, 0, 'C'},
|
||||
{0, 0, 0, 0}
|
||||
};
|
||||
|
||||
c = getopt_long (argc, argv,"i:c:l:e:hvd64DRrIak", long_options,
|
||||
c = getopt_long (argc, argv,"i:c:l:e:hvd64DRrIakC", long_options,
|
||||
&option_index);
|
||||
if (c == -1)
|
||||
break;
|
||||
|
||||
switch(c)
|
||||
{
|
||||
case 'v':
|
||||
case 'C':
|
||||
ntk_thread_creatation();
|
||||
break;
|
||||
|
||||
case 'v':
|
||||
printf("%s\n",VERSION_STR);
|
||||
exit(0);
|
||||
break;
|
||||
|
136
src/ntk-console-bindings.c
Normal file
136
src/ntk-console-bindings.c
Normal file
@ -0,0 +1,136 @@
|
||||
#include <utmp.h>
|
||||
#include <sys/un.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "netsukuku.h"
|
||||
|
||||
#define SERVER_PATH "/tmp/ntk-console"
|
||||
|
||||
int sockfd_1, sockfd_2;
|
||||
struct sockaddr_un serveraddr;
|
||||
struct sockaddr ntkdaddr;
|
||||
int rc;
|
||||
|
||||
int millisleep(unsigned ms)
|
||||
{
|
||||
return usleep(1000 * ms);
|
||||
}
|
||||
|
||||
void clean_up(void) {
|
||||
|
||||
const int optVal = 1;
|
||||
const socklen_t optLen = sizeof(optVal);
|
||||
|
||||
setsockopt(sockfd_1, SOL_SOCKET, SO_REUSEADDR, (void*) &optVal, optLen);
|
||||
setsockopt(sockfd_2, SOL_SOCKET, SO_REUSEADDR, (void*) &optVal, optLen);
|
||||
|
||||
if (sockfd_1 != -1)
|
||||
close(sockfd_1);
|
||||
|
||||
if (sockfd_2 != -1)
|
||||
close(sockfd_2);
|
||||
|
||||
unlink(SERVER_PATH);
|
||||
|
||||
}
|
||||
|
||||
void opensocket(void) {
|
||||
|
||||
int stop_trying;
|
||||
|
||||
sockfd_1 = socket(AF_UNIX, SOCK_STREAM, 0);
|
||||
if (sockfd_1 < 0) {
|
||||
perror("socket creation failed");
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
memset(&serveraddr, 0, sizeof(serveraddr));
|
||||
serveraddr.sun_family = AF_UNIX;
|
||||
strcpy(serveraddr.sun_path, SERVER_PATH);
|
||||
|
||||
rc = bind(sockfd_1, (struct sockaddr *)&serveraddr, SUN_LEN(&serveraddr));
|
||||
if (rc < 0) {
|
||||
perror("bind() failed");
|
||||
clean_up();
|
||||
if(stop_trying >= 2) {
|
||||
perror("bind() failed");
|
||||
clean_up();
|
||||
opensocket();
|
||||
exit(-1);
|
||||
}
|
||||
stop_trying++;
|
||||
opensocket();
|
||||
}
|
||||
}
|
||||
|
||||
void send_response(char *response, ...) {
|
||||
|
||||
rc = sendto(sockfd_1, response, strlen(response), 0, (struct sockaddr *)&serveraddr, (socklen_t)sizeof(&serveraddr));
|
||||
if (rc < 0) {
|
||||
perror("sendto() failed");
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
int request_processing(char *unprocessed_request) {
|
||||
|
||||
if(strncmp(unprocessed_request,"uptime", (int)strlen(unprocessed_request)) == 0)
|
||||
send_response((char*)time(0)-me.uptime);
|
||||
|
||||
else if(strncmp(unprocessed_request,"version", (int)strlen(unprocessed_request)) == 0)
|
||||
send_response(VERSION_STR);
|
||||
|
||||
else if(strncmp(unprocessed_request,"inet_connected", (int)strlen(unprocessed_request)) == 0)
|
||||
send_response((char*)me.inet_connected);
|
||||
|
||||
else if(strncmp(unprocessed_request,"cur_ifs", (int)strlen(unprocessed_request)) == 0)
|
||||
send_response((char*)me.cur_ifs);
|
||||
|
||||
else if(strncmp(unprocessed_request,"cur_ifs_n", (int)strlen(unprocessed_request)) == 0)
|
||||
send_response((char*)me.cur_ifs_n);
|
||||
|
||||
else if(strncmp(unprocessed_request,"cur_qspn_id", (int)strlen(unprocessed_request)) == 0)
|
||||
send_response((char*)me.cur_qspn_id);
|
||||
|
||||
else if(strncmp(unprocessed_request,"cur_ip", (int)strlen(unprocessed_request)) == 0)
|
||||
send_response((char*)me.cur_ip.data);
|
||||
|
||||
/*else if(strncmp(unprocessed_request,"cur_node", (int)strlen(unprocessed_request)) == 0)
|
||||
send_response(me.cur_node);
|
||||
|
||||
else if(strncmp(unprocessed_request,"ifs", (int)strlen(unprocessed_request)) == 0)
|
||||
return 0;
|
||||
|
||||
else if(strncmp(unprocessed_request,"ifs_n", (int)strlen(unprocessed_request)) == 0)
|
||||
return 0;*/
|
||||
send_response(unprocessed_request, " Is invalid or yet to be implemented.");
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Sends and receives to the ntk console */
|
||||
void ntkd_request(void) {
|
||||
|
||||
char *request;
|
||||
|
||||
while(0 == 0) {
|
||||
|
||||
millisleep(100);
|
||||
|
||||
request = 0;
|
||||
|
||||
rc = recvfrom(sockfd_1, request, strlen(request), MSG_WAITALL, (struct sockaddr *)&ntkdaddr, (socklen_t *__restrict)sizeof(&ntkdaddr));
|
||||
if (rc < 0) {
|
||||
perror("recvfrom() failed");
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
if(request != 0)
|
||||
request_processing(request);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
int console_recv_send(void) {
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user