1 /* ivac.c -- main ivac file
3 * author: hackbard@hackdaworld.dyndns.org
4 * frank.zirkelbach@physik.uni-augsburg.de
6 * Copyright (C) 2004 Frank Zirkelbach
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.
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.
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
24 // #define USE_NCURSES
25 /* dont care about ncurses .. go for gtk(2)! */
33 puts("usage: ivac <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");
44 int main(int argc,char **argv) {
52 strcpy(ivac.username,"ivac");
53 ivac.net.l_port=IVAC_LISTEN_PORT;
55 /* parse argv and change default values */
63 strncpy(ivac.username,argv[i+1],CHAR_USERNAME);
66 ivac.net.l_port=atoi(argv[i+1]);
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);
83 /* set capabilities (futur: set by check routines) */
85 ivac.av_cap=AUDIO|VIDEO|DUPLEX;
87 /* set event timeout */
88 ivac.event.timeout.tv_sec=IVAC_S_SEC;
89 ivac.event.timeout.tv_usec=IVAC_S_USEC;
91 event_init(&(ivac.event));
94 ivac.input.mode=CONTENT_BUFFER;
95 input_init(&(ivac.input));
98 if(network_init(&(ivac.net))==N_ERROR) {
99 printf("[ivac] use 'fuser -n tcp %d' to determine the process to kill!\n",
101 ivac_shutdown(&ivac);
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);
111 display_init(&(ivac.display));
115 ivac_display(&(ivac.display));
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);
123 int ivac_shutdown(t_ivac *ivac) {
125 network_shutdown(&(ivac->net));
126 input_shutdown(&(ivac->input));
127 event_stop(&(ivac->event));
129 display_shutdown(&(ivac->display));
135 int ivac_send_info(int channel,t_ivac *ivac) {
137 char data[SEND_N_MAX];
140 size=strlen(ivac->username);
142 data[0]=IVAC_SEND_NAME;
144 strncpy(data+2,ivac->username,size);
147 data[size]=IVAC_SEND_G_CAP;
149 data[size+2]=ivac->g_cap;
152 data[size]=IVAC_SEND_AV_CAP;
154 data[size+2]=(ivac->av_cap)>>8;
155 data[size+3]=(ivac->av_cap)&0xff;
158 if(network_send(ivac->net.connection[channel].fd,data,size)==N_ERROR) {
159 puts("[ivac] ivac_send_info failed");
166 int ivac_send_quit(int channel,t_ivac *ivac) {
168 char data[7]; /* one more for \0 */
170 data[0]=IVAC_SEND_QUIT;
172 strcpy(data+2,"quit");
174 if(network_send(ivac->net.connection[channel].fd,data,6)==N_ERROR) {
175 puts("[ivac] ivac_send_quit failed");
182 int ivac_receive_info(int channel,t_ivac *ivac) {
184 char data[SEND_N_MAX];
185 char c_str[IVAC_CONSOLE_STRING_LEN];
190 if((length=network_receive(ivac->net.connection[channel].fd,
191 data,SEND_N_MAX))==N_ERROR) {
192 puts("[ivac] ivac_receive_info failed");
196 while(length-count) {
197 switch(data[count]) {
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);
203 case IVAC_SEND_G_CAP:
204 ivac->challenger[channel].g_cap=data[count+2];
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];
213 if(!(strncmp(data+count+2,"quit",data[1])))
214 sprintf(c_str,"channel %02d: connection closed by remote host",
216 event_math(ivac->net.connection[channel].fd,&(ivac->event),
218 network_close(&(ivac->net),channel);
219 memset(&(ivac->challenger[channel]),0,sizeof(t_challenger));
220 ivac_add_to_monitor(ivac,c_str);
224 sprintf(c_str,"ivac_receive_info, unknown character: 0x%02x\n",
226 ivac_add_to_monitor(ivac,c_str);
235 int ivac_event_cb(t_event *event,void *ptr) {
239 char c_str[IVAC_CONSOLE_STRING_LEN];
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");
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);
258 ivac_display_content(ivac);
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 ivac_receive_info(channel,ivac);
267 ivac_display_content(ivac);
272 /* user interaction */
273 if(FD_ISSET(0,&(event->rfds)))
274 input_get_event(&(ivac->input),ivac_parse_command,ivac);
279 int ivac_regular_cb(t_event *event,void *ptr) {
281 /* usual jobs like audio & video transmit ... */
286 int ivac_parse_command(t_input *input,void *ptr) {
293 char c_str[IVAC_CONSOLE_STRING_LEN];
294 char arg[IVAC_ARG_COUNT][IVAC_ARG_LEN];
300 /* refresh prompt content only! */
301 ivac_display_prompt_content(ivac);
303 /* parse command routines */
304 if(data[input->c_count-1]=='\n') {
306 /* delete console string + args */
307 memset(c_str,0,IVAC_CONSOLE_STRING_LEN);
308 for(j=0;j<IVAC_ARG_COUNT;j++) memset(arg[j],0,IVAC_ARG_LEN);
312 while(data[len]!='\n') len++;
314 while((i<len) && (j<IVAC_ARG_COUNT)) {
316 while((data[i+k]!=' ')&&(data[i+k]!='\n')) {
323 while(data[i+k]==' ') k++;
327 /* parse command aka arg[0] */
328 if(!(strncmp(arg[0],"quit",4))) {
330 sprintf(c_str,"ivac shutdown ...");
331 for(i=0;i<MAX_CONNECTIONS;i++) {
332 if(ivac->net.connection[i].status&C_ESTABL) {
333 ivac_send_quit(i,ivac);
334 network_close(&(ivac->net),i);
339 if(!(strncmp(arg[0],"set",3))) {
341 if((arg[1][0]>='0')&&(arg[1][0]<='9')) channel=atoi(arg[1]);
343 if(!(strncmp(arg[1],"name",4))) {
344 strncpy(ivac->username,arg[2],CHAR_USERNAME);
345 sprintf(c_str,"changed username to %s",ivac->username);
347 else if((channel>=0)&&(channel<MAX_CONNECTIONS)) {
348 i=network_set_connection_info(&(ivac->net),channel,arg[2],atoi(arg[3]));
350 sprintf(c_str,"channel %02d: connection in use",channel);
352 sprintf(c_str,"channel %02d: set connection info",channel);
353 } else snprintf(c_str,IVAC_CONSOLE_STRING_LEN,"unknown argument: '%s'",
356 if(!(strncmp(arg[0],"connect",7))) {
358 if((arg[1][0]>='0')&&(arg[1][0]<='9')) channel=atoi(arg[1]);
360 if((channel>=0)&&(channel<MAX_CONNECTIONS)) {
361 i=network_connect(&(ivac->net),channel);
363 sprintf(c_str,"channel %02d: connection in use",channel);
364 else if(i==N_E_NO_INFO)
365 sprintf(c_str,"channel %02d: channel not configured",channel);
367 sprintf(c_str,"channel %02d: connected to %s:%d",channel,
368 ivac->net.connection[channel].ip,
369 ivac->net.connection[channel].port);
370 event_math(ivac->net.connection[channel].fd,&(ivac->event),READ,ADD);
371 ivac_send_info(channel,ivac);
374 else sprintf(c_str,"invalid argument: '%s'",arg[1]);
376 if(!(strncmp(arg[0],"close",5))) {
378 if((arg[1][0]>='0')&&(arg[1][0]<='9')) channel=atoi(arg[1]);
380 if((channel>=0)&&(channel<MAX_CONNECTIONS)) {
381 if(ivac->net.connection[channel].status&C_ESTABL) {
382 ivac_send_quit(channel,ivac);
383 event_math(ivac->net.connection[channel].fd,&(ivac->event),
385 network_close(&(ivac->net),channel);
386 sprintf(c_str,"channel %02d: connection closed",channel);
387 memset(&(ivac->challenger[channel]),0,sizeof(t_challenger));
390 sprintf(c_str,"channel %02d: no active connection",channel);
392 else sprintf(c_str,"invalid argument: '%s'",arg[1]);
394 if(!(strncmp(arg[0],"select",6))) {
396 if((arg[1][0]>='0')&&(arg[1][0]<='9')) channel=atoi(arg[1]);
398 if((channel>=0)&&(channel<MAX_CONNECTIONS)) {
399 network_select(&(ivac->net),channel);
400 sprintf(c_str,"selected channel %d",channel);
402 else if(arg[1][0]='*') {
403 network_select(&(ivac->net),MAX_CONNECTIONS);
404 strcpy(c_str,"selected all channels");
406 else sprintf(c_str,"invalid argument: '%s'",arg[1]);
408 if(!(strncmp(arg[0],"deselect",8))) {
410 if((arg[1][0]>='0')&&(arg[1][0]<='9')) channel=atoi(arg[1]);
412 if((channel>=0)&&(channel<MAX_CONNECTIONS)) {
413 network_deselect(&(ivac->net),channel);
414 sprintf(c_str,"deselected channel %d",channel);
416 else if(arg[1][0]='*') {
417 network_deselect(&(ivac->net),MAX_CONNECTIONS);
418 strcpy(c_str,"deselected all channels");
420 else sprintf(c_str,"invalid argument: '%s'",arg[1]);
424 snprintf(c_str,IVAC_CONSOLE_STRING_LEN,"unknown command: '%s'",arg[0]);
426 /* call network functions */
427 network_manage_connection(&(ivac->net));
429 /* add console string to console buffer */
430 ivac_add_to_monitor(ivac,c_str);
432 /* refresh whole display content */
433 ivac_display_content(ivac);
435 /* delete content buffer + reset counter */
436 memset(input->content,0,input->c_count-1);
444 int ivac_display_head(t_display *display) {
450 for(x=0;x<display->max_x;x++) addch('#');
452 mvaddstr(1,(display->max_x-4)/2-4,"- ivac -");
453 mvaddstr(1,(display->max_x-2),"##");
455 for(x=0;x<display->max_x;x++) addch('#');
458 puts("#########################################################");
459 puts("##### ivac - - Copyright (C) 2004 Frank Zirkelbach #####");
460 puts("#########################################################");
466 int ivac_display_box(t_display *display) {
471 for(y=IVAC_PROMPT_LEN;y<display->max_y-IVAC_PROMPT_LEN;y++) {
473 mvaddch(y,display->max_x-1,'#');
480 int ivac_display_box_content(t_ivac *ivac) {
484 /* prepare challenger names */
485 for(channel=0;channel<MAX_CONNECTIONS;channel++)
486 if(ivac->challenger[channel].name[0]==0)
487 strcpy(ivac->challenger[channel].name,"<empty>");
491 for(channel=0;channel<MAX_CONNECTIONS;channel++) {
492 if(ivac->net.connection[channel].status&C_INFO_A)
493 printf("channel %02d: ip:%s port:%d status: %02x - name: %s\n",channel,
494 ivac->net.connection[channel].ip,
495 ivac->net.connection[channel].port,
496 ivac->net.connection[channel].status,
497 ivac->challenger[channel].name);
504 int ivac_display_console(t_display *display) {
509 move(display->max_y-IVAC_CONSOLE_LEN-IVAC_PROMPT_LEN-1,0);
510 for(x=0;x<display->max_x;x++) addch('#');
516 int ivac_display_console_content(t_ivac *ivac) {
522 for(y=0;y<IVAC_CONSOLE_LEN;y++) {
523 len=strlen(ivac->console[y]);
524 move(ivac->display.max_y-IVAC_CONSOLE_LEN-IVAC_PROMPT_LEN+y,2);
526 addch(((ivac->console[y][x]>' ')||(ivac->console[y][x]<='~'))
527 ?ivac->console[y][x]:' ');
528 for(x=len;x<IVAC_CONSOLE_STRING_LEN-4;x++) addch(' ');
534 for(i=0;i<IVAC_CONSOLE_LEN;i++)
535 printf("[ivac] console line %d: %s\n",i,ivac->console[i]);
541 int ivac_display_prompt(t_display *display) {
546 move(display->max_y-3,0);
547 for(x=0;x<display->max_x;x++) addch('#');
548 mvaddstr(display->max_y-2,0,"## command: ");
549 mvaddstr(display->max_y-2,display->max_x-2,"##");
550 move(display->max_y-1,0);
551 for(x=0;x<display->max_x;x++) addch('#');
558 int ivac_display_prompt_content(t_ivac *ivac) {
563 /* delete old command */
564 if(ivac->input.c_count==0) {
565 move(ivac->display.max_y-2,12);
566 for(x=12;x<ivac->display.max_x-1;x++) addch(' ');
569 for(x=0;x<ivac->input.c_count;x++)
570 mvaddch(ivac->display.max_y-2,x+12,ivac->input.content[x]);
573 printf("%c",ivac->input.content[ivac->input.c_count-1]);
580 int ivac_display(t_display *display) {
585 ivac_display_head(display);
588 ivac_display_box(display);
590 /* display console */
591 ivac_display_console(display);
593 /* display command prompt */
594 ivac_display_prompt(display);
599 int ivac_display_content(t_ivac *ivac) {
601 /* display box content */
602 ivac_display_box_content(ivac);
604 /* display console content */
605 ivac_display_console_content(ivac);
607 /* display prompt content */
608 ivac_display_prompt_content(ivac);
613 int ivac_add_to_monitor(t_ivac *ivac,char *msg) {
617 for(i=0;i<IVAC_CONSOLE_LEN-1;i++)
618 memcpy(ivac->console[i],ivac->console[i+1],IVAC_CONSOLE_STRING_LEN);
619 memcpy(ivac->console[IVAC_CONSOLE_LEN-1],msg,IVAC_CONSOLE_STRING_LEN);