X-Git-Url: https://hackdaworld.org/gitweb/?p=my-code%2Fivac.git;a=blobdiff_plain;f=src%2Fivac.c;h=43b7ef6545f23cc5826ae92ffd8971de08093ce1;hp=70d1a35c234809708086d60c423e6a2c9c68b587;hb=eb0f685570bbc4e1f0f97d6d1bad5a905a1e036f;hpb=8a6fa2a921c79c007f3203aab1388fe2f0620eda diff --git a/src/ivac.c b/src/ivac.c index 70d1a35..43b7ef6 100644 --- a/src/ivac.c +++ b/src/ivac.c @@ -21,6 +21,8 @@ * */ +// #define USE_NCURSES + #include "ivac.h" int main(int argc,char **argv) { @@ -28,9 +30,21 @@ int main(int argc,char **argv) { /* TESTING BY NOW */ t_ivac ivac; + int i; - /* set username (futur: read from config or entered later) */ - strcpy(ivac.username,"hackbard"); + if(argc!=2) { + printf("\nusage: %s \n\n",argv[0]); + return ERROR; + } + + /* set username */ + strncpy(ivac.username,argv[1],CHAR_USERNAME); + /* clear challenger struct */ + for(i=0;inet)); input_shutdown(&(ivac->input)); event_stop(&(ivac->event)); + // display_shutdown(&(ivac->display)); return SUCCESS; } @@ -156,29 +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 */ - ivac_display(ivac); - return SUCCESS; } @@ -192,74 +220,326 @@ int ivac_regular_cb(t_event *event,void *ptr) { int ivac_parse_command(t_input *input,void *ptr) { t_ivac *ivac; + int i,j,k; + int len; int channel; + char *data,valid; + char c_str[IVAC_CONSOLE_STRING_LEN]; + char arg[IVAC_ARG_COUNT][IVAC_ARG_LEN]; ivac=(t_ivac *)ptr; + data=input->content; + valid=0; + + /* refresh prompt content only! */ + ivac_display_prompt_content(ivac); /* parse command routines */ + if(data[input->c_count-1]=='\n') { + + /* delete console string + args */ + memset(c_str,0,IVAC_CONSOLE_STRING_LEN); + for(j=0;j='0')&&(arg[1][0]<='9')) channel=atoi(arg[1]); + else channel=-1; + if(!(strncmp(arg[1],"name",4))) { + strncpy(ivac->username,arg[2],CHAR_USERNAME); + sprintf(c_str,"changed username to %s",ivac->username); + } + else if((channel>=0)&&(channelnet),channel,arg[2],atoi(arg[3])); + if(i==N_E_IN_USE) + sprintf(c_str,"channel %02d: connection in use",channel); + if(i==N_SUCCESS) + sprintf(c_str,"channel %02d: set connection info",channel); + } else snprintf(c_str,IVAC_CONSOLE_STRING_LEN,"unknown argument: '%s'", + arg[1]); + } + if(!(strncmp(arg[0],"connect",7))) { + valid=1; + if((arg[1][0]>='0')&&(arg[1][0]<='9')) channel=atoi(arg[1]); + else channel=-1; + if((channel>=0)&&(channelnet),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 { + 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]); + } + if(!(strncmp(arg[0],"close",5))) { + valid=1; + if((arg[1][0]>='0')&&(arg[1][0]<='9')) channel=atoi(arg[1]); + else channel=-1; + if((channel>=0)&&(channelnet),channel); + if(i==N_E_NC) + sprintf(c_str,"channel %02d: no active connection",channel); + else + sprintf(c_str,"channel %02d: connection closed",channel); + } + else sprintf(c_str,"invalid argument: '%s'",arg[1]); + } + if(!(strncmp(arg[0],"select",6))) { + valid=1; + if((arg[1][0]>='0')&&(arg[1][0]<='9')) channel=atoi(arg[1]); + else channel=-1; + if((channel>=0)&&(channelnet),channel); + sprintf(c_str,"selected channel %d",channel); + } + else if(arg[1][0]='*') { + network_select(&(ivac->net),MAX_CONNECTIONS); + strcpy(c_str,"selected all channels"); + } + else sprintf(c_str,"invalid argument: '%s'",arg[1]); + } + if(!(strncmp(arg[0],"deselect",8))) { + valid=1; + if((arg[1][0]>='0')&&(arg[1][0]<='9')) channel=atoi(arg[1]); + else channel=-1; + if((channel>=0)&&(channelnet),channel); + sprintf(c_str,"deselected channel %d",channel); + } + else if(arg[1][0]='*') { + network_deselect(&(ivac->net),MAX_CONNECTIONS); + strcpy(c_str,"deselected all channels"); + } + else sprintf(c_str,"invalid argument: '%s'",arg[1]); + } + + 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); - if(input->content[input->c_count-1]=='\n') { /* delete content buffer + reset counter */ - memset(input->content,0,input->c_count); + memset(input->content,0,input->c_count-1); input->c_count=0; + + } + + return SUCCESS; +} + +int ivac_display_head(t_display *display) { + +#ifdef USE_NCURSES + int x,y; + + move(0,0); + for(x=0;xmax_x;x++) addch('#'); + mvaddstr(1,0,"##"); + mvaddstr(1,(display->max_x-4)/2-4,"- ivac -"); + mvaddstr(1,(display->max_x-2),"##"); + move(2,0); + for(x=0;xmax_x;x++) addch('#'); + refresh(); +#else + puts("#########################################################"); + puts("##### ivac - - Copyright (C) 2004 Frank Zirkelbach #####"); + puts("#########################################################"); +#endif + + return SUCCESS; +} + +int ivac_display_box(t_display *display) { + +#ifdef USE_NCURSES + int x,y; + + for(y=IVAC_PROMPT_LEN;ymax_y-IVAC_PROMPT_LEN;y++) { + mvaddch(y,0,'#'); + mvaddch(y,display->max_x-1,'#'); } +#endif return SUCCESS; } -int ivac_display_head(void) { +int ivac_display_box_content(t_ivac *ivac) { - /* 23 x 80 */ - int column,line; + int channel; - for(column=0;columnchallenger[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 - printf("##"); - for(column=2;column<(COLUMN-8)/2;column++) printf(" "); - printf("- ivac -"); - for(column=2;column<(COLUMN-8)/2;column++) printf(" "); - printf("##\n"); + return SUCCESS; +} - for(column=0;columnmax_y-IVAC_CONSOLE_LEN-IVAC_PROMPT_LEN-1,0); + for(x=0;xmax_x;x++) addch('#'); +#endif + return SUCCESS; } -int ivac_display_prompt(t_ivac *ivac) { +int ivac_display_console_content(t_ivac *ivac) { + +#ifdef USE_NCURSES + int x,y; + int len; - int column,line; + for(y=0;yconsole[y]); + move(ivac->display.max_y-IVAC_CONSOLE_LEN-IVAC_PROMPT_LEN+y,2); + for(x=0;xconsole[y][x]>' ')||(ivac->console[y][x]<='~')) + ?ivac->console[y][x]:' '); + for(x=len;xinput.c_count;column++) - printf("%c",ivac->input.content[column-12]); - for(column=12+ivac->input.c_count;columnconsole[i]); +#endif return SUCCESS; } -int ivac_display(t_ivac *ivac) { +int ivac_display_prompt(t_display *display) { - int line,column; +#ifdef USE_NCURSES + int x,y; - /* display head */ - ivac_display_head(); + move(display->max_y-3,0); + for(x=0;xmax_x;x++) addch('#'); + mvaddstr(display->max_y-2,0,"## command: "); + mvaddstr(display->max_y-2,display->max_x-2,"##"); + move(display->max_y-1,0); + for(x=0;xmax_x;x++) addch('#'); + refresh(); +#endif - /* build content of middle part + display */ - for(line=3;lineinput.c_count==0) { + move(ivac->display.max_y-2,12); + for(x=12;xdisplay.max_x-1;x++) addch(' '); } + 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; +} + +int ivac_display(t_display *display) { + + int x,y; + + /* display head */ + ivac_display_head(display); + + /* display box */ + ivac_display_box(display); + + /* display console */ + ivac_display_console(display); + /* display command prompt */ - ivac_display_prompt(ivac); + ivac_display_prompt(display); + + return SUCCESS; +} + +int ivac_display_content(t_ivac *ivac) { + + /* display box content */ + ivac_display_box_content(ivac); + + /* display console content */ + ivac_display_console_content(ivac); + + /* display prompt content */ + ivac_display_prompt_content(ivac); + + return SUCCESS; +} + +int ivac_add_to_monitor(t_ivac *ivac,char *msg) { + + int i; + + for(i=0;iconsole[i],ivac->console[i+1],IVAC_CONSOLE_STRING_LEN); + memcpy(ivac->console[IVAC_CONSOLE_LEN-1],msg,IVAC_CONSOLE_STRING_LEN); return SUCCESS; }