[MSWSOCK] Call explicitly DnsQuery_W. Copy host aliases if available as answer from...
authorPeter Hater <7element@mail.bg>
Fri, 2 Jun 2017 17:52:20 +0000 (17:52 +0000)
committerPeter Hater <7element@mail.bg>
Fri, 2 Jun 2017 17:52:20 +0000 (17:52 +0000)
svn path=/trunk/; revision=74750

reactos/dll/win32/mswsock/mswhelper.c
reactos/dll/win32/mswsock/mswhelper.h
reactos/dll/win32/mswsock/nsplookup.c

index e3f20d0..1a08ccd 100644 (file)
@@ -423,7 +423,7 @@ StrA2WHeapAlloc(_In_opt_ HANDLE hHeap,
         return NULL;
     }
 
-    ret = MultiByteToWideChar(1252,
+    ret = MultiByteToWideChar(CP_ACP,
                               0,
                               aStr,
                               aStrByteLen,
@@ -464,7 +464,7 @@ StrW2AHeapAlloc(_In_opt_ HANDLE hHeap,
         return NULL;
     }
 
-    ret = WideCharToMultiByte(1252,
+    ret = WideCharToMultiByte(CP_ACP,
                               0,
                               wStr,
                               charLen,
@@ -553,7 +553,7 @@ StrAryCpyHeapAllocA(_In_opt_ HANDLE hHeap,
         if (aCount >= MAX_ARRAY_SIZE)
             return NULL;
 
-        bItmLen = (strlen(*aSrcPtr) + 1) * sizeof(char);
+        bItmLen = strlen(*aSrcPtr) + 1;
         aStrByteLen[aCount] = bItmLen;
 
         bLen += sizeof(*aSrcPtr) + bItmLen;
@@ -591,3 +591,93 @@ StrAryCpyHeapAllocA(_In_opt_ HANDLE hHeap,
 
     return resA;
 }
+
+char**
+StrAryCpyHeapAllocWToA(_In_opt_ HANDLE hHeap,
+    _In_ WCHAR** wStrAry)
+{
+    WCHAR** wSrcPtr;
+    char** aDstPtr;
+    char* aDstNextStr;
+    DWORD aStrByteLen[MAX_ARRAY_SIZE];
+    int bLen;
+    int bItmLen;
+    int aCount;
+    int i1;
+    char** resA;
+    int ret;
+    char* aStr;
+
+    if (hHeap == 0)
+        hHeap = GetProcessHeap();
+
+    /* Calculating size of array ... */
+    wSrcPtr = wStrAry;
+    bLen = 0;
+    aCount = 0;
+
+    while (*wSrcPtr != NULL)
+    {
+        if (aCount >= MAX_ARRAY_SIZE)
+            return NULL;
+
+        bItmLen = wcslen(*wSrcPtr) + 1;
+        aStrByteLen[aCount] = bItmLen;
+
+        bLen += sizeof(*wSrcPtr) + bItmLen;
+
+        wSrcPtr++;
+        aCount++;
+    }
+
+    /* size for NULL-terminator */
+    bLen += sizeof(*wSrcPtr);
+
+    /* get memory */
+    resA = HeapAlloc(hHeap, 0, bLen);
+
+    /* copy data */
+    wSrcPtr = wStrAry;
+    aDstPtr = resA;
+
+    /* pos for the first string */
+    aDstNextStr = (char*)(resA + aCount + 1);
+    for (i1 = 0; i1 < aCount; i1++)
+    {
+        bItmLen = aStrByteLen[i1];
+
+        *aDstPtr = aDstNextStr;
+
+        aStr = HeapAlloc(hHeap, 0, bItmLen);
+        if (aStr == NULL)
+        {
+            HeapFree(hHeap, 0, aStr);
+            return NULL;
+        }
+
+        ret = WideCharToMultiByte(CP_ACP,
+                                  0,
+                                  *wSrcPtr,
+                                  bItmLen,
+                                  aStr,
+                                  bItmLen,
+                                  NULL,
+                                  NULL);
+        if (ret != bItmLen)
+        {
+            HeapFree(hHeap, 0, aStr);
+            return NULL;
+        }
+        RtlCopyMemory(*aDstPtr, aStr, bItmLen);
+        HeapFree(hHeap, 0, aStr);
+
+        aDstNextStr = (char*)((DWORD)aDstNextStr + (DWORD)bItmLen);
+        aDstPtr++;
+        wSrcPtr++;
+    }
+
+    /* terminate with NULL */
+    *aDstPtr = NULL;
+
+    return resA;
+}
index 1680752..c163648 100644 (file)
@@ -117,4 +117,13 @@ StrAryCpyHeapAllocA(
   _In_opt_ HANDLE hHeap,
   _In_ char** aStrAry);
 
+/* strary:
+   ptr1 ... ptrn \0
+   data1 ... datan
+*/
+char**
+StrAryCpyHeapAllocWToA(
+  _In_opt_ HANDLE hHeap,
+  _In_ WCHAR** aStrAry);
+
 #endif // _MSWHELPER_H
index 23bbe59..b11712a 100644 (file)
@@ -469,12 +469,14 @@ NSP_GetHostByNameHeapAllocW(_In_ PWSHANDLEINTERN data,
                             _Out_ PWSHOSTINFOINTERN hostinfo)
 {
     HANDLE hHeap = GetProcessHeap();
-    DNS_STATUS dns_status = {0};
+    DNS_STATUS dns_status = { 0 };
     /* include/WinDNS.h -- look up DNS_RECORD on MSDN */
-    PDNS_RECORD dp;
-    PDNS_RECORD curr;
+    PDNS_RECORDW dp;
+    PDNS_RECORDW curr;
     INT result = ERROR_SUCCESS;
     DWORD dwQueryFlags = DNS_QUERY_STANDARD;
+    PWCHAR Aliases[WS2_INTERNAL_MAX_ALIAS] = { 0 };
+    int AliasIndex = 0;
 
     /* needed to be cleaned up if != NULL */
     dp = NULL;
@@ -492,13 +494,12 @@ NSP_GetHostByNameHeapAllocW(_In_ PWSHANDLEINTERN data,
 
     /* DNS_TYPE_A: include/WinDNS.h */
     /* DnsQuery -- lib/dnsapi/dnsapi/query.c */
-    dns_status = DnsQuery(data->hostnameW,
-                          DNS_TYPE_A,
-                          dwQueryFlags,
-                          /* extra dns servers */ 0,
-                          &dp,
-                          0);
-
+    dns_status = DnsQuery_W(data->hostnameW,
+                            DNS_TYPE_A,
+                            dwQueryFlags,
+                            NULL /* extra dns servers */,
+                            &dp,
+                            NULL);
     if (dns_status == ERROR_INVALID_NAME)
     {
         WSASetLastError(WSAEFAULT);
@@ -517,7 +518,10 @@ NSP_GetHostByNameHeapAllocW(_In_ PWSHANDLEINTERN data,
     curr = dp;
     while ((curr->pNext != NULL) || (curr->wType != DNS_TYPE_A))
     {
-        /* FIXME build aliases list */
+        if (curr->wType == DNS_TYPE_CNAME)
+        {
+            Aliases[AliasIndex++] = curr->Data.Cname.pNameHost;
+        }
         curr = curr->pNext;
     }
 
@@ -526,10 +530,12 @@ NSP_GetHostByNameHeapAllocW(_In_ PWSHANDLEINTERN data,
         result = WSASERVICE_NOT_FOUND;
         goto cleanup;
     }
-
     hostinfo->hostnameW = StrCpyHeapAllocW(hHeap, curr->pName);
     hostinfo->addr4 = curr->Data.A.IpAddress;
-    /* FIXME attach built aliases list */
+    if (AliasIndex)
+    {
+        hostinfo->servaliasesA = StrAryCpyHeapAllocWToA(hHeap, (WCHAR**)&Aliases);
+    }
     result = ERROR_SUCCESS;
 
 cleanup: