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
31 puts("usage: ivac <options>");
34 puts("-h \t\t show this help");
35 puts("-n <name> \t specify your name");
36 puts("-p <port> \t specify port to listen for incoming connections");
42 int main(int argc,char **argv) {
50 strcpy(ivac.username,"ivac");
51 ivac.net.l_port=IVAC_LISTEN_PORT;
53 /* parse argv and change default values */
61 strncpy(ivac.username,argv[i+1],CHAR_USERNAME);
64 ivac.net.l_port=atoi(argv[i+1]);
74 /* clear challenger struct */
75 for(i=0;i<MAX_CONNECTIONS;i++)
76 memset(&(ivac.challenger[i]),0,sizeof(t_challenger));
77 /* zero console buffer */
78 for(i=0;i<IVAC_CONSOLE_LEN;i++)
79 memset(ivac.console[i],0,IVAC_CONSOLE_STRING_LEN);
81 /* set capabilities (futur: set by check routines) */
83 ivac.av_cap=AUDIO|VIDEO|DUPLEX;
85 /* set event timeout */
86 ivac.event.timeout.tv_sec=IVAC_S_SEC;
87 ivac.event.timeout.tv_usec=IVAC_S_USEC;
89 event_init(&(ivac.event));
92 ivac.input.mode=CONTENT_BUFFER;
93 input_init(&(ivac.input));
96 if(network_init(&(ivac.net))==N_ERROR) {
97 printf("[ivac] use 'fuser -n tcp %d' to determine the process to kill!\n",
103 /* add listening port + stdin to (read) event system */
104 event_math(ivac.net.l_fd,&(ivac.event),READ,ADD);
105 event_math(0,&(ivac.event),READ,ADD);
109 display_init(&(ivac.display));
113 ivac_display(&(ivac.display));
115 /* start event system - callbacks used: ivac_event_cb + ivac_regular_cb */
116 event_start(&(ivac.event),(void *)&ivac,ivac_event_cb,ivac_regular_cb);
121 int ivac_shutdown(t_ivac *ivac) {
123 network_shutdown(&(ivac->net));
124 input_shutdown(&(ivac->input));
125 event_stop(&(ivac->event));
127 display_shutdown(&(ivac->display));
133 int ivac_send_info(int channel,t_ivac *ivac) {
135 char data[SEND_N_MAX];
138 size=strlen(ivac->username);
140 data[0]=IVAC_SEND_NAME;
142 strncpy(data+2,ivac->username,size);
145 data[size]=IVAC_SEND_G_CAP;
147 data[size+2]=ivac->g_cap;
150 data[size]=IVAC_SEND_AV_CAP;
152 data[size+2]=(ivac->av_cap)>>8;
153 data[size+3]=(ivac->av_cap)&0xff;
156 if(network_send(ivac->net.connection[channel].fd,data,size)==N_ERROR) {
157 puts("[ivac] ivac_send_info failed");
164 int ivac_send_quit(int channel,t_ivac *ivac) {
166 char data[7]; /* one more for \0 */
168 data[0]=IVAC_SEND_QUIT;
170 strcpy(data+2,"quit");
172 if(network_send(ivac->net.connection[channel].fd,data,6)==N_ERROR) {
173 puts("[ivac] ivac_send_quit failed");
180 int ivac_receive_info(int channel,t_ivac *ivac) {
182 char data[SEND_N_MAX];
187 if((length=network_receive(ivac->net.connection[channel].fd,
188 data,SEND_N_MAX))==N_ERROR) {
189 puts("[ivac] ivac_receive_info failed");
193 while(length-count) {
194 switch(data[count]) {
196 strncpy(ivac->challenger[channel].name,data+count+2,data[count+1]);
197 ivac->challenger[channel].name[data[count+1]]='\0';
198 count+=(data[count+1]+2);
200 case IVAC_SEND_G_CAP:
201 ivac->challenger[channel].g_cap=data[count+2];
204 case IVAC_SEND_AV_CAP:
205 ivac->challenger[channel].av_cap=data[count+2]<<8;
206 ivac->challenger[channel].av_cap|=data[count+3];
210 if(!(strncmp(data+count+2,"quit",data[1])))
211 event_math(ivac->net.connection[channel].fd,&(ivac->event),
213 network_close(&(ivac->net),channel);
214 memset(&(ivac->challenger[channel]),0,sizeof(t_challenger));
217 printf("[ivac] ivac_receive_info, unknown character: (%c,%02x\n",
227 int ivac_event_cb(t_event *event,void *ptr) {
231 char c_str[IVAC_CONSOLE_STRING_LEN];
235 /* incoming connection -- first contact => send info */
236 if(FD_ISSET(ivac->net.l_fd,&(event->rfds))) {
237 channel=network_manage_incoming(&(ivac->net));
238 if(channel==N_E_ACCEPT)
239 sprintf(c_str,"accept failed");
240 else if(channel==N_E_MAXC)
241 sprintf(c_str,"maximum connections reached");
243 sprintf(c_str,"connection from %s port %d on channel %d",
244 ivac->net.connection[channel].ip,
245 ivac->net.connection[channel].port,channel);
246 ivac_add_to_monitor(ivac,c_str);
247 event_math(ivac->net.connection[channel].fd,event,READ,ADD);
248 ivac_send_info(channel,ivac);
250 ivac_display_content(ivac);
253 /* wait for user info */
254 for(channel=0;channel<MAX_CONNECTIONS;channel++) {
255 if(ivac->net.connection[channel].status&C_ESTABL) {
256 /* remote is sending info */
257 if(FD_ISSET(ivac->net.connection[channel].fd,&(event->rfds)))
258 ivac_receive_info(channel,ivac);
259 ivac_display_content(ivac);
263 /* user interaction */
264 if(FD_ISSET(0,&(event->rfds)))
265 input_get_event(&(ivac->input),ivac_parse_command,ivac);
270 int ivac_regular_cb(t_event *event,void *ptr) {
272 /* usual jobs like audio & video transmit ... */
277 int ivac_parse_command(t_input *input,void *ptr) {
284 char c_str[IVAC_CONSOLE_STRING_LEN];
285 char arg[IVAC_ARG_COUNT][IVAC_ARG_LEN];
291 /* refresh prompt content only! */
292 ivac_display_prompt_content(ivac);
294 /* parse command routines */
295 if(data[input->c_count-1]=='\n') {
297 /* delete console string + args */
298 memset(c_str,0,IVAC_CONSOLE_STRING_LEN);
299 for(j=0;j<IVAC_ARG_COUNT;j++) memset(arg[j],0,IVAC_ARG_LEN);
303 while(data[len]!='\n') len++;
305 while((i<len) && (j<IVAC_ARG_COUNT)) {
307 while((data[i+k]!=' ')&&(data[i+k]!='\n')) {
314 while(data[i+k]==' ') k++;
318 /* parse command aka arg[0] */
319 if(!(strncmp(arg[0],"quit",4))) {
321 sprintf(c_str,"ivac shutdown ...");
322 for(i=0;i<MAX_CONNECTIONS;i++) {
323 if(ivac->net.connection[i].status&C_ESTABL) {
324 ivac_send_quit(i,ivac);
325 network_close(&(ivac->net),i);
330 if(!(strncmp(arg[0],"set",3))) {
332 if((arg[1][0]>='0')&&(arg[1][0]<='9')) channel=atoi(arg[1]);
334 if(!(strncmp(arg[1],"name",4))) {
335 strncpy(ivac->username,arg[2],CHAR_USERNAME);
336 sprintf(c_str,"changed username to %s",ivac->username);
338 else if((channel>=0)&&(channel<MAX_CONNECTIONS)) {
339 i=network_set_connection_info(&(ivac->net),channel,arg[2],atoi(arg[3]));
341 sprintf(c_str,"channel %02d: connection in use",channel);
343 sprintf(c_str,"channel %02d: set connection info",channel);
344 } else snprintf(c_str,IVAC_CONSOLE_STRING_LEN,"unknown argument: '%s'",
347 if(!(strncmp(arg[0],"connect",7))) {
349 if((arg[1][0]>='0')&&(arg[1][0]<='9')) channel=atoi(arg[1]);
351 if((channel>=0)&&(channel<MAX_CONNECTIONS)) {
352 i=network_connect(&(ivac->net),channel);
354 sprintf(c_str,"channel %02d: connection in use",channel);
355 else if(i==N_E_NO_INFO)
356 sprintf(c_str,"channel %02d: channel not configured",channel);
358 sprintf(c_str,"channel %02d: connected to %s:%d",channel,
359 ivac->net.connection[channel].ip,
360 ivac->net.connection[channel].port);
361 event_math(ivac->net.connection[channel].fd,&(ivac->event),READ,ADD);
362 ivac_send_info(channel,ivac);
365 else sprintf(c_str,"invalid argument: '%s'",arg[1]);
367 if(!(strncmp(arg[0],"close",5))) {
369 if((arg[1][0]>='0')&&(arg[1][0]<='9')) channel=atoi(arg[1]);
371 if((channel>=0)&&(channel<MAX_CONNECTIONS)) {
372 if(ivac->net.connection[channel].status&C_ESTABL) {
373 ivac_send_quit(channel,ivac);
374 event_math(ivac->net.connection[channel].fd,&(ivac->event),
376 network_close(&(ivac->net),channel);
377 sprintf(c_str,"channel %02d: connection closed",channel);
378 memset(&(ivac->challenger[channel]),0,sizeof(t_challenger));
381 sprintf(c_str,"channel %02d: no active connection",channel);
383 else sprintf(c_str,"invalid argument: '%s'",arg[1]);
385 if(!(strncmp(arg[0],"select",6))) {
387 if((arg[1][0]>='0')&&(arg[1][0]<='9')) channel=atoi(arg[1]);
389 if((channel>=0)&&(channel<MAX_CONNECTIONS)) {
390 network_select(&(ivac->net),channel);
391 sprintf(c_str,"selected channel %d",channel);
393 else if(arg[1][0]='*') {
394 network_select(&(ivac->net),MAX_CONNECTIONS);
395 strcpy(c_str,"selected all channels");
397 else sprintf(c_str,"invalid argument: '%s'",arg[1]);
399 if(!(strncmp(arg[0],"deselect",8))) {
401 if((arg[1][0]>='0')&&(arg[1][0]<='9')) channel=atoi(arg[1]);
403 if((channel>=0)&&(channel<MAX_CONNECTIONS)) {
404 network_deselect(&(ivac->net),channel);
405 sprintf(c_str,"deselected channel %d",channel);
407 else if(arg[1][0]='*') {
408 network_deselect(&(ivac->net),MAX_CONNECTIONS);
409 strcpy(c_str,"deselected all channels");
411 else sprintf(c_str,"invalid argument: '%s'",arg[1]);
415 snprintf(c_str,IVAC_CONSOLE_STRING_LEN,"unknown command: '%s'",arg[0]);
417 /* call network functions */
418 network_manage_connection(&(ivac->net));
420 /* add console string to console buffer */
421 ivac_add_to_monitor(ivac,c_str);
423 /* refresh whole display content */
424 ivac_display_content(ivac);
426 /* delete content buffer + reset counter */
427 memset(input->content,0,input->c_count-1);
435 int ivac_display_head(t_display *display) {
441 for(x=0;x<display->max_x;x++) addch('#');
443 mvaddstr(1,(display->max_x-4)/2-4,"- ivac -");
444 mvaddstr(1,(display->max_x-2),"##");
446 for(x=0;x<display->max_x;x++) addch('#');
449 puts("#########################################################");
450 puts("##### ivac - - Copyright (C) 2004 Frank Zirkelbach #####");
451 puts("#########################################################");
457 int ivac_display_box(t_display *display) {
462 for(y=IVAC_PROMPT_LEN;y<display->max_y-IVAC_PROMPT_LEN;y++) {
464 mvaddch(y,display->max_x-1,'#');
471 int ivac_display_box_content(t_ivac *ivac) {
475 /* prepare challenger names */
476 for(channel=0;channel<MAX_CONNECTIONS;channel++)
477 if(ivac->challenger[channel].name[0]==0)
478 strcpy(ivac->challenger[channel].name,"<empty>");
482 for(channel=0;channel<MAX_CONNECTIONS;channel++) {
483 if(ivac->net.connection[channel].status&C_INFO_A)
484 printf("channel %02d: ip:%s port:%d status: %02x - name: %s\n",channel,
485 ivac->net.connection[channel].ip,
486 ivac->net.connection[channel].port,
487 ivac->net.connection[channel].status,
488 ivac->challenger[channel].name);
495 int ivac_display_console(t_display *display) {
500 move(display->max_y-IVAC_CONSOLE_LEN-IVAC_PROMPT_LEN-1,0);
501 for(x=0;x<display->max_x;x++) addch('#');
507 int ivac_display_console_content(t_ivac *ivac) {
513 for(y=0;y<IVAC_CONSOLE_LEN;y++) {
514 len=strlen(ivac->console[y]);
515 move(ivac->display.max_y-IVAC_CONSOLE_LEN-IVAC_PROMPT_LEN+y,2);
517 addch(((ivac->console[y][x]>' ')||(ivac->console[y][x]<='~'))
518 ?ivac->console[y][x]:' ');
519 for(x=len;x<IVAC_CONSOLE_STRING_LEN-4;x++) addch(' ');
525 for(i=0;i<IVAC_CONSOLE_LEN;i++)
526 printf("[ivac] console line %d: %s\n",i,ivac->console[i]);
532 int ivac_display_prompt(t_display *display) {
537 move(display->max_y-3,0);
538 for(x=0;x<display->max_x;x++) addch('#');
539 mvaddstr(display->max_y-2,0,"## command: ");
540 mvaddstr(display->max_y-2,display->max_x-2,"##");
541 move(display->max_y-1,0);
542 for(x=0;x<display->max_x;x++) addch('#');
549 int ivac_display_prompt_content(t_ivac *ivac) {
554 /* delete old command */
555 if(ivac->input.c_count==0) {
556 move(ivac->display.max_y-2,12);
557 for(x=12;x<ivac->display.max_x-1;x++) addch(' ');
560 for(x=0;x<ivac->input.c_count;x++)
561 mvaddch(ivac->display.max_y-2,x+12,ivac->input.content[x]);
564 printf("%c",ivac->input.content[ivac->input.c_count-1]);
571 int ivac_display(t_display *display) {
576 ivac_display_head(display);
579 ivac_display_box(display);
581 /* display console */
582 ivac_display_console(display);
584 /* display command prompt */
585 ivac_display_prompt(display);
590 int ivac_display_content(t_ivac *ivac) {
592 /* display box content */
593 ivac_display_box_content(ivac);
595 /* display console content */
596 ivac_display_console_content(ivac);
598 /* display prompt content */
599 ivac_display_prompt_content(ivac);
604 int ivac_add_to_monitor(t_ivac *ivac,char *msg) {
608 for(i=0;i<IVAC_CONSOLE_LEN-1;i++)
609 memcpy(ivac->console[i],ivac->console[i+1],IVAC_CONSOLE_STRING_LEN);
610 memcpy(ivac->console[IVAC_CONSOLE_LEN-1],msg,IVAC_CONSOLE_STRING_LEN);