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
29 puts("usage: ivac <options>");
32 puts("-h \t\t show this help");
33 puts("-n <name> \t specify your name");
34 puts("-p <port> \t specify port to listen for incoming connections");
35 puts("-u <port> \t specify udp data port");
36 puts("-d <device> \t specify audio device");
42 int main(int argc,char **argv) {
50 strcpy(ivac.username,"ivac");
51 ivac.net.l_port=IVAC_LISTEN_PORT;
52 ivac.net.l_udp_port=IVAC_UDP_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 ivac.net.l_udp_port=atoi(argv[++i]);
72 strncpy(ivac.audio.dsp_dev,argv[++i],MAX_CHAR_DEVICE);
82 /* clear challenger struct */
83 for(i=0;i<MAX_CONNECTIONS;i++)
84 memset(&(ivac.challenger[i]),0,sizeof(t_challenger));
85 /* zero console buffer */
86 for(i=0;i<IVAC_CONSOLE_LEN;i++)
87 memset(ivac.console[i],0,IVAC_CONSOLE_STRING_LEN);
89 /* set capabilities (futur: set by check routines) */
91 ivac.av_cap=AUDIO|VIDEO|DUPLEX;
93 /* set event timeout */
94 ivac.event.timeout.tv_sec=IVAC_S_SEC;
95 ivac.event.timeout.tv_usec=IVAC_S_USEC;
97 event_init(&(ivac.event));
100 ivac.input.mode=CONTENT_BUFFER;
101 input_init(&(ivac.input));
104 if(network_init(&(ivac.net))==N_ERROR) {
105 printf("[ivac] use 'fuser -n tcp %d' to determine the process to kill!\n",
107 ivac_shutdown(&ivac);
110 network_udp_listen_init(&(ivac.net));
112 /* add listening port(s) + stdin to (read) event system */
113 event_math(ivac.net.l_fd,&(ivac.event),READ,ADD);
114 event_math(ivac.net.l_udp_fd,&(ivac.event),READ,ADD);
115 event_math(0,&(ivac.event),READ,ADD);
118 display_init(&(ivac.display));
120 /* use hardcoded audio settings by now */
121 ivac.audio.fmt=BIT_8;
122 ivac.audio.channels=MONO;
123 ivac.audio.speed=8000;
125 audio_init(&(ivac.audio));
126 audio_setup(&(ivac.audio));
129 ivac_display(&(ivac.display));
131 /* start event system - callbacks used: ivac_event_cb + ivac_regular_cb */
132 event_start(&(ivac.event),(void *)&ivac,ivac_event_cb,ivac_regular_cb);
137 int ivac_shutdown(t_ivac *ivac) {
139 network_shutdown(&(ivac->net));
140 network_udp_shutdown(&(ivac->net));
141 input_shutdown(&(ivac->input));
142 event_stop(&(ivac->event));
143 audio_shutdown(&(ivac->audio));
144 display_shutdown(&(ivac->display));
149 int ivac_send_info(int channel,t_ivac *ivac) {
151 char data[SEND_N_MAX];
154 size=strlen(ivac->username);
156 data[0]=IVAC_SEND_NAME;
158 strncpy(data+2,ivac->username,size);
161 data[size]=IVAC_SEND_G_CAP;
163 data[size+2]=ivac->g_cap;
166 data[size]=IVAC_SEND_AV_CAP;
168 data[size+2]=(ivac->av_cap)>>8;
169 data[size+3]=(ivac->av_cap)&0xff;
172 if(network_send(ivac->net.connection[channel].fd,data,size)==N_ERROR) {
173 puts("[ivac] ivac_send_info failed");
180 int ivac_send_quit(int channel,t_ivac *ivac) {
182 char data[7]; /* one more for \0 */
184 data[0]=IVAC_SEND_QUIT;
186 strcpy(data+2,"quit");
188 if(network_send(ivac->net.connection[channel].fd,data,6)==N_ERROR) {
189 puts("[ivac] ivac_send_quit failed");
196 int ivac_receive_info(int channel,t_ivac *ivac) {
198 char data[SEND_N_MAX];
199 char c_str[IVAC_CONSOLE_STRING_LEN];
204 if((length=network_receive(ivac->net.connection[channel].fd,
205 data,SEND_N_MAX))==N_ERROR) {
206 puts("[ivac] ivac_receive_info failed");
210 while(length-count) {
211 switch(data[count]) {
213 strncpy(ivac->challenger[channel].name,data+count+2,data[count+1]);
214 ivac->challenger[channel].name[data[count+1]]='\0';
215 count+=(data[count+1]+2);
217 case IVAC_SEND_G_CAP:
218 ivac->challenger[channel].g_cap=data[count+2];
221 case IVAC_SEND_AV_CAP:
222 ivac->challenger[channel].av_cap=data[count+2]<<8;
223 ivac->challenger[channel].av_cap|=data[count+3];
227 if(!(strncmp(data+count+2,"quit",data[1])))
228 sprintf(c_str,"channel %02d: connection closed by remote host",
230 event_math(ivac->net.connection[channel].fd,&(ivac->event),
232 network_close(&(ivac->net),channel);
233 memset(&(ivac->challenger[channel]),0,sizeof(t_challenger));
234 ivac_add_to_monitor(ivac,c_str);
238 sprintf(c_str,"ivac_receive_info, unknown character: 0x%02x\n",
240 ivac_add_to_monitor(ivac,c_str);
249 int ivac_event_cb(t_event *event,void *ptr) {
253 char c_str[IVAC_CONSOLE_STRING_LEN];
257 /* incoming connection -- first contact => send info */
258 if(FD_ISSET(ivac->net.l_fd,&(event->rfds))) {
259 channel=network_manage_incoming(&(ivac->net));
260 if(channel==N_E_ACCEPT)
261 sprintf(c_str,"accept failed");
262 else if(channel==N_E_MAXC)
263 sprintf(c_str,"maximum connections reached");
265 sprintf(c_str,"connection from %s port %d on channel %d",
266 ivac->net.connection[channel].ip,
267 ivac->net.connection[channel].port,channel);
268 ivac_add_to_monitor(ivac,c_str);
269 event_math(ivac->net.connection[channel].fd,event,READ,ADD);
270 ivac_send_info(channel,ivac);
272 ivac_display_content(ivac);
275 /* wait for user info */
276 for(channel=0;channel<MAX_CONNECTIONS;channel++) {
277 if(ivac->net.connection[channel].status&C_ESTABL) {
278 /* remote is sending info */
279 if(FD_ISSET(ivac->net.connection[channel].fd,&(event->rfds))) {
280 if(ivac_receive_info(channel,ivac)==0) {
281 event_math(ivac->net.connection[channel].fd,event,READ,REMOVE);
282 network_close(&(ivac->net),channel);
283 sprintf(c_str,"channel %02d: broken pipe - disconnected",channel);
284 ivac_add_to_monitor(ivac,c_str);
286 ivac_display_content(ivac);
291 /* user interaction */
292 if(FD_ISSET(0,&(event->rfds)))
293 input_get_event(&(ivac->input),ivac_parse_command,ivac);
298 int ivac_regular_cb(t_event *event,void *ptr) {
300 /* usual jobs like audio & video transmit ... */
305 int ivac_parse_command(t_input *input,void *ptr) {
312 char c_str[IVAC_CONSOLE_STRING_LEN];
313 char arg[IVAC_ARG_COUNT][IVAC_ARG_LEN];
319 /* refresh prompt content only! */
320 ivac_display_prompt_content(ivac);
322 /* parse command routines */
323 if(data[input->c_count-1]=='\n') {
325 /* delete console string + args */
326 memset(c_str,0,IVAC_CONSOLE_STRING_LEN);
327 for(j=0;j<IVAC_ARG_COUNT;j++) memset(arg[j],0,IVAC_ARG_LEN);
331 while(data[len]!='\n') len++;
333 while((i<len) && (j<IVAC_ARG_COUNT)) {
335 while((data[i+k]!=' ')&&(data[i+k]!='\n')) {
342 while(data[i+k]==' ') k++;
346 /* parse command aka arg[0] */
347 if(!(strncmp(arg[0],"quit",4))) {
349 sprintf(c_str,"ivac shutdown ...");
350 for(i=0;i<MAX_CONNECTIONS;i++) {
351 if(ivac->net.connection[i].status&C_ESTABL) {
352 ivac_send_quit(i,ivac);
353 network_close(&(ivac->net),i);
358 if(!(strncmp(arg[0],"set",3))) {
360 if((arg[1][0]>='0')&&(arg[1][0]<='9')) channel=atoi(arg[1]);
362 if(!(strncmp(arg[1],"name",4))) {
363 strncpy(ivac->username,arg[2],CHAR_USERNAME);
364 sprintf(c_str,"changed username to %s",ivac->username);
366 else if((channel>=0)&&(channel<MAX_CONNECTIONS)) {
367 i=network_set_connection_info(&(ivac->net),channel,arg[2],atoi(arg[3]));
369 sprintf(c_str,"channel %02d: connection in use",channel);
371 sprintf(c_str,"channel %02d: set connection info",channel);
372 } else snprintf(c_str,IVAC_CONSOLE_STRING_LEN,"unknown argument: '%s'",
375 if(!(strncmp(arg[0],"connect",7))) {
377 if((arg[1][0]>='0')&&(arg[1][0]<='9')) channel=atoi(arg[1]);
379 if((channel>=0)&&(channel<MAX_CONNECTIONS)) {
380 i=network_connect(&(ivac->net),channel);
382 sprintf(c_str,"channel %02d: connection in use",channel);
383 else if(i==N_E_NO_INFO)
384 sprintf(c_str,"channel %02d: channel not configured",channel);
386 sprintf(c_str,"channel %02d: connected to %s:%d",channel,
387 ivac->net.connection[channel].ip,
388 ivac->net.connection[channel].port);
389 event_math(ivac->net.connection[channel].fd,&(ivac->event),READ,ADD);
390 ivac_send_info(channel,ivac);
393 else sprintf(c_str,"invalid argument: '%s'",arg[1]);
395 if(!(strncmp(arg[0],"close",5))) {
397 if((arg[1][0]>='0')&&(arg[1][0]<='9')) channel=atoi(arg[1]);
399 if((channel>=0)&&(channel<MAX_CONNECTIONS)) {
400 if(ivac->net.connection[channel].status&C_ESTABL) {
401 ivac_send_quit(channel,ivac);
402 event_math(ivac->net.connection[channel].fd,&(ivac->event),
404 network_close(&(ivac->net),channel);
405 sprintf(c_str,"channel %02d: connection closed",channel);
406 memset(&(ivac->challenger[channel]),0,sizeof(t_challenger));
409 sprintf(c_str,"channel %02d: no active connection",channel);
411 else sprintf(c_str,"invalid argument: '%s'",arg[1]);
413 if(!(strncmp(arg[0],"select",6))) {
415 if((arg[1][0]>='0')&&(arg[1][0]<='9')) channel=atoi(arg[1]);
417 if((channel>=0)&&(channel<MAX_CONNECTIONS)) {
418 network_select(&(ivac->net),channel);
419 sprintf(c_str,"selected channel %d",channel);
421 else if(arg[1][0]='*') {
422 network_select(&(ivac->net),MAX_CONNECTIONS);
423 strcpy(c_str,"selected all channels");
425 else sprintf(c_str,"invalid argument: '%s'",arg[1]);
427 if(!(strncmp(arg[0],"deselect",8))) {
429 if((arg[1][0]>='0')&&(arg[1][0]<='9')) channel=atoi(arg[1]);
431 if((channel>=0)&&(channel<MAX_CONNECTIONS)) {
432 network_deselect(&(ivac->net),channel);
433 sprintf(c_str,"deselected channel %d",channel);
435 else if(arg[1][0]='*') {
436 network_deselect(&(ivac->net),MAX_CONNECTIONS);
437 strcpy(c_str,"deselected all channels");
439 else sprintf(c_str,"invalid argument: '%s'",arg[1]);
443 snprintf(c_str,IVAC_CONSOLE_STRING_LEN,"unknown command: '%s'",arg[0]);
445 /* call network functions */
446 network_manage_connection(&(ivac->net));
448 /* add console string to console buffer */
449 ivac_add_to_monitor(ivac,c_str);
451 /* refresh whole display content */
452 ivac_display_content(ivac);
454 /* delete content buffer + reset counter */
455 memset(input->content,0,input->c_count-1);
463 int ivac_display_head(t_display *display) {
465 puts("#########################################################");
466 puts("##### ivac - - Copyright (C) 2004 Frank Zirkelbach #####");
467 puts("#########################################################");
472 int ivac_display_box(t_display *display) {
477 int ivac_display_box_content(t_ivac *ivac) {
481 /* prepare challenger names */
482 for(channel=0;channel<MAX_CONNECTIONS;channel++)
483 if(ivac->challenger[channel].name[0]==0)
484 strcpy(ivac->challenger[channel].name,"<empty>");
489 int ivac_display_console(t_display *display) {
494 int ivac_display_console_content(t_ivac *ivac) {
498 for(i=0;i<IVAC_CONSOLE_LEN;i++)
499 printf("[ivac] console line %d: %s\n",i,ivac->console[i]);
504 int ivac_display_prompt(t_display *display) {
509 int ivac_display_prompt_content(t_ivac *ivac) {
511 printf("%c",ivac->input.content[ivac->input.c_count-1]);
517 int ivac_display(t_display *display) {
522 ivac_display_head(display);
525 ivac_display_box(display);
527 /* display console */
528 ivac_display_console(display);
530 /* display command prompt */
531 ivac_display_prompt(display);
536 int ivac_display_content(t_ivac *ivac) {
538 /* display box content */
539 ivac_display_box_content(ivac);
541 /* display console content */
542 ivac_display_console_content(ivac);
544 /* display prompt content */
545 ivac_display_prompt_content(ivac);
550 int ivac_add_to_monitor(t_ivac *ivac,char *msg) {
554 for(i=0;i<IVAC_CONSOLE_LEN-1;i++)
555 memcpy(ivac->console[i],ivac->console[i+1],IVAC_CONSOLE_STRING_LEN);
556 memcpy(ivac->console[IVAC_CONSOLE_LEN-1],msg,IVAC_CONSOLE_STRING_LEN);