0108131e4a9e9e2ad3b5ab5e646fddbfeb2510bb
[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       default:
183         puts("[ivac] ivac_receive_info, unknown character");
184         return ERROR;
185         break;
186     }
187   }
188
189   return SUCCESS;
190 }
191
192 int ivac_event_cb(t_event *event,void *ptr) {
193  
194   t_ivac *ivac;
195   int channel;
196   char c_str[IVAC_CONSOLE_STRING_LEN];
197
198   ivac=(t_ivac *)ptr;
199
200   /* incoming connection -- first contact => send info */
201   if(FD_ISSET(ivac->net.l_fd,&(event->rfds))) {
202     channel=network_manage_incoming(&(ivac->net));
203     if(channel==N_E_ACCEPT)
204       sprintf(c_str,"accept failed");
205     else if(channel==N_E_MAXC)
206       sprintf(c_str,"maximum connections reached");
207     else {
208       sprintf(c_str,"connection from %s port %d on channel %d",
209               ivac->net.connection[channel].ip,
210               ivac->net.connection[channel].port,channel);
211       ivac_add_to_monitor(ivac,c_str);
212       event_math(ivac->net.connection[channel].fd,event,READ,ADD);
213       ivac_send_info(channel,ivac);
214     }
215     ivac_display_content(ivac);
216   }
217
218   /* wait for user info */
219   for(channel=0;channel<MAX_CONNECTIONS;channel++) {
220     if(ivac->net.connection[channel].status&C_ESTABL) {
221       /* remote is sending info */
222       if(FD_ISSET(ivac->net.connection[channel].fd,&(event->rfds)))
223         ivac_receive_info(channel,ivac);
224         // ivac_display_content(ivac);
225     }
226   } 
227
228   /* user interaction */
229   if(FD_ISSET(0,&(event->rfds)))
230     input_get_event(&(ivac->input),ivac_parse_command,ivac);
231
232   return SUCCESS;
233 }
234
235 int ivac_regular_cb(t_event *event,void *ptr) {
236
237   /* usual jobs like audio & video transmit ... */
238
239   return SUCCESS;
240 }
241
242 int ivac_parse_command(t_input *input,void *ptr) {
243
244   t_ivac *ivac;
245   int i,j,k;
246   int len;
247   int channel;
248   char *data,valid;
249   char c_str[IVAC_CONSOLE_STRING_LEN];
250   char arg[IVAC_ARG_COUNT][IVAC_ARG_LEN];
251
252   ivac=(t_ivac *)ptr;
253   data=input->content;
254   valid=0;
255
256   /* refresh prompt content only! */
257   ivac_display_prompt_content(ivac);
258
259   /* parse command routines */
260   if(data[input->c_count-1]=='\n') {
261
262     /* delete console string + args */
263     memset(c_str,0,IVAC_CONSOLE_STRING_LEN);
264     for(j=0;j<IVAC_ARG_COUNT;j++) memset(arg[j],0,IVAC_ARG_LEN);
265
266     /* get args */
267     len=0;
268     while(data[len]!='\n') len++;
269     i=0; j=0;
270     while((i<len) && (j<IVAC_ARG_COUNT)) {
271       k=0;
272       while((data[i+k]!=' ')&&(data[i+k]!='\n')) {
273         arg[j][k]=data[i+k];
274         k++;
275       }
276       arg[j][k]='\0';
277       j++;
278       /* skip all ' ' */
279       while(data[i+k]==' ') k++;
280       i+=k;
281     }
282
283     /* parse command  aka arg[0] */
284     if(!(strncmp(arg[0],"quit",4))) {
285       valid=1;
286       sprintf(c_str,"ivac shutdown ...");
287       ivac_shutdown(ivac);
288     }
289     if(!(strncmp(arg[0],"set",3))) {
290       valid=1;
291       if((arg[1][0]>='0')&&(arg[1][0]<='9')) channel=atoi(arg[1]);
292       else channel=-1;
293       if(!(strncmp(arg[1],"name",4))) {
294         strncpy(ivac->username,arg[2],CHAR_USERNAME);
295         sprintf(c_str,"changed username to %s",ivac->username);
296       }
297       else if((channel>=0)&&(channel<MAX_CONNECTIONS)) {
298         i=network_set_connection_info(&(ivac->net),channel,arg[2],atoi(arg[3]));
299         if(i==N_E_IN_USE)
300           sprintf(c_str,"channel %02d: connection in use",channel);
301         if(i==N_SUCCESS)
302           sprintf(c_str,"channel %02d: set connection info",channel);
303       } else snprintf(c_str,IVAC_CONSOLE_STRING_LEN,"unknown argument: '%s'",
304                       arg[1]);
305     }
306     if(!(strncmp(arg[0],"connect",7))) {
307       valid=1;
308       if((arg[1][0]>='0')&&(arg[1][0]<='9')) channel=atoi(arg[1]);
309       else channel=-1;
310       if((channel>=0)&&(channel<MAX_CONNECTIONS)) {
311         i=network_connect(&(ivac->net),channel);
312         if(i==N_E_IN_USE)
313           sprintf(c_str,"channel %02d: connection in use",channel);
314         else if(i==N_E_NO_INFO)
315           sprintf(c_str,"channel %02d: channel not configured",channel);
316         else {
317           sprintf(c_str,"channel %02d: connected to %s:%d",channel,
318                   ivac->net.connection[channel].ip,
319                   ivac->net.connection[channel].port);
320           event_math(ivac->net.connection[channel].fd,&(ivac->event),READ,ADD);
321           ivac_send_info(channel,ivac);
322         }
323       }
324       else sprintf(c_str,"invalid argument: '%s'",arg[1]);
325     } 
326     if(!(strncmp(arg[0],"close",5))) {
327       valid=1;
328       if((arg[1][0]>='0')&&(arg[1][0]<='9')) channel=atoi(arg[1]);
329       else channel=-1;
330       if((channel>=0)&&(channel<MAX_CONNECTIONS)) {
331         if(ivac->net.connection[channel].status&C_ESTABL) {
332           ivac_send_quit(channel,ivac);
333           event_math(ivac->net.connection[channel].fd,&(ivac->event),
334                      READ,REMOVE);
335           network_close(&(ivac->net),channel);
336           sprintf(c_str,"channel %02d: connection closed",channel);
337           memset(&(ivac->challenger[channel]),0,sizeof(t_challenger));
338         }
339         else
340           sprintf(c_str,"channel %02d: no active connection",channel);
341       }
342       else sprintf(c_str,"invalid argument: '%s'",arg[1]);
343     }
344     if(!(strncmp(arg[0],"select",6))) {
345       valid=1;
346       if((arg[1][0]>='0')&&(arg[1][0]<='9')) channel=atoi(arg[1]);
347       else channel=-1;
348       if((channel>=0)&&(channel<MAX_CONNECTIONS)) {
349         network_select(&(ivac->net),channel);
350         sprintf(c_str,"selected channel %d",channel);
351       }
352       else if(arg[1][0]='*') {
353         network_select(&(ivac->net),MAX_CONNECTIONS);
354         strcpy(c_str,"selected all channels");
355       }
356       else sprintf(c_str,"invalid argument: '%s'",arg[1]);
357     }
358     if(!(strncmp(arg[0],"deselect",8))) {
359       valid=1;
360       if((arg[1][0]>='0')&&(arg[1][0]<='9')) channel=atoi(arg[1]);
361       else channel=-1;
362       if((channel>=0)&&(channel<MAX_CONNECTIONS)) {
363         network_deselect(&(ivac->net),channel);
364         sprintf(c_str,"deselected channel %d",channel);
365       }
366       else if(arg[1][0]='*') {
367         network_deselect(&(ivac->net),MAX_CONNECTIONS);
368         strcpy(c_str,"deselected all channels");
369       }
370       else sprintf(c_str,"invalid argument: '%s'",arg[1]);
371     }
372
373     if(!valid)
374       snprintf(c_str,IVAC_CONSOLE_STRING_LEN,"unknown command: '%s'",arg[0]);
375
376     /* call network functions */
377     network_manage_connection(&(ivac->net));
378
379     /* add console string to console buffer */
380     ivac_add_to_monitor(ivac,c_str);
381
382     /* refresh whole display content */
383     ivac_display_content(ivac);
384
385     /* delete content buffer + reset counter */
386     memset(input->content,0,input->c_count-1);
387     input->c_count=0;
388
389   }
390
391   return SUCCESS;
392 }
393
394 int ivac_display_head(t_display *display) {
395
396 #ifdef USE_NCURSES
397   int x,y;
398
399   move(0,0);
400   for(x=0;x<display->max_x;x++) addch('#');
401   mvaddstr(1,0,"##");
402   mvaddstr(1,(display->max_x-4)/2-4,"- ivac -");
403   mvaddstr(1,(display->max_x-2),"##");
404   move(2,0);
405   for(x=0;x<display->max_x;x++) addch('#');
406   refresh();
407 #else
408   puts("#########################################################");
409   puts("##### ivac - -  Copyright (C) 2004 Frank Zirkelbach #####");
410   puts("#########################################################");
411 #endif
412
413   return SUCCESS;
414 }
415
416 int ivac_display_box(t_display *display) {
417  
418 #ifdef USE_NCURSES 
419   int x,y;
420
421   for(y=IVAC_PROMPT_LEN;y<display->max_y-IVAC_PROMPT_LEN;y++) {
422     mvaddch(y,0,'#');
423     mvaddch(y,display->max_x-1,'#');
424   }
425 #endif
426
427   return SUCCESS;
428 }
429
430 int ivac_display_box_content(t_ivac *ivac) {
431
432   int channel;
433
434   /* prepare challenger names */
435   for(channel=0;channel<MAX_CONNECTIONS;channel++)
436     if(ivac->challenger[channel].name[0]==0)
437       strcpy(ivac->challenger[channel].name,"<empty>");
438
439 #ifdef USE_NCURSES
440 #else
441   for(channel=0;channel<MAX_CONNECTIONS;channel++) {
442     if(ivac->net.connection[channel].status&C_INFO_A)
443       printf("channel %02d: ip:%s port:%d status: %02x - name: %s\n",channel,
444              ivac->net.connection[channel].ip,
445              ivac->net.connection[channel].port,
446              ivac->net.connection[channel].status,
447              ivac->challenger[channel].name);
448   }
449 #endif
450
451   return SUCCESS;
452 }
453
454 int ivac_display_console(t_display *display) {
455
456 #ifdef USE_NCURSES                                             
457   int x,y;
458          
459   move(display->max_y-IVAC_CONSOLE_LEN-IVAC_PROMPT_LEN-1,0);
460   for(x=0;x<display->max_x;x++) addch('#');
461 #endif
462                                           
463   return SUCCESS;
464 }
465
466 int ivac_display_console_content(t_ivac *ivac) {
467
468 #ifdef USE_NCURSES
469   int x,y;
470   int len;
471
472   for(y=0;y<IVAC_CONSOLE_LEN;y++) {
473     len=strlen(ivac->console[y]);
474     move(ivac->display.max_y-IVAC_CONSOLE_LEN-IVAC_PROMPT_LEN+y,2);
475     for(x=0;x<len;x++)
476        addch(((ivac->console[y][x]>' ')||(ivac->console[y][x]<='~'))
477                ?ivac->console[y][x]:' ');
478     for(x=len;x<IVAC_CONSOLE_STRING_LEN-4;x++) addch(' ');
479   }
480   refresh();
481 #else
482   int i;
483
484   for(i=0;i<IVAC_CONSOLE_LEN;i++)
485     printf("[ivac] console line %d: %s\n",i,ivac->console[i]);
486 #endif
487
488   return SUCCESS;
489 }
490
491 int ivac_display_prompt(t_display *display) {
492
493 #ifdef USE_NCURSES
494   int x,y;
495
496   move(display->max_y-3,0);
497   for(x=0;x<display->max_x;x++) addch('#');
498   mvaddstr(display->max_y-2,0,"## command: ");
499   mvaddstr(display->max_y-2,display->max_x-2,"##");
500   move(display->max_y-1,0);
501   for(x=0;x<display->max_x;x++) addch('#');
502   refresh();
503 #endif
504
505   return SUCCESS;
506 }
507
508 int ivac_display_prompt_content(t_ivac *ivac) {
509
510 #ifdef USE_NCURSES
511   int x,y;
512
513   /* delete old command */
514   if(ivac->input.c_count==0) {
515     move(ivac->display.max_y-2,12);
516     for(x=12;x<ivac->display.max_x-1;x++) addch(' ');
517   }
518
519   for(x=0;x<ivac->input.c_count;x++)
520     mvaddch(ivac->display.max_y-2,x+12,ivac->input.content[x]);
521   refresh();
522 #else
523   printf("%c",ivac->input.content[ivac->input.c_count-1]);
524   fflush(NULL);
525 #endif
526
527   return SUCCESS;
528 }
529
530 int ivac_display(t_display *display) {
531
532   int x,y;
533
534   /* display head */
535   ivac_display_head(display);
536
537   /* display box */
538   ivac_display_box(display);
539
540   /* display console */
541   ivac_display_console(display);
542
543   /* display command prompt */
544   ivac_display_prompt(display);
545
546   return SUCCESS;
547 }
548
549 int ivac_display_content(t_ivac *ivac) {
550
551   /* display box content */
552   ivac_display_box_content(ivac);
553
554   /* display console content */
555   ivac_display_console_content(ivac);
556
557   /* display prompt content */
558   ivac_display_prompt_content(ivac);
559
560   return SUCCESS;
561 }
562
563 int ivac_add_to_monitor(t_ivac *ivac,char *msg) {
564
565   int i;
566
567   for(i=0;i<IVAC_CONSOLE_LEN-1;i++)
568     memcpy(ivac->console[i],ivac->console[i+1],IVAC_CONSOLE_STRING_LEN);
569   memcpy(ivac->console[IVAC_CONSOLE_LEN-1],msg,IVAC_CONSOLE_STRING_LEN);
570
571   return SUCCESS;
572 }