*
*/
-// #define USE_NCURSES
-
#include "ivac.h"
+int usage(void) {
+
+ puts("");
+ puts("usage: ivac <options>");
+ puts("");
+ puts("options:");
+ puts("-h \t\t show this help");
+ puts("-n <name> \t specify your name");
+ puts("-p <port> \t specify port to listen for incoming connections");
+ puts("-u <port> \t specify udp data port");
+ puts("-d <device> \t specify audio device");
+ puts("-i <iface> \t specify network interface");
+ puts("");
+
+ return SUCCESS;
+}
+
int main(int argc,char **argv) {
/* TESTING BY NOW */
t_ivac ivac;
int i;
- if(argc!=2) {
- printf("\nusage: %s <username>\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;i<argc;i++) {
+ if(argv[i][0]=='-') {
+ switch(argv[i][1]) {
+ case 'h':
+ usage();
+ return SUCCESS;
+ case 'n':
+ strncpy(ivac.username,argv[++i],CHAR_USERNAME);
+ break;
+ case 'p':
+ ivac.net.l_port=atoi(argv[++i]);
+ break;
+ case 'u':
+ ivac.net.l_udp_port=atoi(argv[++i]);
+ break;
+ case 'd':
+ strncpy(ivac.audio.dsp_dev,argv[++i],MAX_CHAR_DEVICE);
+ break;
+ case 'i':
+ strncpy(ivac.net.nic,argv[++i],MAX_NIC_DEVICE);
+ default:
+ usage();
+ return ERROR;
+ }
+ }
+ else usage();
}
- /* set username */
- strncpy(ivac.username,argv[1],CHAR_USERNAME);
/* clear challenger struct */
for(i=0;i<MAX_CONNECTIONS;i++)
memset(&(ivac.challenger[i]),0,sizeof(t_challenger));
ivac.input.mode=CONTENT_BUFFER;
input_init(&(ivac.input));
- /* set listen port (futur: read from config or entered later) */
- network_set_listen_port(&(ivac.net),IVAC_LISTEN_PORT);
/* network init */
if(network_init(&(ivac.net))==N_ERROR) {
printf("[ivac] use 'fuser -n tcp %d' to determine the process to kill!\n",
ivac_shutdown(&ivac);
return ERROR;
}
+ network_udp_listen_init(&(ivac.net));
- /* add listening port + stdin to (read) event system */
+ /* add listening port(s) + stdin to (read) event system */
event_math(ivac.net.l_fd,&(ivac.event),READ,ADD);
+ event_math(ivac.net.l_udp_fd,&(ivac.event),READ,ADD);
event_math(0,&(ivac.event),READ,ADD);
/* display init */
- // display_init(&(ivac.display));
+ display_init(&(ivac.display));
+
+ /* use hardcoded audio settings by now */
+ ivac.audio.fmt=BIT_8;
+ ivac.audio.channels=MONO;
+ ivac.audio.speed=8000;
+ /* audio init */
+ audio_init(&(ivac.audio));
+ audio_setup(&(ivac.audio));
/* display */
ivac_display(&(ivac.display));
/* start event system - callbacks used: ivac_event_cb + ivac_regular_cb */
event_start(&(ivac.event),(void *)&ivac,ivac_event_cb,ivac_regular_cb);
- // ivac_shutdown(&ivac);
-
return SUCCESS;
}
int ivac_shutdown(t_ivac *ivac) {
network_shutdown(&(ivac->net));
+ 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;
}
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;
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) {
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);
for(channel=0;channel<MAX_CONNECTIONS;channel++) {
if(ivac->net.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_content(ivac);
+ }
}
}
if(!(strncmp(arg[0],"quit",4))) {
valid=1;
sprintf(c_str,"ivac shutdown ...");
+ for(i=0;i<MAX_CONNECTIONS;i++) {
+ if(ivac->net.connection[i].status&C_ESTABL) {
+ ivac_send_quit(i,ivac);
+ network_close(&(ivac->net),i);
+ }
+ }
ivac_shutdown(ivac);
}
if(!(strncmp(arg[0],"set",3))) {
sprintf(c_str,"changed username to %s",ivac->username);
}
else if((channel>=0)&&(channel<MAX_CONNECTIONS)) {
- i=network_set_connection_info(&(ivac->net),channel,
- ivac->net.connection[channel].ip,
- ivac->net.connection[channel].port);
+ 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);
if(i==N_SUCCESS)
if((arg[1][0]>='0')&&(arg[1][0]<='9')) channel=atoi(arg[1]);
else channel=-1;
if((channel>=0)&&(channel<MAX_CONNECTIONS)) {
- i=network_close(&(ivac->net),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]);
}
int ivac_display_head(t_display *display) {
-#ifdef USE_NCURSES
- int x,y;
-
- move(0,0);
- for(x=0;x<display->max_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;x<display->max_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;y<display->max_y-IVAC_PROMPT_LEN;y++) {
- mvaddch(y,0,'#');
- mvaddch(y,display->max_x-1,'#');
- }
-#endif
-
return SUCCESS;
}
if(ivac->challenger[channel].name[0]==0)
strcpy(ivac->challenger[channel].name,"<empty>");
-#ifdef USE_NCURSES
-#else
- for(channel=0;channel<MAX_CONNECTIONS;channel++) {
- if(ivac->net.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;x<display->max_x;x++) addch('#');
-#endif
-
return SUCCESS;
}
int ivac_display_console_content(t_ivac *ivac) {
-#ifdef USE_NCURSES
- int x,y;
- int len;
-
- for(y=0;y<IVAC_CONSOLE_LEN;y++) {
- len=strlen(ivac->console[y]);
- move(ivac->display.max_y-IVAC_CONSOLE_LEN-IVAC_PROMPT_LEN+y,2);
- for(x=0;x<len;x++)
- addch(((ivac->console[y][x]>' ')||(ivac->console[y][x]<='~'))
- ?ivac->console[y][x]:' ');
- for(x=len;x<IVAC_CONSOLE_STRING_LEN-4;x++) addch(' ');
- }
- refresh();
-#else
int i;
for(i=0;i<IVAC_CONSOLE_LEN;i++)
printf("[ivac] console line %d: %s\n",i,ivac->console[i]);
-#endif
return SUCCESS;
}
int ivac_display_prompt(t_display *display) {
-#ifdef USE_NCURSES
- int x,y;
-
- move(display->max_y-3,0);
- for(x=0;x<display->max_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;x<display->max_x;x++) addch('#');
- refresh();
-#endif
-
return SUCCESS;
}
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;x<ivac->display.max_x-1;x++) addch(' ');
- }
-
- for(x=0;x<ivac->input.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;
}