INN commit: branches/2.6 (7 files)

INN Commit rra at isc.org
Thu May 5 12:48:21 UTC 2016


    Date: Thursday, May 5, 2016 @ 05:48:21
  Author: iulius
Revision: 10021

Update to latest rra-c-util upstream version

As network_set_freebind, network_set_reuseaddr, and network_set_v6only
are now public functions, they can be removed from our local network-innbind.c
file.

Modified:
  branches/2.6/include/inn/network.h
  branches/2.6/lib/network-innbind.c
  branches/2.6/lib/network.c
  branches/2.6/lib/vector.c
  branches/2.6/support/getrra-c-util
  branches/2.6/tests/lib/network/addr-ipv4-t.c
  branches/2.6/tests/lib/network/addr-ipv6-t.c

---------------------------------+
 include/inn/network.h           |   18 ++++++++++
 lib/network-innbind.c           |   64 --------------------------------------
 lib/network.c                   |   39 +++++++----------------
 lib/vector.c                    |   12 +------
 support/getrra-c-util           |   10 ++---
 tests/lib/network/addr-ipv4-t.c |   24 ++++++++++++--
 tests/lib/network/addr-ipv6-t.c |   45 ++++++++++++++++++++++++--
 7 files changed, 100 insertions(+), 112 deletions(-)

Modified: include/inn/network.h
===================================================================
--- include/inn/network.h	2016-05-05 12:46:27 UTC (rev 10020)
+++ include/inn/network.h	2016-05-05 12:48:21 UTC (rev 10021)
@@ -6,7 +6,7 @@
  * which can be found at <http://www.eyrie.org/~eagle/software/rra-c-util/>.
  *
  * Written by Russ Allbery <eagle at eyrie.org>
- * Copyright 2014 Russ Allbery <eagle at eyrie.org>
+ * Copyright 2014, 2016 Russ Allbery <eagle at eyrie.org>
  * Copyright 2009, 2010, 2011, 2012, 2013
  *     The Board of Trustees of the Leland Stanford Junior University
  * Copyright (c) 2004, 2005, 2006, 2007, 2008, 2010
@@ -125,6 +125,22 @@
 socket_type network_client_create(int domain, int type, const char *source);
 
 /*
+ * Set various socket flags if possible, but do nothing, silently, if that
+ * option is not supported.  If the option is supported but setting the flag
+ * fails, log a warning with syswarn.
+ *
+ * network_set_freebind sets IP_FREEBIND, which allows binding IPv6 addresses
+ * that may not have been set up yet.  network_set_reuseaddr sets SO_REUSEADDR
+ * so that something new can listen on the same port immediately if the daemon
+ * dies unexpectedly.  network_set_v6only sets IP_V6ONLY, which avoids binding
+ * to the backward-compatibility IPv4 address when binding an IPv6 socket
+ * (generally preferred since the behavior is more predictable).
+ */
+void network_set_freebind(socket_type fd);
+void network_set_reuseaddr(socket_type fd);
+void network_set_v6only(socket_type fd);
+
+/*
  * Read or write the specified number of bytes to the network, enforcing a
  * timeout.  Both return true on success and false on failure; on failure, the
  * socket errno is set.

Modified: lib/network-innbind.c
===================================================================
--- lib/network-innbind.c	2016-05-05 12:46:27 UTC (rev 10020)
+++ lib/network-innbind.c	2016-05-05 12:48:21 UTC (rev 10021)
@@ -32,72 +32,8 @@
 #include "inn/network-innbind.h"
 #include "inn/xmalloc.h"
 
-/* If SO_REUSEADDR isn't available, make calls to set_reuseaddr go away. */
-#ifndef SO_REUSEADDR
-# define network_set_reuseaddr(fd)      /* empty */
-#endif
 
-/* If IPV6_V6ONLY isn't available, make calls to set_v6only go away. */
-#ifndef IPV6_V6ONLY
-# define network_set_v6only(fd)         /* empty */
-#endif
-
-/* If IP_FREEBIND isn't available, make calls to set_freebind go away. */
-#ifndef IP_FREEBIND
-# define network_set_freebind(fd)       /* empty */
-#endif
-
-
 /*
- * Set SO_REUSEADDR on a socket if possible (so that something new can listen
- * on the same port immediately if the daemon dies unexpectedly).
- */
-#ifdef SO_REUSEADDR
-static void
-network_set_reuseaddr(socket_type fd)
-{
-    int flag = 1;
-    const void *flagaddr = &flag;
-
-    if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, flagaddr, sizeof(flag)) < 0)
-        syswarn("cannot mark bind address reusable");
-}
-#endif
-
-
-/*
- * Set IPV6_V6ONLY on a socket if possible, since the IPv6 behavior is more
- * consistent and easier to understand.
- */
-#ifdef IPV6_V6ONLY
-static void
-network_set_v6only(socket_type fd)
-{
-    int flag = 1;
-
-    if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &flag, sizeof(flag)) < 0)
-        syswarn("cannot set IPv6 socket to v6only");
-}
-#endif
-
-
-/*
- * Set IP_FREEBIND on a socket if possible, which allows binding servers to
- * IPv6 addresses that may not have been set up yet.
- */
-#ifdef IP_FREEBIND
-static void
-network_set_freebind(socket_type fd)
-{
-    int flag = 1;
-
-    if (setsockopt(fd, IPPROTO_IP, IP_FREEBIND, &flag, sizeof(flag)) < 0)
-        syswarn("cannot set IPv6 socket to free binding");
-}
-#endif
-
-
-/*
  * Function used as a die handler in child processes to prevent any atexit
  * functions from being run and any buffers from being flushed twice.
  */

Modified: lib/network.c
===================================================================
--- lib/network.c	2016-05-05 12:46:27 UTC (rev 10020)
+++ lib/network.c	2016-05-05 12:48:21 UTC (rev 10021)
@@ -21,7 +21,7 @@
  * which can be found at <http://www.eyrie.org/~eagle/software/rra-c-util/>.
  *
  * Written by Russ Allbery <eagle at eyrie.org>
- * Copyright 2014, 2015 Russ Allbery <eagle at eyrie.org>
+ * Copyright 2014, 2015, 2016 Russ Allbery <eagle at eyrie.org>
  * Copyright 2009, 2011, 2012, 2013, 2014
  *     The Board of Trustees of the Leland Stanford Junior University
  * Copyright (c) 2004, 2005, 2006, 2007, 2008
@@ -75,21 +75,6 @@
 # define sin6_set_length(s)     /* empty */
 #endif
 
-/* If SO_REUSEADDR isn't available, make calls to set_reuseaddr go away. */
-#ifndef SO_REUSEADDR
-# define network_set_reuseaddr(fd)      /* empty */
-#endif
-
-/* If IPV6_V6ONLY isn't available, make calls to set_v6only go away. */
-#ifndef IPV6_V6ONLY
-# define network_set_v6only(fd)         /* empty */
-#endif
-
-/* If IP_FREEBIND isn't available, make calls to set_freebind go away. */
-#ifndef IP_FREEBIND
-# define network_set_freebind(fd)       /* empty */
-#endif
-
 /*
  * Windows requires a different function when sending to sockets, but can't
  * return short writes on blocking sockets.
@@ -100,21 +85,21 @@
 # define socket_xwrite(fd, b, s)        xwrite((fd), (b), (s))
 #endif
 
+
 /*
  * Set SO_REUSEADDR on a socket if possible (so that something new can listen
  * on the same port immediately if the daemon dies unexpectedly).
  */
-#ifdef SO_REUSEADDR
-static void
+void
 network_set_reuseaddr(socket_type fd)
 {
+#ifdef SO_REUSEADDR
     int flag = 1;
-    const void *flagaddr = &flag;
 
-    if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, flagaddr, sizeof(flag)) < 0)
+    if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &flag, sizeof(flag)) < 0)
         syswarn("cannot mark bind address reusable");
+#endif
 }
-#endif
 
 
 /*
@@ -121,16 +106,16 @@
  * Set IPV6_V6ONLY on a socket if possible, since the IPv6 behavior is more
  * consistent and easier to understand.
  */
-#ifdef IPV6_V6ONLY
-static void
+void
 network_set_v6only(socket_type fd)
 {
+#ifdef IPV6_V6ONLY
     int flag = 1;
 
     if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &flag, sizeof(flag)) < 0)
         syswarn("cannot set IPv6 socket to v6only");
+#endif
 }
-#endif
 
 
 /*
@@ -137,16 +122,16 @@
  * Set IP_FREEBIND on a socket if possible, which allows binding servers to
  * IPv6 addresses that may not have been set up yet.
  */
-#ifdef IP_FREEBIND
-static void
+void
 network_set_freebind(socket_type fd)
 {
+#ifdef IP_FREEBIND
     int flag = 1;
 
     if (setsockopt(fd, IPPROTO_IP, IP_FREEBIND, &flag, sizeof(flag)) < 0)
         syswarn("cannot set IPv6 socket to free binding");
+#endif
 }
-#endif
 
 
 /*

Modified: lib/vector.c
===================================================================
--- lib/vector.c	2016-05-05 12:46:27 UTC (rev 10020)
+++ lib/vector.c	2016-05-05 12:48:21 UTC (rev 10021)
@@ -114,12 +114,10 @@
 void
 vector_add(struct vector *vector, const char *string)
 {
-    size_t next = vector->count;
-
     assert(vector != NULL);
     if (vector->count == vector->allocated)
         vector_resize(vector, vector->allocated + 1);
-    vector->strings[next] = xstrdup(string);
+    vector->strings[vector->count] = xstrdup(string);
     vector->count++;
 }
 
@@ -126,12 +124,10 @@
 void
 cvector_add(struct cvector *vector, const char *string)
 {
-    size_t next = vector->count;
-
     assert(vector != NULL);
     if (vector->count == vector->allocated)
         cvector_resize(vector, vector->allocated + 1);
-    vector->strings[next] = string;
+    vector->strings[vector->count] = string;
     vector->count++;
 }
 
@@ -145,12 +141,10 @@
 void
 vector_addn(struct vector *vector, const char *string, size_t length)
 {
-    size_t next = vector->count;
-
     assert(vector != NULL);
     if (vector->count == vector->allocated)
         vector_resize(vector, vector->allocated + 1);
-    vector->strings[next] = xstrndup(string, length);
+    vector->strings[vector->count] = xstrndup(string, length);
     vector->count++;
 }
 

Modified: support/getrra-c-util
===================================================================
--- support/getrra-c-util	2016-05-05 12:46:27 UTC (rev 10020)
+++ support/getrra-c-util	2016-05-05 12:48:21 UTC (rev 10021)
@@ -63,7 +63,7 @@
         sed -i -e "3 i \\
 # \$Id\$\\
 #" \
-               -e 's/\$BUILD\/util/\$BUILD\/lib/g' \
+               -e 's/\$C_TAP_BUILD\/util/\$C_TAP_BUILD\/lib/g' \
                ${TEMP}
         chmod 755 ${TEMP}
       else
@@ -73,9 +73,9 @@
           # and sourceaddress6 work.
           sed -i -e "61 i \\
 #include \"inn/innconf.h\"" \
-                 -e '470 s/)$/ \&\& innconf == NULL)/' \
-                 -e '471,472d' \
-                 -e "477 i \\
+                 -e '455 s/)$/ \&\& innconf == NULL)/' \
+                 -e '456,457d' \
+                 -e "462 i \\
         if (source == NULL && innconf != NULL)\\
             source = innconf->sourceaddress;\\
         if (source == NULL ||\\
@@ -82,7 +82,7 @@
             strcmp(source, \"all\") == 0 || strcmp(source, \"any\") == 0)\\
               return true;\\
 " \
-                 -e "490 i \\
+                 -e "475 i \\
         if (source == NULL && innconf != NULL)\\
             source = innconf->sourceaddress6;\\
         if (source == NULL ||\\

Modified: tests/lib/network/addr-ipv4-t.c
===================================================================
--- tests/lib/network/addr-ipv4-t.c	2016-05-05 12:46:27 UTC (rev 10020)
+++ tests/lib/network/addr-ipv4-t.c	2016-05-05 12:48:21 UTC (rev 10021)
@@ -6,7 +6,7 @@
  * which can be found at <http://www.eyrie.org/~eagle/software/rra-c-util/>.
  *
  * Written by Russ Allbery <eagle at eyrie.org>
- * Copyright 2005, 2013 Russ Allbery <eagle at eyrie.org>
+ * Copyright 2005, 2013, 2016 Russ Allbery <eagle at eyrie.org>
  * Copyright 2009, 2010, 2011, 2012, 2013
  *     The Board of Trustees of the Leland Stanford Junior University
  *
@@ -58,14 +58,16 @@
 int
 main(void)
 {
-    int status;
+    int flag, status;
+    socklen_t flaglen;
     struct addrinfo *ai, *ai2;
     struct addrinfo hints;
     char addr[INET6_ADDRSTRLEN];
+    socket_type fd;
     static const char *port = "119";
 
     /* Set up the plan. */
-    plan(29);
+    plan(31);
 
     /* Get a sockaddr to use for subsequent tests. */
     memset(&hints, 0, sizeof(hints));
@@ -126,5 +128,21 @@
     is_addr_compare(0, "127.0.0.1", "127.0.0.1",   "1p");
     is_addr_compare(0, "127.0.0.1", "127.0.0.1",   "-1");
     is_addr_compare(0, "127.0.0.1", "127.0.0.1",   "33");
+
+    /* Test setting various socket options. */
+    fd = socket(PF_INET6, SOCK_STREAM, IPPROTO_IP);
+    if (fd == INVALID_SOCKET)
+        sysbail("cannot create socket");
+    network_set_reuseaddr(fd);
+#ifdef SO_REUSEADDR
+    flag = 0;
+    flaglen = sizeof(flag);
+    is_int(0, getsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &flag, &flaglen),
+           "Getting SO_REUSEADDR works");
+    is_int(1, flag, "...and it is set");
+#else
+    skip_block(2, "SO_REUSEADDR not supported");
+#endif
+    close(fd);
     return 0;
 }

Modified: tests/lib/network/addr-ipv6-t.c
===================================================================
--- tests/lib/network/addr-ipv6-t.c	2016-05-05 12:46:27 UTC (rev 10020)
+++ tests/lib/network/addr-ipv6-t.c	2016-05-05 12:48:21 UTC (rev 10021)
@@ -6,7 +6,7 @@
  * which can be found at <http://www.eyrie.org/~eagle/software/rra-c-util/>.
  *
  * Written by Russ Allbery <eagle at eyrie.org>
- * Copyright 2005, 2013 Russ Allbery <eagle at eyrie.org>
+ * Copyright 2005, 2013, 2016 Russ Allbery <eagle at eyrie.org>
  * Copyright 2009, 2010, 2011, 2012, 2013
  *     The Board of Trustees of the Leland Stanford Junior University
  *
@@ -60,11 +60,13 @@
 int
 main(void)
 {
-    int status;
+    int flag, status;
+    socklen_t flaglen;
     struct addrinfo *ai4, *ai6;
     struct addrinfo hints;
     char addr[INET6_ADDRSTRLEN];
     char *p;
+    socket_type fd;
     static const char *port = "119";
     static const char *ipv6_addr = "FEDC:BA98:7654:3210:FEDC:BA98:7654:3210";
 
@@ -73,7 +75,7 @@
 #endif
 
     /* Set up the plan. */
-    plan(28);
+    plan(34);
 
     /* Get IPv4 and IPv6 sockaddrs to use for subsequent tests. */
     memset(&hints, 0, sizeof(hints));
@@ -145,5 +147,42 @@
     is_addr_compare(0, "ffff::1",   "7fff::1",     "-1");
     is_addr_compare(0, "ffff::1",   "ffff::1",     "-1");
     is_addr_compare(0, "ffff::1",   "ffff::1",     "129");
+
+    /* Test setting various socket options. */
+    fd = socket(PF_INET6, SOCK_STREAM, IPPROTO_IP);
+    if (fd == INVALID_SOCKET)
+        sysbail("cannot create socket");
+    network_set_reuseaddr(fd);
+#ifdef SO_REUSEADDR
+    flag = 0;
+    flaglen = sizeof(flag);
+    is_int(0, getsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &flag, &flaglen),
+           "Getting SO_REUSEADDR works");
+    is_int(1, flag, "...and it is set");
+#else
+    skip_block(2, "SO_REUSEADDR not supported");
+#endif
+    network_set_v6only(fd);
+#ifdef IPV6_V6ONLY
+    flag = 0;
+    flaglen = sizeof(flag);
+    is_int(0, getsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &flag, &flaglen),
+           "Getting IPV6_V6ONLY works");
+    is_int(1, flag, "...and it is set");
+#else
+    skip_block(2, "IPV6_V6ONLY not supported");
+#endif
+    network_set_freebind(fd);
+#ifdef IP_FREEBIND
+    flag = 0;
+    flaglen = sizeof(flag);
+    is_int(0, getsockopt(fd, IPPROTO_IP, IP_FREEBIND, &flag, &flaglen),
+           "Getting IP_FREEBIND works");
+    is_int(1, flag, "...and it is set");
+#else
+    skip_block(2, "IP_FREEBIND not supported");
+#endif
+    close(fd);
+
     return 0;
 }



More information about the inn-committers mailing list