[systemd-devel] [PATCH 01/17] dhcp-network: allow UDP socket to listen on any address

Tom Gundersen teg at jklm.no
Mon May 26 12:39:30 PDT 2014


For this to work nicely we need to use REUSEADDR so that more than one socket
can be open at the same time. Also, we request the ifindex to be appended
to incoming messages, so we know whence it came.
---
 src/libsystemd-network/dhcp-network.c | 15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)

diff --git a/src/libsystemd-network/dhcp-network.c b/src/libsystemd-network/dhcp-network.c
index dd26ef3..6fac80e 100644
--- a/src/libsystemd-network/dhcp-network.c
+++ b/src/libsystemd-network/dhcp-network.c
@@ -68,7 +68,7 @@ int dhcp_network_bind_raw_socket(int index, union sockaddr_union *link,
             .filter = filter
         };
         _cleanup_close_ int s = -1;
-        int r, one = 1;
+        int r, on = 1;
 
         assert(index > 0);
         assert(link);
@@ -77,7 +77,7 @@ int dhcp_network_bind_raw_socket(int index, union sockaddr_union *link,
         if (s < 0)
                 return -errno;
 
-        r = setsockopt (s, SOL_PACKET, PACKET_AUXDATA, &one, sizeof(one));
+        r = setsockopt (s, SOL_PACKET, PACKET_AUXDATA, &on, sizeof(on));
         if (r < 0)
                 return -errno;
 
@@ -117,6 +117,17 @@ int dhcp_network_bind_udp_socket(be32_t address, uint16_t port) {
         r = setsockopt(s, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
         if (r < 0)
                 return -errno;
+        if (address == INADDR_ANY) {
+                int on = 1;
+
+                r = setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
+                if (r < 0)
+                        return -errno;
+
+                r = setsockopt(s, IPPROTO_IP, IP_PKTINFO, &on, sizeof(on));
+                if (r < 0)
+                        return -errno;
+        }
 
         r = bind(s, &src.sa, sizeof(src.in));
         if (r < 0)
-- 
1.9.0



More information about the systemd-devel mailing list