From 13edecdc20487ad074cc0a7b1b14c8aec9bd9ff9 Mon Sep 17 00:00:00 2001 From: Tobias Brunner Date: Tue, 10 Apr 2018 18:14:32 +0200 Subject: [PATCH] dhcp: Reduce receive buffer size on send socket 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 | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/libcharon/plugins/dhcp/dhcp_socket.c b/src/libcharon/plugins/dhcp/dhcp_socket.c index 765171ff4..3167b0503 100644 --- a/src/libcharon/plugins/dhcp/dhcp_socket.c +++ b/src/libcharon/plugins/dhcp/dhcp_socket.c @@ -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