X-Git-Url: https://hackdaworld.org/gitweb/?p=my-code%2Fivac.git;a=blobdiff_plain;f=src%2Fivac.c;h=43b7ef6545f23cc5826ae92ffd8971de08093ce1;hp=101819271976248fa3e0df2e38da4aa49124d4a7;hb=eb0f685570bbc4e1f0f97d6d1bad5a905a1e036f;hpb=4abb6416b40e9d47386e1c9dd9400c3185d5bf06 diff --git a/src/ivac.c b/src/ivac.c index 1018192..43b7ef6 100644 --- a/src/ivac.c +++ b/src/ivac.c @@ -39,6 +39,10 @@ int main(int argc,char **argv) { /* set username */ strncpy(ivac.username,argv[1],CHAR_USERNAME); + /* clear challenger struct */ + for(i=0;inet)); input_shutdown(&(ivac->input)); event_stop(&(ivac->event)); -#ifdef USE_NCURSES - display_shutdown(&(ivac->display)); -#endif + // display_shutdown(&(ivac->display)); return SUCCESS; } @@ -170,31 +172,41 @@ 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 */ channel=network_manage_incoming(&(ivac->net)); - event_math(ivac->net.connection[channel].fd,event,READ,ADD); - ivac_send_info(channel,ivac); + 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) + /* 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))) ivac_receive_info(channel,ivac); + } + } /* user interaction */ if(FD_ISSET(0,&(event->rfds))) input_get_event(&(ivac->input),ivac_parse_command,ivac); - /* display ivac gui */ -#ifdef USE_NCURSES - ivac_display_content(ivac); -#endif - return SUCCESS; } @@ -219,11 +231,10 @@ int ivac_parse_command(t_input *input,void *ptr) { data=input->content; valid=0; - printf("%c",data[input->c_count-1]); - fflush(NULL); + /* refresh prompt content only! */ + ivac_display_prompt_content(ivac); /* parse command routines */ - if(data[input->c_count-1]=='\n') { /* delete console string + args */ @@ -231,7 +242,6 @@ int ivac_parse_command(t_input *input,void *ptr) { for(j=0;jusername); } 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]); } @@ -280,16 +286,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); - } + 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 +306,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.connection[channel].status&C_ESTABL)) + i=network_close(&(ivac->net),channel); + if(i==N_E_NC) sprintf(c_str,"channel %02d: no active connection",channel); - else { - ivac->net.connection[channel].status|=C_HANGUP; + else sprintf(c_str,"channel %02d: connection closed",channel); - } } else sprintf(c_str,"invalid argument: '%s'",arg[1]); } @@ -313,11 +319,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 +333,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]); @@ -340,8 +346,14 @@ int ivac_parse_command(t_input *input,void *ptr) { if(!valid) snprintf(c_str,IVAC_CONSOLE_STRING_LEN,"unknown command: '%s'",arg[0]); + /* call network functions */ + network_manage_connection(&(ivac->net)); + + /* add console string to console buffer */ ivac_add_to_monitor(ivac,c_str); - + + /* refresh whole display content */ + ivac_display_content(ivac); /* delete content buffer + reset counter */ memset(input->content,0,input->c_count-1); @@ -390,22 +402,43 @@ int ivac_display_box(t_display *display) { int ivac_display_box_content(t_ivac *ivac) { + int channel; + + /* prepare challenger names */ + for(channel=0;channelchallenger[channel].name[0]==0) + strcpy(ivac->challenger[channel].name,""); + +#ifdef USE_NCURSES +#else + for(channel=0;channelnet.connection[channel].status&C_INFO_A) + printf("channel %02d: ip:%s port:%d status: %02x - name: %s\n",channel, + ivac->net.connection[channel].ip, + ivac->net.connection[channel].port, + ivac->net.connection[channel].status, + ivac->challenger[channel].name); + } +#endif return SUCCESS; } int ivac_display_console(t_display *display) { - + +#ifdef USE_NCURSES int x,y; move(display->max_y-IVAC_CONSOLE_LEN-IVAC_PROMPT_LEN-1,0); - for(x=0;xmax_x;x++) addch('#'); + for(x=0;xmax_x;x++) addch('#'); +#endif return SUCCESS; } int ivac_display_console_content(t_ivac *ivac) { +#ifdef USE_NCURSES int x,y; int len; @@ -418,12 +451,19 @@ int ivac_display_console_content(t_ivac *ivac) { for(x=len;xconsole[i]); +#endif return SUCCESS; } int ivac_display_prompt(t_display *display) { +#ifdef USE_NCURSES int x,y; move(display->max_y-3,0); @@ -433,12 +473,14 @@ int ivac_display_prompt(t_display *display) { move(display->max_y-1,0); for(x=0;xmax_x;x++) addch('#'); refresh(); +#endif return SUCCESS; } int ivac_display_prompt_content(t_ivac *ivac) { +#ifdef USE_NCURSES int x,y; /* delete old command */ @@ -450,6 +492,10 @@ int ivac_display_prompt_content(t_ivac *ivac) { for(x=0;xinput.c_count;x++) mvaddch(ivac->display.max_y-2,x+12,ivac->input.content[x]); refresh(); +#else + printf("%c",ivac->input.content[ivac->input.c_count-1]); + fflush(NULL); +#endif return SUCCESS; } @@ -495,11 +541,5 @@ int ivac_add_to_monitor(t_ivac *ivac,char *msg) { memcpy(ivac->console[i],ivac->console[i+1],IVAC_CONSOLE_STRING_LEN); memcpy(ivac->console[IVAC_CONSOLE_LEN-1],msg,IVAC_CONSOLE_STRING_LEN); - for(i=0;iconsole[i]); -#ifdef USE_NCURSES - ivac_display_console_content(ivac); -#endif - return SUCCESS; }