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
28 int main(int argc,char **argv) {
35 /* set username (futur: read from config or entered later) */
36 strcpy(ivac.username,"hackbard");
37 for(i=0;i<IVAC_CONSOLE_LEN;i++)
38 memset(ivac.console[i],0,IVAC_CONSOLE_STRING_LEN);
40 /* set capabilities (futur: set by check routines) */
42 ivac.av_cap=AUDIO|VIDEO|DUPLEX;
44 /* set event timeout */
45 ivac.event.timeout.tv_sec=IVAC_S_SEC;
46 ivac.event.timeout.tv_usec=IVAC_S_USEC;
48 event_init(&(ivac.event));
51 ivac.input.mode=CONTENT_BUFFER;
52 input_init(&(ivac.input));
54 /* set listen port (futur: read from config or entered later) */
55 network_set_listen_port(&(ivac.net),IVAC_LISTEN_PORT);
57 if(network_init(&(ivac.net))==N_ERROR) {
58 printf("[ivac] use 'fuser -n tcp %d' to determine the process to kill!\n",
64 /* add listening port + stdin to (read) event system */
65 event_math(ivac.net.l_fd,&(ivac.event),READ,ADD);
66 event_math(0,&(ivac.event),READ,ADD);
70 display_init(&(ivac.display));
75 ivac_display(&(ivac.display));
78 /* start event system - callbacks used: ivac_event_cb + ivac_regular_cb */
79 event_start(&(ivac.event),(void *)&ivac,ivac_event_cb,ivac_regular_cb);
81 // ivac_shutdown(&ivac);
86 int ivac_shutdown(t_ivac *ivac) {
88 network_shutdown(&(ivac->net));
89 input_shutdown(&(ivac->input));
90 event_stop(&(ivac->event));
92 display_shutdown(&(ivac->display));
98 int ivac_send_info(int channel,t_ivac *ivac) {
100 char data[SEND_N_MAX];
103 size=strlen(ivac->username);
105 data[0]=IVAC_SEND_NAME;
107 strncpy(data+2,ivac->username,size);
110 data[size]=IVAC_SEND_G_CAP;
112 data[size+2]=ivac->g_cap;
115 data[size]=IVAC_SEND_AV_CAP;
117 data[size+2]=(ivac->av_cap)>>8;
118 data[size+3]=(ivac->av_cap)&0xff;
121 if(network_send(ivac->net.connection[channel].fd,data,size)==N_ERROR) {
122 puts("[ivac] ivac_send_info failed");
129 int ivac_receive_info(int channel,t_ivac *ivac) {
131 char data[SEND_N_MAX];
136 if((length=network_receive(ivac->net.connection[channel].fd,
137 data,SEND_N_MAX))==N_ERROR) {
138 puts("[ivac] ivac_receive_info failed");
142 while(length-count) {
143 switch(data[count]) {
145 strncpy(ivac->challenger[channel].name,data+count+2,data[count+1]);
146 ivac->challenger[channel].name[data[count+1]]='\0';
147 count+=(data[count+1]+2);
149 case IVAC_SEND_G_CAP:
150 ivac->challenger[channel].g_cap=data[count+2];
153 case IVAC_SEND_AV_CAP:
154 ivac->challenger[channel].av_cap=data[count+2]<<8;
155 ivac->challenger[channel].av_cap|=data[count+3];
159 puts("[ivac] ivac_receive_info, unknown character");
168 int ivac_event_cb(t_event *event,void *ptr) {
175 if(FD_ISSET(ivac->net.l_fd,&(event->rfds))) {
176 /* manage incoming + send info */
177 channel=network_manage_incoming(&(ivac->net));
178 event_math(ivac->net.connection[channel].fd,event,READ,ADD);
179 ivac_send_info(channel,ivac);
183 for(channel=0;channel<MAX_CONNECTIONS;channel++)
184 if(ivac->net.connection[channel].status&C_ESTABL)
185 if(FD_ISSET(ivac->net.connection[channel].fd,&(event->rfds)))
186 ivac_receive_info(channel,ivac);
188 /* user interaction */
189 if(FD_ISSET(0,&(event->rfds)))
190 input_get_event(&(ivac->input),ivac_parse_command,ivac);
192 /* display ivac gui */
194 ivac_display_content(ivac);
200 int ivac_regular_cb(t_event *event,void *ptr) {
202 /* usual jobs like audio & video transmit ... */
207 int ivac_parse_command(t_input *input,void *ptr) {
214 char c_str[IVAC_CONSOLE_STRING_LEN];
215 char arg[IVAC_ARG_COUNT][IVAC_ARG_LEN];
221 printf("%c",data[input->c_count-1]);
224 /* parse command routines */
226 if(data[input->c_count-1]=='\n') {
228 /* delete console string + args */
229 memset(c_str,0,IVAC_CONSOLE_STRING_LEN);
230 for(j=0;j<IVAC_ARG_COUNT;j++) memset(arg[j],0,IVAC_ARG_LEN);
235 while(data[len]!='\n') len++;
237 while((i<len) && (j<IVAC_ARG_COUNT)) {
239 while((data[i+k]!=' ')&&(data[i+k]!='\n')) {
246 while(data[i+k]==' ') k++;
250 /* parse command aka arg[0] */
251 if(!(strncmp(arg[0],"quit",4))) {
253 sprintf(c_str,"ivac shutdown ...");
256 if(!(strncmp(arg[0],"set",3))) {
258 if((arg[1][0]>='0')&&(arg[1][0]<='9')) channel=atoi(arg[1]);
260 if(!(strncmp(arg[1],"name",4))) {
261 strncpy(ivac->username,arg[2],CHAR_USERNAME);
262 sprintf(c_str,"changed username to %s",ivac->username);
264 else if((channel>=0)&&(channel<MAX_CONNECTIONS)) {
265 if(ivac->net.connection[channel].status&C_IN_USE) {
266 sprintf(c_str,"channel %02d: connection in use",channel);
269 strncpy(ivac->net.connection[channel].ip,arg[2],IP_DIGITS);
270 ivac->net.connection[channel].port=atoi(arg[3]);
271 sprintf(c_str,"channel %02d: set connection info",channel);
272 ivac->net.connection[channel].status|=C_INFO_A;
274 } else snprintf(c_str,IVAC_CONSOLE_STRING_LEN,"unknown argument: '%s'",
277 if(!(strncmp(arg[0],"connect",7))) {
279 if((arg[1][0]>='0')&&(arg[1][0]<='9')) channel=atoi(arg[1]);
281 if((channel>=0)&&(channel<MAX_CONNECTIONS)) {
282 if(!ivac->net.connection[channel].status&C_INFO_A)
283 sprintf(c_str,"channel %02d: channel not configured",channel);
285 if(ivac->net.connection[channel].status&C_IN_USE)
286 sprintf(c_str,"channel %02d: connection in use",channel);
288 sprintf(c_str,"channel %02d: trying to connect to %s:%d",channel,
289 ivac->net.connection[channel].ip,
290 ivac->net.connection[channel].port);
294 else sprintf(c_str,"invalid argument: '%s'",arg[1]);
296 if(!(strncmp(arg[0],"close",5))) {
298 if((arg[1][0]>='0')&&(arg[1][0]<='9')) channel=atoi(arg[1]);
300 if((channel>=0)&&(channel<MAX_CONNECTIONS)) {
301 if(!ivac->net.connection[channel].status&C_ESTABL)
302 sprintf(c_str,"channel %02d: no active connection",channel);
304 ivac->net.connection[channel].status|=C_HANGUP;
305 sprintf(c_str,"channel %02d: connection closed",channel);
308 else sprintf(c_str,"invalid argument: '%s'",arg[1]);
310 if(!(strncmp(arg[0],"select",6))) {
312 if((arg[1][0]>='0')&&(arg[1][0]<='9')) channel=atoi(arg[1]);
314 if((channel>=0)&&(channel<MAX_CONNECTIONS)) {
315 ivac->net.sendmask|=(1<<channel);
316 sprintf(c_str,"selected channel %d",channel);
318 else if(arg[1][0]='*') {
319 ivac->net.sendmask=0xff;
320 strcpy(c_str,"selected all channels");
322 else sprintf(c_str,"invalid argument: '%s'",arg[1]);
324 if(!(strncmp(arg[0],"deselect",8))) {
326 if((arg[1][0]>='0')&&(arg[1][0]<='9')) channel=atoi(arg[1]);
328 if((channel>=0)&&(channel<MAX_CONNECTIONS)) {
329 ivac->net.sendmask&=(~(1<<channel));
330 sprintf(c_str,"deselected channel %d",channel);
332 else if(arg[1][0]='*') {
333 ivac->net.sendmask=0;
334 strcpy(c_str,"deselected all channels");
336 else sprintf(c_str,"invalid argument: '%s'",arg[1]);
340 snprintf(c_str,IVAC_CONSOLE_STRING_LEN,"unknown command: '%s'",arg[0]);
342 ivac_add_to_monitor(ivac,c_str);
345 /* delete content buffer + reset counter */
346 memset(input->content,0,input->c_count-1);
354 int ivac_display_head(t_display *display) {
359 for(x=0;x<display->max_x;x++) addch('#');
361 mvaddstr(1,(display->max_x-4)/2-4,"- ivac -");
362 mvaddstr(1,(display->max_x-2),"##");
364 for(x=0;x<display->max_x;x++) addch('#');
370 int ivac_display_box(t_display *display) {
374 for(y=IVAC_PROMPT_LEN;y<display->max_y-IVAC_PROMPT_LEN;y++) {
376 mvaddch(y,display->max_x-1,'#');
382 int ivac_display_box_content(t_ivac *ivac) {
388 int ivac_display_console(t_display *display) {
392 move(display->max_y-IVAC_CONSOLE_LEN-IVAC_PROMPT_LEN-1,0);
393 for(x=0;x<display->max_x;x++) addch('#');
398 int ivac_display_console_content(t_ivac *ivac) {
403 for(y=0;y<IVAC_CONSOLE_LEN;y++) {
404 len=strlen(ivac->console[y]);
405 move(ivac->display.max_y-IVAC_CONSOLE_LEN-IVAC_PROMPT_LEN+y,2);
407 addch(((ivac->console[y][x]>' ')||(ivac->console[y][x]<='~'))
408 ?ivac->console[y][x]:' ');
409 for(x=len;x<IVAC_CONSOLE_STRING_LEN-4;x++) addch(' ');
416 int ivac_display_prompt(t_display *display) {
420 move(display->max_y-3,0);
421 for(x=0;x<display->max_x;x++) addch('#');
422 mvaddstr(display->max_y-2,0,"## command: ");
423 mvaddstr(display->max_y-2,display->max_x-2,"##");
424 move(display->max_y-1,0);
425 for(x=0;x<display->max_x;x++) addch('#');
431 int ivac_display_prompt_content(t_ivac *ivac) {
435 /* delete old command */
436 if(ivac->input.c_count==0) {
437 move(ivac->display.max_y-2,12);
438 for(x=12;x<ivac->display.max_x-1;x++) addch(' ');
441 for(x=0;x<ivac->input.c_count;x++)
442 mvaddch(ivac->display.max_y-2,x+12,ivac->input.content[x]);
448 int ivac_display(t_display *display) {
453 ivac_display_head(display);
456 ivac_display_box(display);
458 /* display console */
459 ivac_display_console(display);
461 /* display command prompt */
462 ivac_display_prompt(display);
467 int ivac_display_content(t_ivac *ivac) {
469 /* display box content */
470 ivac_display_box_content(ivac);
472 /* display console content */
473 ivac_display_console_content(ivac);
475 /* display prompt content */
476 ivac_display_prompt_content(ivac);
481 int ivac_add_to_monitor(t_ivac *ivac,char *msg) {
485 for(i=0;i<IVAC_CONSOLE_LEN-1;i++)
486 memcpy(ivac->console[i],ivac->console[i+1],IVAC_CONSOLE_STRING_LEN);
487 memcpy(ivac->console[IVAC_CONSOLE_LEN-1],msg,IVAC_CONSOLE_STRING_LEN);
489 for(i=0;i<IVAC_CONSOLE_LEN;i++)
490 printf("[ivac] console line %d: %s\n",i,ivac->console[i]);
492 ivac_display_console_content(ivac);