[NETAPI32] Implement DsGetDcSiteCoverageA
[reactos.git] / dll / win32 / netapi32 / netlogon.c
index 98dc5c7..0843cf7 100644 (file)
@@ -496,9 +496,86 @@ DsGetDcSiteCoverageA(
     _Out_ PULONG EntryCount,
     _Out_ LPSTR **SiteNames)
 {
-    FIXME("DsGetDcSiteCoverageA(%s, %p, %p)\n",
+    PWSTR pServerNameW = NULL;
+    PWSTR *pSiteNamesW = NULL;
+    PSTR *pSiteNamesA = NULL;
+    UNICODE_STRING UnicodeString;
+    ANSI_STRING AnsiString;
+    PSTR Ptr;
+    ULONG BufferSize, i;
+    NTSTATUS Status;
+    NET_API_STATUS status = ERROR_SUCCESS;
+
+    TRACE("DsGetDcSiteCoverageA(%s, %p, %p)\n",
           debugstr_a(ServerName), EntryCount, SiteNames);
-    return ERROR_CALL_NOT_IMPLEMENTED;
+
+    if (ServerName != NULL)
+    {
+        pServerNameW = NetpAllocWStrFromAnsiStr((PSTR)ServerName);
+        if (pServerNameW == NULL)
+        {
+            status = ERROR_NOT_ENOUGH_MEMORY;
+            goto done;
+        }
+    }
+
+    status = DsGetDcSiteCoverageW(pServerNameW,
+                                  EntryCount,
+                                  &pSiteNamesW);
+    if (status != ERROR_SUCCESS)
+        goto done;
+
+    BufferSize = *EntryCount * sizeof(PSTR);
+    for (i = 0; i < *EntryCount; i++)
+    {
+        RtlInitUnicodeString(&UnicodeString, pSiteNamesW[i]);
+        BufferSize += RtlUnicodeStringToAnsiSize(&UnicodeString);
+    }
+
+    status = NetApiBufferAllocate(BufferSize, (PVOID*)&pSiteNamesA);
+    if (status != NERR_Success)
+        goto done;
+
+    ZeroMemory(pSiteNamesA, BufferSize);
+
+    Ptr = (PSTR)((ULONG_PTR)pSiteNamesA + *EntryCount * sizeof(PSTR));
+    for (i = 0; i < *EntryCount; i++)
+    {
+        pSiteNamesA[i] = Ptr;
+
+        RtlInitUnicodeString(&UnicodeString, pSiteNamesW[i]);
+
+        AnsiString.Length = 0;
+        AnsiString.MaximumLength = BufferSize;
+        AnsiString.Buffer = Ptr;
+
+        Status = RtlUnicodeStringToAnsiString(&AnsiString,
+                                              &UnicodeString,
+                                              FALSE);
+        if (!NT_SUCCESS(Status))
+        {
+            status = RtlNtStatusToDosError(Status);
+            goto done;
+        }
+
+        Ptr = (PSTR)((ULONG_PTR)Ptr + AnsiString.Length + sizeof(CHAR));
+        BufferSize -= (AnsiString.Length + sizeof(CHAR));
+    }
+
+    *SiteNames = pSiteNamesA;
+    pSiteNamesA = NULL;
+
+done:
+    if (status != NERR_Success && pSiteNamesA != NULL)
+        NetApiBufferFree(pSiteNamesA);
+
+    if (pSiteNamesW != NULL)
+        NetApiBufferFree(pSiteNamesW);
+
+    if (pServerNameW != NULL)
+        NetApiBufferFree(pServerNameW);
+
+    return status;
 }
 
 
@@ -604,19 +681,52 @@ DsGetForestTrustInformationW(
 DWORD
 WINAPI
 DsGetSiteNameA(
-    _In_ LPCSTR ComputerName,
+    _In_opt_ LPCSTR ComputerName,
     _Out_ LPSTR *SiteName)
 {
-    FIXME("DsGetSiteNameA(%s, %p)\n",
+    PWSTR pComputerNameW = NULL;
+    PWSTR pSiteNameW = NULL;
+    NET_API_STATUS status = ERROR_SUCCESS;
+
+    TRACE("DsGetSiteNameA(%s, %p)\n",
           debugstr_a(ComputerName), SiteName);
-    return ERROR_CALL_NOT_IMPLEMENTED;
+
+    if (ComputerName != NULL)
+    {
+        pComputerNameW = NetpAllocWStrFromAnsiStr((PSTR)ComputerName);
+        if (pComputerNameW == NULL)
+        {
+            status = ERROR_NOT_ENOUGH_MEMORY;
+            goto done;
+        }
+    }
+
+    status = DsGetSiteNameW(pComputerNameW,
+                            &pSiteNameW);
+    if (status != ERROR_SUCCESS)
+        goto done;
+
+    *SiteName = NetpAllocAnsiStrFromWStr(pSiteNameW);
+    if (*SiteName == NULL)
+    {
+        status = ERROR_NOT_ENOUGH_MEMORY;
+    }
+
+done:
+    if (pSiteNameW != NULL)
+        NetApiBufferFree(pSiteNameW);
+
+    if (pComputerNameW != NULL)
+        NetApiBufferFree(pComputerNameW);
+
+    return status;
 }
 
 
 DWORD
 WINAPI
 DsGetSiteNameW(
-    _In_ LPCWSTR ComputerName,
+    _In_opt_ LPCWSTR ComputerName,
     _Out_ LPWSTR *SiteName)
 {
     NET_API_STATUS status;