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 event_init(&(ivac.event));
104 ivac.input.mode=CONTENT_BUFFER;
105 input_init(&(ivac.input));
108 if(network_init(&(ivac.net))==N_ERROR) {
109 printf("[ivac] use 'fuser -n tcp %d' to determine the process to kill!\n",
111 ivac_shutdown(&ivac);
114 network_udp_listen_init(&(ivac.net));
116 /* add listening port(s) + stdin to (read) event system */
117 event_math(ivac.net.l_fd,&(ivac.event),READ,ADD);
118 event_math(ivac.net.l_udp_fd,&(ivac.event),READ,ADD);
119 event_math(0,&(ivac.event),READ,ADD);
122 display_init(&(ivac.display));
124 /* use hardcoded audio settings by now */
125 ivac.audio.fmt=BIT_8;
126 ivac.audio.channels=MONO;
127 ivac.audio.speed=8000;
129 audio_init(&(ivac.audio));
130 audio_setup(&(ivac.audio));
133 ivac_display(&(ivac.display));
135 /* start event system - callbacks used: ivac_event_cb + ivac_regular_cb */
136 event_start(&(ivac.event),(void *)&ivac,ivac_event_cb,ivac_regular_cb);
141 int ivac_shutdown(t_ivac *ivac) {
143 network_shutdown(&(ivac->net));
144 network_udp_shutdown(&(ivac->net));
145 input_shutdown(&(ivac->input));
146 event_stop(&(ivac->event));
147 audio_shutdown(&(ivac->audio));
148 display_shutdown(&(ivac->display));
153 int ivac_send_info(int channel,t_ivac *ivac) {
155 char data[SEND_N_MAX];
158 size=strlen(ivac->username);
160 data[0]=IVAC_SEND_NAME;
162 strncpy(data+2,ivac->username,size);
165 data[size]=IVAC_SEND_G_CAP;
167 data[size+2]=ivac->g_cap;
170 data[size]=IVAC_SEND_AV_CAP;
172 data[size+2]=(ivac->av_cap)>>8;
173 data[size+3]=(ivac->av_cap)&0xff;
176 if(network_send(ivac->net.connection[channel].fd,data,size)==N_ERROR) {
177 puts("[ivac] ivac_send_info failed");
184 int ivac_send_quit(int channel,t_ivac *ivac) {
186 char data[7]; /* one more for \0 */
188 data[0]=IVAC_SEND_QUIT;
190 strcpy(data+2,"quit");
192 if(network_send(ivac->net.connection[channel].fd,data,6)==N_ERROR) {
193 puts("[ivac] ivac_send_quit failed");
200 int ivac_receive_info(int channel,t_ivac *ivac) {
202 char data[SEND_N_MAX];
203 char c_str[IVAC_CONSOLE_STRING_LEN];
208 if((length=network_receive(ivac->net.connection[channel].fd,
209 data,SEND_N_MAX))==N_ERROR) {
210 puts("[ivac] ivac_receive_info failed");
214 while(length-count) {
215 switch(data[count]) {
217 strncpy(ivac->challenger[channel].name,data+count+2,data[count+1]);
218 ivac->challenger[channel].name[data[count+1]]='\0';
219 count+=(data[count+1]+2);
221 case IVAC_SEND_G_CAP:
222 ivac->challenger[channel].g_cap=data[count+2];
225 case IVAC_SEND_AV_CAP:
226 ivac->challenger[channel].av_cap=data[count+2]<<8;
227 ivac->challenger[channel].av_cap|=data[count+3];
231 if(!(strncmp(data+count+2,"quit",data[1])))
232 sprintf(c_str,"channel %02d: connection closed by remote host",
234 event_math(ivac->net.connection[channel].fd,&(ivac->event),
236 network_close(&(ivac->net),channel);
237 memset(&(ivac->challenger[channel]),0,sizeof(t_challenger));
238 ivac_add_to_monitor(ivac,c_str);
242 sprintf(c_str,"ivac_receive_info, unknown character: 0x%02x\n",
244 ivac_add_to_monitor(ivac,c_str);
253 int ivac_event_cb(t_event *event,void *ptr) {
257 char c_str[IVAC_CONSOLE_STRING_LEN];
261 /* incoming connection -- first contact => send info */
262 if(FD_ISSET(ivac->net.l_fd,&(event->rfds))) {
263 channel=network_manage_incoming(&(ivac->net));
264 if(channel==N_E_ACCEPT)
265 sprintf(c_str,"accept failed");
266 else if(channel==N_E_MAXC)
267 sprintf(c_str,"maximum connections reached");
269 sprintf(c_str,"connection from %s port %d on channel %d",
270 ivac->net.connection[channel].ip,
271 ivac->net.connection[channel].port,channel);
272 ivac_add_to_monitor(ivac,c_str);
273 event_math(ivac->net.connection[channel].fd,event,READ,ADD);
274 ivac_send_info(channel,ivac);
276 ivac_display_content(ivac);
279 /* wait for user info */
280 for(channel=0;channel<MAX_CONNECTIONS;channel++) {
281 if(ivac->net.connection[channel].status&C_ESTABL) {
282 /* remote is sending info */
283 if(FD_ISSET(ivac->net.connection[channel].fd,&(event->rfds))) {
284 if(ivac_receive_info(channel,ivac)==0) {
285 event_math(ivac->net.connection[channel].fd,event,READ,REMOVE);
286 network_close(&(ivac->net),channel);
287 sprintf(c_str,"channel %02d: broken pipe - disconnected",channel);
288 ivac_add_to_monitor(ivac,c_str);
290 ivac_display_content(ivac);
295 /* user interaction */
296 if(FD_ISSET(0,&(event->rfds)))
297 input_get_event(&(ivac->input),ivac_parse_command,ivac);
302 int ivac_regular_cb(t_event *event,void *ptr) {
304 /* usual jobs like audio & video transmit ... */
309 int ivac_parse_command(t_input *input,void *ptr) {
316 char c_str[IVAC_CONSOLE_STRING_LEN];
317 char arg[IVAC_ARG_COUNT][IVAC_ARG_LEN];
323 /* refresh prompt content only! */
324 ivac_display_prompt_content(ivac);
326 /* parse command routines */
327 if(data[input->c_count-1]=='\n') {
329 /* delete console string + args */
330 memset(c_str,0,IVAC_CONSOLE_STRING_LEN);
331 for(j=0;j<IVAC_ARG_COUNT;j++) memset(arg[j],0,IVAC_ARG_LEN);
335 while(data[len]!='\n') len++;
337 while((i<len) && (j<IVAC_ARG_COUNT)) {
339 while((data[i+k]!=' ')&&(data[i+k]!='\n')) {
346 while(data[i+k]==' ') k++;
350 /* parse command aka arg[0] */
351 if(!(strncmp(arg[0],"quit",4))) {
353 sprintf(c_str,"ivac shutdown ...");
354 for(i=0;i<MAX_CONNECTIONS;i++) {
355 if(ivac->net.connection[i].status&C_ESTABL) {
356 ivac_send_quit(i,ivac);
357 network_close(&(ivac->net),i);
362 if(!(strncmp(arg[0],"set",3))) {
364 if((arg[1][0]>='0')&&(arg[1][0]<='9')) channel=atoi(arg[1]);
366 if(!(strncmp(arg[1],"name",4))) {
367 strncpy(ivac->username,arg[2],CHAR_USERNAME);
368 sprintf(c_str,"changed username to %s",ivac->username);
370 else if((channel>=0)&&(channel<MAX_CONNECTIONS)) {
371 i=network_set_connection_info(&(ivac->net),channel,arg[2],atoi(arg[3]));
373 sprintf(c_str,"channel %02d: connection in use",channel);
375 sprintf(c_str,"channel %02d: set connection info",channel);
376 } else snprintf(c_str,IVAC_CONSOLE_STRING_LEN,"unknown argument: '%s'",
379 if(!(strncmp(arg[0],"connect",7))) {
381 if((arg[1][0]>='0')&&(arg[1][0]<='9')) channel=atoi(arg[1]);
383 if((channel>=0)&&(channel<MAX_CONNECTIONS)) {
384 i=network_connect(&(ivac->net),channel);
386 sprintf(c_str,"channel %02d: connection in use",channel);
387 else if(i==N_E_NO_INFO)
388 sprintf(c_str,"channel %02d: channel not configured",channel);
390 sprintf(c_str,"channel %02d: connected to %s:%d",channel,
391 ivac->net.connection[channel].ip,
392 ivac->net.connection[channel].port);
393 event_math(ivac->net.connection[channel].fd,&(ivac->event),READ,ADD);
394 ivac_send_info(channel,ivac);
397 else sprintf(c_str,"invalid argument: '%s'",arg[1]);
399 if(!(strncmp(arg[0],"close",5))) {
401 if((arg[1][0]>='0')&&(arg[1][0]<='9')) channel=atoi(arg[1]);
403 if((channel>=0)&&(channel<MAX_CONNECTIONS)) {
404 if(ivac->net.connection[channel].status&C_ESTABL) {
405 ivac_send_quit(channel,ivac);
406 event_math(ivac->net.connection[channel].fd,&(ivac->event),
408 network_close(&(ivac->net),channel);
409 sprintf(c_str,"channel %02d: connection closed",channel);
410 memset(&(ivac->challenger[channel]),0,sizeof(t_challenger));
413 sprintf(c_str,"channel %02d: no active connection",channel);
415 else sprintf(c_str,"invalid argument: '%s'",arg[1]);
417 if(!(strncmp(arg[0],"select",6))) {
419 if((arg[1][0]>='0')&&(arg[1][0]<='9')) channel=atoi(arg[1]);
421 if((channel>=0)&&(channel<MAX_CONNECTIONS)) {
422 network_select(&(ivac->net),channel);
423 sprintf(c_str,"selected channel %d",channel);
425 else if(arg[1][0]='*') {
426 network_select(&(ivac->net),MAX_CONNECTIONS);
427 strcpy(c_str,"selected all channels");
429 else sprintf(c_str,"invalid argument: '%s'",arg[1]);
431 if(!(strncmp(arg[0],"deselect",8))) {
433 if((arg[1][0]>='0')&&(arg[1][0]<='9')) channel=atoi(arg[1]);
435 if((channel>=0)&&(channel<MAX_CONNECTIONS)) {
436 network_deselect(&(ivac->net),channel);
437 sprintf(c_str,"deselected channel %d",channel);
439 else if(arg[1][0]='*') {
440 network_deselect(&(ivac->net),MAX_CONNECTIONS);
441 strcpy(c_str,"deselected all channels");
443 else sprintf(c_str,"invalid argument: '%s'",arg[1]);
447 snprintf(c_str,IVAC_CONSOLE_STRING_LEN,"unknown command: '%s'",arg[0]);
449 /* call network functions */
450 network_manage_connection(&(ivac->net));
452 /* add console string to console buffer */
453 ivac_add_to_monitor(ivac,c_str);
455 /* refresh whole display content */
456 ivac_display_content(ivac);
458 /* delete content buffer + reset counter */
459 memset(input->content,0,input->c_count-1);
467 int ivac_display_head(t_display *display) {
469 puts("#########################################################");
470 puts("##### ivac - - Copyright (C) 2004 Frank Zirkelbach #####");
471 puts("#########################################################");
476 int ivac_display_box(t_display *display) {
481 int ivac_display_box_content(t_ivac *ivac) {
485 /* prepare challenger names */
486 for(channel=0;channel<MAX_CONNECTIONS;channel++)
487 if(ivac->challenger[channel].name[0]==0)
488 strcpy(ivac->challenger[channel].name,"<empty>");
493 int ivac_display_console(t_display *display) {
498 int ivac_display_console_content(t_ivac *ivac) {
502 for(i=0;i<IVAC_CONSOLE_LEN;i++)
503 printf("[ivac] console line %d: %s\n",i,ivac->console[i]);
508 int ivac_display_prompt(t_display *display) {
513 int ivac_display_prompt_content(t_ivac *ivac) {
515 printf("%c",ivac->input.content[ivac->input.c_count-1]);
521 int ivac_display(t_display *display) {
526 ivac_display_head(display);
529 ivac_display_box(display);
531 /* display console */
532 ivac_display_console(display);
534 /* display command prompt */
535 ivac_display_prompt(display);
540 int ivac_display_content(t_ivac *ivac) {
542 /* display box content */
543 ivac_display_box_content(ivac);
545 /* display console content */
546 ivac_display_console_content(ivac);
548 /* display prompt content */
549 ivac_display_prompt_content(ivac);
554 int ivac_add_to_monitor(t_ivac *ivac,char *msg) {
558 for(i=0;i<IVAC_CONSOLE_LEN-1;i++)
559 memcpy(ivac->console[i],ivac->console[i+1],IVAC_CONSOLE_STRING_LEN);
560 memcpy(ivac->console[IVAC_CONSOLE_LEN-1],msg,IVAC_CONSOLE_STRING_LEN);