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