Initial libnet code for testing. Works on Linux now, do make USE_LIBNET=1 to use
authorAndreas Tonnesen <andreto@olsr.org>
Fri, 6 Jan 2006 06:54:37 +0000 (06:54 +0000)
committerAndreas Tonnesen <andreto@olsr.org>
Fri, 6 Jan 2006 06:54:37 +0000 (06:54 +0000)
make/Makefile.linux
src/interfaces.h
src/net_olsr.c
src/net_olsr.h
src/unix/ifnet.c

index e8ca02f..31d8f06 100644 (file)
@@ -26,6 +26,11 @@ ifdef GENERATE_PIC
 CFLAGS +=      -fPIC
 LDFLAGS +=     -fPIC
 endif
-               
+
+ifdef USE_LIBNET
+LIBS +=                -lnet
+CFLAGS +=      -DUSE_LIBNET
+endif
+
 OS_LIB_PTHREAD = -lpthread
 OS_LIB_DYNLOAD = -ldl
index e0eb521..2b9a1e6 100644 (file)
@@ -36,7 +36,7 @@
  * to the project. For more information see the website or contact
  * the copyright holders.
  *
- * $Id: interfaces.h,v 1.30 2005/06/03 08:00:55 kattemat Exp $
+ * $Id: interfaces.h,v 1.31 2006/01/06 06:54:04 kattemat Exp $
  */
 
 
@@ -135,6 +135,8 @@ struct interface
   float         hello_etime;
   struct        vtimes valtimes;
 
+  void          *libnet_ctx;                    /* libnet context(void to avoid dependency */
+
   struct        if_gen_property *gen_properties;/* Generic interface properties */
 
   struct       interface *int_next;
index f635ebe..95e51b0 100644 (file)
@@ -36,7 +36,7 @@
  * to the project. For more information see the website or contact
  * the copyright holders.
  *
- * $Id: net_olsr.c,v 1.7 2005/12/29 18:37:16 tlopatic Exp $
+ * $Id: net_olsr.c,v 1.8 2006/01/06 06:54:04 kattemat Exp $
  */
 
 #include "net_olsr.h"
 #include "print_packet.h"
 #include "link_set.h"
 #include <stdlib.h>
+#include <assert.h>
+
+#ifdef USE_LIBNET
+#include <libnet.h>
+#endif
 
 extern olsr_bool lq_tc_pending;
 
@@ -87,6 +92,15 @@ static char *deny_ipv6_defaults[] =
     NULL
   };
 
+#ifdef USE_LIBNET
+static char errbuf[LIBNET_ERRBUF_SIZE];
+
+char *
+get_libnet_errbuf()
+{
+  return errbuf;
+}
+#endif
 
 void
 init_net()
@@ -454,6 +468,61 @@ net_output(struct interface *ifp)
   
   if(olsr_cnf->ip_version == AF_INET)
     {
+#ifdef USE_LIBNET
+      libnet_ptag_t udp_ptag = 0, ip_ptag = 0;
+      int bytes_written;
+
+      printf("LIBNET TX %d bytes on %s\n", 
+            netbufs[ifp->if_nr]->pending, ifp->int_name);
+      
+      assert(ifp->libnet_ctx != NULL);
+
+      udp_ptag = libnet_build_udp(OLSRPORT, 
+                                 OLSRPORT,
+                                 LIBNET_UDP_H + netbufs[ifp->if_nr]->pending,
+                                 0,
+                                 (u_int8_t *)netbufs[ifp->if_nr]->buff, 
+                                 netbufs[ifp->if_nr]->pending, 
+                                 ifp->libnet_ctx,
+                                 udp_ptag);
+      if(udp_ptag == -1)
+       {
+         OLSR_PRINTF (1, "libnet UDP header: %s\n", libnet_geterror (ifp->libnet_ctx))
+         return (0);
+       }
+      
+      ip_ptag = libnet_build_ipv4(LIBNET_IPV4_H + LIBNET_UDP_H + netbufs[ifp->if_nr]->pending,
+                                 olsr_cnf->tos,
+                                 ifp->olsr_seqnum,
+                                 0,
+                                 255,
+                                 IPPROTO_UDP,
+                                 0,
+                                 ifp->ip_addr.v4,
+                                 sin->sin_addr.s_addr,
+                                 NULL,
+                                 0,
+                                 ifp->libnet_ctx,
+                                 ip_ptag);
+      if(ip_ptag == -1)
+       {
+         OLSR_PRINTF (1, "libnet IP header: %s\n", libnet_geterror (ifp->libnet_ctx))
+           return (0);
+       }
+      
+      if((bytes_written = libnet_write(ifp->libnet_ctx)) == -1)
+       {
+         OLSR_PRINTF (1, "libnet packet write: %s\n", libnet_geterror (ifp->libnet_ctx))
+           return (0);
+       }
+      if(bytes_written != (int)(LIBNET_IPV4_H + LIBNET_UDP_H + netbufs[ifp->if_nr]->pending))
+       {
+         printf("libnet_write returned %d, expected %d\n", 
+                bytes_written, LIBNET_IPV4_H + LIBNET_UDP_H + netbufs[ifp->if_nr]->pending);
+       }
+      
+      libnet_clear_packet(ifp->libnet_ctx);
+#else
       /* IP version 4 */
       if(olsr_sendto(ifp->olsr_socket, 
                     netbufs[ifp->if_nr]->buff, 
@@ -468,6 +537,7 @@ net_output(struct interface *ifp)
          netbufs[ifp->if_nr]->pending = 0;
          return -1;
        }
+#endif
     }
   else
     {
index 7257f20..5c66a9a 100644 (file)
@@ -36,7 +36,7 @@
  * to the project. For more information see the website or contact
  * the copyright holders.
  *
- * $Id: net_olsr.h,v 1.1 2005/05/25 20:59:46 kattemat Exp $
+ * $Id: net_olsr.h,v 1.2 2006/01/06 06:54:37 kattemat Exp $
  */
 
 
@@ -68,6 +68,11 @@ struct olsr_netbuf
   int reserved;   /* Plugins can reserve space in buffers */
 };
 
+#ifdef USE_LIBNET
+char *
+get_libnet_errbuf(void);
+#endif
+
 void
 init_net(void);
 
index d55433a..690f046 100644 (file)
@@ -36,7 +36,7 @@
  * to the project. For more information see the website or contact
  * the copyright holders.
  *
- * $Id: ifnet.c,v 1.31 2005/12/29 18:37:16 tlopatic Exp $
+ * $Id: ifnet.c,v 1.32 2006/01/06 06:54:04 kattemat Exp $
  */
 
 
@@ -49,6 +49,7 @@
 #include "defs.h"
 #include "olsr.h"
 #include "net_os.h"
+#include "net_olsr.h"
 #include "socket_parser.h"
 #include "parser.h"
 #include "scheduler.h"
 #include <netdb.h>
 #include <unistd.h>
 
+#ifdef USE_LIBNET
+#include <libnet.h>
+#endif
+
 int bufspace = 127*1024;       /* max. input buffer size to request */
 
 
@@ -484,6 +489,10 @@ chk_if_changed(struct olsr_if *iface)
   /* Close olsr socket */
   close(ifp->olsr_socket);
   remove_olsr_socket(ifp->olsr_socket, &olsr_input);
+#ifdef USE_LIBNET
+  libnet_destroy(ifp->libnet_ctx);
+#endif
+
   /* Free memory */
   free(ifp->int_name);
   free(ifp);
@@ -918,6 +927,20 @@ chk_if_up(struct olsr_if *iface, int debuglvl)
          exit_value = EXIT_FAILURE;
          kill(getpid(), SIGINT);
        }
+
+#ifdef USE_LIBNET
+      ifp->libnet_ctx = libnet_init(LIBNET_RAW4, ifp->int_name, get_libnet_errbuf());
+
+      if(ifp->libnet_ctx == NULL)
+       {
+         fprintf(stderr, "Could not initialize libnet... exiting!\n\n");
+         olsr_syslog(OLSR_LOG_ERR, "Could not initialize libnet... exiting!\n\n");
+         exit_value = EXIT_FAILURE;
+         kill(getpid(), SIGINT);
+       }
+#else
+      ifp->libnet_ctx = NULL;
+#endif
     }
   else
     {