From 9c4f1fb8481245671201f4def8ef5dd31a938e24 Mon Sep 17 00:00:00 2001 From: David Carlier Date: Tue, 26 Dec 2023 08:29:30 +0000 Subject: [PATCH] Fix GH-13025: sapi/cli Windows using SO_EXCLUSIVEADDRUSE flag. With this, connections attempts when ip address and port are already bound prior will be refused, thus the behavior will be closer to unixes ; only little difference tough once the server shutdown the said binding might still be made unavailable for future connections for a little undetermined time. --- sapi/cli/php_cli_server.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/sapi/cli/php_cli_server.c b/sapi/cli/php_cli_server.c index 4ed2095e840c5..156d04b3ab147 100644 --- a/sapi/cli/php_cli_server.c +++ b/sapi/cli/php_cli_server.c @@ -1280,6 +1280,16 @@ static php_socket_t php_network_listen_socket(const char *host, int *port, int s setsockopt(retval, SOL_SOCKET, SO_REUSEADDR, (char*)&val, sizeof(val)); } #endif +#ifdef SO_EXCLUSIVEADDRUSE + { + /** + * By default, windows allows multiple sockets to bind the same ip with the same port. + * SO_EXCLUSIVEADDRUSE prevents this while still allowing binding other different ip/port combinations. + **/ + int val = 1; + setsockopt(retval, SOL_SOCKET, SO_EXCLUSIVEADDRUSE, (char*)&val, sizeof(val)); + } +#endif if (bind(retval, sa, *socklen) == SOCK_CONN_ERR) { err = php_socket_errno();