Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions listen.c
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,24 @@ int uh_socket_bind(const char *host, const char *port, bool tls)
goto error;
}

/*
* Allow binding to an address that is not (yet) assigned to a
* local interface. Without this, starting before the network is
* fully configured makes bind() fail with EADDRNOTAVAIL when a
* specific listen address is used, and uhttpd never recovers.
* Best effort: ignore failures so older kernels keep working.
*/
#if defined(IP_FREEBIND)
if (p->ai_family == AF_INET &&
setsockopt(sock, IPPROTO_IP, IP_FREEBIND, &yes, sizeof(yes)) < 0)
perror("setsockopt(IP_FREEBIND)");
#endif
#if defined(IPV6_FREEBIND)
if (p->ai_family == AF_INET6 &&
setsockopt(sock, IPPROTO_IPV6, IPV6_FREEBIND, &yes, sizeof(yes)) < 0)
perror("setsockopt(IPV6_FREEBIND)");
#endif

/* bind */
if (bind(sock, p->ai_addr, p->ai_addrlen) < 0) {
perror("bind()");
Expand Down