Skip to content

Commit be04769

Browse files
crrodriguezarnaud-lb
authored andcommitted
main: set IP_BIND_ADDRESS_NO_PORT if available when connecting to remote host
When you choose to bind() to a local address and connect to a remote host the kernel does not know if socket is listener or whatnot and has to reserve a port within a ~32k range on which you are also competing with other applications bound to the same address, this is easy to exhaust and get a EADDRINUSE. The linux kernel implemented IP_BIND_ADDRESS_NO_PORT on torvalds/linux@90c337d which delays the port allocation until the 4-tuple is known in case source port is 0.
1 parent 31692a1 commit be04769

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

main/network.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -884,6 +884,12 @@ php_socket_t php_network_connect_socket_to_host(const char *host, unsigned short
884884
local_address.in6.sin6_port = htons(bindport);
885885
}
886886
}
887+
#endif
888+
#ifdef IP_BIND_ADDRESS_NO_PORT
889+
{
890+
int val = 1;
891+
(void) setsockopt(sock, SOL_IP, IP_BIND_ADDRESS_NO_PORT, &val, sizeof(val));
892+
}
887893
#endif
888894
if (local_address_len == 0) {
889895
php_error_docref(NULL, E_WARNING, "Invalid IP Address: %s", bindto);

0 commit comments

Comments
 (0)