dhcp: Reduce receive buffer size on send socket
authorTobias Brunner <tobias@strongswan.org>
Tue, 10 Apr 2018 16:14:32 +0000 (18:14 +0200)
committerTobias Brunner <tobias@strongswan.org>
Fri, 18 May 2018 16:04:01 +0000 (18:04 +0200)
Since we won't read from the socket reducing the receive buffer saves
some memory and it should also minimize the impact on other processes that
bind the same port (Linux distributes packets to the sockets round-robin).

src/libcharon/plugins/dhcp/dhcp_socket.c

index 765171f..3167b05 100644 (file)
@@ -688,7 +688,7 @@ dhcp_socket_t *dhcp_socket_create()
                },
        };
        char *iface;
-       int on = 1;
+       int on = 1, rcvbuf = 0;
        struct sock_filter dhcp_filter_code[] = {
                BPF_STMT(BPF_LD+BPF_B+BPF_ABS,
                                 offsetof(struct iphdr, protocol)),
@@ -779,6 +779,19 @@ dhcp_socket_t *dhcp_socket_create()
                destroy(this);
                return NULL;
        }
+       /* we won't read any data from this socket, so reduce the buffer to save
+        * some memory (there is some minimum, still try 0, though).
+        * note that we might steal some packets from other processes if e.g. a DHCP
+        * client (or server) is running on the same host, but by reducing the
+        * buffer size the impact should be minimized */
+       if (setsockopt(this->send, SOL_SOCKET, SO_RCVBUF, &rcvbuf,
+                                  sizeof(rcvbuf)) == -1)
+       {
+               DBG1(DBG_CFG, "unable to reduce receive buffer on DHCP send socket: %s",
+                        strerror(errno));
+               destroy(this);
+               return NULL;
+       }
        if (!is_broadcast(this->dst))
        {
                /* when setting giaddr (which we do when we don't broadcast), the server