diff options
-rw-r--r-- | agni.c | 34 | ||||
-rw-r--r-- | buf.h | 2 | ||||
-rw-r--r-- | darray.c | 16 | ||||
-rw-r--r-- | irc_parse.c | 2 | ||||
-rw-r--r-- | mmm.c | 18 | ||||
-rw-r--r-- | mq_cmd.c | 2 | ||||
-rw-r--r-- | mq_cmd.h | 2 | ||||
-rw-r--r-- | sock_conn.c | 248 | ||||
-rw-r--r-- | sock_conn.h | 44 | ||||
-rw-r--r-- | tbl_qcmd.c | 8 | ||||
-rw-r--r-- | tbl_qcmd.h | 12 |
11 files changed, 339 insertions, 49 deletions
@@ -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"); @@ -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 @@ -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"); }; @@ -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 - - @@ -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; @@ -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 ((j<ib->max_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; + (i<sz)&&(j<ib->max_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 <stdlib.h> +#include <stdio.h> +#include <unistd.h> +#include <netdb.h> +#include <string.h> + +#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 @@ -38,7 +38,7 @@ int tbl_exec_add(tbl_exec *tbl, tble_exec *cmd) if (tbl->size+1<tbl->max_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;i<tbl->max_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+1<tbl->max_size) { tbl->size += 1; - } else + } else { return -1; } @@ -181,7 +181,7 @@ int tbl_qcmd_chk(tbl_qcmd *tbl) for (i=0;i<tbl->max_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)) @@ -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 |