44e831c8b649ad08a996269ac48ec9a27907e639
[my-code/ivac.git] / src / ivac.c
1 /* ivac.c -- main ivac file
2  *
3  * author: hackbard@hackdaworld.dyndns.org
4  *         frank.zirkelbach@physik.uni-augsburg.de
5  *
6  * Copyright (C) 2004 Frank Zirkelbach
7  *
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.
12  *
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.
17  *
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
21  *
22  */
23
24 #include "ivac.h"
25
26 int usage(void) {
27
28   puts("");
29   puts("usage: ivac <options>");
30   puts("");
31   puts("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");
38   puts("");
39
40   return SUCCESS;
41 }
42
43 int main(int argc,char **argv) {
44
45   /* TESTING BY NOW */
46
47   t_ivac ivac;
48   int i;
49
50   /* default values */
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");
56  
57   /* parse argv and change default values */
58   for(i=1;i<argc;i++) {
59     if(argv[i][0]=='-') {
60       switch(argv[i][1]) {
61         case 'h':
62           usage();
63           return SUCCESS;
64         case 'n':
65           strncpy(ivac.username,argv[++i],CHAR_USERNAME);
66           break;
67         case 'p':
68           ivac.net.l_port=atoi(argv[++i]);
69           break;
70         case 'u':
71           ivac.net.l_udp_port=atoi(argv[++i]);
72           break;
73         case 'd':
74           strncpy(ivac.audio.dsp_dev,argv[++i],MAX_CHAR_DEVICE);
75           break;
76         case 'i':
77           strncpy(ivac.net.nic,argv[++i],MAX_NIC_DEVICE);
78         default:
79           usage();
80           return ERROR;
81       }
82     }
83     else usage();
84   }
85
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);
92
93   /* set capabilities (futur: set by check routines) */
94   ivac.g_cap=NETWORK;
95   ivac.av_cap=AUDIO|VIDEO|DUPLEX;
96
97   /* set event timeout */
98   ivac.event.timeout.tv_sec=IVAC_S_SEC;
99   ivac.event.timeout.tv_usec=IVAC_S_USEC;
100   /* event init */
101   event_init(&(ivac.event));
102
103   /* input init */
104   ivac.input.mode=CONTENT_BUFFER;
105   input_init(&(ivac.input));
106
107   /* network init */
108   if(network_init(&(ivac.net))==N_ERROR) {
109     printf("[ivac] use 'fuser -n tcp %d' to determine the process to kill!\n",
110            ivac.net.l_port);
111     ivac_shutdown(&ivac);
112     return ERROR;
113   }
114   network_udp_listen_init(&(ivac.net));
115
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);
120
121   /* display init */
122   display_init(&(ivac.display));
123
124   /* use hardcoded audio settings by now */
125   ivac.audio.fmt=BIT_8;
126   ivac.audio.channels=MONO;
127   ivac.audio.speed=8000;
128   /* audio init */
129   audio_init(&(ivac.audio));
130   audio_setup(&(ivac.audio));
131
132   /* display */
133   ivac_display(&(ivac.display));
134
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);
137
138   return SUCCESS;
139 }
140
141 int ivac_shutdown(t_ivac *ivac) {
142
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));
149
150   return SUCCESS;
151 }
152
153 int ivac_send_info(int channel,t_ivac *ivac) {
154
155   char data[SEND_N_MAX];
156   int size;
157
158   size=strlen(ivac->username);
159
160   data[0]=IVAC_SEND_NAME;
161   data[1]=size;
162   strncpy(data+2,ivac->username,size);
163   size+=2;
164
165   data[size]=IVAC_SEND_G_CAP;
166   data[size+1]=1;
167   data[size+2]=ivac->g_cap;
168   size+=3;
169
170   data[size]=IVAC_SEND_AV_CAP;
171   data[size+1]=2;
172   data[size+2]=(ivac->av_cap)>>8;
173   data[size+3]=(ivac->av_cap)&0xff;
174   size+=4;
175
176   if(network_send(ivac->net.connection[channel].fd,data,size)==N_ERROR) {
177     puts("[ivac] ivac_send_info failed");
178     return ERROR;
179   }
180
181   return SUCCESS;
182 }
183
184 int ivac_send_quit(int channel,t_ivac *ivac) {
185
186   char data[7]; /* one more for \0 */
187
188   data[0]=IVAC_SEND_QUIT;
189   data[1]=4;
190   strcpy(data+2,"quit");
191
192   if(network_send(ivac->net.connection[channel].fd,data,6)==N_ERROR) {
193     puts("[ivac] ivac_send_quit failed");
194     return ERROR;
195   }
196
197   return SUCCESS;
198 }
199
200 int ivac_receive_info(int channel,t_ivac *ivac) {
201
202   char data[SEND_N_MAX];
203   char c_str[IVAC_CONSOLE_STRING_LEN];
204   int count,length;
205
206   count=0;
207
208   if((length=network_receive(ivac->net.connection[channel].fd,
209                              data,SEND_N_MAX))==N_ERROR) {
210     puts("[ivac] ivac_receive_info failed");
211     return ERROR;
212   }
213
214   while(length-count) {
215     switch(data[count]) {
216       case IVAC_SEND_NAME:
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);
220         break;
221       case IVAC_SEND_G_CAP:
222         ivac->challenger[channel].g_cap=data[count+2];
223         count+=3;
224         break;
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];
228         count+=4;
229         break;
230       case IVAC_SEND_QUIT:
231         if(!(strncmp(data+count+2,"quit",data[1])))
232           sprintf(c_str,"channel %02d: connection closed by remote host",
233                   channel);
234           event_math(ivac->net.connection[channel].fd,&(ivac->event),
235                      READ,REMOVE);
236           network_close(&(ivac->net),channel);
237           memset(&(ivac->challenger[channel]),0,sizeof(t_challenger));
238           ivac_add_to_monitor(ivac,c_str);
239           count+=6;
240           break;
241       default:
242         sprintf(c_str,"ivac_receive_info, unknown character: 0x%02x\n",
243                data[count]);
244         ivac_add_to_monitor(ivac,c_str);
245         return ERROR;
246         break;
247     }
248   }
249
250   return length;
251 }
252
253 int ivac_event_cb(t_event *event,void *ptr) {
254  
255   t_ivac *ivac;
256   int channel;
257   char c_str[IVAC_CONSOLE_STRING_LEN];
258
259   ivac=(t_ivac *)ptr;
260
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");
268     else {
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);
275     }
276     ivac_display_content(ivac);
277   }
278
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);
289         }
290         ivac_display_content(ivac);
291       }
292     }
293   } 
294
295   /* user interaction */
296   if(FD_ISSET(0,&(event->rfds)))
297     input_get_event(&(ivac->input),ivac_parse_command,ivac);
298
299   return SUCCESS;
300 }
301
302 int ivac_regular_cb(t_event *event,void *ptr) {
303
304   /* usual jobs like audio & video transmit ... */
305
306   return SUCCESS;
307 }
308
309 int ivac_parse_command(t_input *input,void *ptr) {
310
311   t_ivac *ivac;
312   int i,j,k;
313   int len;
314   int channel;
315   char *data,valid;
316   char c_str[IVAC_CONSOLE_STRING_LEN];
317   char arg[IVAC_ARG_COUNT][IVAC_ARG_LEN];
318
319   ivac=(t_ivac *)ptr;
320   data=input->content;
321   valid=0;
322
323   /* refresh prompt content only! */
324   ivac_display_prompt_content(ivac);
325
326   /* parse command routines */
327   if(data[input->c_count-1]=='\n') {
328
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);
332
333     /* get args */
334     len=0;
335     while(data[len]!='\n') len++;
336     i=0; j=0;
337     while((i<len) && (j<IVAC_ARG_COUNT)) {
338       k=0;
339       while((data[i+k]!=' ')&&(data[i+k]!='\n')) {
340         arg[j][k]=data[i+k];
341         k++;
342       }
343       arg[j][k]='\0';
344       j++;
345       /* skip all ' ' */
346       while(data[i+k]==' ') k++;
347       i+=k;
348     }
349
350     /* parse command  aka arg[0] */
351     if(!(strncmp(arg[0],"quit",4))) {
352       valid=1;
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);
358         }
359       }
360       ivac_shutdown(ivac);
361     }
362     if(!(strncmp(arg[0],"set",3))) {
363       valid=1;
364       if((arg[1][0]>='0')&&(arg[1][0]<='9')) channel=atoi(arg[1]);
365       else channel=-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);
369       }
370       else if((channel>=0)&&(channel<MAX_CONNECTIONS)) {
371         i=network_set_connection_info(&(ivac->net),channel,arg[2],atoi(arg[3]));
372         if(i==N_E_IN_USE)
373           sprintf(c_str,"channel %02d: connection in use",channel);
374         if(i==N_SUCCESS)
375           sprintf(c_str,"channel %02d: set connection info",channel);
376       } else snprintf(c_str,IVAC_CONSOLE_STRING_LEN,"unknown argument: '%s'",
377                       arg[1]);
378     }
379     if(!(strncmp(arg[0],"connect",7))) {
380       valid=1;
381       if((arg[1][0]>='0')&&(arg[1][0]<='9')) channel=atoi(arg[1]);
382       else channel=-1;
383       if((channel>=0)&&(channel<MAX_CONNECTIONS)) {
384         i=network_connect(&(ivac->net),channel);
385         if(i==N_E_IN_USE)
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);
389         else {
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);
395         }
396       }
397       else sprintf(c_str,"invalid argument: '%s'",arg[1]);
398     } 
399     if(!(strncmp(arg[0],"close",5))) {
400       valid=1;
401       if((arg[1][0]>='0')&&(arg[1][0]<='9')) channel=atoi(arg[1]);
402       else channel=-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),
407                      READ,REMOVE);
408           network_close(&(ivac->net),channel);
409           sprintf(c_str,"channel %02d: connection closed",channel);
410           memset(&(ivac->challenger[channel]),0,sizeof(t_challenger));
411         }
412         else
413           sprintf(c_str,"channel %02d: no active connection",channel);
414       }
415       else sprintf(c_str,"invalid argument: '%s'",arg[1]);
416     }
417     if(!(strncmp(arg[0],"select",6))) {
418       valid=1;
419       if((arg[1][0]>='0')&&(arg[1][0]<='9')) channel=atoi(arg[1]);
420       else channel=-1;
421       if((channel>=0)&&(channel<MAX_CONNECTIONS)) {
422         network_select(&(ivac->net),channel);
423         sprintf(c_str,"selected channel %d",channel);
424       }
425       else if(arg[1][0]='*') {
426         network_select(&(ivac->net),MAX_CONNECTIONS);
427         strcpy(c_str,"selected all channels");
428       }
429       else sprintf(c_str,"invalid argument: '%s'",arg[1]);
430     }
431     if(!(strncmp(arg[0],"deselect",8))) {
432       valid=1;
433       if((arg[1][0]>='0')&&(arg[1][0]<='9')) channel=atoi(arg[1]);
434       else channel=-1;
435       if((channel>=0)&&(channel<MAX_CONNECTIONS)) {
436         network_deselect(&(ivac->net),channel);
437         sprintf(c_str,"deselected channel %d",channel);
438       }
439       else if(arg[1][0]='*') {
440         network_deselect(&(ivac->net),MAX_CONNECTIONS);
441         strcpy(c_str,"deselected all channels");
442       }
443       else sprintf(c_str,"invalid argument: '%s'",arg[1]);
444     }
445
446     if(!valid)
447       snprintf(c_str,IVAC_CONSOLE_STRING_LEN,"unknown command: '%s'",arg[0]);
448
449     /* call network functions */
450     network_manage_connection(&(ivac->net));
451
452     /* add console string to console buffer */
453     ivac_add_to_monitor(ivac,c_str);
454
455     /* refresh whole display content */
456     ivac_display_content(ivac);
457
458     /* delete content buffer + reset counter */
459     memset(input->content,0,input->c_count-1);
460     input->c_count=0;
461
462   }
463
464   return SUCCESS;
465 }
466
467 int ivac_display_head(t_display *display) {
468
469   puts("#########################################################");
470   puts("##### ivac - -  Copyright (C) 2004 Frank Zirkelbach #####");
471   puts("#########################################################");
472
473   return SUCCESS;
474 }
475
476 int ivac_display_box(t_display *display) {
477  
478   return SUCCESS;
479 }
480
481 int ivac_display_box_content(t_ivac *ivac) {
482
483   int channel;
484
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>");
489
490   return SUCCESS;
491 }
492
493 int ivac_display_console(t_display *display) {
494
495   return SUCCESS;
496 }
497
498 int ivac_display_console_content(t_ivac *ivac) {
499
500   int i;
501
502   for(i=0;i<IVAC_CONSOLE_LEN;i++)
503     printf("[ivac] console line %d: %s\n",i,ivac->console[i]);
504
505   return SUCCESS;
506 }
507
508 int ivac_display_prompt(t_display *display) {
509
510   return SUCCESS;
511 }
512
513 int ivac_display_prompt_content(t_ivac *ivac) {
514
515   printf("%c",ivac->input.content[ivac->input.c_count-1]);
516   fflush(NULL);
517
518   return SUCCESS;
519 }
520
521 int ivac_display(t_display *display) {
522
523   int x,y;
524
525   /* display head */
526   ivac_display_head(display);
527
528   /* display box */
529   ivac_display_box(display);
530
531   /* display console */
532   ivac_display_console(display);
533
534   /* display command prompt */
535   ivac_display_prompt(display);
536
537   return SUCCESS;
538 }
539
540 int ivac_display_content(t_ivac *ivac) {
541
542   /* display box content */
543   ivac_display_box_content(ivac);
544
545   /* display console content */
546   ivac_display_console_content(ivac);
547
548   /* display prompt content */
549   ivac_display_prompt_content(ivac);
550
551   return SUCCESS;
552 }
553
554 int ivac_add_to_monitor(t_ivac *ivac,char *msg) {
555
556   int i;
557
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);
561
562   return SUCCESS;
563 }