*
*/
+// #define USE_NCURSES
+
#include "ivac.h"
int main(int argc,char **argv) {
/* TESTING BY NOW */
t_ivac ivac;
+ int i;
+
+ if(argc!=2) {
+ printf("\nusage: %s <username>\n\n",argv[0]);
+ return ERROR;
+ }
- /* set username (futur: read from config or entered later) */
- strcpy(ivac.username,"hackbard");
- memset(&ivac.console[0][0],0,IVAC_CONSOLE_LEN*IVAC_CONSOLE_STRING_LEN);
+ /* set username */
+ strncpy(ivac.username,argv[1],CHAR_USERNAME);
+ /* clear challenger struct */
+ for(i=0;i<MAX_CONNECTIONS;i++)
+ memset(&(ivac.challenger[i]),0,sizeof(t_challenger));
+ /* zero console buffer */
+ for(i=0;i<IVAC_CONSOLE_LEN;i++)
+ memset(ivac.console[i],0,IVAC_CONSOLE_STRING_LEN);
/* set capabilities (futur: set by check routines) */
ivac.g_cap=NETWORK;
/* set event timeout */
ivac.event.timeout.tv_sec=IVAC_S_SEC;
ivac.event.timeout.tv_usec=IVAC_S_USEC;
-
- /* set listen port (futur: read from config or entered later) */
- network_set_listen_port(&(ivac.net),IVAC_LISTEN_PORT);
-
/* event init */
event_init(&(ivac.event));
ivac.input.mode=CONTENT_BUFFER;
input_init(&(ivac.input));
+ /* set listen port (futur: read from config or entered later) */
+ network_set_listen_port(&(ivac.net),IVAC_LISTEN_PORT);
/* network init */
if(network_init(&(ivac.net))==N_ERROR) {
printf("[ivac] use 'fuser -n tcp %d' to determine the process to kill!\n",
event_math(0,&(ivac.event),READ,ADD);
/* display init */
- display_init(&(ivac.display));
+ // display_init(&(ivac.display));
/* display */
ivac_display(&(ivac.display));
/* start event system - callbacks used: ivac_event_cb + ivac_regular_cb */
event_start(&(ivac.event),(void *)&ivac,ivac_event_cb,ivac_regular_cb);
- ivac_shutdown(&ivac);
+ // ivac_shutdown(&ivac);
return SUCCESS;
}
network_shutdown(&(ivac->net));
input_shutdown(&(ivac->input));
event_stop(&(ivac->event));
- display_shutdown(&(ivac->display));
+ // display_shutdown(&(ivac->display));
return SUCCESS;
}
t_ivac *ivac;
int channel;
+ char c_str[IVAC_CONSOLE_STRING_LEN];
ivac=(t_ivac *)ptr;
+ /* incoming connection -- first contact => send info */
if(FD_ISSET(ivac->net.l_fd,&(event->rfds))) {
- /* manage incoming + send info */
channel=network_manage_incoming(&(ivac->net));
- event_math(ivac->net.connection[channel].fd,event,READ,ADD);
- ivac_send_info(channel,ivac);
+ if(channel==N_E_ACCEPT)
+ sprintf(c_str,"accept failed");
+ else if(channel==N_E_MAXC)
+ sprintf(c_str,"maximum connections reached");
+ else {
+ sprintf(c_str,"connection from %s port %d on channel %d",
+ ivac->net.connection[channel].ip,
+ ivac->net.connection[channel].port,channel);
+ ivac_add_to_monitor(ivac,c_str);
+ event_math(ivac->net.connection[channel].fd,event,READ,ADD);
+ ivac_send_info(channel,ivac);
+ }
+ ivac_display_content(ivac);
}
- /* receive info */
- for(channel=0;channel<MAX_CONNECTIONS;channel++)
- if(ivac->net.connection[channel].status&C_ESTABL)
+ /* wait for user info */
+ for(channel=0;channel<MAX_CONNECTIONS;channel++) {
+ if(ivac->net.connection[channel].status&C_ESTABL) {
+ /* remote is sending info */
if(FD_ISSET(ivac->net.connection[channel].fd,&(event->rfds)))
ivac_receive_info(channel,ivac);
+ ivac_display_content(ivac);
+ }
+ }
/* user interaction */
if(FD_ISSET(0,&(event->rfds)))
input_get_event(&(ivac->input),ivac_parse_command,ivac);
- /* display ivac gui */
- ivac_display_content(ivac);
-
return SUCCESS;
}
int ivac_parse_command(t_input *input,void *ptr) {
t_ivac *ivac;
- int channel,i;
+ int i,j,k;
int len;
- char *data,valid=0;
+ int channel;
+ char *data,valid;
char c_str[IVAC_CONSOLE_STRING_LEN];
+ char arg[IVAC_ARG_COUNT][IVAC_ARG_LEN];
ivac=(t_ivac *)ptr;
data=input->content;
- i=input->c_count-1;
- memset(c_str,0,IVAC_CONSOLE_STRING_LEN);
+ valid=0;
+
+ /* refresh prompt content only! */
+ ivac_display_prompt_content(ivac);
/* parse command routines */
+ if(data[input->c_count-1]=='\n') {
+
+ /* delete console string + args */
+ memset(c_str,0,IVAC_CONSOLE_STRING_LEN);
+ for(j=0;j<IVAC_ARG_COUNT;j++) memset(arg[j],0,IVAC_ARG_LEN);
+
+ /* get args */
+ len=0;
+ while(data[len]!='\n') len++;
+ i=0; j=0;
+ while((i<len) && (j<IVAC_ARG_COUNT)) {
+ k=0;
+ while((data[i+k]!=' ')&&(data[i+k]!='\n')) {
+ arg[j][k]=data[i+k];
+ k++;
+ }
+ arg[j][k]='\0';
+ j++;
+ /* skip all ' ' */
+ while(data[i+k]==' ') k++;
+ i+=k;
+ }
- if((data[i]=='\r') || (data[i]=='\n')) {
-
- /* parse commands */
- switch(data[0]) {
- case '/':
- len=strlen(data+1);
- if(len==4) {
- /* len 4 commands */
- if(!strncmp(data+1,"quit",4)) {
- valid=1;
- sprintf(c_str,"ivac shutdown\n");
- ivac_add_to_monitor(ivac,c_str);
- ivac_shutdown(ivac);
- }
+ /* parse command aka arg[0] */
+ if(!(strncmp(arg[0],"quit",4))) {
+ valid=1;
+ sprintf(c_str,"ivac shutdown ...");
+ ivac_shutdown(ivac);
+ }
+ if(!(strncmp(arg[0],"set",3))) {
+ valid=1;
+ if((arg[1][0]>='0')&&(arg[1][0]<='9')) channel=atoi(arg[1]);
+ else channel=-1;
+ if(!(strncmp(arg[1],"name",4))) {
+ strncpy(ivac->username,arg[2],CHAR_USERNAME);
+ sprintf(c_str,"changed username to %s",ivac->username);
+ }
+ else if((channel>=0)&&(channel<MAX_CONNECTIONS)) {
+ i=network_set_connection_info(&(ivac->net),channel,arg[2],atoi(arg[3]));
+ if(i==N_E_IN_USE)
+ sprintf(c_str,"channel %02d: connection in use",channel);
+ if(i==N_SUCCESS)
+ sprintf(c_str,"channel %02d: set connection info",channel);
+ } else snprintf(c_str,IVAC_CONSOLE_STRING_LEN,"unknown argument: '%s'",
+ arg[1]);
+ }
+ if(!(strncmp(arg[0],"connect",7))) {
+ valid=1;
+ if((arg[1][0]>='0')&&(arg[1][0]<='9')) channel=atoi(arg[1]);
+ else channel=-1;
+ if((channel>=0)&&(channel<MAX_CONNECTIONS)) {
+ i=network_connect(&(ivac->net),channel);
+ if(i==N_E_IN_USE)
+ sprintf(c_str,"channel %02d: connection in use",channel);
+ else if(i==N_E_NO_INFO)
+ sprintf(c_str,"channel %02d: channel not configured",channel);
+ else {
+ sprintf(c_str,"channel %02d: connected to %s:%d",channel,
+ ivac->net.connection[channel].ip,
+ ivac->net.connection[channel].port);
+ event_math(ivac->net.connection[channel].fd,&(ivac->event),READ,ADD);
+ ivac_send_info(channel,ivac);
}
- break;
- default:
- break;
+ }
+ else sprintf(c_str,"invalid argument: '%s'",arg[1]);
+ }
+ if(!(strncmp(arg[0],"close",5))) {
+ valid=1;
+ if((arg[1][0]>='0')&&(arg[1][0]<='9')) channel=atoi(arg[1]);
+ else channel=-1;
+ if((channel>=0)&&(channel<MAX_CONNECTIONS)) {
+ i=network_close(&(ivac->net),channel);
+ if(i==N_E_NC)
+ sprintf(c_str,"channel %02d: no active connection",channel);
+ else
+ sprintf(c_str,"channel %02d: connection closed",channel);
+ }
+ else sprintf(c_str,"invalid argument: '%s'",arg[1]);
}
-
- if(!valid) {
- snprintf(c_str,IVAC_CONSOLE_STRING_LEN-1,"unknown command: '%s...'\n",
- data);
- ivac_add_to_monitor(ivac,c_str);
+ if(!(strncmp(arg[0],"select",6))) {
+ valid=1;
+ if((arg[1][0]>='0')&&(arg[1][0]<='9')) channel=atoi(arg[1]);
+ else channel=-1;
+ if((channel>=0)&&(channel<MAX_CONNECTIONS)) {
+ network_select(&(ivac->net),channel);
+ sprintf(c_str,"selected channel %d",channel);
+ }
+ else if(arg[1][0]='*') {
+ network_select(&(ivac->net),MAX_CONNECTIONS);
+ strcpy(c_str,"selected all channels");
+ }
+ else sprintf(c_str,"invalid argument: '%s'",arg[1]);
+ }
+ if(!(strncmp(arg[0],"deselect",8))) {
+ valid=1;
+ if((arg[1][0]>='0')&&(arg[1][0]<='9')) channel=atoi(arg[1]);
+ else channel=-1;
+ if((channel>=0)&&(channel<MAX_CONNECTIONS)) {
+ network_deselect(&(ivac->net),channel);
+ sprintf(c_str,"deselected channel %d",channel);
+ }
+ else if(arg[1][0]='*') {
+ network_deselect(&(ivac->net),MAX_CONNECTIONS);
+ strcpy(c_str,"deselected all channels");
+ }
+ else sprintf(c_str,"invalid argument: '%s'",arg[1]);
}
+ if(!valid)
+ snprintf(c_str,IVAC_CONSOLE_STRING_LEN,"unknown command: '%s'",arg[0]);
+
+ /* call network functions */
+ network_manage_connection(&(ivac->net));
+
+ /* add console string to console buffer */
+ ivac_add_to_monitor(ivac,c_str);
+
+ /* refresh whole display content */
+ ivac_display_content(ivac);
+
/* delete content buffer + reset counter */
- memset(input->content,0,input->c_count);
+ memset(input->content,0,input->c_count-1);
input->c_count=0;
}
int ivac_display_head(t_display *display) {
+#ifdef USE_NCURSES
int x,y;
move(0,0);
move(2,0);
for(x=0;x<display->max_x;x++) addch('#');
refresh();
+#else
+ puts("#########################################################");
+ puts("##### ivac - - Copyright (C) 2004 Frank Zirkelbach #####");
+ puts("#########################################################");
+#endif
return SUCCESS;
}
int ivac_display_box(t_display *display) {
-
+
+#ifdef USE_NCURSES
int x,y;
for(y=IVAC_PROMPT_LEN;y<display->max_y-IVAC_PROMPT_LEN;y++) {
mvaddch(y,0,'#');
mvaddch(y,display->max_x-1,'#');
}
+#endif
return SUCCESS;
}
int ivac_display_box_content(t_ivac *ivac) {
+ int channel;
+
+ /* prepare challenger names */
+ for(channel=0;channel<MAX_CONNECTIONS;channel++)
+ if(ivac->challenger[channel].name[0]==0)
+ strcpy(ivac->challenger[channel].name,"<empty>");
+
+#ifdef USE_NCURSES
+#else
+ for(channel=0;channel<MAX_CONNECTIONS;channel++) {
+ if(ivac->net.connection[channel].status&C_INFO_A)
+ printf("channel %02d: ip:%s port:%d status: %02x - name: %s\n",channel,
+ ivac->net.connection[channel].ip,
+ ivac->net.connection[channel].port,
+ ivac->net.connection[channel].status,
+ ivac->challenger[channel].name);
+ }
+#endif
return SUCCESS;
}
int ivac_display_console(t_display *display) {
-
+
+#ifdef USE_NCURSES
int x,y;
move(display->max_y-IVAC_CONSOLE_LEN-IVAC_PROMPT_LEN-1,0);
- for(x=0;x<display->max_x;x++) addch('#');
+ for(x=0;x<display->max_x;x++) addch('#');
+#endif
return SUCCESS;
}
int ivac_display_console_content(t_ivac *ivac) {
- int x,y,len;
+#ifdef USE_NCURSES
+ int x,y;
+ int len;
+
for(y=0;y<IVAC_CONSOLE_LEN;y++) {
len=strlen(ivac->console[y]);
move(ivac->display.max_y-IVAC_CONSOLE_LEN-IVAC_PROMPT_LEN+y,2);
- for(x=0;x<IVAC_CONSOLE_STRING_LEN;x++) addch(ivac->console[y][x]);
+ for(x=0;x<len;x++)
+ addch(((ivac->console[y][x]>' ')||(ivac->console[y][x]<='~'))
+ ?ivac->console[y][x]:' ');
+ for(x=len;x<IVAC_CONSOLE_STRING_LEN-4;x++) addch(' ');
}
refresh();
+#else
+ int i;
+
+ for(i=0;i<IVAC_CONSOLE_LEN;i++)
+ printf("[ivac] console line %d: %s\n",i,ivac->console[i]);
+#endif
return SUCCESS;
}
int ivac_display_prompt(t_display *display) {
+#ifdef USE_NCURSES
int x,y;
move(display->max_y-3,0);
move(display->max_y-1,0);
for(x=0;x<display->max_x;x++) addch('#');
refresh();
+#endif
return SUCCESS;
}
int ivac_display_prompt_content(t_ivac *ivac) {
+#ifdef USE_NCURSES
int x,y;
/* delete old command */
for(x=0;x<ivac->input.c_count;x++)
mvaddch(ivac->display.max_y-2,x+12,ivac->input.content[x]);
refresh();
+#else
+ printf("%c",ivac->input.content[ivac->input.c_count-1]);
+ fflush(NULL);
+#endif
return SUCCESS;
}
for(i=0;i<IVAC_CONSOLE_LEN-1;i++)
memcpy(ivac->console[i],ivac->console[i+1],IVAC_CONSOLE_STRING_LEN);
memcpy(ivac->console[IVAC_CONSOLE_LEN-1],msg,IVAC_CONSOLE_STRING_LEN);
- ivac_display_console_content(ivac);
return SUCCESS;
}