From 913db4648dabfce65e58a128a35c356ac0042ae9 Mon Sep 17 00:00:00 2001 From: FreeArtMan Date: Sat, 25 Feb 2017 14:00:08 +0000 Subject: joined up socket connection code to main server thread. hardcoded echo/replay command is working --- agni.c | 153 ++++++++++++++++++++++++++++++++++++++++++++++++++++--- config_servers.h | 8 +-- sock_conn.c | 38 +++++++++++++- 3 files changed, 187 insertions(+), 12 deletions(-) diff --git a/agni.c b/agni.c index a2f14a4..431e746 100644 --- a/agni.c +++ b/agni.c @@ -44,6 +44,7 @@ int uniq_id() int ret=-1,id; //what possible could go wrong? + // sry emilsp this is not important for now id = atomic_load(&_glbl_id); ret = id; id += 1; @@ -546,7 +547,7 @@ int recv_mq_cmd(mq_ntf_mdt *mq, |--------|--OUT-->|---------| */ -#define STACK_SIZE (1024*16) +#define STACK_SIZE (1024*1024) /* not supposed to be changed. */ typedef struct server_cfg { @@ -559,9 +560,9 @@ typedef struct server_cfg /* irc server config */ char *user; char *password; - char *server; + char *server; //should be changed to hostname? char **channels; - int port; + char *port; int ssl; } server_cfg; @@ -576,6 +577,7 @@ typedef struct server_cfg #define TH_STATE_TERMINATION 6 #define TH_STATE_EXIT 7 +#define TH_CONN_BUF_SZ 1024 int th_start_client(void *data) { int cmd_id = 1; @@ -593,6 +595,18 @@ int th_start_client(void *data) char *out_buf = NULL; char *in_buf = NULL; + //network creation var + int cret = -1; + int conn=-1; + char conn_buf[TH_CONN_BUF_SZ]; + char cmd_buf[TH_CONN_BUF_SZ]; + + //irc parsting + irc_buf *ib = NULL; + irc_token *itok = NULL; + char *irc_line = NULL; + + atomic_fetch_add(&cfg->running,1); printf("Start client\n"); printf("Server %d\n",cfg->tid); @@ -619,35 +633,79 @@ int th_start_client(void *data) } in_buf = malloc(in_attr.mq_msgsize); + PNL(); + //create irc parsing structures + memset(conn_buf, 0, TH_CONN_BUF_SZ); + memset(cmd_buf, 0, TH_CONN_BUF_SZ); + PNL(); + ib = irc_buf_create(); + PNL(); + if (ib == NULL) + { + PNL(); + PERM(); + } + PNL(); + PRINT("SERVER:%s PORT:%s USER:%s\n", cfg->server, cfg->port, cfg->user); + //conn = irc_connect(cfg->server, cfg->port); + conn = irc_connect("irc.freenode.net", cfg->port); + PNL(); + if (conn < 0) + { + PNL(); + printf("cant connect to server\n"); + //well we dont whant to just exit from thread + //lets put inside main thread CONNection checker + //if no connection just send some commmand and create + //some logic how to deal with it + atomic_fetch_sub(&cfg->running,1); + return 0; + } + PNL(); + //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); + write(conn, cmd_buf, strlen(cmd_buf)); + snprintf(cmd_buf, TH_CONN_BUF_SZ,"NICK %s \r\n", cfg->user); + write(conn, cmd_buf, strlen(cmd_buf)); + //send command wait for response - + printf("Start loop\n"); run = 1; while (run) { + //Collect events from MQ printf("Client loop tick\n"); mq_event = mq_ntf_select(mq, MQ_IN); switch(mq_event) { - case 0: + case 1: PRINT("EVENT 0\n"); break; - case 1: + case 0: PRINT("EVENT 1\n"); + PNL(); if (mq_ntf_read(mq, MQ_IN, in_buf, in_attr.mq_msgsize) == -1) { + PNL(); printf("Cant read input message \n"); } else { + PNL(); in_buf[in_attr.mq_msgsize-1] = 0x0; printf("Recieve %s\n", in_buf); } + PNL(); break; default: printf("Unknown event type\n"); } + PNL(); sleep(1); + PNL(); + + //fast code to exit if QUIT command is recieved if (mq_event == 1) { PNL(); @@ -661,6 +719,83 @@ int th_start_client(void *data) } } } + PNL(); + //recieve commands from queue + + //recieve irc commands and pass to MQ + //fix this by allowing drain just chunks, not whole buffer + PNL(); + if((cret = read(conn,conn_buf, 128))>0) + { + printf("IN>>%s",conn_buf); + irc_buf_puts(ib, conn_buf, cret); + if (irc_buf_ready(ib) == 1) + { + PNL(); + irc_line = irc_buf_line(ib); + if (irc_line) + { + printf("PARSE>>%s\n",irc_line); + if (memcmp(irc_line,"PING",4)==0) + { + write(conn,"PONG",strlen("PONG")); + printf("OUT>>PONG\n"); + } + + + PNL(); + itok = irc_parse(irc_line,strlen(irc_line)); + PNL(); + if (itok!=NULL) + { + int j; + for (j=0;jtk_list[j]->token); + } + + if (token_cmp(itok,1,"NOTICE")==1) + { + int fret; + fret = write(conn,"NOTICE",strlen("NOTICE")); + printf("OUT<user,uname,msg); + printf("FORMATED [%s]",send_back); + fret = write(conn,send_back,fret2); + + free(msg); + free(uname); + printf("OUT<tid = i; srvc->stack = malloc(STACK_SIZE); //NULL will brake everything ;) + if (srvc->stack == NULL) + { + ERROR("BLow"); + } srvc->user = isrvc->user; srvc->password = isrvc->password; srvc->server = isrvc->server; diff --git a/config_servers.h b/config_servers.h index 9bf4b9e..5bd315e 100644 --- a/config_servers.h +++ b/config_servers.h @@ -11,18 +11,18 @@ typedef struct irc_server_conf char *password; char *server; char **channels; - int port; + char *port; int ssl; } irc_server_conf; static irc_server_conf server_list[] = { { - .user = "agni", - .password = NULL, + .user = "cbot_git", + .password = "asdsada", .server = "irc.freenode.net", .channels = {"#mainlv",NULL}, - .port = 6667, + .port = "6667", .ssl = 0, }/*, { diff --git a/sock_conn.c b/sock_conn.c index a57ca7b..b15ce47 100644 --- a/sock_conn.c +++ b/sock_conn.c @@ -27,11 +27,23 @@ irc_buf* irc_buf_create() ret->out_size = 0; ret->max_out_size = IRC_BUF_OUT_SIZE; ret->buf_out = malloc(ret->max_out_size); + if (ret->buf_out == NULL) + { + free(ret); + PERM(); + return NULL; + } 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); + if (ret->buf_in == NULL) + { + free(ret->buf_out); + free(ret); + return NULL; + } memset(ret->buf_in, 0, ret->max_in_size); ret->ready = 0; @@ -228,15 +240,37 @@ connect to hostname:port return fd>0 if connection succesful int irc_connect( char *hostname, char *port ) { int fd=0; + int fret=-1; struct addrinfo serv, *res; + if (hostname == NULL) + { + PERM(); + return -1; + } + + if (port == NULL) + { + PERM(); + return -1; + } + memset(&serv, 0, sizeof(serv)); serv.ai_family = AF_INET; serv.ai_socktype = SOCK_STREAM; - getaddrinfo(hostname, port, &serv, &res); + if (0 != getaddrinfo(hostname, port, &serv, &res)) + { + PERM(); + return -1; + } fd = socket(res->ai_family, res->ai_socktype, res->ai_protocol); - connect(fd, res->ai_addr, res->ai_addrlen); + fret = connect(fd, res->ai_addr, res->ai_addrlen); + if (fret!=0) + { + PERM(); + return -1; + } //? //freeaddrinfo(&serv); -- cgit v1.2.3