From 0ba9567a5651418b2f373643e70895fc2de6d9fc Mon Sep 17 00:00:00 2001
From: Paul Gofman <pgofman@codeweavers.com>
Date: Mon, 31 Oct 2022 15:58:23 -0600
Subject: [PATCH] http.sys: Use SO_EXCLUSIVEADDRUSE on listening socket.

---
 dlls/http.sys/http.c         | 4 +++-
 dlls/httpapi/tests/httpapi.c | 6 +++---
 2 files changed, 6 insertions(+), 4 deletions(-)

diff --git a/dlls/http.sys/http.c b/dlls/http.sys/http.c
index e16805ccbdb..680ef30730c 100644
--- a/dlls/http.sys/http.c
+++ b/dlls/http.sys/http.c
@@ -715,7 +715,7 @@ static NTSTATUS http_add_url(struct request_queue *queue, IRP *irp)
     struct listening_socket *listening_sock;
     char *url, *endptr;
     size_t queue_url_len, new_url_len;
-    ULONG true = 1;
+    ULONG true = 1, value;
     SOCKET s = INVALID_SOCKET;
 
     TRACE("host %s, context %s.\n", debugstr_a(params->url), wine_dbgstr_longlong(params->context));
@@ -781,6 +781,8 @@ static NTSTATUS http_add_url(struct request_queue *queue, IRP *irp)
 
         addr.sin_family = AF_INET;
         addr.sin_addr.S_un.S_addr = INADDR_ANY;
+        value = 1;
+        setsockopt(s, SOL_SOCKET, SO_EXCLUSIVEADDRUSE, (char *)&value, sizeof(value));
         if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) == -1)
         {
             LeaveCriticalSection(&http_cs);
diff --git a/dlls/httpapi/tests/httpapi.c b/dlls/httpapi/tests/httpapi.c
index ec1be5bc82a..1d00aa22c50 100644
--- a/dlls/httpapi/tests/httpapi.c
+++ b/dlls/httpapi/tests/httpapi.c
@@ -1397,7 +1397,7 @@ static void test_v2_bound_port(void)
     ok(!ret, "Failed to bind to port\n");
     swprintf(url, ARRAY_SIZE(url), L"http://localhost:%u/", port);
     ret = pHttpAddUrlToUrlGroup(group, url, 0xdeadbeef, 0);
-    todo_wine ok(ret == ERROR_SHARING_VIOLATION, "Unexpected failure adding %s, error %u.\n", debugstr_w(url), ret);
+    ok(ret == ERROR_SHARING_VIOLATION, "Unexpected failure adding %s, error %u.\n", debugstr_w(url), ret);
     shutdown(s2, SD_BOTH);
     closesocket(s2);
 
@@ -1407,8 +1407,8 @@ static void test_v2_bound_port(void)
 
     s = socket(AF_INET, SOCK_STREAM, 0);
     ret = connect(s, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
-    todo_wine ok(ret, "Connecting to socket succeeded, %lu.\n", GetLastError());
-    todo_wine ok(GetLastError() == WSAECONNREFUSED, "Unexpected error connecting to socket, %lu.\n", GetLastError());
+    ok(ret, "Connecting to socket succeeded, %lu.\n", GetLastError());
+    ok(GetLastError() == WSAECONNREFUSED, "Unexpected error connecting to socket, %lu.\n", GetLastError());
 
     closesocket(s);
     ret = pHttpCloseRequestQueue(dummy_queue);
-- 
GitLab