X-Git-Url: https://hackdaworld.org/gitweb/?p=my-code%2Fivac.git;a=blobdiff_plain;f=src%2Fivac.c;h=e3e206013aee7ba74ef556cb7ec53bbf1df44418;hp=9645c33f59444e0e74b66b9b85814c61e657cf9f;hb=44d1396bcdd42a518855981fc8aa0b0fbc665231;hpb=5f7a9e08cad9caa3aa593eba18e6a22f763ecc37 diff --git a/src/ivac.c b/src/ivac.c index 9645c33..e3e2060 100644 --- a/src/ivac.c +++ b/src/ivac.c @@ -22,9 +22,25 @@ */ // #define USE_NCURSES +/* dont care about ncurses .. go for gtk(2)! */ +#define USE_GTK #include "ivac.h" +int usage(void) { + + puts(""); + puts("usage: ivac "); + puts(""); + puts("options:"); + puts("-h \t\t show this help"); + puts("-n \t specify your name"); + puts("-p \t specify port to listen for incoming connections"); + puts(""); + + return SUCCESS; +} + int main(int argc,char **argv) { /* TESTING BY NOW */ @@ -32,13 +48,31 @@ int main(int argc,char **argv) { t_ivac ivac; int i; - if(argc!=2) { - printf("\nusage: %s \n\n",argv[0]); - return ERROR; + /* default values */ + strcpy(ivac.username,"ivac"); + ivac.net.l_port=IVAC_LISTEN_PORT; + + /* parse argv and change default values */ + for(i=1;inet)); input_shutdown(&(ivac->input)); event_stop(&(ivac->event)); - // display_shutdown(&(ivac->display)); +#ifdef USE_NCURSES + display_shutdown(&(ivac->display)); +#endif return SUCCESS; } @@ -129,9 +163,26 @@ int ivac_send_info(int channel,t_ivac *ivac) { return SUCCESS; } +int ivac_send_quit(int channel,t_ivac *ivac) { + + char data[7]; /* one more for \0 */ + + data[0]=IVAC_SEND_QUIT; + data[1]=4; + strcpy(data+2,"quit"); + + if(network_send(ivac->net.connection[channel].fd,data,6)==N_ERROR) { + puts("[ivac] ivac_send_quit failed"); + return ERROR; + } + + return SUCCESS; +} + int ivac_receive_info(int channel,t_ivac *ivac) { char data[SEND_N_MAX]; + char c_str[IVAC_CONSOLE_STRING_LEN]; int count,length; count=0; @@ -158,44 +209,75 @@ int ivac_receive_info(int channel,t_ivac *ivac) { ivac->challenger[channel].av_cap|=data[count+3]; count+=4; break; + case IVAC_SEND_QUIT: + if(!(strncmp(data+count+2,"quit",data[1]))) + sprintf(c_str,"channel %02d: connection closed by remote host", + channel); + event_math(ivac->net.connection[channel].fd,&(ivac->event), + READ,REMOVE); + network_close(&(ivac->net),channel); + memset(&(ivac->challenger[channel]),0,sizeof(t_challenger)); + ivac_add_to_monitor(ivac,c_str); + count+=6; + break; default: - puts("[ivac] ivac_receive_info, unknown character"); + sprintf(c_str,"ivac_receive_info, unknown character: 0x%02x\n", + data[count]); + ivac_add_to_monitor(ivac,c_str); return ERROR; break; } } - return SUCCESS; + return length; } int ivac_event_cb(t_event *event,void *ptr) { t_ivac *ivac; int channel; + char c_str[IVAC_CONSOLE_STRING_LEN]; ivac=(t_ivac *)ptr; + /* incoming connection -- first contact => send info */ if(FD_ISSET(ivac->net.l_fd,&(event->rfds))) { - /* manage incoming + send info */ - if((channel=network_manage_incoming(&(ivac->net)))!=N_ERROR) { + channel=network_manage_incoming(&(ivac->net)); + if(channel==N_E_ACCEPT) + sprintf(c_str,"accept failed"); + else if(channel==N_E_MAXC) + sprintf(c_str,"maximum connections reached"); + else { + sprintf(c_str,"connection from %s port %d on channel %d", + ivac->net.connection[channel].ip, + ivac->net.connection[channel].port,channel); + ivac_add_to_monitor(ivac,c_str); event_math(ivac->net.connection[channel].fd,event,READ,ADD); ivac_send_info(channel,ivac); } + ivac_display_content(ivac); } - /* receive info */ - for(channel=0;channelnet.connection[channel].status&C_ESTABL) - if(FD_ISSET(ivac->net.connection[channel].fd,&(event->rfds))) - ivac_receive_info(channel,ivac); + /* wait for user info */ + for(channel=0;channelnet.connection[channel].status&C_ESTABL) { + /* remote is sending info */ + if(FD_ISSET(ivac->net.connection[channel].fd,&(event->rfds))) { + if(ivac_receive_info(channel,ivac)==0) { + event_math(ivac->net.connection[channel].fd,event,READ,REMOVE); + network_close(&(ivac->net),channel); + sprintf(c_str,"channel %02d: broken pipe - disconnected",channel); + ivac_add_to_monitor(ivac,c_str); + } + ivac_display_content(ivac); + } + } + } /* user interaction */ if(FD_ISSET(0,&(event->rfds))) input_get_event(&(ivac->input),ivac_parse_command,ivac); - /* display ivac gui */ - // ivac_display_content(ivac); - return SUCCESS; } @@ -251,6 +333,12 @@ int ivac_parse_command(t_input *input,void *ptr) { if(!(strncmp(arg[0],"quit",4))) { valid=1; sprintf(c_str,"ivac shutdown ..."); + for(i=0;inet.connection[i].status&C_ESTABL) { + ivac_send_quit(i,ivac); + network_close(&(ivac->net),i); + } + } ivac_shutdown(ivac); } if(!(strncmp(arg[0],"set",3))) { @@ -262,15 +350,11 @@ int ivac_parse_command(t_input *input,void *ptr) { sprintf(c_str,"changed username to %s",ivac->username); } else if((channel>=0)&&(channelnet.connection[channel].status&C_IN_USE) { + i=network_set_connection_info(&(ivac->net),channel,arg[2],atoi(arg[3])); + if(i==N_E_IN_USE) sprintf(c_str,"channel %02d: connection in use",channel); - } - else { - strncpy(ivac->net.connection[channel].ip,arg[2],IP_DIGITS); - ivac->net.connection[channel].port=atoi(arg[3]); + if(i==N_SUCCESS) sprintf(c_str,"channel %02d: set connection info",channel); - ivac->net.connection[channel].status|=C_INFO_A; - } } else snprintf(c_str,IVAC_CONSOLE_STRING_LEN,"unknown argument: '%s'", arg[1]); } @@ -279,17 +363,17 @@ int ivac_parse_command(t_input *input,void *ptr) { if((arg[1][0]>='0')&&(arg[1][0]<='9')) channel=atoi(arg[1]); else channel=-1; if((channel>=0)&&(channelnet.connection[channel].status&C_INFO_A)) + i=network_connect(&(ivac->net),channel); + if(i==N_E_IN_USE) + sprintf(c_str,"channel %02d: connection in use",channel); + else if(i==N_E_NO_INFO) sprintf(c_str,"channel %02d: channel not configured",channel); else { - if(ivac->net.connection[channel].status&C_IN_USE) - sprintf(c_str,"channel %02d: connection in use",channel); - else { - sprintf(c_str,"channel %02d: trying to connect to %s:%d",channel, - ivac->net.connection[channel].ip, - ivac->net.connection[channel].port); - ivac->net.connection[channel].status|=C_IN_USE; - } + sprintf(c_str,"channel %02d: connected to %s:%d",channel, + ivac->net.connection[channel].ip, + ivac->net.connection[channel].port); + event_math(ivac->net.connection[channel].fd,&(ivac->event),READ,ADD); + ivac_send_info(channel,ivac); } } else sprintf(c_str,"invalid argument: '%s'",arg[1]); @@ -299,12 +383,16 @@ int ivac_parse_command(t_input *input,void *ptr) { if((arg[1][0]>='0')&&(arg[1][0]<='9')) channel=atoi(arg[1]); else channel=-1; if((channel>=0)&&(channelnet.connection[channel].status&C_ESTABL)) - sprintf(c_str,"channel %02d: no active connection",channel); - else { - ivac->net.connection[channel].status|=C_HANGUP; + if(ivac->net.connection[channel].status&C_ESTABL) { + ivac_send_quit(channel,ivac); + event_math(ivac->net.connection[channel].fd,&(ivac->event), + READ,REMOVE); + network_close(&(ivac->net),channel); sprintf(c_str,"channel %02d: connection closed",channel); + memset(&(ivac->challenger[channel]),0,sizeof(t_challenger)); } + else + sprintf(c_str,"channel %02d: no active connection",channel); } else sprintf(c_str,"invalid argument: '%s'",arg[1]); } @@ -313,11 +401,11 @@ int ivac_parse_command(t_input *input,void *ptr) { if((arg[1][0]>='0')&&(arg[1][0]<='9')) channel=atoi(arg[1]); else channel=-1; if((channel>=0)&&(channelnet.sendmask|=(1<net),channel); sprintf(c_str,"selected channel %d",channel); } else if(arg[1][0]='*') { - ivac->net.sendmask=0xff; + network_select(&(ivac->net),MAX_CONNECTIONS); strcpy(c_str,"selected all channels"); } else sprintf(c_str,"invalid argument: '%s'",arg[1]); @@ -327,11 +415,11 @@ int ivac_parse_command(t_input *input,void *ptr) { if((arg[1][0]>='0')&&(arg[1][0]<='9')) channel=atoi(arg[1]); else channel=-1; if((channel>=0)&&(channelnet.sendmask&=(~(1<net),channel); sprintf(c_str,"deselected channel %d",channel); } else if(arg[1][0]='*') { - ivac->net.sendmask=0; + network_deselect(&(ivac->net),MAX_CONNECTIONS); strcpy(c_str,"deselected all channels"); } else sprintf(c_str,"invalid argument: '%s'",arg[1]);