From 58493553ec4d0efaca1c1adddcdeb00e94e881bf Mon Sep 17 00:00:00 2001 From: FreeArtMan Date: Sat, 25 Feb 2017 00:44:03 +0000 Subject: Formatring patch from Ms.IJ applied --- agni.c | 34 ++++----- buf.h | 2 +- darray.c | 16 ++-- irc_parse.c | 2 +- mmm.c | 18 ++--- mq_cmd.c | 2 +- mq_cmd.h | 2 +- sock_conn.c | 248 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ sock_conn.h | 44 +++++++++++ tbl_qcmd.c | 8 +- tbl_qcmd.h | 12 +-- 11 files changed, 339 insertions(+), 49 deletions(-) create mode 100644 sock_conn.c create mode 100644 sock_conn.h diff --git a/agni.c b/agni.c index 9b13cc2..c1ac68f 100644 --- a/agni.c +++ b/agni.c @@ -24,8 +24,8 @@ #include "mq_cmd.h" #include "tbl_qcmd.h" -/* -no defence programming, no error checking, no argument checking just PoC +/* +no defence programming, no error checking, no argument checking just PoC nothing else */ @@ -78,8 +78,8 @@ typedef struct mq_ntf_io_mplx #define MQ_IN 2 -/* -supposed format agni-[id]-[in/out] +/* +supposed format agni-[id]-[in/out] in is for input of thread (send to in and thread will recieve), out if for output of thread (read from out to recieve from thread) */ @@ -94,8 +94,8 @@ typedef struct mq_ntf_mdt } mq_ntf_mdt; -/* -return -1 on error +/* +return -1 on error */ int mq_ntf_open(mq_ntf_mdt *mq, int id) { @@ -196,7 +196,7 @@ int mq_ntf_select(mq_ntf_mdt *mq, int dir) FD_SET(fd, &io_fds); tv.tv_sec = 1; tv.tv_usec = 0; - fdnum = select(1, + fdnum = select(1, &io_fds, NULL, NULL, @@ -241,7 +241,7 @@ int mq_ntf_read(mq_ntf_mdt *mq, int dir, char *buf, size_t size) printf("queue %d out full\n", mq->id); return -1; } - bytes = mq_receive(mqd, buf, + bytes = mq_receive(mqd, buf, size > attr.mq_msgsize ? attr.mq_msgsize : size, &prio); if (bytes == -1) @@ -278,7 +278,7 @@ int mq_ntf_write(mq_ntf_mdt *mq, int dir, const char *buf, size_t size) printf("queue %d out full\n", mq->id); return -1; } - bytes = mq_send(mqd, buf, + bytes = mq_send(mqd, buf, size > attr.mq_msgsize ? attr.mq_msgsize : size, prio); if (bytes == -1) @@ -451,12 +451,12 @@ int mq_ntf_getattr(mq_ntf_mdt *mq, int dir, struct mq_attr **attr) -int send_mq_cmd(mq_ntf_mdt *mq, +int send_mq_cmd(mq_ntf_mdt *mq, int io, - int cmd_id, - char *cmd, - size_t cmd_sz, - char *param, + int cmd_id, + char *cmd, + size_t cmd_sz, + char *param, size_t param_sz) { int err = 0; @@ -493,7 +493,7 @@ int send_mq_cmd(mq_ntf_mdt *mq, return 0; } -int recv_mq_cmd(mq_ntf_mdt *mq, +int recv_mq_cmd(mq_ntf_mdt *mq, int io, mq_cmd **cmd) //return back recieved command { @@ -652,7 +652,7 @@ int th_start_client(void *data) if (mq_ntf_read(mq, MQ_IN, in_buf, in_attr.mq_msgsize) == -1) { printf("Cant read input message \n"); - } else + } else { in_buf[in_attr.mq_msgsize-1] = 0x0; printf("Recieve %s\n", in_buf); @@ -873,7 +873,7 @@ int main(int argc, char **argv) clone(th_event_manager, evhnd_cfg->stack+EVENT_HND_STACK_SIZE, CLONE_VM|CLONE_FILES, (void *)evhnd_cfg); /* run until all threads are up */ - cnt_running = 1; + cnt_running = 1; while(cnt_running != 0) { //printf("Count\n"); diff --git a/buf.h b/buf.h index 99872ef..3af86ca 100644 --- a/buf.h +++ b/buf.h @@ -11,7 +11,7 @@ typedef struct bbuf { char *buf; } bbuf; -//operations on plain buffers also operations on bufer to buffer should be +//operations on plain buffers also operations on bufer to buffer should be //supoorted //create new buffer without content diff --git a/darray.c b/darray.c index 231bc5b..7978845 100644 --- a/darray.c +++ b/darray.c @@ -50,13 +50,13 @@ error: } -/*****************************************************************************/ +/*****************************************************************************/ void darr_destroy(darray *da) { //PRINT("\n"); - if(da) + if(da) { - if(da->data) + if(da->data) { free(da->data); } @@ -71,11 +71,11 @@ void darr_clear(darray *da) { //PRINT("\n"); int i = 0; - if(da->size > 0) + if(da->size > 0) { - for(i = 0; i < da->max; i++) + for(i = 0; i < da->max; i++) { - if(da->data[i] != NULL) + if(da->data[i] != NULL) { free(da->data[i]); } @@ -204,7 +204,7 @@ int darr_push(darray *da, void *val) da->data[da->end] = val; da->end++; - if(darr_end(da) >= darr_max(da)) + if(darr_end(da) >= darr_max(da)) { return darr_expand(da); } @@ -228,7 +228,7 @@ void* darr_pop(darray *da) val = darr_remove(da, da->end - 1); da->end--; - if((darr_end(da) > (int)da->expand) && + if((darr_end(da) > (int)da->expand) && (darr_end(da) % da->expand)) { darr_contract(da); diff --git a/irc_parse.c b/irc_parse.c index 98820a3..29a98dc 100644 --- a/irc_parse.c +++ b/irc_parse.c @@ -284,7 +284,7 @@ irc_token* irc_parse(char *str, int sz) //*(tok[j].e+1)=0x0; //write(0,"]\n",2); } - if (tok_cnt < 2 ) + if (tok_cnt < 2 ) { printf("Not enought tokens\n"); }; diff --git a/mmm.c b/mmm.c index 4dc9a04..0df8330 100644 --- a/mmm.c +++ b/mmm.c @@ -5,22 +5,20 @@ #ifdef MMM_RECORD - -void* mmm_malloc(size_t size, const char *fname, int line ) +void* mmm_malloc(size_t size, const char *fname, int line) { - void *ret=NULL; - ret = malloc( size ); - printf("alloc,0x%8p,%zu,%s,%d\n",(void *)ret,size,fname,line); + void *ret = NULL; + ret = malloc(size); + printf("alloc,0x%8p,%zu,%s,%d\n", (void *) ret, size, fname, line); + return ret; } -void mmm_free( void *ptr, const char *fname, int line ) +void mmm_free(void *ptr, const char *fname, int line) { - printf("free,0x%8p,%s,%d\n",(void *)ptr,fname,line); - free( ptr ); + printf("free,0x%8p,%s,%d\n", (void *) ptr, fname, line); + free(ptr); } #endif - - diff --git a/mq_cmd.c b/mq_cmd.c index e573721..c5bd1b4 100644 --- a/mq_cmd.c +++ b/mq_cmd.c @@ -36,7 +36,7 @@ mq_cmd* mq_cmd_create(int id, char *cmd, size_t cmd_sz, char *param, size_t para } //if not null string that everything will blow up pal //cmd[cmd_sz-1] = 0x0; - //param[param_sz-1] = 0x0; + //param[param_sz-1] = 0x0; snprintf(ret->buf,ret->sz-1,":%d:%s: %s", id, cmd, param); return ret; diff --git a/mq_cmd.h b/mq_cmd.h index 4a6f6ea..e83cf3e 100644 --- a/mq_cmd.h +++ b/mq_cmd.h @@ -7,7 +7,7 @@ #include "debug.h" -typedef struct mq_cmd +typedef struct mq_cmd { int id; char *buf; diff --git a/sock_conn.c b/sock_conn.c new file mode 100644 index 0000000..a57ca7b --- /dev/null +++ b/sock_conn.c @@ -0,0 +1,248 @@ +#include "sock_conn.h" + +/*mvar things*/ +//shitty macro +#define MVAR_ALLOC_STRC(VNAME,VTYPE,VRET)\ +VTYPE *VNAME;\ +VNAME=malloc(sizeof(VTYPE));\ +if ((VNAME)==NULL){\ +VRET\ +}\ +memset(VNAME,0,sizeof(VTYPE)); + +#define MVAR_ALLOC_ARR(VNAME,VTYPE,VSZ,VRET)\ +VTYPE *VNAME;\ +VNAME=malloc(sizeof(VTYPE)*(VSZ));\ +if ((VNAME)==NULL){\ +VRET\ +}\ +memset(VNAME,0,sizeof(VTYPE)*(VSZ)); + +irc_buf* irc_buf_create() +{ + MVAR_ALLOC_STRC(ret,irc_buf,{ + return NULL; + }); + + ret->out_size = 0; + ret->max_out_size = IRC_BUF_OUT_SIZE; + ret->buf_out = malloc(ret->max_out_size); + memset(ret->buf_out, 0, ret->max_out_size); + + ret->in_size = 0; + ret->max_in_size = IRC_BUF_IN_SIZE; + ret->buf_in = malloc(ret->max_in_size); + memset(ret->buf_in, 0, ret->max_in_size); + + ret->ready = 0; + + return ret; +} + +//cpy bytes from input to output +int __irc_buf_drain_io(irc_buf *ib) +{ + int ret=-1; + int cnt=0; + int i; + int j; + + //out buffer is allready full + if (ib->out_size-1 == ib->max_out_size) + { + printf("Out buffer is full\n"); + return -1; + } + + //check if buffer is complete buffer + if ((ib->ready == 1) //its marked as complete + || + //in case if its not marked, -1 becouse its index in array + ((ib->out_size >= 2) + &&(ib->buf_out[ib->out_size-1]=='\n') + &&(ib->buf_out[ib->out_size-2]=='\r'))) + { + printf("Buffer is ready\n"); + return 0; + } + + //lets from in buffer put as much as possible to out buffer + i = ib->in_size-1; + j = ib->out_size; + //PRINT("%d %d\n",i,j); + cnt = 0; + while ((jmax_out_size)&&(i>=0)) //buffers ar full or empty + { + if (ib->out_size >= 2) + if (((ib->buf_out[j-2]=='\r')&&(ib->buf_out[j-1]=='\n')))//bufferunderflow + { + ib->ready = 1; + //PNL(); + break; + } + ib->buf_out[j] = ib->buf_in[i]; + i--; + j++; + cnt++; + } + //update + PRINT("Copied %d bytes\n", cnt); + ib->in_size = i+1; //i can end up as -1 + ib->out_size = j; // + //if out buffer full and its doesnt have proper ending + if (ib->out_size >= 2) + if ((ib->buf_out[ib->out_size-1]=='\n')&&(ib->buf_out[ib->out_size-2]=='\r')) + { + ib->ready = 1; + return cnt; + } + + return cnt; +} + +//return amount not written bytes +int irc_buf_puts(irc_buf *ib, char *in_buf, int sz) +{ + int i,j; + int cnt; + int ret = -1; + int fret = -1; + + if (ib == NULL) + { + PERM(); + return -1; + } + + if (in_buf == NULL) + { + PERM(); + return -1; + } + + if (sz<0) + { + PERM(); + return -1; + } + + cnt = 0; + for ( + i=0, j=ib->in_size,cnt=0; + (imax_in_size); + i++, j++,cnt++) + { + //PRINT("%d %d\n",i,j); + ib->buf_in[j] = in_buf[sz-i-1]; + } + ib->in_size = j; + + fret = __irc_buf_drain_io(ib); + if (fret == 0) + { + printf("Cant do it pal\n"); + } + + //printf("%d %d\n",sz,cnt); + ret = cnt; + + return ret; +} + + +int irc_buf_putc(irc_buf *ib, char c) +{ + printf("Not implemtented\n"); + return -1; +} + +//return size of output buffer +int irc_buf_sz(irc_buf *ib) +{ + if (ib == NULL) + return -1; + + return ib->out_size; +} + + +int irc_buf_ready(irc_buf *ib) +{ + if (ib == NULL) + { + return -1; + } + + return ib->ready; +} + +//unset ready and return line +char *irc_buf_line(irc_buf *ib) +{ + char *ret = NULL; + + if (ib == NULL) + { + PERM(); + return NULL; + } + + if (ib->ready == 0) + { + PERM(); + return NULL; + } + + ret = alloc_new_str_s(ib->buf_out, ib->out_size); + if (ret == NULL) + { + PERM(); + return NULL; + } + + + ib->ready = 0; + ib->out_size = 0; + + return ret; +} + +int irc_buf_destroy(irc_buf *ib) +{ + if (ib == NULL) + { + return -1; + } + + free(ib->buf_in); + free(ib->buf_out); + free(ib); + + return 0; +} + + +/* +connect to hostname:port return fd>0 if connection succesful +*/ +int irc_connect( char *hostname, char *port ) +{ + int fd=0; + + struct addrinfo serv, *res; + + memset(&serv, 0, sizeof(serv)); + serv.ai_family = AF_INET; + serv.ai_socktype = SOCK_STREAM; + getaddrinfo(hostname, port, &serv, &res); + fd = socket(res->ai_family, res->ai_socktype, res->ai_protocol); + connect(fd, res->ai_addr, res->ai_addrlen); + + //? + //freeaddrinfo(&serv); + + return fd; +} + + + diff --git a/sock_conn.h b/sock_conn.h new file mode 100644 index 0000000..966a7f4 --- /dev/null +++ b/sock_conn.h @@ -0,0 +1,44 @@ +#ifndef __AGNI_SOCK_CONN_H +#define __AGNI_SOCK_CONN_H + +#include +#include +#include +#include +#include + +#include "util.h" + +#include "debug.h" + +#define IRC_BUF_IN_SIZE 1024*64 +#define IRC_BUF_OUT_SIZE 512 + +/* +Reads ins and spils out IRC lines ended with \r\n. +*/ +//?what will happend if there is no string consumer? +typedef struct irc_buf +{ + int out_size; + int max_out_size; + int in_size; + int max_in_size; + char *buf_in; + char *buf_out; + int ready;//if 1 there is one full line +} irc_buf; + +irc_buf* irc_buf_create(); +//put string inside in reverse order =P +int irc_buf_puts(irc_buf *ib, char *in_buf, int sz); +int irc_buf_putc(irc_buf *ib, char c); +int irc_buf_sz(irc_buf *ib); +int irc_buf_ready(irc_buf *ib); +char *irc_buf_line(irc_buf *ib); +int irc_buf_destroy(irc_buf *ib); + +int irc_connect(char *hostname, char *port); + + +#endif diff --git a/tbl_qcmd.c b/tbl_qcmd.c index fb45c5a..3d511b4 100644 --- a/tbl_qcmd.c +++ b/tbl_qcmd.c @@ -38,7 +38,7 @@ int tbl_exec_add(tbl_exec *tbl, tble_exec *cmd) if (tbl->size+1max_size) { tbl->size += 1; - } else + } else { return -1; } @@ -65,7 +65,7 @@ tble_exec *tbl_exec_search_cmd(tbl_exec *tbl, char *cmd) for (i=0;imax_size;i++) { cur_cmd = tbl->reg_cmd[i]; - if ((strncmp(cur_cmd->cmd,cmd,strlen(cmd)) == 0) && + if ((strncmp(cur_cmd->cmd,cmd,strlen(cmd)) == 0) && (strlen(cmd) == strlen(cur_cmd->cmd))) { ret = cur_cmd; @@ -160,7 +160,7 @@ int tbl_qcmd_add(tbl_qcmd *tbl, tble_qcmd *cmd) if (tbl->size+1max_size) { tbl->size += 1; - } else + } else { return -1; } @@ -181,7 +181,7 @@ int tbl_qcmd_chk(tbl_qcmd *tbl) for (i=0;imax_size;i++) { - //if command in some state like segfaulted or + //if command in some state like segfaulted or // finished running then lets check if ((tbl->cmd[i]->state == QCMD_TIMEOUT)|| (tbl->cmd[i]->state == QCMD_DONE)) diff --git a/tbl_qcmd.h b/tbl_qcmd.h index 4052812..3485117 100644 --- a/tbl_qcmd.h +++ b/tbl_qcmd.h @@ -40,7 +40,7 @@ typedef struct tbl_exec /* Fields: */ -typedef struct tble_qcmd +typedef struct tble_qcmd { int id; int timestamp; //when command started to execute @@ -57,7 +57,7 @@ typedef struct tble_qcmd table of commands executing Fields: */ -typedef struct tbl_qcmd +typedef struct tbl_qcmd { int size; int max_size; @@ -200,7 +200,7 @@ Output: >=0 - if everything whent ok, returns index in the table (ret-1) -1 - if there was some kind of mistake, or nothing happened */ -int tbl_qcmd_chk(tbl_qcmd *tbl); +int tbl_qcmd_chk(tbl_qcmd *tbl); /* delete command from the list @@ -211,10 +211,10 @@ Output: 0 - if everything whent ok -1 - if there was some kind of mistake */ -int tbl_qcmd_del(tbl_qcmd *tbl, int idx); +int tbl_qcmd_del(tbl_qcmd *tbl, int idx); /* -if there is response then try to match response and return code to +if there is response then try to match response and return code to executed command requester Input: tbl - array of executed cmds @@ -223,7 +223,7 @@ Output: 0 - if everything whent ok -1 - if there was some kind of mistake */ -int tbl_qcmd_resp(tbl_qcmd *tbl, tble_cmd_resp *resp); +int tbl_qcmd_resp(tbl_qcmd *tbl, tble_cmd_resp *resp); /* create cmd param -- cgit v1.2.3