X-Git-Url: https://hackdaworld.org/gitweb/?p=my-code%2Fivac.git;a=blobdiff_plain;f=src%2Fivac.c;h=e959ef8ba6f4e0dc7bec8b8b4dc2322167d9feb7;hp=70d1a35c234809708086d60c423e6a2c9c68b587;hb=40031b2d692a7b83e437535045ece6c58f8bf31e;hpb=8a6fa2a921c79c007f3203aab1388fe2f0620eda diff --git a/src/ivac.c b/src/ivac.c index 70d1a35..e959ef8 100644 --- a/src/ivac.c +++ b/src/ivac.c @@ -23,14 +23,72 @@ #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("-u \t specify udp data port"); + puts("-d \t specify audio device"); + puts("-i \t specify network interface"); + puts(""); + + return SUCCESS; +} + int main(int argc,char **argv) { /* TESTING BY NOW */ t_ivac ivac; + int i; + + /* default values */ + strcpy(ivac.username,"ivac"); + ivac.net.l_port=IVAC_LISTEN_PORT; + ivac.net.l_udp_port=IVAC_UDP_PORT; + strcpy(ivac.audio.dsp_dev,SOUND_DEVICE); + strcpy(ivac.net.nic,"eth0"); + + /* parse argv and change default values */ + for(i=1;idisplay)); network_shutdown(&(ivac->net)); + network_udp_shutdown(&(ivac->net)); input_shutdown(&(ivac->input)); event_stop(&(ivac->event)); + audio_shutdown(&(ivac->audio)); return SUCCESS; } @@ -113,9 +193,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; @@ -130,7 +227,7 @@ int ivac_receive_info(int channel,t_ivac *ivac) { switch(data[count]) { case IVAC_SEND_NAME: strncpy(ivac->challenger[channel].name,data+count+2,data[count+1]); - ivac->challenger[channel].name[data[count+1]]='\0'; + ivac->challenger[channel].name[(int)data[count+1]]='\0'; count+=(data[count+1]+2); break; case IVAC_SEND_G_CAP: @@ -142,48 +239,88 @@ 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 */ 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); + ivac_display_draw(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); + ivac_display_draw(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; } int ivac_regular_cb(t_event *event,void *ptr) { + t_ivac *ivac; + + ivac=(t_ivac *)ptr; + + ivac_add_to_monitor(ivac,"event HUGH"); + /* usual jobs like audio & video transmit ... */ return SUCCESS; @@ -192,74 +329,323 @@ 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]; + char debug_string[128]; ivac=(t_ivac *)ptr; + data=input->content; + valid=0; + + /* refresh prompt content only */ + ivac_display_prompt_content(ivac); + ivac_display_draw(ivac); + +#ifdef DEBUG + ivac_add_to_monitor(ivac,"nach display refresh"); +#endif /* parse command routines */ + if(data[input->c_count-1]=='\n'||data[input->c_count-1]=='\r') { + +#ifdef DEBUG + ivac_add_to_monitor(ivac,"got newline or carriage return!"); +#endif + + /* delete console string + args */ + memset(c_str,0,IVAC_CONSOLE_STRING_LEN); + for(j=0;jnet.connection[i].status&C_ESTABL) { + ivac_send_quit(i,ivac); + network_close(&(ivac->net),i); + } + } + ivac_shutdown(ivac); + } + if(!(strncmp(arg[0],"set",3))) { + valid=1; + if((arg[1][0]>='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.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]); + } + 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); - if(input->content[input->c_count-1]=='\n') { - /* delete content buffer + reset counter */ - memset(input->content,0,input->c_count); + /* refresh whole display content */ + ivac_display_content(ivac); + ivac_display_draw(ivac); + + /* delete input content buffer + reset counter */ + memset(input->content,0,input->c_count-1); input->c_count=0; + } + ivac_add_to_monitor(ivac,"debug: end of parse function"); + + return SUCCESS; +} + +int ivac_display_head(t_ivac *ivac) { + + display_line(&(ivac->display),0,0,ivac->display.max_x,0,'#'); + display_string(&(ivac->display),(ivac->display.max_x-strlen(PROG_NAME))/2,1, + PROG_NAME,strlen(PROG_NAME)); + display_line(&(ivac->display),0,2,ivac->display.max_x,2,'#'); + + return SUCCESS; +} + +int ivac_display_box(t_ivac *ivac) { + + display_line(&(ivac->display),0,3,ivac->display.max_x,3,'-'); + display_line(&(ivac->display),0,5,ivac->display.max_x,5,'-'); + display_string(&(ivac->display),0,6,"connections:",12); + display_line(&(ivac->display), + 0,ivac->display.max_y-IVAC_PROMPT_LEN-IVAC_CONSOLE_LEN-1, + ivac->display.max_x, + ivac->display.max_y-IVAC_PROMPT_LEN-IVAC_CONSOLE_LEN-1,'-'); + return SUCCESS; } -int ivac_display_head(void) { +int ivac_display_box_content(t_ivac *ivac) { - /* 23 x 80 */ - int column,line; + int channel; + char string[MAX_BOX_CHARS]; + + /* prepare challenger names */ + for(channel=0;channelchallenger[channel].name[0]==0) + strcpy(ivac->challenger[channel].name,""); + + sprintf(string,"username: %s, capabilities: %02x|%04x", + ivac->username,ivac->g_cap,ivac->av_cap); + display_string(&(ivac->display),0,4,string,strlen(string)); + + for(channel=0;channelnet.connection[channel].status&C_INFO_A) { + snprintf(string,MAX_BOX_CHARS, + "channel %02d: %s:%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); + display_string(&(ivac->display),0,IVAC_HEAD_LEN+4+channel, + string,strlen(string)); + } + } + + return SUCCESS; +} + +int ivac_display_console(t_ivac *ivac) { + + display_string(&(ivac->display), + 0,ivac->display.max_y-IVAC_PROMPT_LEN-IVAC_CONSOLE_LEN, + "console messages:",17); + + return SUCCESS; +} - for(column=0;columnconsole[i]); + display_string(&(ivac->display), + 0,ivac->display.max_y-IVAC_PROMPT_LEN-IVAC_CONSOLE_LEN+1+i, + string,strlen(string)); + } return SUCCESS; } int ivac_display_prompt(t_ivac *ivac) { - int column,line; + display_string(&(ivac->display),0,ivac->display.max_y-1,"prompt: ",8); + + return SUCCESS; +} + +int ivac_display_prompt_content(t_ivac *ivac) { + + char string[IVAC_CONSOLE_STRING_LEN]; + int count; + + for(count=0;countinput.c_count;count++) + string[count]=ivac->input.content[count]; + for(;countdisplay.max_x-8;count++) string[count]=' '; + display_string(&(ivac->display),8,ivac->display.max_y-1, + string,ivac->display.max_x); + + 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); - for(column=0;columninput.c_count;column++) - printf("%c",ivac->input.content[column-12]); - for(column=12+ivac->input.c_count;columndisplay)); + display_set_cursor(&(ivac->display),ivac->input.c_count+8, + ivac->display.max_y-1); + + 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); + + ivac_display_console_content(ivac); + ivac_display_draw(ivac); + + return SUCCESS; +}