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));
123 ivac_display(&(ivac.display));
125 /* start event system - callbacks used: ivac_event_cb + ivac_regular_cb */
126 event_start(&(ivac.event),(void *)&ivac,ivac_event_cb,ivac_regular_cb);
131 int ivac_shutdown(t_ivac *ivac) {
133 network_shutdown(&(ivac->net));
134 input_shutdown(&(ivac->input));
135 event_stop(&(ivac->event));
136 audio_shutdown(&(ivac->audio));
137 display_shutdown(&(ivac->display));
142 int ivac_send_info(int channel,t_ivac *ivac) {
144 char data[SEND_N_MAX];
147 size=strlen(ivac->username);
149 data[0]=IVAC_SEND_NAME;
151 strncpy(data+2,ivac->username,size);
154 data[size]=IVAC_SEND_G_CAP;
156 data[size+2]=ivac->g_cap;
159 data[size]=IVAC_SEND_AV_CAP;
161 data[size+2]=(ivac->av_cap)>>8;
162 data[size+3]=(ivac->av_cap)&0xff;
165 if(network_send(ivac->net.connection[channel].fd,data,size)==N_ERROR) {
166 puts("[ivac] ivac_send_info failed");
173 int ivac_send_quit(int channel,t_ivac *ivac) {
175 char data[7]; /* one more for \0 */
177 data[0]=IVAC_SEND_QUIT;
179 strcpy(data+2,"quit");
181 if(network_send(ivac->net.connection[channel].fd,data,6)==N_ERROR) {
182 puts("[ivac] ivac_send_quit failed");
189 int ivac_receive_info(int channel,t_ivac *ivac) {
191 char data[SEND_N_MAX];
192 char c_str[IVAC_CONSOLE_STRING_LEN];
197 if((length=network_receive(ivac->net.connection[channel].fd,
198 data,SEND_N_MAX))==N_ERROR) {
199 puts("[ivac] ivac_receive_info failed");
203 while(length-count) {
204 switch(data[count]) {
206 strncpy(ivac->challenger[channel].name,data+count+2,data[count+1]);
207 ivac->challenger[channel].name[data[count+1]]='\0';
208 count+=(data[count+1]+2);
210 case IVAC_SEND_G_CAP:
211 ivac->challenger[channel].g_cap=data[count+2];
214 case IVAC_SEND_AV_CAP:
215 ivac->challenger[channel].av_cap=data[count+2]<<8;
216 ivac->challenger[channel].av_cap|=data[count+3];
220 if(!(strncmp(data+count+2,"quit",data[1])))
221 sprintf(c_str,"channel %02d: connection closed by remote host",
223 event_math(ivac->net.connection[channel].fd,&(ivac->event),
225 network_close(&(ivac->net),channel);
226 memset(&(ivac->challenger[channel]),0,sizeof(t_challenger));
227 ivac_add_to_monitor(ivac,c_str);
231 sprintf(c_str,"ivac_receive_info, unknown character: 0x%02x\n",
233 ivac_add_to_monitor(ivac,c_str);
242 int ivac_event_cb(t_event *event,void *ptr) {
246 char c_str[IVAC_CONSOLE_STRING_LEN];
250 /* incoming connection -- first contact => send info */
251 if(FD_ISSET(ivac->net.l_fd,&(event->rfds))) {
252 channel=network_manage_incoming(&(ivac->net));
253 if(channel==N_E_ACCEPT)
254 sprintf(c_str,"accept failed");
255 else if(channel==N_E_MAXC)
256 sprintf(c_str,"maximum connections reached");
258 sprintf(c_str,"connection from %s port %d on channel %d",
259 ivac->net.connection[channel].ip,
260 ivac->net.connection[channel].port,channel);
261 ivac_add_to_monitor(ivac,c_str);
262 event_math(ivac->net.connection[channel].fd,event,READ,ADD);
263 ivac_send_info(channel,ivac);
265 ivac_display_content(ivac);
268 /* wait for user info */
269 for(channel=0;channel<MAX_CONNECTIONS;channel++) {
270 if(ivac->net.connection[channel].status&C_ESTABL) {
271 /* remote is sending info */
272 if(FD_ISSET(ivac->net.connection[channel].fd,&(event->rfds))) {
273 if(ivac_receive_info(channel,ivac)==0) {
274 event_math(ivac->net.connection[channel].fd,event,READ,REMOVE);
275 network_close(&(ivac->net),channel);
276 sprintf(c_str,"channel %02d: broken pipe - disconnected",channel);
277 ivac_add_to_monitor(ivac,c_str);
279 ivac_display_content(ivac);
284 /* user interaction */
285 if(FD_ISSET(0,&(event->rfds)))
286 input_get_event(&(ivac->input),ivac_parse_command,ivac);
291 int ivac_regular_cb(t_event *event,void *ptr) {
293 /* usual jobs like audio & video transmit ... */
298 int ivac_parse_command(t_input *input,void *ptr) {
305 char c_str[IVAC_CONSOLE_STRING_LEN];
306 char arg[IVAC_ARG_COUNT][IVAC_ARG_LEN];
312 /* refresh prompt content only! */
313 ivac_display_prompt_content(ivac);
315 /* parse command routines */
316 if(data[input->c_count-1]=='\n') {
318 /* delete console string + args */
319 memset(c_str,0,IVAC_CONSOLE_STRING_LEN);
320 for(j=0;j<IVAC_ARG_COUNT;j++) memset(arg[j],0,IVAC_ARG_LEN);
324 while(data[len]!='\n') len++;
326 while((i<len) && (j<IVAC_ARG_COUNT)) {
328 while((data[i+k]!=' ')&&(data[i+k]!='\n')) {
335 while(data[i+k]==' ') k++;
339 /* parse command aka arg[0] */
340 if(!(strncmp(arg[0],"quit",4))) {
342 sprintf(c_str,"ivac shutdown ...");
343 for(i=0;i<MAX_CONNECTIONS;i++) {
344 if(ivac->net.connection[i].status&C_ESTABL) {
345 ivac_send_quit(i,ivac);
346 network_close(&(ivac->net),i);
351 if(!(strncmp(arg[0],"set",3))) {
353 if((arg[1][0]>='0')&&(arg[1][0]<='9')) channel=atoi(arg[1]);
355 if(!(strncmp(arg[1],"name",4))) {
356 strncpy(ivac->username,arg[2],CHAR_USERNAME);
357 sprintf(c_str,"changed username to %s",ivac->username);
359 else if((channel>=0)&&(channel<MAX_CONNECTIONS)) {
360 i=network_set_connection_info(&(ivac->net),channel,arg[2],atoi(arg[3]));
362 sprintf(c_str,"channel %02d: connection in use",channel);
364 sprintf(c_str,"channel %02d: set connection info",channel);
365 } else snprintf(c_str,IVAC_CONSOLE_STRING_LEN,"unknown argument: '%s'",
368 if(!(strncmp(arg[0],"connect",7))) {
370 if((arg[1][0]>='0')&&(arg[1][0]<='9')) channel=atoi(arg[1]);
372 if((channel>=0)&&(channel<MAX_CONNECTIONS)) {
373 i=network_connect(&(ivac->net),channel);
375 sprintf(c_str,"channel %02d: connection in use",channel);
376 else if(i==N_E_NO_INFO)
377 sprintf(c_str,"channel %02d: channel not configured",channel);
379 sprintf(c_str,"channel %02d: connected to %s:%d",channel,
380 ivac->net.connection[channel].ip,
381 ivac->net.connection[channel].port);
382 event_math(ivac->net.connection[channel].fd,&(ivac->event),READ,ADD);
383 ivac_send_info(channel,ivac);
386 else sprintf(c_str,"invalid argument: '%s'",arg[1]);
388 if(!(strncmp(arg[0],"close",5))) {
390 if((arg[1][0]>='0')&&(arg[1][0]<='9')) channel=atoi(arg[1]);
392 if((channel>=0)&&(channel<MAX_CONNECTIONS)) {
393 if(ivac->net.connection[channel].status&C_ESTABL) {
394 ivac_send_quit(channel,ivac);
395 event_math(ivac->net.connection[channel].fd,&(ivac->event),
397 network_close(&(ivac->net),channel);
398 sprintf(c_str,"channel %02d: connection closed",channel);
399 memset(&(ivac->challenger[channel]),0,sizeof(t_challenger));
402 sprintf(c_str,"channel %02d: no active connection",channel);
404 else sprintf(c_str,"invalid argument: '%s'",arg[1]);
406 if(!(strncmp(arg[0],"select",6))) {
408 if((arg[1][0]>='0')&&(arg[1][0]<='9')) channel=atoi(arg[1]);
410 if((channel>=0)&&(channel<MAX_CONNECTIONS)) {
411 network_select(&(ivac->net),channel);
412 sprintf(c_str,"selected channel %d",channel);
414 else if(arg[1][0]='*') {
415 network_select(&(ivac->net),MAX_CONNECTIONS);
416 strcpy(c_str,"selected all channels");
418 else sprintf(c_str,"invalid argument: '%s'",arg[1]);
420 if(!(strncmp(arg[0],"deselect",8))) {
422 if((arg[1][0]>='0')&&(arg[1][0]<='9')) channel=atoi(arg[1]);
424 if((channel>=0)&&(channel<MAX_CONNECTIONS)) {
425 network_deselect(&(ivac->net),channel);
426 sprintf(c_str,"deselected channel %d",channel);
428 else if(arg[1][0]='*') {
429 network_deselect(&(ivac->net),MAX_CONNECTIONS);
430 strcpy(c_str,"deselected all channels");
432 else sprintf(c_str,"invalid argument: '%s'",arg[1]);
436 snprintf(c_str,IVAC_CONSOLE_STRING_LEN,"unknown command: '%s'",arg[0]);
438 /* call network functions */
439 network_manage_connection(&(ivac->net));
441 /* add console string to console buffer */
442 ivac_add_to_monitor(ivac,c_str);
444 /* refresh whole display content */
445 ivac_display_content(ivac);
447 /* delete content buffer + reset counter */
448 memset(input->content,0,input->c_count-1);
456 int ivac_display_head(t_display *display) {
458 puts("#########################################################");
459 puts("##### ivac - - Copyright (C) 2004 Frank Zirkelbach #####");
460 puts("#########################################################");
465 int ivac_display_box(t_display *display) {
470 int ivac_display_box_content(t_ivac *ivac) {
474 /* prepare challenger names */
475 for(channel=0;channel<MAX_CONNECTIONS;channel++)
476 if(ivac->challenger[channel].name[0]==0)
477 strcpy(ivac->challenger[channel].name,"<empty>");
482 int ivac_display_console(t_display *display) {
487 int ivac_display_console_content(t_ivac *ivac) {
491 for(i=0;i<IVAC_CONSOLE_LEN;i++)
492 printf("[ivac] console line %d: %s\n",i,ivac->console[i]);
497 int ivac_display_prompt(t_display *display) {
502 int ivac_display_prompt_content(t_ivac *ivac) {
504 printf("%c",ivac->input.content[ivac->input.c_count-1]);
510 int ivac_display(t_display *display) {
515 ivac_display_head(display);
518 ivac_display_box(display);
520 /* display console */
521 ivac_display_console(display);
523 /* display command prompt */
524 ivac_display_prompt(display);
529 int ivac_display_content(t_ivac *ivac) {
531 /* display box content */
532 ivac_display_box_content(ivac);
534 /* display console content */
535 ivac_display_console_content(ivac);
537 /* display prompt content */
538 ivac_display_prompt_content(ivac);
543 int ivac_add_to_monitor(t_ivac *ivac,char *msg) {
547 for(i=0;i<IVAC_CONSOLE_LEN-1;i++)
548 memcpy(ivac->console[i],ivac->console[i+1],IVAC_CONSOLE_STRING_LEN);
549 memcpy(ivac->console[IVAC_CONSOLE_LEN-1],msg,IVAC_CONSOLE_STRING_LEN);