diff --git a/include/libwebsockets/lws-client.h b/include/libwebsockets/lws-client.h index 8563371d3..232b883a6 100644 --- a/include/libwebsockets/lws-client.h +++ b/include/libwebsockets/lws-client.h @@ -50,6 +50,12 @@ enum lws_client_connect_ssl_connection_flags { LCCSCF_HTTP_NO_FOLLOW_REDIRECT = (1 << 12), LCCSCF_HTTP_NO_CACHE_CONTROL = (1 << 13), + LCCSCF_ALLOW_REUSE_ADDR = (1 << 14), + /**< allow reuse local addresses in a bind call + * When the listening socket is bound to INADDR_ANY with a specific port + * then it is not possible to bind to this port for any local address + */ + LCCSCF_PIPELINE = (1 << 16), /**< Serialize / pipeline multiple client connections * on a single connection where possible. diff --git a/lib/plat/freertos/freertos-sockets.c b/lib/plat/freertos/freertos-sockets.c index c6bfec35d..56d80069c 100644 --- a/lib/plat/freertos/freertos-sockets.c +++ b/lib/plat/freertos/freertos-sockets.c @@ -175,6 +175,20 @@ lws_plat_set_socket_options_ip(lws_sockfd_type fd, uint8_t pri, int lws_flags) } #endif + if (lws_flags & LCCSCF_ALLOW_REUSE_ADDR) { + optval = 1; + if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, + (const void *)&optval, optlen) < 0) { +#if !defined(LWS_WITH_NO_LOGS) + en = errno; + lwsl_warn("%s: unable to reuse local addresses: errno %d\n", + __func__, en); +#endif + ret = 1; + } else + lwsl_notice("%s: set reuse addresses\n", __func__); + } + for (n = 0; n < 4; n++) { if (!(lws_flags & ip_opt_lws_flags[n])) continue; diff --git a/lib/plat/unix/unix-sockets.c b/lib/plat/unix/unix-sockets.c index 07df994cc..394e858b7 100644 --- a/lib/plat/unix/unix-sockets.c +++ b/lib/plat/unix/unix-sockets.c @@ -255,6 +255,21 @@ lws_plat_set_socket_options_ip(lws_sockfd_type fd, uint8_t pri, int lws_flags) } #endif + if (lws_flags & LCCSCF_ALLOW_REUSE_ADDR) { + optval = 1; + if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, + (const void *)&optval, optlen) < 0) { +#if !defined(LWS_WITH_NO_LOGS) + en = errno; + lwsl_warn("%s: unable to reuse local addresses: errno %d\n", + __func__, en); +#endif + ret = 1; + } else + lwsl_notice("%s: set reuse addresses\n", __func__); + } + + for (n = 0; n < 4; n++) { if (!(lws_flags & ip_opt_lws_flags[n])) continue; diff --git a/lib/plat/windows/windows-sockets.c b/lib/plat/windows/windows-sockets.c index 7570a038d..6bbc066b8 100644 --- a/lib/plat/windows/windows-sockets.c +++ b/lib/plat/windows/windows-sockets.c @@ -146,16 +146,56 @@ lws_plat_set_socket_options(struct lws_vhost *vhost, lws_sockfd_type fd, int lws_plat_set_socket_options_ip(lws_sockfd_type fd, uint8_t pri, int lws_flags) -{ +{ + int optval = 1, ret = 0; + socklen_t optlen = sizeof(optval); +#if !defined(LWS_WITH_NO_LOGS) + int en; +#endif + /* * Seems to require "differeniated services" but no docs * * https://docs.microsoft.com/en-us/windows/win32/winsock/ipproto-ip-socket-options * https://docs.microsoft.com/en-us/previous-versions/windows/desktop/qos/differentiated-services */ - lwsl_warn("%s: not implemented on windows platform\n", __func__); + lwsl_warn("%s: priority and ip sockets options not implemented on windows platform\n", __func__); + - return 0; + /* + * only accept that we are the only listener on the port + * https://msdn.microsoft.com/zh-tw/library/ + * windows/desktop/ms740621(v=vs.85).aspx + * + * for lws, to match Linux, we default to exclusive listen + */ + if (lws_flags & LCCSCF_ALLOW_REUSE_ADDR) { + if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, + (const void *)&optval, optlen) < 0) { +#if !defined(LWS_WITH_NO_LOGS) + en = errno; + lwsl_warn("%s: unable to reuse local addresses: errno %d\n", + __func__, en); +#endif + ret = 1; + } else + lwsl_notice("%s: set reuse addresses\n", __func__); + + } else { + if (setsockopt(fd, SOL_SOCKET, SO_EXCLUSIVEADDRUSE, + (const void *)&optval, optlen) < 0) { +#if !defined(LWS_WITH_NO_LOGS) + en = errno; + lwsl_warn("%s: unable to use exclusive addresses: errno %d\n", + __func__, en); +#endif + ret = 1; + } else + lwsl_notice("%s: set use exclusive addresses\n", __func__); + } + + + return ret; } int