[PATCH] ws2_32: Implement GetHostNameW.

Hans Leidekker hans at codeweavers.com
Sun Apr 11 14:55:40 CDT 2021


Wine-Bug: https://bugs.winehq.org/show_bug.cgi?id=49286
Signed-off-by: Hans Leidekker <hans at codeweavers.com>
---
 dlls/ws2_32/socket.c     | 30 ++++++++++++++++++++++++++++++
 dlls/ws2_32/tests/sock.c | 29 +++++++++++++++++++++++++++++
 dlls/ws2_32/ws2_32.spec  |  1 +
 include/winsock2.h       |  1 +
 4 files changed, 61 insertions(+)

diff --git a/dlls/ws2_32/socket.c b/dlls/ws2_32/socket.c
index 4f26d6475aa..262171ebab0 100644
--- a/dlls/ws2_32/socket.c
+++ b/dlls/ws2_32/socket.c
@@ -7224,6 +7224,36 @@ int WINAPI WS_gethostname(char *name, int namelen)
     return 0;
 }
 
+/***********************************************************************
+ *              GetHostNameW           (WS2_32.@)
+ */
+int WINAPI GetHostNameW(WCHAR *name, int namelen)
+{
+    char buf[256];
+    int len;
+
+    TRACE("name %p, len %d\n", name, namelen);
+
+    if (!name)
+    {
+        SetLastError(WSAEFAULT);
+        return SOCKET_ERROR;
+    }
+
+    if (gethostname(buf, sizeof(buf)))
+    {
+        SetLastError(wsaErrno());
+        return SOCKET_ERROR;
+    }
+
+    if ((len = MultiByteToWideChar(CP_ACP, 0, buf, -1, NULL, 0)) > namelen)
+    {
+        SetLastError(WSAEFAULT);
+        return SOCKET_ERROR;
+    }
+    MultiByteToWideChar(CP_ACP, 0, buf, -1, name, namelen);
+    return 0;
+}
 
 /* ------------------------------------- Windows sockets extensions -- *
  *								       *
diff --git a/dlls/ws2_32/tests/sock.c b/dlls/ws2_32/tests/sock.c
index cedbf423d97..5a1a271b099 100644
--- a/dlls/ws2_32/tests/sock.c
+++ b/dlls/ws2_32/tests/sock.c
@@ -4323,6 +4323,34 @@ static void test_gethostname(void)
     ok(he != NULL, "gethostbyname(\"%s\") failed: %d\n", name, WSAGetLastError());
 }
 
+static void test_GetHostNameW(void)
+{
+    WCHAR name[256];
+    int ret, len;
+
+    WSASetLastError(0xdeadbeef);
+    ret = GetHostNameW(NULL, 256);
+    ok(ret == -1, "GetHostNameW() returned %d\n", ret);
+    ok(WSAGetLastError() == WSAEFAULT, "GetHostNameW with null buffer "
+            "failed with %d, expected %d\n", WSAGetLastError(), WSAEFAULT);
+
+    ret = GetHostNameW(name, sizeof(name));
+    ok(ret == 0, "GetHostNameW() call failed: %d\n", WSAGetLastError());
+
+    len = wcslen(name);
+    WSASetLastError(0xdeadbeef);
+    wcscpy(name, L"deadbeef");
+    ret = GetHostNameW(name, len);
+    ok(ret == -1, "GetHostNameW() returned %d\n", ret);
+    ok(!wcscmp(name, L"deadbeef"), "name changed unexpected!\n");
+    ok(WSAGetLastError() == WSAEFAULT, "GetHostNameW with insufficient length "
+            "failed with %d, expected %d\n", WSAGetLastError(), WSAEFAULT);
+
+    len++;
+    ret = GetHostNameW(name, len);
+    ok(ret == 0, "GetHostNameW() call failed: %d\n", WSAGetLastError());
+}
+
 static void test_inet_addr(void)
 {
     u_long addr;
@@ -10631,6 +10659,7 @@ START_TEST( sock )
     test_gethostbyname();
     test_gethostbyname_hack();
     test_gethostname();
+    test_GetHostNameW();
 
     test_WSASendMsg();
     test_WSASendTo();
diff --git a/dlls/ws2_32/ws2_32.spec b/dlls/ws2_32/ws2_32.spec
index 87e0bdfa2f0..29545165a48 100644
--- a/dlls/ws2_32/ws2_32.spec
+++ b/dlls/ws2_32/ws2_32.spec
@@ -57,6 +57,7 @@
 @ stdcall GetAddrInfoExOverlappedResult(ptr)
 @ stdcall GetAddrInfoExW(wstr wstr long ptr ptr ptr ptr ptr ptr ptr)
 @ stdcall GetAddrInfoW(wstr wstr ptr ptr)
+@ stdcall GetHostNameW(ptr long)
 @ stdcall GetNameInfoW(ptr long ptr long ptr long long)
 @ stdcall InetNtopW(long ptr ptr long)
 @ stdcall InetPtonW(long wstr ptr)
diff --git a/include/winsock2.h b/include/winsock2.h
index e8d033976f4..1935c99ad40 100644
--- a/include/winsock2.h
+++ b/include/winsock2.h
@@ -695,6 +695,7 @@ typedef SOCKET (WINAPI *LPFN_SOCKET)(int,int,int);
  * "Winsock2 Function Typedefs" section below.
  */
 #if WS_API_PROTOTYPES
+int WINAPI GetHostNameW(WCHAR *, int);
 SOCKET WINAPI WSAAccept(SOCKET,struct WS(sockaddr)*,LPINT,LPCONDITIONPROC,DWORD_PTR);
 INT WINAPI WSAAddressToStringA(LPSOCKADDR,DWORD,LPWSAPROTOCOL_INFOA,LPSTR,LPDWORD);
 INT WINAPI WSAAddressToStringW(LPSOCKADDR,DWORD,LPWSAPROTOCOL_INFOW,LPWSTR,LPDWORD);
-- 
2.30.2




More information about the wine-devel mailing list