[Olsr-dev] ipv6 patch for txtinfo plugin

John Hay (spam-protected)
Fri Oct 12 08:54:56 CEST 2007


Hi,

Here is a patch that make the txtinfo plugin work when olsr is used in
ipv6 "mode". Its output side seems ok, I only had to tweak the parts
that opened the socket and did the access control.

One piece that might be a little "different" is in the access control.
I added code to check if the ipc_accept_ip was set to in6addr_any and
then allow access to anyone. I did it only for the ipv6 side because
I don't know if the ipv4 users want it. :-) So now if you just enable 
the txtinfo plugin in the olsrd.conf (in ipv6 mode) only localhost
will be allowed, if you add something like this

PlParam "accept" "fd9c:6829:597c:10:215:6dff:fe53:1fee"

only that address will be allowed, and if you use this

PlParam "accept" "::"

anyone will be allowed.

John
-- 
John Hay -- (spam-protected) / (spam-protected)


--- lib/txtinfo/src/olsrd_txtinfo.c.orig	2007-09-18 10:55:50.000000000 +0200
+++ lib/txtinfo/src/olsrd_txtinfo.c	2007-10-12 08:19:13.000000000 +0200
@@ -143,11 +143,13 @@
 static int
 plugin_ipc_init(void)
 {
-    struct sockaddr_in sin;
+    struct sockaddr_storage sst;
+    struct sockaddr_in *sin;
+    struct sockaddr_in6 *sin6;
     olsr_u32_t yes = 1;
 
     /* Init ipc socket */
-    if ((ipc_socket = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
+    if ((ipc_socket = socket(olsr_cnf->ip_version, SOCK_STREAM, 0)) == -1) {
 #ifndef NODEBUG
         olsr_printf(1, "(TXTINFO) socket()=%s\n", strerror(errno));
 #endif
@@ -169,13 +171,23 @@
         /* Bind the socket */
 
         /* complete the socket structure */
-        memset(&sin, 0, sizeof(sin));
-        sin.sin_family = AF_INET;
-        sin.sin_addr.s_addr = INADDR_ANY;
-        sin.sin_port = htons(ipc_port);
+        memset(&sst, 0, sizeof(sst));
+        if (olsr_cnf->ip_version == AF_INET) {
+	    sin = (struct sockaddr_in *)&sst;
+	    sin->sin_family = AF_INET;
+	    sin->sin_len = sizeof(struct sockaddr_in);
+	    sin->sin_addr.s_addr = INADDR_ANY;
+	    sin->sin_port = htons(ipc_port);
+        } else {
+	    sin6 = (struct sockaddr_in6 *)&sst;
+	    sin6->sin6_family = AF_INET6;
+	    sin6->sin6_len = sizeof(struct sockaddr_in6);
+	    sin6->sin6_addr = in6addr_any;
+	    sin6->sin6_port = htons(ipc_port);
+        }
       
         /* bind the socket to the port number */
-        if (bind(ipc_socket, (struct sockaddr *) &sin, sizeof(sin)) == -1) {
+        if (bind(ipc_socket, (struct sockaddr *) &sst, sst.ss_len) == -1) {
 #ifndef NODEBUG
             olsr_printf(1, "(TXTINFO) bind()=%s\n", strerror(errno));
 #endif
@@ -204,13 +216,15 @@
 
 static void ipc_action(int fd)
 {
-    struct sockaddr_in pin;
-    char *addr;  
+    struct sockaddr_storage pin;
+    struct sockaddr_in *sin4;
+    struct sockaddr_in6 *sin6;
+    char addr[INET6_ADDRSTRLEN];
     fd_set rfds;
     struct timeval tv;
     int neighonly = 0;
 
-    socklen_t addrlen = sizeof(struct sockaddr_in);
+    socklen_t addrlen = sizeof(struct sockaddr_storage);
 
     if(ipc_open)
         return;
@@ -223,11 +237,28 @@
     }
 
     tv.tv_sec = tv.tv_usec = 0;
-    addr = inet_ntoa(pin.sin_addr);
-    if (ntohl(pin.sin_addr.s_addr) != ntohl(ipc_accept_ip.v4)) {
-        olsr_printf(1, "(TXTINFO) From host(%s) not allowed!\n", addr);
-        close(ipc_connection);
-        return;
+    if (olsr_cnf->ip_version == AF_INET) {
+        sin4 = (struct sockaddr_in *)&pin;
+        if (inet_ntop(olsr_cnf->ip_version, &sin4->sin_addr, addr,
+	    INET6_ADDRSTRLEN) == NULL)
+	      addr[0] = '\0';
+        if (!COMP_IP(&sin4->sin_addr, &ipc_accept_ip.v4)) {
+            olsr_printf(1, "(TXTINFO) From host(%s) not allowed!\n", addr);
+            close(ipc_connection);
+            return;
+        }
+    } else {
+        sin6 = (struct sockaddr_in6 *)&pin;
+        if (inet_ntop(olsr_cnf->ip_version, &sin6->sin6_addr, addr,
+	    INET6_ADDRSTRLEN) == NULL)
+	      addr[0] = '\0';
+	/* Use in6addr_any (::) in olsr.conf to allow anybody. */
+        if (!COMP_IP(&in6addr_any, &ipc_accept_ip.v6) &&
+	    !COMP_IP(&sin6->sin6_addr, &ipc_accept_ip.v6)) {
+            olsr_printf(1, "(TXTINFO) From host(%s) not allowed!\n", addr);
+            close(ipc_connection);
+            return;
+        }
     }
     ipc_open = 1;
 #ifndef NODEBUG




More information about the Olsr-dev mailing list