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