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