bugfixes, temp. removed ncurses display funtions, fixed command parser.
authorhackbard <hackbard>
Sat, 8 May 2004 20:02:12 +0000 (20:02 +0000)
committerhackbard <hackbard>
Sat, 8 May 2004 20:02:12 +0000 (20:02 +0000)
src/Makefile
src/input.c
src/input.h
src/ivac.c
src/ivac.h
src/network.c

index 396ebc8..f3b4e06 100644 (file)
@@ -2,7 +2,7 @@
 
 INCLUDEDIR = /usr/include
 
-CFLAGS = -DDEBUG -O3 -Wall
+CFLAGS = -DDISPLAY -DDEBUG -O3 -Wall
 LIBS = -lncurses
 
 OBJS = network.o event.o input.o display.o
index 30f60b9..b29688c 100644 (file)
@@ -9,25 +9,33 @@
 int input_init(t_input *input) {
 
   struct termios tios;
+  int size;
 
   puts("[input] initializing input system ...");
 
-  if((input->content=(char *)malloc(MAX_CONTENT))==NULL) {
+  size=((input->mode&CONTENT_BUFFER)?MAX_CONTENT:1);
+
+  if((input->content=(char *)malloc(size))==NULL) {
     perror("[input] malloc call");
     return I_ERROR;
   }
+    
+  memset(input->content,0,size);
   input->c_count=0;
 
   tcgetattr(0,&(input->tios));
   tios=input->tios;
 
-  if(!(input->mode&LINE_BUFFERED)) {
-    tios.c_lflag&=(~ICANON);
-    tios.c_lflag&=(~ECHO);
-    tios.c_cc[VTIME]=0;
-    tios.c_cc[VMIN]=1;
-    tcsetattr(0,TCSANOW,&tios);
-  }
+  /* general settings */
+  tios.c_iflag&=ICRNL; /* \r -> \n */
+  tios.c_cc[VTIME]=0; /* no timeout */
+  tios.c_cc[VMIN]=1; /* 1 char for non-can. mode */
+
+  /* depending on used modes */
+  if(!(input->mode&LINE_BUFFERED)) tios.c_lflag&=(~ICANON);
+  if(!(input->mode&INPUT_ECHO)) tios.c_lflag&=(~ECHO);
+
+  tcsetattr(0,TCSANOW,&tios);
 
   return I_SUCCESS;
 }
index b4929fb..6efcf6e 100644 (file)
@@ -22,6 +22,7 @@
 
 #define LINE_BUFFERED (1<<0)
 #define CONTENT_BUFFER (1<<1)
+#define INPUT_ECHO (1<<2)
 
 /* input specific variables */
 typedef struct s_input {
index 82b9a7c..8b8acbc 100644 (file)
@@ -21,6 +21,8 @@
  *
  */
 
+// #define DISPLAY
+
 #include "ivac.h"
 
 int main(int argc,char **argv) {
@@ -28,10 +30,12 @@ int main(int argc,char **argv) {
   /* TESTING BY NOW */
 
   t_ivac ivac;
+  int i;
 
   /* 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);
+  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;
@@ -40,10 +44,6 @@ int main(int argc,char **argv) {
   /* 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));
 
@@ -51,6 +51,8 @@ int main(int argc,char **argv) {
   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",
@@ -64,15 +66,19 @@ int main(int argc,char **argv) {
   event_math(0,&(ivac.event),READ,ADD);
 
   /* display init */
+#ifdef DISPLAY
   display_init(&(ivac.display));
+#endif
 
   /* display */
+#ifdef DISPLAY
   ivac_display(&(ivac.display));
+#endif
 
   /* 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;
 }
@@ -82,7 +88,9 @@ int ivac_shutdown(t_ivac *ivac) {
   network_shutdown(&(ivac->net));
   input_shutdown(&(ivac->input));
   event_stop(&(ivac->event));
+#ifdef DISPLAY
   display_shutdown(&(ivac->display));
+#endif
 
   return SUCCESS;
 }
@@ -182,7 +190,9 @@ int ivac_event_cb(t_event *event,void *ptr) {
     input_get_event(&(ivac->input),ivac_parse_command,ivac);
 
   /* display ivac gui */
+#ifdef DISPLAY
   ivac_display_content(ivac);
+#endif
     
   return SUCCESS;
 }
@@ -197,46 +207,143 @@ int ivac_regular_cb(t_event *event,void *ptr) {
 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;
+
+  printf("%c",data[input->c_count-1]);
+  fflush(NULL);
 
   /* parse command routines */
 
-  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);
+  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;
+    }
+
+    /* 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)) {
+        if(ivac->net.connection[channel].status&C_IN_USE) {
+          sprintf(c_str,"channel %02d: connection in use",channel);
+        }
+        else {
+          strncpy(ivac->net.connection[channel].ip,arg[2],IP_DIGITS);
+          ivac->net.connection[channel].port=atoi(arg[3]);
+          sprintf(c_str,"channel %02d: set connection info",channel);
+          ivac->net.connection[channel].status|=C_INFO_A;
+        }
+      } 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)) {
+        if(!ivac->net.connection[channel].status&C_INFO_A)
+          sprintf(c_str,"channel %02d: channel not configured",channel);
+        else {
+          if(ivac->net.connection[channel].status&C_IN_USE)
+            sprintf(c_str,"channel %02d: connection in use",channel);
+          else {
+            sprintf(c_str,"channel %02d: trying to connect to %s:%d",channel,
+                    ivac->net.connection[channel].ip,
+                    ivac->net.connection[channel].port);
           }
         }
-        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)) {
+        if(!ivac->net.connection[channel].status&C_ESTABL)
+          sprintf(c_str,"channel %02d: no active connection",channel);
+        else {
+          ivac->net.connection[channel].status|=C_HANGUP;
+          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)) {
+        ivac->net.sendmask|=(1<<channel);
+        sprintf(c_str,"selected channel %d",channel);
+      }
+      else if(arg[1][0]='*') {
+        ivac->net.sendmask=0xff;
+        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)) {
+        ivac->net.sendmask&=(~(1<<channel));
+        sprintf(c_str,"deselected channel %d",channel);
+      }
+      else if(arg[1][0]='*') {
+        ivac->net.sendmask=0;
+        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]);
+
+    ivac_add_to_monitor(ivac,c_str);
+      
 
     /* delete content buffer + reset counter */
-    memset(input->content,0,input->c_count);
+    memset(input->content,0,input->c_count-1);
     input->c_count=0;
 
   }
@@ -290,11 +397,16 @@ int ivac_display_console(t_display *display) {
 
 int ivac_display_console_content(t_ivac *ivac) {
 
-  int x,y,len;
+  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();
 
@@ -373,7 +485,12 @@ int ivac_add_to_monitor(t_ivac *ivac,char *msg) {
   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);
+
+  for(i=0;i<IVAC_CONSOLE_LEN;i++)
+    printf("[ivac] console line %d: %s\n",i,ivac->console[i]);
+#ifdef DISPLAY
   ivac_display_console_content(ivac);
+#endif
 
   return SUCCESS;
 }
index 9fb5263..a946580 100644 (file)
@@ -41,6 +41,9 @@
 #define IVAC_PROMPT_LEN 3
 #define IVAC_CONSOLE_LEN 8
 
+#define IVAC_ARG_LEN CHAR_USERNAME
+#define IVAC_ARG_COUNT 4
+
 /* variables */
 typedef struct s_challenger {
   char name[CHAR_USERNAME];
index 5fae09d..9ccb2b5 100644 (file)
@@ -101,9 +101,11 @@ int network_manage_connection(t_net *net) {
             perror("[network] socket call");
             return N_ERROR;
           }
+          net->connection[i].status|=C_SOCKET;
         }
 
-        if(!net->connection[i].status&C_ESTABL) {
+        if((!net->connection[i].status&C_ESTABL)&&
+           (net->connection[i].status&C_SOCKET)) {
 
           memset(&addr,0,sizeof(struct sockaddr));
           addr.sin_family=AF_INET;
@@ -121,6 +123,7 @@ int network_manage_connection(t_net *net) {
 
           printf("[network] established connection to %s port %d on channel %d\n",
                  net->connection[i].ip,net->connection[i].port,i);
+          net->connection[i].status|=C_ESTABL;
 
         }