[Olsr-cvs] olsrd-current/src/olsr_switch main.c, 1.20, 1.21 ohs_cmd.c, 1.16, 1.17 ohs_cmd.h, 1.4, 1.5

Thomas Lopatic (spam-protected)
Sun Oct 23 21:01:06 CEST 2005


Update of /cvsroot/olsrd/olsrd-current/src/olsr_switch
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv23711/src/olsr_switch

Modified Files:
	main.c ohs_cmd.c ohs_cmd.h 
Log Message:
olsr_switch looks okay on Windows. Windows port uses correct MTU. With LQ
enabled minimal HELLO-validity-time is HELLO-interval x LQ-Window-Size.


Index: ohs_cmd.c
===================================================================
RCS file: /cvsroot/olsrd/olsrd-current/src/olsr_switch/ohs_cmd.c,v
retrieving revision 1.16
retrieving revision 1.17
diff -C2 -d -r1.16 -r1.17
*** ohs_cmd.c	4 Aug 2005 18:57:11 -0000	1.16
--- ohs_cmd.c	23 Oct 2005 19:01:04 -0000	1.17
***************
*** 56,62 ****
  
  
- #define ARG_BUF_SIZE 500
- static char arg_buf[ARG_BUF_SIZE];
- 
  #define TOK_BUF_SIZE 500
  static char tok_buf[TOK_BUF_SIZE];
--- 56,59 ----
***************
*** 65,84 ****
  static char olsrd_path[FILENAME_MAX];
  
- static void
- get_arg_buf(FILE *handle, char *buf, size_t size)
- {
-   char c = 0;
-   int pos = 0;
- 
-   while(((c = fgetc(handle)) != '\n') &&
- 	pos < ((int)size - 2))
-     {
-       buf[pos] = c;
-       pos++;
-     }
- 
-   buf[pos] = 0;
- }
- 
  static int
  get_next_token(char *src, char *dst, size_t buflen)
--- 62,65 ----
***************
*** 115,119 ****
  
  int
! ohs_cmd_olsrd(FILE *handle, char *args)
  {
  #ifdef WIN32
--- 96,100 ----
  
  int
! ohs_cmd_olsrd(char *args)
  {
  #ifdef WIN32
***************
*** 251,255 ****
  
  int
! ohs_cmd_link(FILE *handle, char *args)
  {
    olsr_u8_t bi = 0, wildc_src = 0, wildc_dst = 0;
--- 232,236 ----
  
  int
! ohs_cmd_link(char *args)
  {
    olsr_u8_t bi = 0, wildc_src = 0, wildc_dst = 0;
***************
*** 405,409 ****
  
  int
! ohs_cmd_list(FILE *handle, char *args)
  {
    struct ohs_connection *oc = ohs_conns;
--- 386,390 ----
  
  int
! ohs_cmd_list(char *args)
  {
    struct ohs_connection *oc = ohs_conns;
***************
*** 454,458 ****
  
  int
! ohs_cmd_help(FILE *handle, char *args)
  {
    int i;
--- 435,439 ----
  
  int
! ohs_cmd_help(char *args)
  {
    int i;
***************
*** 494,498 ****
  
  int
! ohs_cmd_log(FILE *handle, char *args)
  {
    olsr_u8_t set = 0;
--- 475,479 ----
  
  int
! ohs_cmd_log(char *args)
  {
    olsr_u8_t set = 0;
***************
*** 555,559 ****
  
  int
! ohs_cmd_exit(FILE *handle, char *args)
  {
  
--- 536,540 ----
  
  int
! ohs_cmd_exit(char *args)
  {
  
***************
*** 564,599 ****
  }
  
! int
! ohs_parse_command(FILE *handle)
  {
!   char input_data[100];
    int i;
  
!   fscanf(handle, "%s", input_data);
  
!   get_arg_buf(handle, arg_buf, ARG_BUF_SIZE);
  
!   for(i = 0; ohs_commands[i].cmd; i++)
      {
!       if(!strcmp(input_data, ohs_commands[i].cmd))
! 	{
! 	  if(ohs_commands[i].cmd_cb)
! 	    {
! 	      ohs_commands[i].cmd_cb(handle, arg_buf);
! 	    }
! 	  else
! 	    {
! 	      printf("No action registered on cmd %s!\n", input_data);
! 	    }
! 	  break;
! 	}
      }
    
!   if(!ohs_commands[i].cmd)
!     {
!       printf("%s: no such cmd!\n", input_data);
!     }
! 
  
!   return 0;
  }
--- 545,632 ----
  }
  
! void
! ohs_parse_command(void)
  {
!   static char cmd_line[500];
!   static int cmd_len = 0;
!   char c;
!   char *args;
!   char cmd_token[20];
    int i;
+ #if defined WIN32
+   unsigned long Read;
+   INPUT_RECORD InRec;
+   KEY_EVENT_RECORD *KeyEventRec;
+ #endif
  
! #if defined WIN32
!   if (!ReadConsoleInput(GetStdHandle(STD_INPUT_HANDLE), &InRec, sizeof (InRec), &Read))
!   {
!     fprintf(stderr, "ReadConsoleInput failed: %s\n", strerror(GetLastError()));
!     return;
!   }
  
!   if (InRec.EventType != KEY_EVENT)
!     return;
  
!   KeyEventRec = &InRec.Event.KeyEvent;
! 
!   if (!KeyEventRec->bKeyDown)
!     return;
! 
!   c = KeyEventRec->uChar.AsciiChar;
! 
!   if (c == 8)
!   {
!     if (cmd_len == 0)
!       return;
! 
!     cmd_len--;
! 
!     fputc(8, stdout);
!     fputc(32, stdout);
!     fputc(8, stdout);
! 
!     fflush(stdout);
! 
!     return;
!   }
! 
!   fputc(c, stdout);
!   fflush(stdout);
! 
! #else
!   c = fgetc(stdin);
! #endif
! 
!   if (c != '\n' && c != '\r' && cmd_len < (int)sizeof (cmd_line) - 1)
!     cmd_line[cmd_len++] = (char)c;
! 
!   else
!   {
!     cmd_line[cmd_len] = 0;
!     cmd_len = 0;
! 
!     args = cmd_line + get_next_token(cmd_line, cmd_token, sizeof (cmd_token));
! 
!     for (i = 0; ohs_commands[i].cmd != NULL; i++)
      {
!       if (strcmp(cmd_token, ohs_commands[i].cmd) == 0)
!       {
!         if(ohs_commands[i].cmd_cb != NULL)
!           ohs_commands[i].cmd_cb(args);
! 
!         else
!           printf("No action registered on cmd %s!\n", cmd_token);
! 
!         break;
!       }
      }
    
!     if(ohs_commands[i].cmd == NULL)
!       printf("%s: no such cmd!\n", cmd_token);
  
!     printf("\n> ");
!     fflush(stdout);
!   }
  }

Index: ohs_cmd.h
===================================================================
RCS file: /cvsroot/olsrd/olsrd-current/src/olsr_switch/ohs_cmd.h,v
retrieving revision 1.4
retrieving revision 1.5
diff -C2 -d -r1.4 -r1.5
*** ohs_cmd.h	4 Jun 2005 21:07:33 -0000	1.4
--- ohs_cmd.h	23 Oct 2005 19:01:04 -0000	1.5
***************
*** 53,57 ****
    char *helptext_brief;
    char *helptext_long;
!   int (*cmd_cb)(FILE *, char *);
  };
  
--- 53,57 ----
    char *helptext_brief;
    char *helptext_long;
!   int (*cmd_cb)(char *);
  };
  
***************
*** 59,82 ****
  ohs_set_olsrd_path(char *);
  
! int
! ohs_parse_command(FILE *);
  
  int
! ohs_cmd_olsrd(FILE *, char *);
  
  int
! ohs_cmd_list(FILE *, char *);
  
  int
! ohs_cmd_help(FILE *, char *);
  
  int
! ohs_cmd_exit(FILE *, char *);
  
  int
! ohs_cmd_log(FILE *, char *);
  
  int
! ohs_cmd_link(FILE *, char *);
  
  #endif
--- 59,82 ----
  ohs_set_olsrd_path(char *);
  
! void
! ohs_parse_command(void);
  
  int
! ohs_cmd_olsrd(char *);
  
  int
! ohs_cmd_list(char *);
  
  int
! ohs_cmd_help(char *);
  
  int
! ohs_cmd_exit(char *);
  
  int
! ohs_cmd_log(char *);
  
  int
! ohs_cmd_link(char *);
  
  #endif

Index: main.c
===================================================================
RCS file: /cvsroot/olsrd/olsrd-current/src/olsr_switch/main.c,v
retrieving revision 1.20
retrieving revision 1.21
diff -C2 -d -r1.20 -r1.21
*** main.c	5 Aug 2005 15:15:19 -0000	1.20
--- main.c	23 Oct 2005 19:01:04 -0000	1.21
***************
*** 59,68 ****
  
  #ifdef WIN32
- #undef EINTR
- #define EINTR WSAEINTR
  #undef errno
  #define errno WSAGetLastError()
  #undef strerror
  #define strerror(x) StrError(x)
  #else
  #include <sys/wait.h>
--- 59,67 ----
  
  #ifdef WIN32
  #undef errno
  #define errno WSAGetLastError()
  #undef strerror
  #define strerror(x) StrError(x)
+ #define close(x) closesocket(x)
  #else
  #include <sys/wait.h>
***************
*** 162,165 ****
--- 161,165 ----
    struct ohs_connection *oc;
    olsr_u8_t new_addr[4];
+   int i;
  
    if(logbits & LOG_CONNECT)
***************
*** 180,189 ****
    oc->linkcnt = 0;
  
    /* Get "fake IP" */
!   if(recv(oc->socket, new_addr, 4, 0) != 4)
!     {
!       printf("Failed to fetch IP address!\n");
!       return -1;
!     }
    memcpy(&oc->ip_addr, new_addr, 4);
    oc->ip_addr.v4 = ntohl(oc->ip_addr.v4);
--- 180,204 ----
    oc->linkcnt = 0;
  
+   // hack alert: WSAEventSelect makes sockets non-blocking, so the
+   // recv() may return without having read anything on Windows; hence
+   // re-try for 2 seconds on Windows; shouldn't harm Linux et al.
+ 
    /* Get "fake IP" */
!   for (i = 0; i < 20; i++)
!   {
!     if (recv(oc->socket, new_addr, 4, 0) == 4)
!       break;
! 
! #if defined WIN32
!     Sleep(100);
! #endif
!   }
! 
!   if (i == 20)
!   {
!     printf("Failed to fetch IP address! (%s)\n", strerror(errno));
!     return -1;
!   }
! 
    memcpy(&oc->ip_addr, new_addr, 4);
    oc->ip_addr.v4 = ntohl(oc->ip_addr.v4);
***************
*** 348,367 ****
  }
  
  
  static void
  ohs_listen_loop()
  {
    int n;
    fd_set ibits;
- #ifdef WIN32
-   struct timeval select_timeout;
-   HANDLE  stdIn = GetStdHandle(STD_INPUT_HANDLE);
- #else
    int fn_stdin = fileno(stdin);
- #endif
- 
-   printf("OHS command interpreter reading from STDIN\n");
-   printf("\n> ");
-   fflush(stdout);
  
    while(1)
--- 363,403 ----
  }
  
+ static void accept_handler(void)
+ {
+   struct sockaddr_in pin;
+   socklen_t addrlen = sizeof(pin);
+   int s;
+ 	  
+   memset(&pin, 0 , sizeof(pin));
+ 
+   if((s = accept(srv_socket, (struct sockaddr *)&pin, &addrlen)) < 0)
+     {
+       printf("accept failed socket: %s\n", strerror(errno));
+     }
+   else
+     {
+       /* Create new node */
+       ohs_init_new_connection(s);
+     }
+ }
+ 
+ static void stdin_handler(void)
+ {
+   ohs_parse_command();
+ }
+ 
+ static void read_handler(struct ohs_connection *con)
+ {
+   if (ohs_route_data(con) < 0)
+     ohs_delete_connection(con);
+ }
  
  static void
  ohs_listen_loop()
  {
+ #if !defined WIN32
    int n;
    fd_set ibits;
    int fn_stdin = fileno(stdin);
  
    while(1)
***************
*** 378,390 ****
        FD_SET(srv_socket, &ibits);
  
- #ifdef WIN32
-       select_timeout.tv_sec = 0;
-       select_timeout.tv_usec = 5;
- #else
        if(fn_stdin > high) 
  	high = fn_stdin;
  
        FD_SET(fn_stdin, &ibits);
! #endif
        /* Add clients */
        for(ohs_cs = ohs_conns; ohs_cs; ohs_cs = ohs_cs->next)
--- 414,422 ----
        FD_SET(srv_socket, &ibits);
  
        if(fn_stdin > high) 
  	high = fn_stdin;
  
        FD_SET(fn_stdin, &ibits);
! 
        /* Add clients */
        for(ohs_cs = ohs_conns; ohs_cs; ohs_cs = ohs_cs->next)
***************
*** 396,412 ****
  	}
  
- #ifdef WIN32
-       n = select(high + 1, &ibits, 0, 0, &select_timeout);
- #else
        /* block */
        n = select(high + 1, &ibits, 0, 0, NULL);
- #endif
        
        if(n == 0)
- #ifdef WIN32
- 	goto read_stdin;
- #else
          continue;
- #endif
  
        /* Did somethig go wrong? */
--- 428,436 ----
***************
*** 417,446 ****
  	  
  	  printf("Error select: %s", strerror(errno));
- #ifdef WIN32
-           goto read_stdin;
- #else
            continue;
- #endif
  	}
        
        /* Check server socket */
        if(FD_ISSET(srv_socket, &ibits))
! 	{
! 	  struct sockaddr_in pin;
! 	  socklen_t addrlen = sizeof(pin);
! 	  int s;
! 	  
! 	  memset(&pin, 0 , sizeof(pin));
  
- 	  if((s = accept(srv_socket, (struct sockaddr *)&pin, &addrlen)) < 0)
- 	    {
- 	      printf("accept failed socket: %s\n", strerror(errno));
- 	    }
- 	  else
- 	    {
- 	      /* Create new node */
- 	      ohs_init_new_connection(s);
- 	    }
- 	}
        /* Loop trough clients */
        ohs_cs = ohs_conns;
--- 441,451 ----
  	  
  	  printf("Error select: %s", strerror(errno));
            continue;
  	}
        
        /* Check server socket */
        if(FD_ISSET(srv_socket, &ibits))
!         accept_handler();
  
        /* Loop trough clients */
        ohs_cs = ohs_conns;
***************
*** 451,472 ****
  
  	  if(FD_ISSET(ohs_tmp->socket, &ibits))
! 	    {
! 	      if(ohs_route_data(ohs_tmp) < 0)
! 		  ohs_delete_connection(ohs_tmp);
! 	    }
  	}
! #if WIN32
!     read_stdin:
!       if(WaitForSingleObject(stdIn, 5L) == WAIT_OBJECT_0)
! #else
        if(FD_ISSET(fn_stdin, &ibits))
! #endif
! 	{
! 	  ohs_parse_command(stdin);
! 	  printf("\n> ");
! 	  fflush(stdout);
! 	}
        printf("*");
      }
  }
  
--- 456,535 ----
  
  	  if(FD_ISSET(ohs_tmp->socket, &ibits))
!             read_handler(ohs_tmp);
  	}
! 
        if(FD_ISSET(fn_stdin, &ibits))
!         stdin_handler();
! 
        printf("*");
+       fflush(stdout);
      }
+ #else
+   HANDLE Objects[2];
+   WSANETWORKEVENTS NetEvents;
+   struct ohs_connection *Walker, *TmpWalker;
+   unsigned int Res;
+ 
+   Objects[0] = GetStdHandle(STD_INPUT_HANDLE);
+   Objects[1] = WSACreateEvent();
+ 
+   if (WSAEventSelect(srv_socket, Objects[1], FD_ACCEPT) == SOCKET_ERROR)
+   {
+     fprintf(stderr, "WSAEventSelect failed (1): %s\n", strerror(errno));
+     return;
+   }
+ 
+   while (1)
+   {
+     for (Walker = ohs_conns; Walker != NULL; Walker = Walker->next)
+     {
+       if (WSAEventSelect(Walker->socket, Objects[1], FD_READ | FD_CLOSE) == SOCKET_ERROR)
+       {
+         fprintf(stderr, "WSAEventSelect failed (2): %s\n", strerror(errno));
+         Sleep(1000);
+         continue;
+       }
+     }
+ 
+     Res = WaitForMultipleObjects(2, Objects, FALSE, INFINITE);
+ 
+     if (Res == WAIT_FAILED)
+     {
+       fprintf(stderr, "WaitForMultipleObjects failed: %s\n", strerror(GetLastError()));
+       Sleep(1000);
+       continue;
+     }
+ 
+     if (Res == WAIT_OBJECT_0)
+       stdin_handler();
+ 
+     else if (Res == WAIT_OBJECT_0 + 1)
+     {
+       if (WSAEnumNetworkEvents(srv_socket, Objects[1], &NetEvents) == SOCKET_ERROR)
+         fprintf(stderr, "WSAEnumNetworkEvents failed (1): %s\n", strerror(errno));
+ 
+       else
+       {
+         if ((NetEvents.lNetworkEvents & FD_ACCEPT) != 0)
+           accept_handler();
+       }
+ 
+       for (Walker = ohs_conns; Walker != NULL; Walker = TmpWalker)
+       {
+         TmpWalker = Walker->next;
+ 
+         if (WSAEnumNetworkEvents(Walker->socket, Objects[1], &NetEvents) == SOCKET_ERROR)
+           fprintf(stderr, "WSAEnumNetworkEvents failed (2): %s\n", strerror(errno));
+ 
+         else
+         {
+           if ((NetEvents.lNetworkEvents & (FD_READ | FD_CLOSE)) != 0)
+             read_handler(Walker);
+         }
+       }
+     }
+   }
+   
+ #endif
  }
  
***************
*** 483,487 ****
--- 546,552 ----
        exit(EXIT_FAILURE);
      }
+ 
    SetConsoleCtrlHandler(ohs_close, OLSR_TRUE);
+ 
  #else
    signal(SIGINT, ohs_close);  
***************
*** 506,509 ****
--- 571,578 ----
    ohs_configure();
  
+   printf("OHS command interpreter reading from STDIN\n");
+   printf("\n> ");
+   fflush(stdout);
+ 
    ohs_listen_loop();
  





More information about the Olsr-cvs mailing list