X-Git-Url: https://hackdaworld.org/gitweb/?p=my-code%2Fivac.git;a=blobdiff_plain;f=src%2Fivac.c;h=60338eec055ce19eb75578facb3e3d6124a4c643;hp=f4f69523a7738cf718a4219438c9c72c93eb90c6;hb=bb2eedc347c7457678118762799806db07d5d200;hpb=7e1daf96d3ccb8927ec0bdaf096c7f561bd29410 diff --git a/src/ivac.c b/src/ivac.c index f4f6952..60338ee 100644 --- a/src/ivac.c +++ b/src/ivac.c @@ -21,10 +21,25 @@ * */ -// #define USE_NCURSES - #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 */ @@ -32,13 +47,42 @@ 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; + 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;inet)); + network_udp_shutdown(&(ivac->net)); input_shutdown(&(ivac->input)); event_stop(&(ivac->event)); - // display_shutdown(&(ivac->display)); + audio_shutdown(&(ivac->audio)); + display_shutdown(&(ivac->display)); return SUCCESS; } @@ -129,9 +187,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,14 +233,27 @@ 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) { @@ -184,22 +272,29 @@ int ivac_event_cb(t_event *event,void *ptr) { else if(channel==N_E_MAXC) sprintf(c_str,"maximum connections reached"); else { - sprintf(c_str,"established connection from %s port %d on channel %d\n", + 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(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))) - ivac_receive_info(channel,ivac); + 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(ivac); + } } } @@ -231,7 +326,7 @@ int ivac_parse_command(t_input *input,void *ptr) { data=input->content; valid=0; - /* refresh prompt content only! */ + /* refresh prompt content only */ ivac_display_prompt_content(ivac); /* parse command routines */ @@ -262,6 +357,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))) { @@ -306,11 +407,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),channel); - if(i==N_E_NC) - sprintf(c_str,"channel %02d: no active connection",channel); - else + 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]); } @@ -353,7 +459,7 @@ int ivac_parse_command(t_input *input,void *ptr) { ivac_add_to_monitor(ivac,c_str); /* refresh whole display content */ - ivac_display_content(ivac); + ivac_display(ivac); /* delete content buffer + reset counter */ memset(input->content,0,input->c_count-1); @@ -364,21 +470,10 @@ int ivac_parse_command(t_input *input,void *ptr) { return SUCCESS; } -int ivac_display_head(t_display *display) { - -#ifdef USE_NCURSES - int x,y; +int ivac_display_head(t_ivac *ivac) { - 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("#########################################################"); +#ifndef XXX_GUI + display_term_line(&(ivac->display),'#'); puts("##### ivac - - Copyright (C) 2004 Frank Zirkelbach #####"); puts("#########################################################"); #endif @@ -386,17 +481,16 @@ int ivac_display_head(t_display *display) { return SUCCESS; } -int ivac_display_box(t_display *display) { - -#ifdef USE_NCURSES - int x,y; +int ivac_display_box(t_ivac *ivac) { - for(y=IVAC_PROMPT_LEN;ymax_y-IVAC_PROMPT_LEN;y++) { - mvaddch(y,0,'#'); - mvaddch(y,display->max_x-1,'#'); - } +#ifndef XXX_GUI + puts("---------------------------------------------------------------------"); + printf("username: %s, capabilities: %02x|%04x\n",ivac->username,ivac->g_cap, + ivac->av_cap); + puts("---------------------------------------------------------------------"); + puts("connections:"); #endif - + return SUCCESS; } @@ -409,11 +503,10 @@ int ivac_display_box_content(t_ivac *ivac) { if(ivac->challenger[channel].name[0]==0) strcpy(ivac->challenger[channel].name,""); -#ifdef USE_NCURSES -#else +#ifndef XXX_GUI for(channel=0;channelnet.connection[channel].status&C_INFO_A) - printf("channel %02d: ip:%s port:%d status: %02x - name: %s\n",channel, + printf("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, @@ -424,55 +517,33 @@ int ivac_display_box_content(t_ivac *ivac) { return SUCCESS; } -int ivac_display_console(t_display *display) { +int ivac_display_console(t_ivac *ivac) { -#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('#'); +#ifndef XXX_GUI + puts("---------------------------------------------------------------------"); + puts("console messages:"); #endif - + return SUCCESS; } int ivac_display_console_content(t_ivac *ivac) { -#ifdef USE_NCURSES - int x,y; - int len; - - 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;xconsole[i]); + printf("| %s\n",ivac->console[i]); #endif return SUCCESS; } -int ivac_display_prompt(t_display *display) { - -#ifdef USE_NCURSES - int x,y; +int ivac_display_prompt(t_ivac *ivac) { - 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(); +#ifndef XXX_GUI + puts("---------------------------------------------------------------------"); + printf("prompt: "); #endif return SUCCESS; @@ -480,19 +551,7 @@ int ivac_display_prompt(t_display *display) { int ivac_display_prompt_content(t_ivac *ivac) { -#ifdef USE_NCURSES - int x,y; - - /* delete old command */ - if(ivac->input.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 +#ifndef XXX_GUI printf("%c",ivac->input.content[ivac->input.c_count-1]); fflush(NULL); #endif @@ -500,35 +559,30 @@ int ivac_display_prompt_content(t_ivac *ivac) { return SUCCESS; } -int ivac_display(t_display *display) { +int ivac_display(t_ivac *ivac) { int x,y; +#ifndef XXX_GUI /* display head */ - ivac_display_head(display); + ivac_display_head(ivac); /* display box */ - ivac_display_box(display); - - /* display console */ - ivac_display_console(display); - - /* display command prompt */ - ivac_display_prompt(display); - - return SUCCESS; -} - -int ivac_display_content(t_ivac *ivac) { - - /* display box content */ + ivac_display_box(ivac); +#endif ivac_display_box_content(ivac); - /* display console content */ +#ifndef XXX_GUI + /* display console */ + ivac_display_console(ivac); +#endif ivac_display_console_content(ivac); - /* display prompt content */ - ivac_display_prompt_content(ivac); +#ifndef XXX_GUI + /* display command prompt */ + ivac_display_prompt(ivac); +#endif + fflush(NULL); return SUCCESS; }