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
28 int main(int argc,char **argv) {
36 printf("\nusage: %s <username>\n\n",argv[0]);
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);
49 /* set capabilities (futur: set by check routines) */
51 ivac.av_cap=AUDIO|VIDEO|DUPLEX;
53 /* set event timeout */
54 ivac.event.timeout.tv_sec=IVAC_S_SEC;
55 ivac.event.timeout.tv_usec=IVAC_S_USEC;
57 event_init(&(ivac.event));
60 ivac.input.mode=CONTENT_BUFFER;
61 input_init(&(ivac.input));
63 /* set listen port (futur: read from config or entered later) */
64 network_set_listen_port(&(ivac.net),IVAC_LISTEN_PORT);
66 if(network_init(&(ivac.net))==N_ERROR) {
67 printf("[ivac] use 'fuser -n tcp %d' to determine the process to kill!\n",
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);
78 // display_init(&(ivac.display));
81 ivac_display(&(ivac.display));
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);
86 // ivac_shutdown(&ivac);
91 int ivac_shutdown(t_ivac *ivac) {
93 network_shutdown(&(ivac->net));
94 input_shutdown(&(ivac->input));
95 event_stop(&(ivac->event));
96 // display_shutdown(&(ivac->display));
101 int ivac_send_info(int channel,t_ivac *ivac) {
103 char data[SEND_N_MAX];
106 size=strlen(ivac->username);
108 data[0]=IVAC_SEND_NAME;
110 strncpy(data+2,ivac->username,size);
113 data[size]=IVAC_SEND_G_CAP;
115 data[size+2]=ivac->g_cap;
118 data[size]=IVAC_SEND_AV_CAP;
120 data[size+2]=(ivac->av_cap)>>8;
121 data[size+3]=(ivac->av_cap)&0xff;
124 if(network_send(ivac->net.connection[channel].fd,data,size)==N_ERROR) {
125 puts("[ivac] ivac_send_info failed");
132 int ivac_receive_info(int channel,t_ivac *ivac) {
134 char data[SEND_N_MAX];
139 if((length=network_receive(ivac->net.connection[channel].fd,
140 data,SEND_N_MAX))==N_ERROR) {
141 puts("[ivac] ivac_receive_info failed");
145 while(length-count) {
146 switch(data[count]) {
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);
152 case IVAC_SEND_G_CAP:
153 ivac->challenger[channel].g_cap=data[count+2];
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];
162 puts("[ivac] ivac_receive_info, unknown character");
171 int ivac_event_cb(t_event *event,void *ptr) {
178 if(FD_ISSET(ivac->net.l_fd,&(event->rfds))) {
179 /* manage incoming + send info */
180 if((channel=network_manage_incoming(&(ivac->net)))!=N_ERROR) {
181 event_math(ivac->net.connection[channel].fd,event,READ,ADD);
182 ivac_send_info(channel,ivac);
186 for(channel=0;channel<MAX_CONNECTIONS;channel++) {
187 if(ivac->net.connection[channel].status&C_ESTABL) {
189 if(FD_ISSET(ivac->net.connection[channel].fd,&(event->rfds)))
190 ivac_receive_info(channel,ivac);
191 /* sync network/event api */
192 if(!(FD_ISSET(ivac->net.connection[channel].fd,&(event->rfds_o))))
193 event_math(ivac->net.connection[channel].fd,event,READ,ADD);
197 /* user interaction */
198 if(FD_ISSET(0,&(event->rfds)))
199 input_get_event(&(ivac->input),ivac_parse_command,ivac);
201 /* display ivac gui */
202 // ivac_display_content(ivac);
207 int ivac_regular_cb(t_event *event,void *ptr) {
209 /* usual jobs like audio & video transmit ... */
214 int ivac_parse_command(t_input *input,void *ptr) {
221 char c_str[IVAC_CONSOLE_STRING_LEN];
222 char arg[IVAC_ARG_COUNT][IVAC_ARG_LEN];
228 /* refresh prompt content only! */
229 ivac_display_prompt_content(ivac);
231 /* parse command routines */
232 if(data[input->c_count-1]=='\n') {
234 /* delete console string + args */
235 memset(c_str,0,IVAC_CONSOLE_STRING_LEN);
236 for(j=0;j<IVAC_ARG_COUNT;j++) memset(arg[j],0,IVAC_ARG_LEN);
240 while(data[len]!='\n') len++;
242 while((i<len) && (j<IVAC_ARG_COUNT)) {
244 while((data[i+k]!=' ')&&(data[i+k]!='\n')) {
251 while(data[i+k]==' ') k++;
255 /* parse command aka arg[0] */
256 if(!(strncmp(arg[0],"quit",4))) {
258 sprintf(c_str,"ivac shutdown ...");
261 if(!(strncmp(arg[0],"set",3))) {
263 if((arg[1][0]>='0')&&(arg[1][0]<='9')) channel=atoi(arg[1]);
265 if(!(strncmp(arg[1],"name",4))) {
266 strncpy(ivac->username,arg[2],CHAR_USERNAME);
267 sprintf(c_str,"changed username to %s",ivac->username);
269 else if((channel>=0)&&(channel<MAX_CONNECTIONS)) {
270 if(ivac->net.connection[channel].status&C_IN_USE) {
271 sprintf(c_str,"channel %02d: connection in use",channel);
274 strncpy(ivac->net.connection[channel].ip,arg[2],IP_DIGITS);
275 ivac->net.connection[channel].port=atoi(arg[3]);
276 sprintf(c_str,"channel %02d: set connection info",channel);
277 ivac->net.connection[channel].status|=C_INFO_A;
279 } else snprintf(c_str,IVAC_CONSOLE_STRING_LEN,"unknown argument: '%s'",
282 if(!(strncmp(arg[0],"connect",7))) {
284 if((arg[1][0]>='0')&&(arg[1][0]<='9')) channel=atoi(arg[1]);
286 if((channel>=0)&&(channel<MAX_CONNECTIONS)) {
287 if(!(ivac->net.connection[channel].status&C_INFO_A))
288 sprintf(c_str,"channel %02d: channel not configured",channel);
290 if(ivac->net.connection[channel].status&C_IN_USE)
291 sprintf(c_str,"channel %02d: connection in use",channel);
293 sprintf(c_str,"channel %02d: trying to connect to %s:%d",channel,
294 ivac->net.connection[channel].ip,
295 ivac->net.connection[channel].port);
296 ivac->net.connection[channel].status|=C_IN_USE;
300 else sprintf(c_str,"invalid argument: '%s'",arg[1]);
302 if(!(strncmp(arg[0],"close",5))) {
304 if((arg[1][0]>='0')&&(arg[1][0]<='9')) channel=atoi(arg[1]);
306 if((channel>=0)&&(channel<MAX_CONNECTIONS)) {
307 if(!(ivac->net.connection[channel].status&C_ESTABL))
308 sprintf(c_str,"channel %02d: no active connection",channel);
310 ivac->net.connection[channel].status|=C_HANGUP;
311 sprintf(c_str,"channel %02d: connection closed",channel);
314 else sprintf(c_str,"invalid argument: '%s'",arg[1]);
316 if(!(strncmp(arg[0],"select",6))) {
318 if((arg[1][0]>='0')&&(arg[1][0]<='9')) channel=atoi(arg[1]);
320 if((channel>=0)&&(channel<MAX_CONNECTIONS)) {
321 ivac->net.sendmask|=(1<<channel);
322 sprintf(c_str,"selected channel %d",channel);
324 else if(arg[1][0]='*') {
325 ivac->net.sendmask=0xff;
326 strcpy(c_str,"selected all channels");
328 else sprintf(c_str,"invalid argument: '%s'",arg[1]);
330 if(!(strncmp(arg[0],"deselect",8))) {
332 if((arg[1][0]>='0')&&(arg[1][0]<='9')) channel=atoi(arg[1]);
334 if((channel>=0)&&(channel<MAX_CONNECTIONS)) {
335 ivac->net.sendmask&=(~(1<<channel));
336 sprintf(c_str,"deselected channel %d",channel);
338 else if(arg[1][0]='*') {
339 ivac->net.sendmask=0;
340 strcpy(c_str,"deselected all channels");
342 else sprintf(c_str,"invalid argument: '%s'",arg[1]);
346 snprintf(c_str,IVAC_CONSOLE_STRING_LEN,"unknown command: '%s'",arg[0]);
348 /* call network functions */
349 network_manage_connection(&(ivac->net));
351 /* add console string to console buffer */
352 ivac_add_to_monitor(ivac,c_str);
354 /* refresh whole display content */
355 ivac_display_content(ivac);
357 /* delete content buffer + reset counter */
358 memset(input->content,0,input->c_count-1);
366 int ivac_display_head(t_display *display) {
372 for(x=0;x<display->max_x;x++) addch('#');
374 mvaddstr(1,(display->max_x-4)/2-4,"- ivac -");
375 mvaddstr(1,(display->max_x-2),"##");
377 for(x=0;x<display->max_x;x++) addch('#');
380 puts("#########################################################");
381 puts("##### ivac - - Copyright (C) 2004 Frank Zirkelbach #####");
382 puts("#########################################################");
388 int ivac_display_box(t_display *display) {
393 for(y=IVAC_PROMPT_LEN;y<display->max_y-IVAC_PROMPT_LEN;y++) {
395 mvaddch(y,display->max_x-1,'#');
402 int ivac_display_box_content(t_ivac *ivac) {
406 /* prepare challenger names */
407 for(channel=0;channel<MAX_CONNECTIONS;channel++)
408 if(ivac->challenger[channel].name[0]==0)
409 strcpy(ivac->challenger[channel].name,"<empty>");
413 for(channel=0;channel<MAX_CONNECTIONS;channel++) {
414 if(ivac->net.connection[channel].status&C_INFO_A)
415 printf("channel %02d: ip:%s port:%d status: %02x - name: %s\n",channel,
416 ivac->net.connection[channel].ip,
417 ivac->net.connection[channel].port,
418 ivac->net.connection[channel].status,
419 ivac->challenger[channel].name);
426 int ivac_display_console(t_display *display) {
431 move(display->max_y-IVAC_CONSOLE_LEN-IVAC_PROMPT_LEN-1,0);
432 for(x=0;x<display->max_x;x++) addch('#');
438 int ivac_display_console_content(t_ivac *ivac) {
444 for(y=0;y<IVAC_CONSOLE_LEN;y++) {
445 len=strlen(ivac->console[y]);
446 move(ivac->display.max_y-IVAC_CONSOLE_LEN-IVAC_PROMPT_LEN+y,2);
448 addch(((ivac->console[y][x]>' ')||(ivac->console[y][x]<='~'))
449 ?ivac->console[y][x]:' ');
450 for(x=len;x<IVAC_CONSOLE_STRING_LEN-4;x++) addch(' ');
456 for(i=0;i<IVAC_CONSOLE_LEN;i++)
457 printf("[ivac] console line %d: %s\n",i,ivac->console[i]);
463 int ivac_display_prompt(t_display *display) {
468 move(display->max_y-3,0);
469 for(x=0;x<display->max_x;x++) addch('#');
470 mvaddstr(display->max_y-2,0,"## command: ");
471 mvaddstr(display->max_y-2,display->max_x-2,"##");
472 move(display->max_y-1,0);
473 for(x=0;x<display->max_x;x++) addch('#');
480 int ivac_display_prompt_content(t_ivac *ivac) {
485 /* delete old command */
486 if(ivac->input.c_count==0) {
487 move(ivac->display.max_y-2,12);
488 for(x=12;x<ivac->display.max_x-1;x++) addch(' ');
491 for(x=0;x<ivac->input.c_count;x++)
492 mvaddch(ivac->display.max_y-2,x+12,ivac->input.content[x]);
495 printf("%c",ivac->input.content[ivac->input.c_count-1]);
502 int ivac_display(t_display *display) {
507 ivac_display_head(display);
510 ivac_display_box(display);
512 /* display console */
513 ivac_display_console(display);
515 /* display command prompt */
516 ivac_display_prompt(display);
521 int ivac_display_content(t_ivac *ivac) {
523 /* display box content */
524 ivac_display_box_content(ivac);
526 /* display console content */
527 ivac_display_console_content(ivac);
529 /* display prompt content */
530 ivac_display_prompt_content(ivac);
535 int ivac_add_to_monitor(t_ivac *ivac,char *msg) {
539 for(i=0;i<IVAC_CONSOLE_LEN-1;i++)
540 memcpy(ivac->console[i],ivac->console[i+1],IVAC_CONSOLE_STRING_LEN);
541 memcpy(ivac->console[IVAC_CONSOLE_LEN-1],msg,IVAC_CONSOLE_STRING_LEN);