Exemplo n.º 1
0
/*
 * @implemented
 */
INT
WSAAPI
WSALookupServiceNextA(IN HANDLE hLookup,
                      IN DWORD dwControlFlags,
                      IN OUT LPDWORD lpdwBufferLength,
                      OUT LPWSAQUERYSETA lpqsResults)
{
    LPWSAQUERYSETW UnicodeQuerySet;
    DWORD UnicodeQuerySetSize = *lpdwBufferLength;
    INT ErrorCode;
    DPRINT("WSALookupServiceNextA: %lx\n", hLookup);

    /* Check how much the user is giving */
    if (UnicodeQuerySetSize >= sizeof(WSAQUERYSETW))
    {
        /* Allocate the buffer we'll use */
        UnicodeQuerySet = HeapAlloc(WsSockHeap, 0, UnicodeQuerySetSize);
        if (!UnicodeQuerySet) UnicodeQuerySetSize = 0;
    }
    else
    {
        /* His buffer is too small */
        UnicodeQuerySetSize = 0;
        UnicodeQuerySet = NULL;
    }

    /* Call the Unicode Function */
    ErrorCode = WSALookupServiceNextW(hLookup,
                                      dwControlFlags,
                                      &UnicodeQuerySetSize,
                                      UnicodeQuerySet);
    if (ErrorCode == ERROR_SUCCESS)
    {
        /* Not convert to ANSI */
        ErrorCode = MapUnicodeQuerySetToAnsi(UnicodeQuerySet,
                                             lpdwBufferLength,
                                             lpqsResults);
        if (ErrorCode != ERROR_SUCCESS) SetLastError(ErrorCode);
    }
    else
    {
        /* Check if we ran out of space */
        if (GetLastError() == WSAEFAULT)
        {
            /* Return how much space we'll need, including padding */
            *lpdwBufferLength = UnicodeQuerySetSize +
                                ((sizeof(ULONG) * 6) - (6 * 1));
        }
    }

    /* If we had a local buffer, free it */
    if (UnicodeQuerySet) HeapFree(WsSockHeap, 0, UnicodeQuerySet);

    /* Return to caller */
    return ErrorCode == ERROR_SUCCESS ? ErrorCode : SOCKET_ERROR;
}
Exemplo n.º 2
0
static
INT
WINAPI
LookupNodeByAddr(IN LPWSTR pNodeBuffer,
                 IN DWORD NodeBufferSize,
                 IN BOOLEAN OnlyNodeName,
                 IN PVOID Addr,
                 IN DWORD AddrSize,
                 IN INT AddressFamily)
{
    GUID LookupGuid = SVCID_DNS_TYPE_PTR;
    PIN_ADDR Ip4Addr = Addr;
    WCHAR ReverseBuffer[76];
    WSAQUERYSETW Restrictions, Reply;
    DWORD BufferLength;
    INT ErrorCode;
    HANDLE LookupHandle;

    /* Validate the address */
    if (!Addr) return WSAEFAULT;

    /* Make sure the family and address size match */
    if (AddressFamily == AF_INET6)
    {
        /* Check the address size for this type */
        if (AddrSize != sizeof(IN6_ADDR)) return WSAEFAULT;
        Ip4Addr = (PIN_ADDR)&((PIN6_ADDR)Addr)->u.Byte[12];
    }
    else if (AddressFamily == AF_INET)
    {
        /* Check the address size for this type */
        if (AddrSize != sizeof(IN_ADDR)) return WSAEFAULT;
    }
    else
    {
        /* Address family not supported */
        return WSAEAFNOSUPPORT;
    }

    /* Check if this is a mapped V4 IPv6 or pure IPv4 */
    if (((AddressFamily == AF_INET6) && (IN6_IS_ADDR_V4MAPPED(Addr))) ||
        (AddressFamily == AF_INET))
    {
        /* Get the reverse name */
        Dns_Ip4AddressToReverseName_W(ReverseBuffer, *Ip4Addr);
    }
    /* FIXME: Not implemented for now 
    else if ( */

    /* By this point we have the Reverse Name, so prepare for lookup */
    RtlZeroMemory(&Restrictions, sizeof(Restrictions));
    Restrictions.dwSize = sizeof(Restrictions);
    Restrictions.lpszServiceInstanceName = ReverseBuffer;
    Restrictions.lpServiceClassId = &LookupGuid;
    Restrictions.dwNameSpace = NS_DNS;

    /* Now do the lookup */
    ErrorCode = WSALookupServiceBeginW(&Restrictions,
                                       LUP_RETURN_NAME,
                                       &LookupHandle);
    if (ErrorCode == ERROR_SUCCESS)
    {
        /* Lookup successful, now get the data */
        BufferLength = (NI_MAXHOST - 1) * sizeof(WCHAR) + sizeof(Restrictions);
        ErrorCode = WSALookupServiceNextW(LookupHandle,
                                          0,
                                          &BufferLength,
                                          &Restrictions);
        if (ErrorCode == ERROR_SUCCESS)
        {
            /* Now check if we have a name back */
            Reply = Restrictions;
            if (!Reply.lpszServiceInstanceName)
            {
                /* Fail */
                ErrorCode = WSAHOST_NOT_FOUND;
            }
            else
            {
                /* Check if the caller only wants the node name */
                if (OnlyNodeName)
                {
                    /* Split it and get only the partial host name */
                    Dns_SplitHostFromDomainNameW(Reply.lpszServiceInstanceName);
                }

                /* Check the length and see if it's within our limit */
                if (wcslen(Reply.lpszServiceInstanceName) + 1 >
                    NodeBufferSize)
                {
                    /* It's not, so fail */
                    ErrorCode = WSAEFAULT;
                }
                else
                {
                    /* It will fit, let's copy it*/
                    wcscpy(pNodeBuffer, Reply.lpszServiceInstanceName);
                }
            }
        }
    }
    else if (ErrorCode == WSASERVICE_NOT_FOUND)
    {
        /* Normalize the error code */
        ErrorCode = WSAHOST_NOT_FOUND;
    }

    /* Finish the lookup if one was in progress */
    if (LookupHandle) WSALookupServiceEnd(LookupHandle);

    /* Return the error code */
    return ErrorCode;
}