diff options
author | FreeArtMan <dos21h@gmail.com> | 2017-05-04 22:21:39 +0100 |
---|---|---|
committer | FreeArtMan <dos21h@gmail.com> | 2017-05-04 22:21:39 +0100 |
commit | 417edbce2abde566e94d8986b487293d55e173c0 (patch) | |
tree | 133db23e0c58704f06447a77e8a1e964bf98be2c | |
parent | 0a24537719de7cab1b4df46e7d6a22564f66ec6a (diff) | |
download | agni-417edbce2abde566e94d8986b487293d55e173c0.tar.gz agni-417edbce2abde566e94d8986b487293d55e173c0.zip |
Added new commands CMD,SHA1. Added support of params for commands. Fixed some bugs with uninitialised buffers
-rw-r--r-- | Makefile | 5 | ||||
-rw-r--r-- | agni.c | 36 | ||||
-rw-r--r-- | cmd_cmd.c | 20 | ||||
-rw-r--r-- | cmd_cmd.h | 14 | ||||
-rw-r--r-- | cmd_sha1.c | 41 | ||||
-rw-r--r-- | cmd_sha1.h | 12 | ||||
-rw-r--r-- | config_cmds.h | 4 | ||||
-rw-r--r-- | mq_cmd.c | 4 | ||||
-rw-r--r-- | sha1.c | 296 | ||||
-rw-r--r-- | sha1.h | 44 | ||||
-rw-r--r-- | sock_conn.c | 20 | ||||
-rw-r--r-- | sock_conn.h | 2 | ||||
-rw-r--r-- | tbl_qcmd.c | 57 | ||||
-rw-r--r-- | tbl_qcmd.h | 2 |
14 files changed, 524 insertions, 33 deletions
@@ -19,9 +19,12 @@ make: $(CC) $(CFLAGS) cmd_ping.c -c $(CC) $(CFLAGS) cmd_loadavg.c -c $(CC) $(CFLAGS) cmd_rusage.c -c + $(CC) $(CFLAGS) cmd_cmd.c -c + $(CC) $(CFLAGS) sha1.c -c + $(CC) $(CFLAGS) cmd_sha1.c -c $(CC) $(CFLAGS) tbl_qcmd.o mq_cmd.o mq_ntf.o buf.o mmm.o darray.o util.o sock_conn.o \ irc_parse.o mmm.c cmd_date.o cmd_uptime.o cmd_version.o cmd_ping.o cmd_loadavg.o \ - cmd_rusage.c agni.c \ + cmd_rusage.c cmd_cmd.o sha1.o cmd_sha1.o agni.c \ -o $(PROJECT) -std=c11 -lrt leak: @@ -278,7 +278,8 @@ int th_start_client(void *data) //PRINT("EVENT 0\n"); break; case 1: - PRINT("MQ_EVENT IN\n"); + //PRINT("MQ_EVENT IN\n"); + //memset(in_buf, 0, in_attr.mq_msgsize); if ((byte_num = mq_ntf_read(mq, MQ_IN, in_buf, in_attr.mq_msgsize)) == -1) { printf("Cant read input message \n"); @@ -297,7 +298,6 @@ int th_start_client(void *data) //recieve commands from queue if (mq_event == 1) { - //PNL(); recv_cmd = mq_cmd_creates(in_buf, in_attr.mq_msgsize, -1); if (recv_cmd != NULL) { @@ -307,7 +307,7 @@ int th_start_client(void *data) break; } else if (mq_cmd_o_cmp_cmd(recv_cmd,"PRIVMSG:") == 0) { - PRINT("Response\n") + //PRINT("Response\n") char *paramPtr; size_t paramSz; mq_cmd_param(recv_cmd, ¶mPtr, ¶mSz); @@ -331,7 +331,7 @@ int th_start_client(void *data) if (cmd != NULL) { - PRINT("Found ID in qcmd table %d\n", cmd_id); + //PRINT("Found ID in qcmd table %d\n", cmd_id); char *resp_user = token_new_str(cmd->tok,0); char *dest_user = token_new_str(cmd->tok,2); char *sep = strchr(resp_user,'!'); @@ -364,9 +364,6 @@ int th_start_client(void *data) //for now just remove command without checkings its state tbl_qcmd_del_by_id(qtbl, cmd_id); //no error check - //NOT BEST PLACE WHERE TO CALL THIS FUNCTION - //should be added proper command state check and so on - tbl_qcmd_mng_states(qtbl); // This should be runed regulary } } @@ -385,6 +382,9 @@ int th_start_client(void *data) recv_cmd = NULL; } + + //should be added proper command state check and so on + tbl_qcmd_mng_states(qtbl); // This should be runed regulary //recieve irc commands and pass to MQ, save command to table @@ -430,7 +430,7 @@ int th_start_client(void *data) int fret,fret2; //char send_back[128]; - PNL(); + //PNL(); memset(cmd_buf,0,128); char *uname = token_new_str(itok,0); @@ -444,11 +444,11 @@ int th_start_client(void *data) //if (token_cmp(itok,3,":join") == 1) if (strncmp(":join",msg,5)==0) { - PNL(); + //PNL(); //lets check if one more argument is there { - PNL(); + //PNL(); char *sep_chan = strchr(msg,' '); //lets join channel @@ -487,10 +487,12 @@ int th_start_client(void *data) } //create command and send to mq + PRINT("MSG %s\n",msg); mq_cmd *privcmd = mq_cmd_create(qcmd->cid,"PRIVMSG",strlen("PRIVMSG"),msg,strlen(msg)); char *bufPtr; bufPtr = mq_cmd_buf(privcmd); - mq_ntf_write(mq, MQ_OUT, bufPtr, strlen(bufPtr)); + PRINT("%s\n",bufPtr); + PRINT("write %d bytes\n",mq_ntf_write(mq, MQ_OUT, bufPtr, strlen(bufPtr))); mq_cmd_free(privcmd); //for safety make null, as no one should use it anymore @@ -638,6 +640,7 @@ int th_event_manager(void *data) break; case 1: PRINT("MQ_EVENT OUT\n"); + memset(out_buf, 0, out_attr.mq_msgsize); if (mq_ntf_read(mq, MQ_OUT, out_buf, out_attr.mq_msgsize) == -1) { printf("Cant read output message\n"); @@ -654,8 +657,6 @@ int th_event_manager(void *data) //if QUIT then quit the thread if (mq_event == 1) { - PNL(); - recv_cmd = mq_cmd_creates(out_buf, out_attr.mq_msgsize, 1); //PNL(); if (recv_cmd != NULL) @@ -664,14 +665,8 @@ int th_event_manager(void *data) char *recv_cmd_param=NULL; size_t recv_cmd_param_sz=-1; - PNL(); - PRINT("MQ:CMD %s\n", mq_cmd_buf(recv_cmd)); - - //get command fields mq_cmd_param(recv_cmd, &recv_cmd_param, &recv_cmd_param_sz); - - if (mq_cmd_o_cmp_cmd(recv_cmd,"QUIT") == 0) { @@ -689,16 +684,19 @@ int th_event_manager(void *data) } else if (mq_cmd_o_cmp_cmd(recv_cmd,"PRIVMSG") == 0) { //check if command callback excists + PRINT("recv_cmd_param %s\n", recv_cmd_param); if (recv_cmd_param!=NULL) { if (tbl_exec_in_s(etbl, recv_cmd_param+1)>=0) //there is ':' in front of command, check if ptr not null otherwise die { void *ret_msg = NULL; + PRINT("CMD [%s]\n",recv_cmd_param); if (tbl_exec_run(etbl, recv_cmd_param+1, &ret_msg)>=0) { if (ret_msg!=NULL) { + //prepare response message and send it it back mq_cmd *privcmd = mq_cmd_create( mq_cmd_id(recv_cmd), "PRIVMSG", diff --git a/cmd_cmd.c b/cmd_cmd.c new file mode 100644 index 0000000..d5983cf --- /dev/null +++ b/cmd_cmd.c @@ -0,0 +1,20 @@ +#include "cmd_cmd.h" + +void *cmd_cmd(void *data) +{ + char *ret = NULL; + int fret=-1; + + const int buf_size = 128; + char buf[buf_size+1]; + + printf("CMD\n"); + + snprintf(buf, buf_size, "%s", data); + + + + ret = alloc_new_str(buf); + + return ret; +} diff --git a/cmd_cmd.h b/cmd_cmd.h new file mode 100644 index 0000000..2670abd --- /dev/null +++ b/cmd_cmd.h @@ -0,0 +1,14 @@ +#ifndef __CMD_CMD_H +#define __CMD_CMD_H + +#include <stdlib.h> +#include <stdio.h> + +#include <sys/time.h> +#include <sys/resource.h> + +#include "util.h" + +void *cmd_cmd(void *data); + +#endif
\ No newline at end of file diff --git a/cmd_sha1.c b/cmd_sha1.c new file mode 100644 index 0000000..08b2527 --- /dev/null +++ b/cmd_sha1.c @@ -0,0 +1,41 @@ +#include "cmd_sha1.h" + +void *cmd_sha1(void *data) +{ + char *param = (char *)data; + char *ret = NULL; + int fret=-1; + + + int i; + char hash_result[21]; + char hex_result[41]; + + const int buf_size = 128; + char buf[buf_size+1]; + + printf("SHA1\n"); + + + if (param == NULL) + { + ret = alloc_new_str("No params mate\n"); + return ret; + } + + SHA1(hash_result, param, strlen(param)); + + //cool way how to convert array to string + for (i=0;i<20;i++) + { + sprintf(hex_result+(2*i), "%02x", hash_result[i]&0xff); + } + hex_result[40]=0x0; + + + snprintf(buf, buf_size, "SHA1 %s\n",hex_result); + ret = alloc_new_str(buf); + + + return ret; +}
\ No newline at end of file diff --git a/cmd_sha1.h b/cmd_sha1.h new file mode 100644 index 0000000..69a7bc8 --- /dev/null +++ b/cmd_sha1.h @@ -0,0 +1,12 @@ +#ifndef __CMD_SHA1_H +#define __CMD_SHA1_H + +#include <stdlib.h> +#include <stdio.h> + +#include "util.h" +#include "sha1.h" + +void *cmd_sha1(void *data); + +#endif
\ No newline at end of file diff --git a/config_cmds.h b/config_cmds.h index 125708b..8488cc0 100644 --- a/config_cmds.h +++ b/config_cmds.h @@ -15,6 +15,8 @@ typedef struct single_cmd_def #include "cmd_ping.h" #include "cmd_loadavg.h" #include "cmd_rusage.h" +#include "cmd_cmd.h" +#include "cmd_sha1.h" single_cmd_def confgi_cmd_list[] = { @@ -24,6 +26,8 @@ single_cmd_def confgi_cmd_list[] = {"PING",cmd_ping}, {"LOADAVG",cmd_loadavg}, {"RUSE",cmd_rusage}, + {"CMD",cmd_cmd}, + {"SHA1",cmd_sha1}, {NULL,NULL} }; @@ -198,7 +198,9 @@ int mq_cmd_param(mq_cmd *cmd, char **param, size_t *sz) } strt = i; *param = cmd->buf+strt; - *sz = cmd->sz - strt+1;//BUG MUAHAHA + + //buffer allways ends with zero, no worries + *sz = strlen(*param); return 0; } @@ -0,0 +1,296 @@ +/* +SHA-1 in C +By Steve Reid <steve@edmweb.com> +100% Public Domain + +Test Vectors (from FIPS PUB 180-1) +"abc" + A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D +"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" + 84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1 +A million repetitions of "a" + 34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F +*/ + +/* #define LITTLE_ENDIAN * This should be #define'd already, if true. */ +/* #define SHA1HANDSOFF * Copies data before messing with it. */ + +#define SHA1HANDSOFF + +#include <stdio.h> +#include <string.h> + +/* for uint32_t */ +#include <stdint.h> + +#include "sha1.h" + + +#define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits)))) + +/* blk0() and blk() perform the initial expand. */ +/* I got the idea of expanding during the round function from SSLeay */ +#if BYTE_ORDER == LITTLE_ENDIAN +#define blk0(i) (block->l[i] = (rol(block->l[i],24)&0xFF00FF00) \ + |(rol(block->l[i],8)&0x00FF00FF)) +#elif BYTE_ORDER == BIG_ENDIAN +#define blk0(i) block->l[i] +#else +#error "Endianness not defined!" +#endif +#define blk(i) (block->l[i&15] = rol(block->l[(i+13)&15]^block->l[(i+8)&15] \ + ^block->l[(i+2)&15]^block->l[i&15],1)) + +/* (R0+R1), R2, R3, R4 are the different operations used in SHA1 */ +#define R0(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk0(i)+0x5A827999+rol(v,5);w=rol(w,30); +#define R1(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk(i)+0x5A827999+rol(v,5);w=rol(w,30); +#define R2(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0x6ED9EBA1+rol(v,5);w=rol(w,30); +#define R3(v,w,x,y,z,i) z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=rol(w,30); +#define R4(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=rol(w,30); + + +/* Hash a single 512-bit block. This is the core of the algorithm. */ + +void SHA1Transform( + uint32_t state[5], + const unsigned char buffer[64] +) +{ + uint32_t a, b, c, d, e; + + typedef union + { + unsigned char c[64]; + uint32_t l[16]; + } CHAR64LONG16; + +#ifdef SHA1HANDSOFF + CHAR64LONG16 block[1]; /* use array to appear as a pointer */ + + memcpy(block, buffer, 64); +#else + /* The following had better never be used because it causes the + * pointer-to-const buffer to be cast into a pointer to non-const. + * And the result is written through. I threw a "const" in, hoping + * this will cause a diagnostic. + */ + CHAR64LONG16 *block = (const CHAR64LONG16 *) buffer; +#endif + /* Copy context->state[] to working vars */ + a = state[0]; + b = state[1]; + c = state[2]; + d = state[3]; + e = state[4]; + /* 4 rounds of 20 operations each. Loop unrolled. */ + R0(a, b, c, d, e, 0); + R0(e, a, b, c, d, 1); + R0(d, e, a, b, c, 2); + R0(c, d, e, a, b, 3); + R0(b, c, d, e, a, 4); + R0(a, b, c, d, e, 5); + R0(e, a, b, c, d, 6); + R0(d, e, a, b, c, 7); + R0(c, d, e, a, b, 8); + R0(b, c, d, e, a, 9); + R0(a, b, c, d, e, 10); + R0(e, a, b, c, d, 11); + R0(d, e, a, b, c, 12); + R0(c, d, e, a, b, 13); + R0(b, c, d, e, a, 14); + R0(a, b, c, d, e, 15); + R1(e, a, b, c, d, 16); + R1(d, e, a, b, c, 17); + R1(c, d, e, a, b, 18); + R1(b, c, d, e, a, 19); + R2(a, b, c, d, e, 20); + R2(e, a, b, c, d, 21); + R2(d, e, a, b, c, 22); + R2(c, d, e, a, b, 23); + R2(b, c, d, e, a, 24); + R2(a, b, c, d, e, 25); + R2(e, a, b, c, d, 26); + R2(d, e, a, b, c, 27); + R2(c, d, e, a, b, 28); + R2(b, c, d, e, a, 29); + R2(a, b, c, d, e, 30); + R2(e, a, b, c, d, 31); + R2(d, e, a, b, c, 32); + R2(c, d, e, a, b, 33); + R2(b, c, d, e, a, 34); + R2(a, b, c, d, e, 35); + R2(e, a, b, c, d, 36); + R2(d, e, a, b, c, 37); + R2(c, d, e, a, b, 38); + R2(b, c, d, e, a, 39); + R3(a, b, c, d, e, 40); + R3(e, a, b, c, d, 41); + R3(d, e, a, b, c, 42); + R3(c, d, e, a, b, 43); + R3(b, c, d, e, a, 44); + R3(a, b, c, d, e, 45); + R3(e, a, b, c, d, 46); + R3(d, e, a, b, c, 47); + R3(c, d, e, a, b, 48); + R3(b, c, d, e, a, 49); + R3(a, b, c, d, e, 50); + R3(e, a, b, c, d, 51); + R3(d, e, a, b, c, 52); + R3(c, d, e, a, b, 53); + R3(b, c, d, e, a, 54); + R3(a, b, c, d, e, 55); + R3(e, a, b, c, d, 56); + R3(d, e, a, b, c, 57); + R3(c, d, e, a, b, 58); + R3(b, c, d, e, a, 59); + R4(a, b, c, d, e, 60); + R4(e, a, b, c, d, 61); + R4(d, e, a, b, c, 62); + R4(c, d, e, a, b, 63); + R4(b, c, d, e, a, 64); + R4(a, b, c, d, e, 65); + R4(e, a, b, c, d, 66); + R4(d, e, a, b, c, 67); + R4(c, d, e, a, b, 68); + R4(b, c, d, e, a, 69); + R4(a, b, c, d, e, 70); + R4(e, a, b, c, d, 71); + R4(d, e, a, b, c, 72); + R4(c, d, e, a, b, 73); + R4(b, c, d, e, a, 74); + R4(a, b, c, d, e, 75); + R4(e, a, b, c, d, 76); + R4(d, e, a, b, c, 77); + R4(c, d, e, a, b, 78); + R4(b, c, d, e, a, 79); + /* Add the working vars back into context.state[] */ + state[0] += a; + state[1] += b; + state[2] += c; + state[3] += d; + state[4] += e; + /* Wipe variables */ + a = b = c = d = e = 0; +#ifdef SHA1HANDSOFF + memset(block, '\0', sizeof(block)); +#endif +} + + +/* SHA1Init - Initialize new context */ + +void SHA1Init( + SHA1_CTX * context +) +{ + /* SHA1 initialization constants */ + context->state[0] = 0x67452301; + context->state[1] = 0xEFCDAB89; + context->state[2] = 0x98BADCFE; + context->state[3] = 0x10325476; + context->state[4] = 0xC3D2E1F0; + context->count[0] = context->count[1] = 0; +} + + +/* Run your data through this. */ + +void SHA1Update( + SHA1_CTX * context, + const unsigned char *data, + uint32_t len +) +{ + uint32_t i; + + uint32_t j; + + j = context->count[0]; + if ((context->count[0] += len << 3) < j) + context->count[1]++; + context->count[1] += (len >> 29); + j = (j >> 3) & 63; + if ((j + len) > 63) + { + memcpy(&context->buffer[j], data, (i = 64 - j)); + SHA1Transform(context->state, context->buffer); + for (; i + 63 < len; i += 64) + { + SHA1Transform(context->state, &data[i]); + } + j = 0; + } + else + i = 0; + memcpy(&context->buffer[j], &data[i], len - i); +} + + +/* Add padding and return the message digest. */ + +void SHA1Final( + unsigned char digest[20], + SHA1_CTX * context +) +{ + unsigned i; + + unsigned char finalcount[8]; + + unsigned char c; + +#if 0 /* untested "improvement" by DHR */ + /* Convert context->count to a sequence of bytes + * in finalcount. Second element first, but + * big-endian order within element. + * But we do it all backwards. + */ + unsigned char *fcp = &finalcount[8]; + + for (i = 0; i < 2; i++) + { + uint32_t t = context->count[i]; + + int j; + + for (j = 0; j < 4; t >>= 8, j++) + *--fcp = (unsigned char) t} +#else + for (i = 0; i < 8; i++) + { + finalcount[i] = (unsigned char) ((context->count[(i >= 4 ? 0 : 1)] >> ((3 - (i & 3)) * 8)) & 255); /* Endian independent */ + } +#endif + c = 0200; + SHA1Update(context, &c, 1); + while ((context->count[0] & 504) != 448) + { + c = 0000; + SHA1Update(context, &c, 1); + } + SHA1Update(context, finalcount, 8); /* Should cause a SHA1Transform() */ + for (i = 0; i < 20; i++) + { + digest[i] = (unsigned char) + ((context->state[i >> 2] >> ((3 - (i & 3)) * 8)) & 255); + } + /* Wipe variables */ + memset(context, '\0', sizeof(*context)); + memset(&finalcount, '\0', sizeof(finalcount)); +} + +void SHA1( + char *hash_out, + const char *str, + int len) +{ + SHA1_CTX ctx; + unsigned int ii; + + SHA1Init(&ctx); + for (ii=0; ii<len; ii+=1) + SHA1Update(&ctx, (const unsigned char*)str + ii, 1); + SHA1Final((unsigned char *)hash_out, &ctx); + hash_out[20] = '\0'; +} + @@ -0,0 +1,44 @@ +#ifndef SHA1_H +#define SHA1_H + +/* + SHA-1 in C + By Steve Reid <steve@edmweb.com> + 100% Public Domain + */ + +#include "stdint.h" + +typedef struct +{ + uint32_t state[5]; + uint32_t count[2]; + unsigned char buffer[64]; +} SHA1_CTX; + +void SHA1Transform( + uint32_t state[5], + const unsigned char buffer[64] + ); + +void SHA1Init( + SHA1_CTX * context + ); + +void SHA1Update( + SHA1_CTX * context, + const unsigned char *data, + uint32_t len + ); + +void SHA1Final( + unsigned char digest[20], + SHA1_CTX * context + ); + +void SHA1( + char *hash_out, + const char *str, + int len); + +#endif /* SHA1_H */ diff --git a/sock_conn.c b/sock_conn.c index 37c8892..42cb3a6 100644 --- a/sock_conn.c +++ b/sock_conn.c @@ -81,10 +81,18 @@ int __irc_buf_drain_io(irc_buf *ib) return 0; } + //BUG + //here is hope that server will send 512/1024 bytes that will be terminated with "\r\n" + //but this could be false hope if someone gona abuse it, buffer will fill up and will + //not be able to give back new lines + //FIX + //just add more logic pal to handle this "special" case + + //lets from in buffer put as much as possible to out buffer i = ib->in_size-1; j = ib->out_size; - //PRINT("in_size=%d out_size=%d\n",ib->in_size, ib->out_size); + PRINT("in_size=%d out_size=%d\n",ib->in_size, ib->out_size); cnt = 0; while ((j<ib->max_out_size)&&(i>=0)) //buffers ar full or empty { @@ -93,7 +101,7 @@ int __irc_buf_drain_io(irc_buf *ib) { if (((ib->buf_out[j-3]=='\r')&&(ib->buf_out[j-2]=='\n')))//bufferunderflow { - //PRINT("READY\n"); + PRINT("READY\n"); ib->ready = 1; //PNL(); break; @@ -105,20 +113,20 @@ int __irc_buf_drain_io(irc_buf *ib) cnt++; } //update - //PRINT("Copied %d bytes\n", cnt); + PRINT("Copied %d bytes\n", cnt); ib->in_size = i+1; //i can end up as -1 ib->out_size = j; // - //PRINT("in_size=%d out_size=%d\n",ib->in_size, ib->out_size); + PRINT("in_size=%d out_size=%d\n",ib->in_size, ib->out_size); //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')) { - //PRINT("READY\n"); + PRINT("READY\n"); ib->ready = 1; return cnt; } - //PRINT("%d\n",cnt); + PRINT("%d\n",cnt); return cnt; } diff --git a/sock_conn.h b/sock_conn.h index 966a7f4..55ef8e0 100644 --- a/sock_conn.h +++ b/sock_conn.h @@ -12,7 +12,7 @@ #include "debug.h" #define IRC_BUF_IN_SIZE 1024*64 -#define IRC_BUF_OUT_SIZE 512 +#define IRC_BUF_OUT_SIZE 1024 /* Reads ins and spils out IRC lines ended with \r\n. @@ -30,6 +30,8 @@ tbl_exec *tbl_exec_list_c(int size) //TODO check if command allready excists int tbl_exec_add(tbl_exec *tbl, tble_exec *cmd) { + int i; + if (tbl == NULL || cmd == NULL) { return -1; @@ -61,9 +63,19 @@ int tbl_exec_in_s(tbl_exec *tbl, char *cmd) for (i=0; i<tbl->size; i++) { el = tbl->cmd[i]; + //check first part of the string, and then check that after command there is whitespace if (strncmp(el->cmd, cmd, strlen(el->cmd))==0) { - return i; + if (strlen(cmd)>strlen(el->cmd)) + { + if (cmd[strlen(el->cmd)]==' ') + { + return i; + } + } if (strlen(cmd)==strlen(el->cmd)) + { + return i; + } } } @@ -73,6 +85,8 @@ int tbl_exec_in_s(tbl_exec *tbl, char *cmd) int tbl_exec_run(tbl_exec *tbl, char *cmd, void **resp) { int ret=-1; + void *clbk_data=NULL; + char *char_iter=NULL; int i; tble_exec *el=NULL; @@ -86,13 +100,43 @@ int tbl_exec_run(tbl_exec *tbl, char *cmd, void **resp) for (i=0;i<tbl->size;i++) { el = tbl->cmd[i]; + PRINT("CALLBACK CMD %s\n",cmd); if (strncmp(el->cmd, cmd, strlen(el->cmd))==0) { void *ret_resp=NULL; //execute callback - PRINT("EXEC CLB ID %d\n",i); - ret_resp = el->callback(NULL); //no params for now + //PRINT("EXEC CLB ID %d\n",i); + + //little hack need proper messaging system + //for now pass pointer with data + if (strlen(cmd)>strlen(el->cmd)) + { + if (cmd[strlen(el->cmd)]==' ') + { + char_iter = cmd; + while ((*char_iter!=' ')&&(*char_iter!=0x00)) + { + char_iter++; + } + + while ((*char_iter==' ')&&(*char_iter!=0x00)) + { + char_iter++; + } + + clbk_data = char_iter; + if (strlen(char_iter)==0) + { + clbk_data = NULL; + } + + } + } + + PRINT("CALLBACK PARAMS %s\n", clbk_data); + + ret_resp = el->callback(clbk_data); //no params for now //return if (ret_resp != NULL) @@ -425,23 +469,28 @@ int tbl_qcmd_del_by_id(tbl_qcmd *tbl, int id) if (iter->id == id) { + //PRINT("Found id\n"); // if its just last element reduce size and no other logic if (i == tbl->size-1) { + //PRINT("Remove last element\n"); tbl_qcmd_destroy_cmd(tbl->cmd[i]); + tbl->cmd[i] = NULL; tbl->size -= 1; return 1; } else //if element not last one { replace_cmd = tbl->cmd[tbl->size-1]; + tbl->cmd[tbl->size-1] = NULL; tbl->size -= 1; //free before replace remove_cmd = iter; tbl_qcmd_destroy_cmd(remove_cmd); - iter = replace_cmd; + tbl->cmd[i] = replace_cmd; + return 1; //yea yea i know } } @@ -45,7 +45,7 @@ typedef struct tbl_exec tble_exec **cmd; } tbl_exec; -#define QCMD_DEFAULT_TIMEOUT 3 //default amount of seconds to wait replay +#define QCMD_DEFAULT_TIMEOUT 10 //default amount of seconds to wait replay #define QCMD_NONE 0 //nothing happening with this command #define QCMD_TIMEOUT 1 //command running to long without result |