1 /* ivac.c -- main ivac file
3 * author: hackbard@hackdaworld.dyndns.org
4 * frank.zirkelbach@physik.uni-augsburg.de
6 * Copyright (C) 2004 Frank Zirkelbach
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 puts("usage: ivac <options>");
32 puts("-h \t\t show this help");
33 puts("-n <name> \t specify your name");
34 puts("-p <port> \t specify port to listen for incoming connections");
35 puts("-u <port> \t specify udp data port");
36 puts("-d <device> \t specify audio device");
37 puts("-i <iface> \t specify network interface");
43 int main(int argc,char **argv) {
51 strcpy(ivac.username,"ivac");
52 ivac.net.l_port=IVAC_LISTEN_PORT;
53 ivac.net.l_udp_port=IVAC_UDP_PORT;
54 strcpy(ivac.audio.dsp_dev,SOUND_DEVICE);
55 strcpy(ivac.net.nic,"eth0");
57 /* parse argv and change default values */
65 strncpy(ivac.username,argv[++i],CHAR_USERNAME);
68 ivac.net.l_port=atoi(argv[++i]);
71 ivac.net.l_udp_port=atoi(argv[++i]);
74 strncpy(ivac.audio.dsp_dev,argv[++i],MAX_CHAR_DEVICE);
77 strncpy(ivac.net.nic,argv[++i],MAX_NIC_DEVICE);
86 /* clear challenger struct */
87 for(i=0;i<MAX_CONNECTIONS;i++)
88 memset(&(ivac.challenger[i]),0,sizeof(t_challenger));
89 /* zero console buffer */
90 for(i=0;i<IVAC_CONSOLE_LEN;i++)
91 memset(ivac.console[i],0,IVAC_CONSOLE_STRING_LEN);
93 /* set capabilities (futur: set by check routines) */
95 ivac.av_cap=AUDIO|VIDEO|DUPLEX;
97 /* set event timeout */
98 ivac.event.timeout.tv_sec=IVAC_S_SEC;
99 ivac.event.timeout.tv_usec=IVAC_S_USEC;
101 if(event_init(&(ivac.event))==E_ERROR) {
102 ivac_shutdown(&ivac);
107 ivac.input.mode=CONTENT_BUFFER;
108 ivac.input.c_count=0;
109 if(input_init(&(ivac.input))==I_ERROR) {
110 ivac_shutdown(&ivac);
115 if(network_init(&(ivac.net))==N_ERROR) {
116 printf("[ivac] use 'fuser -n tcp %d' to determine the process to kill!\n",
118 ivac_shutdown(&ivac);
122 /* network udp init */
123 network_udp_listen_init(&(ivac.net));
125 /* add listening port(s) + stdin to (read) event system */
126 event_math(ivac.net.l_fd,&(ivac.event),READ,ADD);
127 event_math(ivac.net.l_udp_fd,&(ivac.event),READ,ADD);
128 event_math(0,&(ivac.event),READ,ADD);
130 /* use hardcoded audio settings by now */
131 ivac.audio.fmt=BIT_8;
132 ivac.audio.channels=MONO;
133 ivac.audio.speed=8000;
135 audio_init(&(ivac.audio));
137 audio_setup(&(ivac.audio));
140 display_init(&(ivac.display));
144 ivac_display_content(&ivac);
145 ivac_display_draw(&ivac);
147 /* start event system - callbacks used: ivac_event_cb + ivac_regular_cb */
148 event_start(&(ivac.event),(void *)&ivac,ivac_event_cb,ivac_regular_cb);
153 int ivac_shutdown(t_ivac *ivac) {
155 display_shutdown(&(ivac->display));
156 network_shutdown(&(ivac->net));
157 network_udp_shutdown(&(ivac->net));
158 input_shutdown(&(ivac->input));
159 event_stop(&(ivac->event));
160 audio_shutdown(&(ivac->audio));
165 int ivac_send_info(int channel,t_ivac *ivac) {
167 char data[SEND_N_MAX];
170 size=strlen(ivac->username);
172 data[0]=IVAC_SEND_NAME;
174 strncpy(data+2,ivac->username,size);
177 data[size]=IVAC_SEND_G_CAP;
179 data[size+2]=ivac->g_cap;
182 data[size]=IVAC_SEND_AV_CAP;
184 data[size+2]=(ivac->av_cap)>>8;
185 data[size+3]=(ivac->av_cap)&0xff;
188 if(network_send(ivac->net.connection[channel].fd,data,size)==N_ERROR) {
189 puts("[ivac] ivac_send_info failed");
196 int ivac_send_quit(int channel,t_ivac *ivac) {
198 char data[7]; /* one more for \0 */
200 data[0]=IVAC_SEND_QUIT;
202 strcpy(data+2,"quit");
204 if(network_send(ivac->net.connection[channel].fd,data,6)==N_ERROR) {
205 puts("[ivac] ivac_send_quit failed");
212 int ivac_receive_info(int channel,t_ivac *ivac) {
214 char data[SEND_N_MAX];
215 char c_str[IVAC_CONSOLE_STRING_LEN];
220 if((length=network_receive(ivac->net.connection[channel].fd,
221 data,SEND_N_MAX))==N_ERROR) {
222 puts("[ivac] ivac_receive_info failed");
226 while(length-count) {
227 switch(data[count]) {
229 strncpy(ivac->challenger[channel].name,data+count+2,data[count+1]);
230 ivac->challenger[channel].name[(int)data[count+1]]='\0';
231 count+=(data[count+1]+2);
233 case IVAC_SEND_G_CAP:
234 ivac->challenger[channel].g_cap=data[count+2];
237 case IVAC_SEND_AV_CAP:
238 ivac->challenger[channel].av_cap=data[count+2]<<8;
239 ivac->challenger[channel].av_cap|=data[count+3];
243 if(!(strncmp(data+count+2,"quit",data[1])))
244 sprintf(c_str,"channel %02d: connection closed by remote host",
246 event_math(ivac->net.connection[channel].fd,&(ivac->event),
248 network_close(&(ivac->net),channel);
249 memset(&(ivac->challenger[channel]),0,sizeof(t_challenger));
250 ivac_add_to_monitor(ivac,c_str);
254 sprintf(c_str,"ivac_receive_info, unknown character: 0x%02x\n",
256 ivac_add_to_monitor(ivac,c_str);
265 int ivac_event_cb(t_event *event,void *ptr) {
269 char c_str[IVAC_CONSOLE_STRING_LEN];
273 /* incoming connection -- first contact => send info */
274 if(FD_ISSET(ivac->net.l_fd,&(event->rfds))) {
275 channel=network_manage_incoming(&(ivac->net));
276 if(channel==N_E_ACCEPT)
277 sprintf(c_str,"accept failed");
278 else if(channel==N_E_MAXC)
279 sprintf(c_str,"maximum connections reached");
281 sprintf(c_str,"connection from %s port %d on channel %d",
282 ivac->net.connection[channel].ip,
283 ivac->net.connection[channel].port,channel);
284 ivac_add_to_monitor(ivac,c_str);
285 event_math(ivac->net.connection[channel].fd,event,READ,ADD);
286 ivac_send_info(channel,ivac);
288 ivac_display_content(ivac);
289 ivac_display_draw(ivac);
292 /* wait for user info */
293 for(channel=0;channel<MAX_CONNECTIONS;channel++) {
294 if(ivac->net.connection[channel].status&C_ESTABL) {
295 /* remote is sending info */
296 if(FD_ISSET(ivac->net.connection[channel].fd,&(event->rfds))) {
297 if(ivac_receive_info(channel,ivac)==0) {
298 event_math(ivac->net.connection[channel].fd,event,READ,REMOVE);
299 network_close(&(ivac->net),channel);
300 sprintf(c_str,"channel %02d: broken pipe - disconnected",channel);
301 ivac_add_to_monitor(ivac,c_str);
303 ivac_display_content(ivac);
304 ivac_display_draw(ivac);
309 /* user interaction */
310 if(FD_ISSET(0,&(event->rfds)))
311 input_get_event(&(ivac->input),ivac_parse_command,ivac);
316 int ivac_regular_cb(t_event *event,void *ptr) {
318 /* usual jobs like audio & video transmit ... */
323 int ivac_parse_command(t_input *input,void *ptr) {
330 char c_str[IVAC_CONSOLE_STRING_LEN];
331 char arg[IVAC_ARG_COUNT][IVAC_ARG_LEN];
332 char debug_string[128];
338 /* refresh prompt content only */
339 ivac_display_prompt_content(ivac);
340 ivac_display_draw(ivac);
343 ivac_add_to_monitor(ivac,"nach display refresh");
347 ivac_add_to_monitor(ivac,"nach display refresh");
352 strcpy(debug_string,"hahahaha!");
353 ivac_add_to_monitor(ivac,debug_string);
356 /* parse command routines */
357 if(data[input->c_count-1]=='\n' || data[input->c_count-1]=='\r') {
360 ivac_add_to_monitor(ivac,"got newline or carriage return!");
363 /* delete console string + args */
364 memset(c_str,0,IVAC_CONSOLE_STRING_LEN);
365 for(j=0;j<IVAC_ARG_COUNT;j++) memset(arg[j],0,IVAC_ARG_LEN);
369 while(data[len]!='\n') len++;
371 while((i<len) && (j<IVAC_ARG_COUNT)) {
373 while((data[i+k]!=' ')&&(data[i+k]!='\n')) {
380 while(data[i+k]==' ') k++;
385 /* parse command aka arg[0] */
386 if(!(strncmp(arg[0],"quit",4))) {
388 sprintf(c_str,"ivac shutdown ...");
389 for(i=0;i<MAX_CONNECTIONS;i++) {
390 if(ivac->net.connection[i].status&C_ESTABL) {
391 ivac_send_quit(i,ivac);
392 network_close(&(ivac->net),i);
397 if(!(strncmp(arg[0],"set",3))) {
399 if((arg[1][0]>='0')&&(arg[1][0]<='9')) channel=atoi(arg[1]);
401 if(!(strncmp(arg[1],"name",4))) {
402 strncpy(ivac->username,arg[2],CHAR_USERNAME);
403 sprintf(c_str,"changed username to %s",ivac->username);
405 else if((channel>=0)&&(channel<MAX_CONNECTIONS)) {
406 i=network_set_connection_info(&(ivac->net),channel,arg[2],atoi(arg[3]));
408 sprintf(c_str,"channel %02d: connection in use",channel);
410 sprintf(c_str,"channel %02d: set connection info",channel);
411 } else snprintf(c_str,IVAC_CONSOLE_STRING_LEN,"unknown argument: '%s'",
414 if(!(strncmp(arg[0],"connect",7))) {
416 if((arg[1][0]>='0')&&(arg[1][0]<='9')) channel=atoi(arg[1]);
418 if((channel>=0)&&(channel<MAX_CONNECTIONS)) {
419 i=network_connect(&(ivac->net),channel);
421 sprintf(c_str,"channel %02d: connection in use",channel);
422 else if(i==N_E_NO_INFO)
423 sprintf(c_str,"channel %02d: channel not configured",channel);
425 sprintf(c_str,"channel %02d: connected to %s:%d",channel,
426 ivac->net.connection[channel].ip,
427 ivac->net.connection[channel].port);
428 event_math(ivac->net.connection[channel].fd,&(ivac->event),READ,ADD);
429 ivac_send_info(channel,ivac);
432 else sprintf(c_str,"invalid argument: '%s'",arg[1]);
434 if(!(strncmp(arg[0],"close",5))) {
436 if((arg[1][0]>='0')&&(arg[1][0]<='9')) channel=atoi(arg[1]);
438 if((channel>=0)&&(channel<MAX_CONNECTIONS)) {
439 if(ivac->net.connection[channel].status&C_ESTABL) {
440 ivac_send_quit(channel,ivac);
441 event_math(ivac->net.connection[channel].fd,&(ivac->event),
443 network_close(&(ivac->net),channel);
444 sprintf(c_str,"channel %02d: connection closed",channel);
445 memset(&(ivac->challenger[channel]),0,sizeof(t_challenger));
448 sprintf(c_str,"channel %02d: no active connection",channel);
450 else sprintf(c_str,"invalid argument: '%s'",arg[1]);
452 if(!(strncmp(arg[0],"select",6))) {
454 if((arg[1][0]>='0')&&(arg[1][0]<='9')) channel=atoi(arg[1]);
456 if((channel>=0)&&(channel<MAX_CONNECTIONS)) {
457 network_select(&(ivac->net),channel);
458 sprintf(c_str,"selected channel %d",channel);
460 else if(arg[1][0]=='*') {
461 network_select(&(ivac->net),MAX_CONNECTIONS);
462 strcpy(c_str,"selected all channels");
464 else sprintf(c_str,"invalid argument: '%s'",arg[1]);
466 if(!(strncmp(arg[0],"deselect",8))) {
468 if((arg[1][0]>='0')&&(arg[1][0]<='9')) channel=atoi(arg[1]);
470 if((channel>=0)&&(channel<MAX_CONNECTIONS)) {
471 network_deselect(&(ivac->net),channel);
472 sprintf(c_str,"deselected channel %d",channel);
474 else if(arg[1][0]=='*') {
475 network_deselect(&(ivac->net),MAX_CONNECTIONS);
476 strcpy(c_str,"deselected all channels");
478 else sprintf(c_str,"invalid argument: '%s'",arg[1]);
482 snprintf(c_str,IVAC_CONSOLE_STRING_LEN,"unknown command: '%s'",arg[0]);
484 /* call network functions */
485 network_manage_connection(&(ivac->net));
487 /* add console string to console buffer */
488 ivac_add_to_monitor(ivac,c_str);
490 /* refresh whole display content */
491 ivac_display_content(ivac);
492 ivac_display_draw(ivac);
494 /* delete input content buffer + reset counter */
495 memset(input->content,0,input->c_count-1);
500 ivac_add_to_monitor(ivac,"debug: end of parse function");
505 int ivac_display_head(t_ivac *ivac) {
507 display_line(&(ivac->display),0,0,ivac->display.max_x,0,'#');
508 display_string(&(ivac->display),(ivac->display.max_x-strlen(PROG_NAME))/2,1,
509 PROG_NAME,strlen(PROG_NAME));
510 display_line(&(ivac->display),0,2,ivac->display.max_x,2,'#');
515 int ivac_display_box(t_ivac *ivac) {
517 display_line(&(ivac->display),0,3,ivac->display.max_x,3,'-');
518 display_line(&(ivac->display),0,5,ivac->display.max_x,5,'-');
519 display_string(&(ivac->display),0,6,"connections:",12);
520 display_line(&(ivac->display),
521 0,ivac->display.max_y-IVAC_PROMPT_LEN-IVAC_CONSOLE_LEN-1,
523 ivac->display.max_y-IVAC_PROMPT_LEN-IVAC_CONSOLE_LEN-1,'-');
528 int ivac_display_box_content(t_ivac *ivac) {
531 char string[MAX_BOX_CHARS];
533 /* prepare challenger names */
534 for(channel=0;channel<MAX_CONNECTIONS;channel++)
535 if(ivac->challenger[channel].name[0]==0)
536 strcpy(ivac->challenger[channel].name,"<empty>");
538 sprintf(string,"username: %s, capabilities: %02x|%04x",
539 ivac->username,ivac->g_cap,ivac->av_cap);
540 display_string(&(ivac->display),0,4,string,strlen(string));
542 for(channel=0;channel<MAX_CONNECTIONS;channel++) {
543 if(ivac->net.connection[channel].status&C_INFO_A) {
544 snprintf(string,MAX_BOX_CHARS,
545 "channel %02d: %s:%d, status: %02x, name: %s\n",channel,
546 ivac->net.connection[channel].ip,
547 ivac->net.connection[channel].port,
548 ivac->net.connection[channel].status,
549 ivac->challenger[channel].name);
550 display_string(&(ivac->display),0,IVAC_HEAD_LEN+4+channel,
551 string,strlen(string));
558 int ivac_display_console(t_ivac *ivac) {
560 display_string(&(ivac->display),
561 0,ivac->display.max_y-IVAC_PROMPT_LEN-IVAC_CONSOLE_LEN,
562 "console messages:",17);
567 int ivac_display_console_content(t_ivac *ivac) {
570 char string[IVAC_CONSOLE_STRING_LEN];
572 for(i=0;i<IVAC_CONSOLE_LEN;i++) {
573 snprintf(string,IVAC_CONSOLE_STRING_LEN,"%s\n",ivac->console[i]);
574 display_string(&(ivac->display),
575 0,ivac->display.max_y-IVAC_PROMPT_LEN-IVAC_CONSOLE_LEN+1+i,
576 string,strlen(string));
582 int ivac_display_prompt(t_ivac *ivac) {
584 display_string(&(ivac->display),0,ivac->display.max_y-1,"prompt: ",8);
589 int ivac_display_prompt_content(t_ivac *ivac) {
591 char string[IVAC_CONSOLE_STRING_LEN];
594 for(count=0;count<ivac->input.c_count;count++)
595 string[count]=ivac->input.content[count];
596 for(;count<ivac->display.max_x-8;count++) string[count]=' ';
597 display_string(&(ivac->display),8,ivac->display.max_y-1,
598 string,ivac->display.max_x);
603 int ivac_display_content(t_ivac *ivac) {
605 /* display box content */
606 ivac_display_box_content(ivac);
608 /* display console content */
609 ivac_display_console_content(ivac);
611 /* display prompt content */
612 ivac_display_prompt_content(ivac);
617 int ivac_display(t_ivac *ivac) {
619 /* display static head */
620 ivac_display_head(ivac);
622 /* display static box */
623 ivac_display_box(ivac);
625 /* display static console */
626 ivac_display_console(ivac);
628 /* display command prompt */
629 ivac_display_prompt(ivac);
634 int ivac_display_draw(t_ivac *ivac) {
636 display_draw(&(ivac->display));
637 display_set_cursor(&(ivac->display),ivac->input.c_count+8,
638 ivac->display.max_y-1);
643 int ivac_add_to_monitor(t_ivac *ivac,char *msg) {
647 for(i=0;i<IVAC_CONSOLE_LEN-1;i++)
648 memcpy(ivac->console[i],ivac->console[i+1],IVAC_CONSOLE_STRING_LEN);
649 memcpy(ivac->console[IVAC_CONSOLE_LEN-1],msg,IVAC_CONSOLE_STRING_LEN);
651 ivac_display_console_content(ivac);
652 ivac_display_draw(ivac);