]> hackdaworld.org Git - my-code/ivac.git/blob - src/ivac.c
corrected network udp functions
[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 // #define USE_GTK
25
26 #include "ivac.h"
27
28 int usage(void) {
29
30   puts("");
31   puts("usage: ivac <options>");
32   puts("");
33   puts("options:");
34   puts("-h \t\t show this help");
35   puts("-n <name> \t specify your name");
36   puts("-p <port> \t specify port to listen for incoming connections");
37   puts("-d <device> \t specify audio device");
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   strcpy(ivac.audio.dsp_dev,SOUND_DEVICE);
54  
55   /* parse argv and change default values */
56   for(i=1;i<argc;i++) {
57     if(argv[i][0]=='-') {
58       switch(argv[i][1]) {
59         case 'h':
60           usage();
61           return SUCCESS;
62         case 'n':
63           strncpy(ivac.username,argv[++i],CHAR_USERNAME);
64           break;
65         case 'p':
66           ivac.net.l_port=atoi(argv[++i]);
67           break;
68         case 'd':
69           strncpy(ivac.audio.dsp_dev,argv[++i],MAX_CHAR_DEVICE);
70           break;
71         default:
72           usage();
73           return ERROR;
74       }
75     }
76     else usage();
77   }
78
79   /* clear challenger struct */
80   for(i=0;i<MAX_CONNECTIONS;i++)
81     memset(&(ivac.challenger[i]),0,sizeof(t_challenger));
82   /* zero console buffer */
83   for(i=0;i<IVAC_CONSOLE_LEN;i++)
84     memset(ivac.console[i],0,IVAC_CONSOLE_STRING_LEN);
85
86   /* set capabilities (futur: set by check routines) */
87   ivac.g_cap=NETWORK;
88   ivac.av_cap=AUDIO|VIDEO|DUPLEX;
89
90   /* set event timeout */
91   ivac.event.timeout.tv_sec=IVAC_S_SEC;
92   ivac.event.timeout.tv_usec=IVAC_S_USEC;
93   /* event init */
94   event_init(&(ivac.event));
95
96   /* input init */
97   ivac.input.mode=CONTENT_BUFFER;
98   input_init(&(ivac.input));
99
100   /* network init */
101   if(network_init(&(ivac.net))==N_ERROR) {
102     printf("[ivac] use 'fuser -n tcp %d' to determine the process to kill!\n",
103            ivac.net.l_port);
104     ivac_shutdown(&ivac);
105     return ERROR;
106   }
107
108   /* add listening port + stdin to (read) event system */
109   event_math(ivac.net.l_fd,&(ivac.event),READ,ADD);
110   event_math(0,&(ivac.event),READ,ADD);
111
112   /* display init */
113   display_init(&(ivac.display));
114
115   /* use hardcoded audio settings by now */
116   ivac.audio.fmt=BIT_8;
117   ivac.audio.channels=MONO;
118   ivac.audio.speed=8000;
119   /* audio init */
120   audio_init(&(ivac.audio));
121   audio_setup(&(ivac.audio));
122
123   /* display */
124   ivac_display(&(ivac.display));
125
126   /* start event system - callbacks used: ivac_event_cb + ivac_regular_cb */
127   event_start(&(ivac.event),(void *)&ivac,ivac_event_cb,ivac_regular_cb);
128
129   return SUCCESS;
130 }
131
132 int ivac_shutdown(t_ivac *ivac) {
133
134   network_shutdown(&(ivac->net));
135   input_shutdown(&(ivac->input));
136   event_stop(&(ivac->event));
137   audio_shutdown(&(ivac->audio));
138   display_shutdown(&(ivac->display));
139
140   return SUCCESS;
141 }
142
143 int ivac_send_info(int channel,t_ivac *ivac) {
144
145   char data[SEND_N_MAX];
146   int size;
147
148   size=strlen(ivac->username);
149
150   data[0]=IVAC_SEND_NAME;
151   data[1]=size;
152   strncpy(data+2,ivac->username,size);
153   size+=2;
154
155   data[size]=IVAC_SEND_G_CAP;
156   data[size+1]=1;
157   data[size+2]=ivac->g_cap;
158   size+=3;
159
160   data[size]=IVAC_SEND_AV_CAP;
161   data[size+1]=2;
162   data[size+2]=(ivac->av_cap)>>8;
163   data[size+3]=(ivac->av_cap)&0xff;
164   size+=4;
165
166   if(network_send(ivac->net.connection[channel].fd,data,size)==N_ERROR) {
167     puts("[ivac] ivac_send_info failed");
168     return ERROR;
169   }
170
171   return SUCCESS;
172 }
173
174 int ivac_send_quit(int channel,t_ivac *ivac) {
175
176   char data[7]; /* one more for \0 */
177
178   data[0]=IVAC_SEND_QUIT;
179   data[1]=4;
180   strcpy(data+2,"quit");
181
182   if(network_send(ivac->net.connection[channel].fd,data,6)==N_ERROR) {
183     puts("[ivac] ivac_send_quit failed");
184     return ERROR;
185   }
186
187   return SUCCESS;
188 }
189
190 int ivac_receive_info(int channel,t_ivac *ivac) {
191
192   char data[SEND_N_MAX];
193   char c_str[IVAC_CONSOLE_STRING_LEN];
194   int count,length;
195
196   count=0;
197
198   if((length=network_receive(ivac->net.connection[channel].fd,
199                              data,SEND_N_MAX))==N_ERROR) {
200     puts("[ivac] ivac_receive_info failed");
201     return ERROR;
202   }
203
204   while(length-count) {
205     switch(data[count]) {
206       case IVAC_SEND_NAME:
207         strncpy(ivac->challenger[channel].name,data+count+2,data[count+1]);
208         ivac->challenger[channel].name[data[count+1]]='\0';
209         count+=(data[count+1]+2);
210         break;
211       case IVAC_SEND_G_CAP:
212         ivac->challenger[channel].g_cap=data[count+2];
213         count+=3;
214         break;
215       case IVAC_SEND_AV_CAP:
216         ivac->challenger[channel].av_cap=data[count+2]<<8;
217         ivac->challenger[channel].av_cap|=data[count+3];
218         count+=4;
219         break;
220       case IVAC_SEND_QUIT:
221         if(!(strncmp(data+count+2,"quit",data[1])))
222           sprintf(c_str,"channel %02d: connection closed by remote host",
223                   channel);
224           event_math(ivac->net.connection[channel].fd,&(ivac->event),
225                      READ,REMOVE);
226           network_close(&(ivac->net),channel);
227           memset(&(ivac->challenger[channel]),0,sizeof(t_challenger));
228           ivac_add_to_monitor(ivac,c_str);
229           count+=6;
230           break;
231       default:
232         sprintf(c_str,"ivac_receive_info, unknown character: 0x%02x\n",
233                data[count]);
234         ivac_add_to_monitor(ivac,c_str);
235         return ERROR;
236         break;
237     }
238   }
239
240   return length;
241 }
242
243 int ivac_event_cb(t_event *event,void *ptr) {
244  
245   t_ivac *ivac;
246   int channel;
247   char c_str[IVAC_CONSOLE_STRING_LEN];
248
249   ivac=(t_ivac *)ptr;
250
251   /* incoming connection -- first contact => send info */
252   if(FD_ISSET(ivac->net.l_fd,&(event->rfds))) {
253     channel=network_manage_incoming(&(ivac->net));
254     if(channel==N_E_ACCEPT)
255       sprintf(c_str,"accept failed");
256     else if(channel==N_E_MAXC)
257       sprintf(c_str,"maximum connections reached");
258     else {
259       sprintf(c_str,"connection from %s port %d on channel %d",
260               ivac->net.connection[channel].ip,
261               ivac->net.connection[channel].port,channel);
262       ivac_add_to_monitor(ivac,c_str);
263       event_math(ivac->net.connection[channel].fd,event,READ,ADD);
264       ivac_send_info(channel,ivac);
265     }
266     ivac_display_content(ivac);
267   }
268
269   /* wait for user info */
270   for(channel=0;channel<MAX_CONNECTIONS;channel++) {
271     if(ivac->net.connection[channel].status&C_ESTABL) {
272       /* remote is sending info */
273       if(FD_ISSET(ivac->net.connection[channel].fd,&(event->rfds))) {
274         if(ivac_receive_info(channel,ivac)==0) {
275           event_math(ivac->net.connection[channel].fd,event,READ,REMOVE);
276           network_close(&(ivac->net),channel);
277           sprintf(c_str,"channel %02d: broken pipe - disconnected",channel);
278           ivac_add_to_monitor(ivac,c_str);
279         }
280         ivac_display_content(ivac);
281       }
282     }
283   } 
284
285   /* user interaction */
286   if(FD_ISSET(0,&(event->rfds)))
287     input_get_event(&(ivac->input),ivac_parse_command,ivac);
288
289   return SUCCESS;
290 }
291
292 int ivac_regular_cb(t_event *event,void *ptr) {
293
294   /* usual jobs like audio & video transmit ... */
295
296   return SUCCESS;
297 }
298
299 int ivac_parse_command(t_input *input,void *ptr) {
300
301   t_ivac *ivac;
302   int i,j,k;
303   int len;
304   int channel;
305   char *data,valid;
306   char c_str[IVAC_CONSOLE_STRING_LEN];
307   char arg[IVAC_ARG_COUNT][IVAC_ARG_LEN];
308
309   ivac=(t_ivac *)ptr;
310   data=input->content;
311   valid=0;
312
313   /* refresh prompt content only! */
314   ivac_display_prompt_content(ivac);
315
316   /* parse command routines */
317   if(data[input->c_count-1]=='\n') {
318
319     /* delete console string + args */
320     memset(c_str,0,IVAC_CONSOLE_STRING_LEN);
321     for(j=0;j<IVAC_ARG_COUNT;j++) memset(arg[j],0,IVAC_ARG_LEN);
322
323     /* get args */
324     len=0;
325     while(data[len]!='\n') len++;
326     i=0; j=0;
327     while((i<len) && (j<IVAC_ARG_COUNT)) {
328       k=0;
329       while((data[i+k]!=' ')&&(data[i+k]!='\n')) {
330         arg[j][k]=data[i+k];
331         k++;
332       }
333       arg[j][k]='\0';
334       j++;
335       /* skip all ' ' */
336       while(data[i+k]==' ') k++;
337       i+=k;
338     }
339
340     /* parse command  aka arg[0] */
341     if(!(strncmp(arg[0],"quit",4))) {
342       valid=1;
343       sprintf(c_str,"ivac shutdown ...");
344       for(i=0;i<MAX_CONNECTIONS;i++) {
345         if(ivac->net.connection[i].status&C_ESTABL) {
346           ivac_send_quit(i,ivac);
347           network_close(&(ivac->net),i);
348         }
349       }
350       ivac_shutdown(ivac);
351     }
352     if(!(strncmp(arg[0],"set",3))) {
353       valid=1;
354       if((arg[1][0]>='0')&&(arg[1][0]<='9')) channel=atoi(arg[1]);
355       else channel=-1;
356       if(!(strncmp(arg[1],"name",4))) {
357         strncpy(ivac->username,arg[2],CHAR_USERNAME);
358         sprintf(c_str,"changed username to %s",ivac->username);
359       }
360       else if((channel>=0)&&(channel<MAX_CONNECTIONS)) {
361         i=network_set_connection_info(&(ivac->net),channel,arg[2],atoi(arg[3]));
362         if(i==N_E_IN_USE)
363           sprintf(c_str,"channel %02d: connection in use",channel);
364         if(i==N_SUCCESS)
365           sprintf(c_str,"channel %02d: set connection info",channel);
366       } else snprintf(c_str,IVAC_CONSOLE_STRING_LEN,"unknown argument: '%s'",
367                       arg[1]);
368     }
369     if(!(strncmp(arg[0],"connect",7))) {
370       valid=1;
371       if((arg[1][0]>='0')&&(arg[1][0]<='9')) channel=atoi(arg[1]);
372       else channel=-1;
373       if((channel>=0)&&(channel<MAX_CONNECTIONS)) {
374         i=network_connect(&(ivac->net),channel);
375         if(i==N_E_IN_USE)
376           sprintf(c_str,"channel %02d: connection in use",channel);
377         else if(i==N_E_NO_INFO)
378           sprintf(c_str,"channel %02d: channel not configured",channel);
379         else {
380           sprintf(c_str,"channel %02d: connected to %s:%d",channel,
381                   ivac->net.connection[channel].ip,
382                   ivac->net.connection[channel].port);
383           event_math(ivac->net.connection[channel].fd,&(ivac->event),READ,ADD);
384           ivac_send_info(channel,ivac);
385         }
386       }
387       else sprintf(c_str,"invalid argument: '%s'",arg[1]);
388     } 
389     if(!(strncmp(arg[0],"close",5))) {
390       valid=1;
391       if((arg[1][0]>='0')&&(arg[1][0]<='9')) channel=atoi(arg[1]);
392       else channel=-1;
393       if((channel>=0)&&(channel<MAX_CONNECTIONS)) {
394         if(ivac->net.connection[channel].status&C_ESTABL) {
395           ivac_send_quit(channel,ivac);
396           event_math(ivac->net.connection[channel].fd,&(ivac->event),
397                      READ,REMOVE);
398           network_close(&(ivac->net),channel);
399           sprintf(c_str,"channel %02d: connection closed",channel);
400           memset(&(ivac->challenger[channel]),0,sizeof(t_challenger));
401         }
402         else
403           sprintf(c_str,"channel %02d: no active connection",channel);
404       }
405       else sprintf(c_str,"invalid argument: '%s'",arg[1]);
406     }
407     if(!(strncmp(arg[0],"select",6))) {
408       valid=1;
409       if((arg[1][0]>='0')&&(arg[1][0]<='9')) channel=atoi(arg[1]);
410       else channel=-1;
411       if((channel>=0)&&(channel<MAX_CONNECTIONS)) {
412         network_select(&(ivac->net),channel);
413         sprintf(c_str,"selected channel %d",channel);
414       }
415       else if(arg[1][0]='*') {
416         network_select(&(ivac->net),MAX_CONNECTIONS);
417         strcpy(c_str,"selected all channels");
418       }
419       else sprintf(c_str,"invalid argument: '%s'",arg[1]);
420     }
421     if(!(strncmp(arg[0],"deselect",8))) {
422       valid=1;
423       if((arg[1][0]>='0')&&(arg[1][0]<='9')) channel=atoi(arg[1]);
424       else channel=-1;
425       if((channel>=0)&&(channel<MAX_CONNECTIONS)) {
426         network_deselect(&(ivac->net),channel);
427         sprintf(c_str,"deselected channel %d",channel);
428       }
429       else if(arg[1][0]='*') {
430         network_deselect(&(ivac->net),MAX_CONNECTIONS);
431         strcpy(c_str,"deselected all channels");
432       }
433       else sprintf(c_str,"invalid argument: '%s'",arg[1]);
434     }
435
436     if(!valid)
437       snprintf(c_str,IVAC_CONSOLE_STRING_LEN,"unknown command: '%s'",arg[0]);
438
439     /* call network functions */
440     network_manage_connection(&(ivac->net));
441
442     /* add console string to console buffer */
443     ivac_add_to_monitor(ivac,c_str);
444
445     /* refresh whole display content */
446     ivac_display_content(ivac);
447
448     /* delete content buffer + reset counter */
449     memset(input->content,0,input->c_count-1);
450     input->c_count=0;
451
452   }
453
454   return SUCCESS;
455 }
456
457 int ivac_display_head(t_display *display) {
458
459   puts("#########################################################");
460   puts("##### ivac - -  Copyright (C) 2004 Frank Zirkelbach #####");
461   puts("#########################################################");
462
463   return SUCCESS;
464 }
465
466 int ivac_display_box(t_display *display) {
467  
468   return SUCCESS;
469 }
470
471 int ivac_display_box_content(t_ivac *ivac) {
472
473   int channel;
474
475   /* prepare challenger names */
476   for(channel=0;channel<MAX_CONNECTIONS;channel++)
477     if(ivac->challenger[channel].name[0]==0)
478       strcpy(ivac->challenger[channel].name,"<empty>");
479
480   return SUCCESS;
481 }
482
483 int ivac_display_console(t_display *display) {
484
485   return SUCCESS;
486 }
487
488 int ivac_display_console_content(t_ivac *ivac) {
489
490   int i;
491
492   for(i=0;i<IVAC_CONSOLE_LEN;i++)
493     printf("[ivac] console line %d: %s\n",i,ivac->console[i]);
494
495   return SUCCESS;
496 }
497
498 int ivac_display_prompt(t_display *display) {
499
500   return SUCCESS;
501 }
502
503 int ivac_display_prompt_content(t_ivac *ivac) {
504
505   printf("%c",ivac->input.content[ivac->input.c_count-1]);
506   fflush(NULL);
507
508   return SUCCESS;
509 }
510
511 int ivac_display(t_display *display) {
512
513   int x,y;
514
515   /* display head */
516   ivac_display_head(display);
517
518   /* display box */
519   ivac_display_box(display);
520
521   /* display console */
522   ivac_display_console(display);
523
524   /* display command prompt */
525   ivac_display_prompt(display);
526
527   return SUCCESS;
528 }
529
530 int ivac_display_content(t_ivac *ivac) {
531
532   /* display box content */
533   ivac_display_box_content(ivac);
534
535   /* display console content */
536   ivac_display_console_content(ivac);
537
538   /* display prompt content */
539   ivac_display_prompt_content(ivac);
540
541   return SUCCESS;
542 }
543
544 int ivac_add_to_monitor(t_ivac *ivac,char *msg) {
545
546   int i;
547
548   for(i=0;i<IVAC_CONSOLE_LEN-1;i++)
549     memcpy(ivac->console[i],ivac->console[i+1],IVAC_CONSOLE_STRING_LEN);
550   memcpy(ivac->console[IVAC_CONSOLE_LEN-1],msg,IVAC_CONSOLE_STRING_LEN);
551
552   return SUCCESS;
553 }