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
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");
37 puts("-d <device> \t specify audio device");
43 int main(int argc,char **argv) {
51 strcpy(ivac.username,"ivac");
52 ivac.net.l_port=IVAC_LISTEN_PORT;
53 strcpy(ivac.audio.dsp_dev,SOUND_DEVICE);
55 /* parse argv and change default values */
63 strncpy(ivac.username,argv[++i],CHAR_USERNAME);
66 ivac.net.l_port=atoi(argv[++i]);
69 strncpy(ivac.audio.dsp_dev,argv[++i],MAX_CHAR_DEVICE);
79 /* clear challenger struct */
80 for(i=0;i<MAX_CONNECTIONS;i++)
81 memset(&(ivac.challenger[i]),0,sizeof(t_challenger));
82 /* zero console buffer */
83 for(i=0;i<IVAC_CONSOLE_LEN;i++)
84 memset(ivac.console[i],0,IVAC_CONSOLE_STRING_LEN);
86 /* set capabilities (futur: set by check routines) */
88 ivac.av_cap=AUDIO|VIDEO|DUPLEX;
90 /* set event timeout */
91 ivac.event.timeout.tv_sec=IVAC_S_SEC;
92 ivac.event.timeout.tv_usec=IVAC_S_USEC;
94 event_init(&(ivac.event));
97 ivac.input.mode=CONTENT_BUFFER;
98 input_init(&(ivac.input));
101 if(network_init(&(ivac.net))==N_ERROR) {
102 printf("[ivac] use 'fuser -n tcp %d' to determine the process to kill!\n",
104 ivac_shutdown(&ivac);
108 /* add listening port + stdin to (read) event system */
109 event_math(ivac.net.l_fd,&(ivac.event),READ,ADD);
110 event_math(0,&(ivac.event),READ,ADD);
113 display_init(&(ivac.display));
115 /* use hardcoded audio settings by now */
116 ivac.audio.fmt=BIT_8;
117 ivac.audio.channels=MONO;
118 ivac.audio.speed=8000;
120 audio_init(&(ivac.audio));
121 audio_setup(&(ivac.audio));
124 ivac_display(&(ivac.display));
126 /* start event system - callbacks used: ivac_event_cb + ivac_regular_cb */
127 event_start(&(ivac.event),(void *)&ivac,ivac_event_cb,ivac_regular_cb);
132 int ivac_shutdown(t_ivac *ivac) {
134 network_shutdown(&(ivac->net));
135 input_shutdown(&(ivac->input));
136 event_stop(&(ivac->event));
137 audio_shutdown(&(ivac->audio));
138 display_shutdown(&(ivac->display));
143 int ivac_send_info(int channel,t_ivac *ivac) {
145 char data[SEND_N_MAX];
148 size=strlen(ivac->username);
150 data[0]=IVAC_SEND_NAME;
152 strncpy(data+2,ivac->username,size);
155 data[size]=IVAC_SEND_G_CAP;
157 data[size+2]=ivac->g_cap;
160 data[size]=IVAC_SEND_AV_CAP;
162 data[size+2]=(ivac->av_cap)>>8;
163 data[size+3]=(ivac->av_cap)&0xff;
166 if(network_send(ivac->net.connection[channel].fd,data,size)==N_ERROR) {
167 puts("[ivac] ivac_send_info failed");
174 int ivac_send_quit(int channel,t_ivac *ivac) {
176 char data[7]; /* one more for \0 */
178 data[0]=IVAC_SEND_QUIT;
180 strcpy(data+2,"quit");
182 if(network_send(ivac->net.connection[channel].fd,data,6)==N_ERROR) {
183 puts("[ivac] ivac_send_quit failed");
190 int ivac_receive_info(int channel,t_ivac *ivac) {
192 char data[SEND_N_MAX];
193 char c_str[IVAC_CONSOLE_STRING_LEN];
198 if((length=network_receive(ivac->net.connection[channel].fd,
199 data,SEND_N_MAX))==N_ERROR) {
200 puts("[ivac] ivac_receive_info failed");
204 while(length-count) {
205 switch(data[count]) {
207 strncpy(ivac->challenger[channel].name,data+count+2,data[count+1]);
208 ivac->challenger[channel].name[data[count+1]]='\0';
209 count+=(data[count+1]+2);
211 case IVAC_SEND_G_CAP:
212 ivac->challenger[channel].g_cap=data[count+2];
215 case IVAC_SEND_AV_CAP:
216 ivac->challenger[channel].av_cap=data[count+2]<<8;
217 ivac->challenger[channel].av_cap|=data[count+3];
221 if(!(strncmp(data+count+2,"quit",data[1])))
222 sprintf(c_str,"channel %02d: connection closed by remote host",
224 event_math(ivac->net.connection[channel].fd,&(ivac->event),
226 network_close(&(ivac->net),channel);
227 memset(&(ivac->challenger[channel]),0,sizeof(t_challenger));
228 ivac_add_to_monitor(ivac,c_str);
232 sprintf(c_str,"ivac_receive_info, unknown character: 0x%02x\n",
234 ivac_add_to_monitor(ivac,c_str);
243 int ivac_event_cb(t_event *event,void *ptr) {
247 char c_str[IVAC_CONSOLE_STRING_LEN];
251 /* incoming connection -- first contact => send info */
252 if(FD_ISSET(ivac->net.l_fd,&(event->rfds))) {
253 channel=network_manage_incoming(&(ivac->net));
254 if(channel==N_E_ACCEPT)
255 sprintf(c_str,"accept failed");
256 else if(channel==N_E_MAXC)
257 sprintf(c_str,"maximum connections reached");
259 sprintf(c_str,"connection from %s port %d on channel %d",
260 ivac->net.connection[channel].ip,
261 ivac->net.connection[channel].port,channel);
262 ivac_add_to_monitor(ivac,c_str);
263 event_math(ivac->net.connection[channel].fd,event,READ,ADD);
264 ivac_send_info(channel,ivac);
266 ivac_display_content(ivac);
269 /* wait for user info */
270 for(channel=0;channel<MAX_CONNECTIONS;channel++) {
271 if(ivac->net.connection[channel].status&C_ESTABL) {
272 /* remote is sending info */
273 if(FD_ISSET(ivac->net.connection[channel].fd,&(event->rfds))) {
274 if(ivac_receive_info(channel,ivac)==0) {
275 event_math(ivac->net.connection[channel].fd,event,READ,REMOVE);
276 network_close(&(ivac->net),channel);
277 sprintf(c_str,"channel %02d: broken pipe - disconnected",channel);
278 ivac_add_to_monitor(ivac,c_str);
280 ivac_display_content(ivac);
285 /* user interaction */
286 if(FD_ISSET(0,&(event->rfds)))
287 input_get_event(&(ivac->input),ivac_parse_command,ivac);
292 int ivac_regular_cb(t_event *event,void *ptr) {
294 /* usual jobs like audio & video transmit ... */
299 int ivac_parse_command(t_input *input,void *ptr) {
306 char c_str[IVAC_CONSOLE_STRING_LEN];
307 char arg[IVAC_ARG_COUNT][IVAC_ARG_LEN];
313 /* refresh prompt content only! */
314 ivac_display_prompt_content(ivac);
316 /* parse command routines */
317 if(data[input->c_count-1]=='\n') {
319 /* delete console string + args */
320 memset(c_str,0,IVAC_CONSOLE_STRING_LEN);
321 for(j=0;j<IVAC_ARG_COUNT;j++) memset(arg[j],0,IVAC_ARG_LEN);
325 while(data[len]!='\n') len++;
327 while((i<len) && (j<IVAC_ARG_COUNT)) {
329 while((data[i+k]!=' ')&&(data[i+k]!='\n')) {
336 while(data[i+k]==' ') k++;
340 /* parse command aka arg[0] */
341 if(!(strncmp(arg[0],"quit",4))) {
343 sprintf(c_str,"ivac shutdown ...");
344 for(i=0;i<MAX_CONNECTIONS;i++) {
345 if(ivac->net.connection[i].status&C_ESTABL) {
346 ivac_send_quit(i,ivac);
347 network_close(&(ivac->net),i);
352 if(!(strncmp(arg[0],"set",3))) {
354 if((arg[1][0]>='0')&&(arg[1][0]<='9')) channel=atoi(arg[1]);
356 if(!(strncmp(arg[1],"name",4))) {
357 strncpy(ivac->username,arg[2],CHAR_USERNAME);
358 sprintf(c_str,"changed username to %s",ivac->username);
360 else if((channel>=0)&&(channel<MAX_CONNECTIONS)) {
361 i=network_set_connection_info(&(ivac->net),channel,arg[2],atoi(arg[3]));
363 sprintf(c_str,"channel %02d: connection in use",channel);
365 sprintf(c_str,"channel %02d: set connection info",channel);
366 } else snprintf(c_str,IVAC_CONSOLE_STRING_LEN,"unknown argument: '%s'",
369 if(!(strncmp(arg[0],"connect",7))) {
371 if((arg[1][0]>='0')&&(arg[1][0]<='9')) channel=atoi(arg[1]);
373 if((channel>=0)&&(channel<MAX_CONNECTIONS)) {
374 i=network_connect(&(ivac->net),channel);
376 sprintf(c_str,"channel %02d: connection in use",channel);
377 else if(i==N_E_NO_INFO)
378 sprintf(c_str,"channel %02d: channel not configured",channel);
380 sprintf(c_str,"channel %02d: connected to %s:%d",channel,
381 ivac->net.connection[channel].ip,
382 ivac->net.connection[channel].port);
383 event_math(ivac->net.connection[channel].fd,&(ivac->event),READ,ADD);
384 ivac_send_info(channel,ivac);
387 else sprintf(c_str,"invalid argument: '%s'",arg[1]);
389 if(!(strncmp(arg[0],"close",5))) {
391 if((arg[1][0]>='0')&&(arg[1][0]<='9')) channel=atoi(arg[1]);
393 if((channel>=0)&&(channel<MAX_CONNECTIONS)) {
394 if(ivac->net.connection[channel].status&C_ESTABL) {
395 ivac_send_quit(channel,ivac);
396 event_math(ivac->net.connection[channel].fd,&(ivac->event),
398 network_close(&(ivac->net),channel);
399 sprintf(c_str,"channel %02d: connection closed",channel);
400 memset(&(ivac->challenger[channel]),0,sizeof(t_challenger));
403 sprintf(c_str,"channel %02d: no active connection",channel);
405 else sprintf(c_str,"invalid argument: '%s'",arg[1]);
407 if(!(strncmp(arg[0],"select",6))) {
409 if((arg[1][0]>='0')&&(arg[1][0]<='9')) channel=atoi(arg[1]);
411 if((channel>=0)&&(channel<MAX_CONNECTIONS)) {
412 network_select(&(ivac->net),channel);
413 sprintf(c_str,"selected channel %d",channel);
415 else if(arg[1][0]='*') {
416 network_select(&(ivac->net),MAX_CONNECTIONS);
417 strcpy(c_str,"selected all channels");
419 else sprintf(c_str,"invalid argument: '%s'",arg[1]);
421 if(!(strncmp(arg[0],"deselect",8))) {
423 if((arg[1][0]>='0')&&(arg[1][0]<='9')) channel=atoi(arg[1]);
425 if((channel>=0)&&(channel<MAX_CONNECTIONS)) {
426 network_deselect(&(ivac->net),channel);
427 sprintf(c_str,"deselected channel %d",channel);
429 else if(arg[1][0]='*') {
430 network_deselect(&(ivac->net),MAX_CONNECTIONS);
431 strcpy(c_str,"deselected all channels");
433 else sprintf(c_str,"invalid argument: '%s'",arg[1]);
437 snprintf(c_str,IVAC_CONSOLE_STRING_LEN,"unknown command: '%s'",arg[0]);
439 /* call network functions */
440 network_manage_connection(&(ivac->net));
442 /* add console string to console buffer */
443 ivac_add_to_monitor(ivac,c_str);
445 /* refresh whole display content */
446 ivac_display_content(ivac);
448 /* delete content buffer + reset counter */
449 memset(input->content,0,input->c_count-1);
457 int ivac_display_head(t_display *display) {
459 puts("#########################################################");
460 puts("##### ivac - - Copyright (C) 2004 Frank Zirkelbach #####");
461 puts("#########################################################");
466 int ivac_display_box(t_display *display) {
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>");
483 int ivac_display_console(t_display *display) {
488 int ivac_display_console_content(t_ivac *ivac) {
492 for(i=0;i<IVAC_CONSOLE_LEN;i++)
493 printf("[ivac] console line %d: %s\n",i,ivac->console[i]);
498 int ivac_display_prompt(t_display *display) {
503 int ivac_display_prompt_content(t_ivac *ivac) {
505 printf("%c",ivac->input.content[ivac->input.c_count-1]);
511 int ivac_display(t_display *display) {
516 ivac_display_head(display);
519 ivac_display_box(display);
521 /* display console */
522 ivac_display_console(display);
524 /* display command prompt */
525 ivac_display_prompt(display);
530 int ivac_display_content(t_ivac *ivac) {
532 /* display box content */
533 ivac_display_box_content(ivac);
535 /* display console content */
536 ivac_display_console_content(ivac);
538 /* display prompt content */
539 ivac_display_prompt_content(ivac);
544 int ivac_add_to_monitor(t_ivac *ivac,char *msg) {
548 for(i=0;i<IVAC_CONSOLE_LEN-1;i++)
549 memcpy(ivac->console[i],ivac->console[i+1],IVAC_CONSOLE_STRING_LEN);
550 memcpy(ivac->console[IVAC_CONSOLE_LEN-1],msg,IVAC_CONSOLE_STRING_LEN);