dhcp-network: enable IP_FREEBIND for UDP sockets

This allows the sockets to be bound to a specific address before it is configured,
also enable SO_REUSEADDR to allow multiple DHCP clients to run at the same time.
This commit is contained in:
Tom Gundersen 2014-07-25 14:43:16 +02:00
parent 2d2349cc3e
commit 076adf013a

View file

@ -126,7 +126,7 @@ int dhcp_network_bind_udp_socket(be32_t address, uint16_t port) {
.in.sin_addr.s_addr = address,
};
_cleanup_close_ int s = -1;
int r, tos = IPTOS_CLASS_CS6;
int r, on = 1, tos = IPTOS_CLASS_CS6;
s = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0);
if (s < 0)
@ -136,13 +136,11 @@ int dhcp_network_bind_udp_socket(be32_t address, uint16_t port) {
if (r < 0)
return -errno;
r = setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
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;
@ -150,6 +148,10 @@ int dhcp_network_bind_udp_socket(be32_t address, uint16_t port) {
r = setsockopt(s, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on));
if (r < 0)
return -errno;
} else {
r = setsockopt(s, IPPROTO_IP, IP_FREEBIND, &on, sizeof(on));
if (r < 0)
return -errno;
}
r = bind(s, &src.sa, sizeof(src.in));