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