From 5de1c5ba999dba7da45316d3c1f2592c0e0e4804 Mon Sep 17 00:00:00 2001 From: FreeArtMan Date: Sun, 24 Sep 2017 11:55:09 +0100 Subject: Rearranged code for more IPC protocols --- agni.c | 6 +- cmd/cmd_todo.c | 10 +++ cmd/cmd_todo.h | 17 ++++ config_cmds.h | 47 ++++++---- nbrpc.c | 232 ------------------------------------------------- nbrpc.h | 31 ------- nbrpc_call.c | 49 +++++++++++ nbrpc_call.h | 40 +++++++++ nbrpc_event.c | 269 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ nbrpc_event.h | 49 +++++++++++ sock_conn.c | 2 +- tbl_qcmd.h | 4 + 12 files changed, 471 insertions(+), 285 deletions(-) create mode 100644 cmd/cmd_todo.c create mode 100644 cmd/cmd_todo.h delete mode 100644 nbrpc.c delete mode 100644 nbrpc.h create mode 100644 nbrpc_call.c create mode 100644 nbrpc_call.h create mode 100644 nbrpc_event.c create mode 100644 nbrpc_event.h diff --git a/agni.c b/agni.c index f89dc17..b8fa534 100644 --- a/agni.c +++ b/agni.c @@ -34,7 +34,7 @@ #include "arg.h" -#include "nbrpc.h" +#include "nbrpc_event.h" /* no defence programming, no error checking, no argument checking just PoC @@ -598,7 +598,7 @@ int th_start_client(void *data) printf("Reconnect in 10 sec\n"); sleep(10); } - sleep(1); + //sleep(1); } //send some commands to irc to register nick snprintf(cmd_buf, TH_CONN_BUF_SZ, "USER %s 0 0 :%s\r\n", cfg->user, cfg->user); @@ -618,7 +618,7 @@ int th_start_client(void *data) printf("Reconnect in 10 sec\n"); sleep(10); } - sleep(1); + //sleep(1); } //send some commands to irc to register nick snprintf(cmd_buf, TH_CONN_BUF_SZ, "USER %s 0 0 :%s\r\n", cfg->user, cfg->user); diff --git a/cmd/cmd_todo.c b/cmd/cmd_todo.c new file mode 100644 index 0000000..0f3ae58 --- /dev/null +++ b/cmd/cmd_todo.c @@ -0,0 +1,10 @@ +#include "cmd_todo.h" + +void *cmd_todo(void *data) +{ + char *ret = NULL; + + + + return ret; +} diff --git a/cmd/cmd_todo.h b/cmd/cmd_todo.h new file mode 100644 index 0000000..b5227e7 --- /dev/null +++ b/cmd/cmd_todo.h @@ -0,0 +1,17 @@ +#ifndef __CMD_TODO_H +#define __CMD_TODO_H + +#include +#include +#include +#include +#include +#include +#include + +#include "util.h" +#include "debug.h" + +void *cmd_todo(void *data); + +#endif \ No newline at end of file diff --git a/config_cmds.h b/config_cmds.h index a123ec8..e532d64 100644 --- a/config_cmds.h +++ b/config_cmds.h @@ -1,8 +1,13 @@ #ifndef __CONFIG_CMDS_H #define __CONFIG_CMDS_H +#define CMD_T_SIMPLE 0 +#define CMD_T_RPC 1 +#define CMD_T_LUA 2 + typedef struct single_cmd_def { + int type; char *name; void *(*callback)(void *); } single_cmd_def; @@ -15,24 +20,30 @@ typedef struct single_cmd_def //nothing else comaptible will be added, as its not about "standarts" single_cmd_def confgi_cmd_list[] = { - {"DATE",cmd_date}, - {"UPTIME",cmd_uptime}, - {"VERSION",cmd_version}, {"!version",cmd_version}, - {"PING",cmd_ping}, {"!ping",cmd_ping}, - {"LOADAVG",cmd_loadavg}, - {"RUSE",cmd_rusage}, - {"CMD",cmd_cmd}, {"!echo",cmd_cmd}, - {"SHA1",cmd_sha1}, - {"COOKIE",cmd_cookie}, - {"BOTU",cmd_botu}, {"!uptime",cmd_botu}, - {"FIR1P",cmd_fir1p}, - {"RAND",cmd_rand}, - {"RANDC",cmd_rand_libc}, - {"FIR",cmd_fir}, - {"HELP",cmd_help}, - {"SPI",cmd_spi}, - {"STAT",cmd_stat}, - {NULL,NULL} + //simple echo response commands + {CMD_T_SIMPLE,"DATE",cmd_date}, + {CMD_T_SIMPLE,"UPTIME",cmd_uptime}, + {CMD_T_SIMPLE,"VERSION",cmd_version}, {CMD_T_SIMPLE,"!version",cmd_version}, + {CMD_T_SIMPLE,"PING",cmd_ping}, {CMD_T_SIMPLE,"!ping",cmd_ping}, + {CMD_T_SIMPLE,"LOADAVG",cmd_loadavg}, + {CMD_T_SIMPLE,"RUSE",cmd_rusage}, + {CMD_T_SIMPLE,"CMD",cmd_cmd}, {CMD_T_SIMPLE,"!echo",cmd_cmd}, + {CMD_T_SIMPLE,"SHA1",cmd_sha1}, + {CMD_T_SIMPLE,"COOKIE",cmd_cookie}, + {CMD_T_SIMPLE,"BOTU",cmd_botu}, {CMD_T_SIMPLE,"!uptime",cmd_botu}, + {CMD_T_SIMPLE,"FIR1P",cmd_fir1p}, + {CMD_T_SIMPLE,"RAND",cmd_rand}, + {CMD_T_SIMPLE,"RANDC",cmd_rand_libc}, + {CMD_T_SIMPLE,"FIR",cmd_fir}, + {CMD_T_SIMPLE,"HELP",cmd_help}, + {CMD_T_SIMPLE,"SPI",cmd_spi}, + {CMD_T_SIMPLE,"STAT",cmd_stat}, + + //more complicated command + {CMD_T_RPC,"TODO",cmd_todo}, + + + {0,NULL,NULL} }; diff --git a/nbrpc.c b/nbrpc.c deleted file mode 100644 index 9adde5d..0000000 --- a/nbrpc.c +++ /dev/null @@ -1,232 +0,0 @@ -#include "nbrpc.h" - -extern char* alloc_new_str(char *); -extern char *alloc_new_str_s(char *, size_t); - -rpc_request* rpc_req_new(char *method, char *params, int id) -{ - rpc_request *ret = NULL; - - ret = malloc(sizeof(rpc_request)); - if (!ret) - { - return NULL; - } - ret->id = id; - ret->method = alloc_new_str(method); - ret->params = alloc_new_str(params); - return ret; -} - - -rpc_response* rpc_resp_new(char *result, char *error, int id) -{ - rpc_response *ret = NULL; - - ret = malloc(sizeof(rpc_response)); - if (!ret) - { - return NULL; - } - - PRINT("resp->id %d\n", id); - ret->id = id; - ret->result = alloc_new_str(result); - ret->error = alloc_new_str(error); - - return ret; -} - - -int rpc_req_free(rpc_request *req) -{ - if (req) - { - free(req->method); - free(req->params); - free(req); - req = NULL; - } - -} - - -int rpc_resp_free(rpc_response *resp) -{ - if (resp) - { - free(resp->result); - free(resp->error); - free(resp); - resp = NULL; - } -} - - -int rpc_req_marsh( rpc_request *req, netbyte_store **nb_req) -{ - netbyte_store *nb = NULL; - nb_u32 nb_id; - nb_u8arr nb_method, nb_params; - - nb = calloc(1,sizeof(netbyte_store)); - nb_init(nb); - - nb_u8arr_create(&nb_method, strlen(req->method), req->method); - nb_add_u8arr(nb, &nb_method); - - nb_u8arr_create(&nb_params, strlen(req->params), req->params); - nb_add_u8arr(nb, &nb_params); - - nb_u32_create(&nb_id, req->id); - //PRINT("%d\n", req->id); - //PRINT("%d\n", nb_id.val); - nb_add_u32(nb, &nb_id); - - //nb_print(nb); - //nb_print(nb); - - *nb_req = nb; - return 0; -} - - -int rpc_resp_marsh( rpc_response *resp, netbyte_store **nb_resp) -{ - int eret; - netbyte_store *nb = NULL; - nb_u32 nb_id; - nb_u8arr nb_result, nb_error; - - nb = calloc(1,sizeof(netbyte_store)); - nb_init(nb); - - eret = nb_u8arr_create(&nb_result, strlen(resp->result), resp->result); - eret |= nb_add_u8arr(nb, &nb_result); - - eret = nb_u8arr_create(&nb_error, strlen(resp->error), resp->error); - eret |= nb_add_u8arr(nb, &nb_error); - - eret = nb_u32_create(&nb_id, resp->id); - //PRINT("%d\n", resp->id); - //PRINT("%d\n", nb_id.val); - eret |= nb_add_u32(nb, &nb_id); - if (!eret) - { - ENL(); - } - - //nb_print(nb); - - *nb_resp = nb; - return 0; -} - - -int rpc_req_unmarsh( netbyte_store *nb_req, rpc_request **req) -{ - __nb_type *type=NULL; - rpc_request *__req=NULL; - - if (!nb_req) - { - return -1; - } - - if (!req) - { - return -1; - } - - __req = calloc(1,sizeof(rpc_request)); - - if (0 == nb_val(nb_req, 0, &type)) - if (type->type == NBT_U8ARRAY) - { - nb_u8arr *u8arr = (nb_u8arr *)type->nb_val; - __req->method = alloc_new_str_s(u8arr->val, u8arr->len); - } else - { - ENL(); - } - - if (0 == nb_val(nb_req, 1, &type)) - if (type->type == NBT_U8ARRAY) - { - nb_u8arr *u8arr = (nb_u8arr *)type->nb_val; - __req->params = alloc_new_str_s(u8arr->val, u8arr->len); - } else - { - ENL(); - } - - if (0 == nb_val(nb_req, 2, &type)) - if (type->type == NBT_U32) - { - //PNL(); - nb_u32 *u32 = (nb_u32 *)type->nb_val; - __req->id = u32->val; - } else - { - ENL(); - } - - *req = __req; - - return 0; -} - - -int rpc_resp_unmarsh(netbyte_store *nb_resp, rpc_response **resp) -{ - __nb_type *type=NULL; - rpc_response *__resp=NULL; - - if (!nb_resp) - { - return -1; - } - - if (!resp) - { - return -1; - } - - __resp = calloc(1,sizeof(rpc_response)); - - if (0 == nb_val(nb_resp, 0, &type)) - if (type->type == NBT_U8ARRAY) - { - nb_u8arr *u8arr = (nb_u8arr *)type->nb_val; - __resp->result = alloc_new_str_s(u8arr->val, u8arr->len); - } else - { - ENL(); - } - - if (0 == nb_val(nb_resp, 1, &type)) - if (type->type == NBT_U8ARRAY) - { - nb_u8arr *u8arr = (nb_u8arr *)type->nb_val; - __resp->error = alloc_new_str_s(u8arr->val, u8arr->len); - } else - { - ENL(); - } - - if (0 == nb_val(nb_resp, 2, &type)) - if (type->type == NBT_U32) - { - PNL(); - nb_u32 *u32 = (nb_u32 *)type->nb_val; - __resp->id = u32->val; - //PRINT("%d\n",u32->val) - } else - { - ENL(); - } - - *resp = __resp; - - return 0; -} \ No newline at end of file diff --git a/nbrpc.h b/nbrpc.h deleted file mode 100644 index 2c7b479..0000000 --- a/nbrpc.h +++ /dev/null @@ -1,31 +0,0 @@ -#ifndef __AGNI_NBRPC_H -#define __AGNI_NBRPC_H - -#include - -#include "mq_ntf.h" - -typedef struct rpc_request -{ - char *method; - char *params; - int id; -} rpc_request; - -typedef struct rpc_response -{ - char *result; - char *error; - int id; -} rpc_response; - -rpc_request* rpc_req_new(char *method, char *params, int id); -rpc_response* rpc_resp_new(char *result, char *error, int id); -int rpc_req_free(rpc_request *req); -int rpc_resp_free(rpc_response *resp); -int rpc_req_marsh( rpc_request *req, netbyte_store **nb_req); -int rpc_resp_marsh( rpc_response *resp, netbyte_store **nb_resp); -int rpc_req_unmarsh( netbyte_store *nb_req, rpc_request **req); -int rpc_resp_unmarsh(netbyte_store *nb_resp, rpc_response **resp); - -#endif \ No newline at end of file diff --git a/nbrpc_call.c b/nbrpc_call.c new file mode 100644 index 0000000..3fddde7 --- /dev/null +++ b/nbrpc_call.c @@ -0,0 +1,49 @@ +#include "nbrpc_call.h" + +rpc_call_request* rpc_call_req_new(char *method, char *params, int id) +{ + +} + + +rpc_call_response* rpc_call_resp_new(char *result, char *error, int id) +{ + +} + + +int rpc_call_req_free(rpc_call_request *req) +{ + +} + + +int rpc_call_resp_free(rpc_call_response *resp) +{ + +} + + +int rpc_call_req_marsh( rpc_call_request *req, netbyte_store **nb_req) +{ + +} + + +int rpc_call_resp_marsh( rpc_call_response *resp, netbyte_store **nb_resp) +{ + +} + + +int rpc_call_req_unmarsh( netbyte_store *nb_req, rpc_call_request **req) +{ + +} + + +int rpc_call_resp_unmarsh(netbyte_store *nb_resp, rpc_call_response **resp) +{ + +} + diff --git a/nbrpc_call.h b/nbrpc_call.h new file mode 100644 index 0000000..183f911 --- /dev/null +++ b/nbrpc_call.h @@ -0,0 +1,40 @@ +#ifndef __AGNI_NBRPC_CALL_H +#define __AGNI_NBRPC_CALL_H + +#include + +#include + +typedef struct rpc_call_request +{ + char *method; + char *params; + char *user; + char *mask; + char *server; + int id; +} rpc_call_request; + +typedef struct rpc_call_response +{ + char *result; + char *error; + char *user; + char *mask; + char *server; + int id; +} rpc_call_response; + +rpc_call_request* rpc_call_req_new(char *method, char *params, int id); +rpc_call_response* rpc_call_resp_new(char *result, char *error, int id); + +int rpc_call_req_free(rpc_call_request *req); +int rpc_call_resp_free(rpc_call_response *resp); + +int rpc_call_req_marsh( rpc_call_request *req, netbyte_store **nb_req); +int rpc_call_resp_marsh( rpc_call_response *resp, netbyte_store **nb_resp); + +int rpc_call_req_unmarsh( netbyte_store *nb_req, rpc_call_request **req); +int rpc_call_resp_unmarsh(netbyte_store *nb_resp, rpc_call_response **resp); + +#endif \ No newline at end of file diff --git a/nbrpc_event.c b/nbrpc_event.c new file mode 100644 index 0000000..dc5cddd --- /dev/null +++ b/nbrpc_event.c @@ -0,0 +1,269 @@ +#include "nbrpc_event.h" + +extern char* alloc_new_str(char *); +extern char *alloc_new_str_s(char *, size_t); + +rpc_request* rpc_req_new(char *method, char *params, int id) +{ + rpc_request *ret = NULL; + + ret = malloc(sizeof(rpc_request)); + if (!ret) + { + return NULL; + } + ret->id = id; + ret->method = alloc_new_str(method); + ret->params = alloc_new_str(params); + return ret; +} + + +rpc_response* rpc_resp_new(char *result, char *error, int id) +{ + rpc_response *ret = NULL; + + ret = malloc(sizeof(rpc_response)); + if (!ret) + { + return NULL; + } + + PRINT("resp->id %d\n", id); + ret->id = id; + ret->result = alloc_new_str(result); + ret->error = alloc_new_str(error); + + return ret; +} + + +int rpc_req_free(rpc_request *req) +{ + if (req) + { + free(req->method); + free(req->params); + free(req); + req = NULL; + } + +} + + +int rpc_resp_free(rpc_response *resp) +{ + if (resp) + { + free(resp->result); + free(resp->error); + free(resp); + resp = NULL; + } +} + + +int rpc_req_marsh( rpc_request *req, netbyte_store **nb_req) +{ + netbyte_store *nb = NULL; + nb_u32 nb_id; + nb_u8arr nb_method, nb_params; + + nb = calloc(1,sizeof(netbyte_store)); + nb_init(nb); + + nb_u8arr_create(&nb_method, strlen(req->method), req->method); + nb_add_u8arr(nb, &nb_method); + + nb_u8arr_create(&nb_params, strlen(req->params), req->params); + nb_add_u8arr(nb, &nb_params); + + nb_u32_create(&nb_id, req->id); + //PRINT("%d\n", req->id); + //PRINT("%d\n", nb_id.val); + nb_add_u32(nb, &nb_id); + + //nb_print(nb); + //nb_print(nb); + + *nb_req = nb; + return 0; +} + + +int rpc_resp_marsh( rpc_response *resp, netbyte_store **nb_resp) +{ + int eret; + netbyte_store *nb = NULL; + nb_u32 nb_id; + nb_u8arr nb_result, nb_error; + + nb = calloc(1,sizeof(netbyte_store)); + nb_init(nb); + + eret = nb_u8arr_create(&nb_result, strlen(resp->result), resp->result); + eret |= nb_add_u8arr(nb, &nb_result); + + eret = nb_u8arr_create(&nb_error, strlen(resp->error), resp->error); + eret |= nb_add_u8arr(nb, &nb_error); + + eret = nb_u32_create(&nb_id, resp->id); + //PRINT("%d\n", resp->id); + //PRINT("%d\n", nb_id.val); + eret |= nb_add_u32(nb, &nb_id); + if (!eret) + { + ENL(); + } + + //nb_print(nb); + + *nb_resp = nb; + return 0; +} + + +int rpc_req_unmarsh( netbyte_store *nb_req, rpc_request **req) +{ + __nb_type *type=NULL; + rpc_request *__req=NULL; + + if (!nb_req) + { + return -1; + } + + if (!req) + { + return -1; + } + + __req = calloc(1,sizeof(rpc_request)); + + if (0 == nb_val(nb_req, 0, &type)) + if (type->type == NBT_U8ARRAY) + { + nb_u8arr *u8arr = (nb_u8arr *)type->nb_val; + __req->method = alloc_new_str_s(u8arr->val, u8arr->len); + } else + { + ENL(); + } + + if (0 == nb_val(nb_req, 1, &type)) + if (type->type == NBT_U8ARRAY) + { + nb_u8arr *u8arr = (nb_u8arr *)type->nb_val; + __req->params = alloc_new_str_s(u8arr->val, u8arr->len); + } else + { + ENL(); + } + + if (0 == nb_val(nb_req, 2, &type)) + if (type->type == NBT_U32) + { + //PNL(); + nb_u32 *u32 = (nb_u32 *)type->nb_val; + __req->id = u32->val; + } else + { + ENL(); + } + + *req = __req; + + return 0; +} + + +int rpc_resp_unmarsh(netbyte_store *nb_resp, rpc_response **resp) +{ + __nb_type *type=NULL; + rpc_response *__resp=NULL; + + if (!nb_resp) + { + return -1; + } + + if (!resp) + { + return -1; + } + + __resp = calloc(1,sizeof(rpc_response)); + + if (0 == nb_val(nb_resp, 0, &type)) + if (type->type == NBT_U8ARRAY) + { + nb_u8arr *u8arr = (nb_u8arr *)type->nb_val; + __resp->result = alloc_new_str_s(u8arr->val, u8arr->len); + } else + { + ENL(); + } + + if (0 == nb_val(nb_resp, 1, &type)) + if (type->type == NBT_U8ARRAY) + { + nb_u8arr *u8arr = (nb_u8arr *)type->nb_val; + __resp->error = alloc_new_str_s(u8arr->val, u8arr->len); + } else + { + ENL(); + } + + if (0 == nb_val(nb_resp, 2, &type)) + if (type->type == NBT_U32) + { + PNL(); + nb_u32 *u32 = (nb_u32 *)type->nb_val; + __resp->id = u32->val; + //PRINT("%d\n",u32->val) + } else + { + ENL(); + } + + *resp = __resp; + + return 0; +} + + +int rpc_req_set_user(rpc_request *req) +{ + +} + + +int rpc_resp_set_user(rpc_response *resp) +{ + +} + + +int rpc_req_set_mask(rpc_request *req) +{ + +} + + +int rpc_resp_set_mask(rpc_response *resp) +{ + +} + + +int rpc_req_set_server(rpc_request *req) +{ + +} + + +int rpc_resp_set_server(rpc_response *resp) +{ + +} + diff --git a/nbrpc_event.h b/nbrpc_event.h new file mode 100644 index 0000000..5f3657a --- /dev/null +++ b/nbrpc_event.h @@ -0,0 +1,49 @@ +#ifndef __AGNI_NBRPC_H +#define __AGNI_NBRPC_H + +#include + +#include + +typedef struct rpc_request +{ + char *method; + char *params; + char *user; + char *mask; + char *server; + int id; +} rpc_request; + +typedef struct rpc_response +{ + char *result; + char *error; + char *user; + char *mask; + char *server; + int id; +} rpc_response; + +rpc_request* rpc_req_new(char *method, char *params, int id); +rpc_response* rpc_resp_new(char *result, char *error, int id); + +int rpc_req_set_user(rpc_request *req); +int rpc_resp_set_user(rpc_response *resp); + +int rpc_req_set_mask(rpc_request *req); +int rpc_resp_set_mask(rpc_response *resp); + +int rpc_req_set_server(rpc_request *req); +int rpc_resp_set_server(rpc_response *resp); + +int rpc_req_free(rpc_request *req); +int rpc_resp_free(rpc_response *resp); + +int rpc_req_marsh( rpc_request *req, netbyte_store **nb_req); +int rpc_resp_marsh( rpc_response *resp, netbyte_store **nb_resp); + +int rpc_req_unmarsh( netbyte_store *nb_req, rpc_request **req); +int rpc_resp_unmarsh(netbyte_store *nb_resp, rpc_response **resp); + +#endif \ No newline at end of file diff --git a/sock_conn.c b/sock_conn.c index 0c206ea..c06bcb2 100644 --- a/sock_conn.c +++ b/sock_conn.c @@ -303,7 +303,7 @@ int irc_connect( char *hostname, char *port ) int irc_open(char *hostname, char *port, irc_conn *conn) { int fd, fret; - struct addrinfo serv, *res; + struct addrinfo serv, *res=NULL;//_res, *res=&_res; if (conn == NULL) { diff --git a/tbl_qcmd.h b/tbl_qcmd.h index 26f805e..6bf799a 100644 --- a/tbl_qcmd.h +++ b/tbl_qcmd.h @@ -15,6 +15,10 @@ #define EXEC_CURRENT 0 //executes in current thread #define EXEC_SEPERATE 1 //executes in seperate thread +#define TBL_T_SIMPLE 0 //simple command that just passes params to callback nothing else +#define TBL_T_RPC 1 //send more params and uses rpc for command +#define TBL_T_LUA 2 //send commands to lua interpreter + /* Fields: */ -- cgit v1.2.3