2006-05-09 10:47:06 +02:00
|
|
|
/*
|
|
|
|
* DNS support
|
|
|
|
*
|
|
|
|
* Copyright (C) 2006 Hans Leidekker
|
2021-03-30 10:35:28 +02:00
|
|
|
*
|
2006-05-09 10:47:06 +02:00
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
2006-05-18 14:49:52 +02:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
2006-05-09 10:47:06 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include "windef.h"
|
|
|
|
#include "winbase.h"
|
|
|
|
#include "winerror.h"
|
|
|
|
#include "winnls.h"
|
|
|
|
#include "windns.h"
|
2006-05-31 15:46:58 +02:00
|
|
|
#include "nb30.h"
|
2021-08-03 10:20:21 +02:00
|
|
|
#include "ws2def.h"
|
2006-05-09 10:47:06 +02:00
|
|
|
|
2021-03-30 10:35:28 +02:00
|
|
|
#include "wine/debug.h"
|
2006-05-09 10:47:06 +02:00
|
|
|
#include "dnsapi.h"
|
|
|
|
|
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(dnsapi);
|
|
|
|
|
2006-05-31 15:46:58 +02:00
|
|
|
#define DEFAULT_TTL 1200
|
|
|
|
|
2021-03-30 10:35:27 +02:00
|
|
|
static DNS_STATUS do_query_netbios( PCSTR name, DNS_RECORDA **recp )
|
2006-05-31 15:46:58 +02:00
|
|
|
{
|
|
|
|
NCB ncb;
|
|
|
|
UCHAR ret;
|
|
|
|
DNS_RRSET rrset;
|
|
|
|
FIND_NAME_BUFFER *buffer;
|
|
|
|
FIND_NAME_HEADER *header;
|
|
|
|
DNS_RECORDA *record = NULL;
|
|
|
|
unsigned int i, len;
|
2006-09-13 14:36:49 +02:00
|
|
|
DNS_STATUS status = ERROR_INVALID_NAME;
|
2006-05-31 15:46:58 +02:00
|
|
|
|
|
|
|
len = strlen( name );
|
|
|
|
if (len >= NCBNAMSZ) return DNS_ERROR_RCODE_NAME_ERROR;
|
|
|
|
|
|
|
|
DNS_RRSET_INIT( rrset );
|
|
|
|
|
|
|
|
memset( &ncb, 0, sizeof(ncb) );
|
|
|
|
ncb.ncb_command = NCBFINDNAME;
|
|
|
|
|
|
|
|
memset( ncb.ncb_callname, ' ', sizeof(ncb.ncb_callname) );
|
|
|
|
memcpy( ncb.ncb_callname, name, len );
|
2006-07-10 22:18:50 +02:00
|
|
|
ncb.ncb_callname[NCBNAMSZ - 1] = '\0';
|
2006-05-31 15:46:58 +02:00
|
|
|
|
|
|
|
ret = Netbios( &ncb );
|
|
|
|
if (ret != NRC_GOODRET) return ERROR_INVALID_NAME;
|
|
|
|
|
|
|
|
header = (FIND_NAME_HEADER *)ncb.ncb_buffer;
|
|
|
|
buffer = (FIND_NAME_BUFFER *)((char *)header + sizeof(FIND_NAME_HEADER));
|
|
|
|
|
2021-03-30 10:35:28 +02:00
|
|
|
for (i = 0; i < header->node_count; i++)
|
2006-05-31 15:46:58 +02:00
|
|
|
{
|
2007-11-28 00:11:14 +01:00
|
|
|
record = heap_alloc_zero( sizeof(DNS_RECORDA) );
|
2006-05-31 15:46:58 +02:00
|
|
|
if (!record)
|
|
|
|
{
|
2006-09-13 14:36:49 +02:00
|
|
|
status = ERROR_NOT_ENOUGH_MEMORY;
|
2006-05-31 15:46:58 +02:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-03-30 10:35:27 +02:00
|
|
|
record->pName = strdup_u( name );
|
2006-05-31 15:46:58 +02:00
|
|
|
if (!record->pName)
|
|
|
|
{
|
2006-09-13 14:36:49 +02:00
|
|
|
status = ERROR_NOT_ENOUGH_MEMORY;
|
2006-05-31 15:46:58 +02:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
record->wType = DNS_TYPE_A;
|
|
|
|
record->Flags.S.Section = DnsSectionAnswer;
|
|
|
|
record->Flags.S.CharSet = DnsCharSetUtf8;
|
|
|
|
record->dwTtl = DEFAULT_TTL;
|
|
|
|
|
|
|
|
/* FIXME: network byte order? */
|
|
|
|
record->Data.A.IpAddress = *(DWORD *)((char *)buffer[i].destination_addr + 2);
|
|
|
|
|
|
|
|
DNS_RRSET_ADD( rrset, (DNS_RECORD *)record );
|
|
|
|
}
|
|
|
|
}
|
2007-01-01 12:42:44 +01:00
|
|
|
status = ERROR_SUCCESS;
|
2006-05-31 15:46:58 +02:00
|
|
|
|
|
|
|
exit:
|
|
|
|
DNS_RRSET_TERMINATE( rrset );
|
|
|
|
|
2006-09-13 14:36:49 +02:00
|
|
|
if (status != ERROR_SUCCESS)
|
2006-06-17 15:24:32 +02:00
|
|
|
DnsRecordListFree( rrset.pFirstRR, DnsFreeRecordList );
|
2006-05-31 15:46:58 +02:00
|
|
|
else
|
|
|
|
*recp = (DNS_RECORDA *)rrset.pFirstRR;
|
|
|
|
|
2006-09-13 14:36:49 +02:00
|
|
|
return status;
|
2006-05-31 15:46:58 +02:00
|
|
|
}
|
|
|
|
|
2015-10-30 19:22:50 +01:00
|
|
|
static const char *debugstr_query_request(const DNS_QUERY_REQUEST *req)
|
|
|
|
{
|
|
|
|
if (!req)
|
|
|
|
return "(null)";
|
|
|
|
|
|
|
|
return wine_dbg_sprintf("{%d %s %s %x%08x %p %d %p %p}", req->Version,
|
2021-03-30 10:35:27 +02:00
|
|
|
debugstr_w(req->QueryName), type_to_str(req->QueryType),
|
2015-10-30 19:22:50 +01:00
|
|
|
(UINT32)(req->QueryOptions>>32u), (UINT32)req->QueryOptions, req->pDnsServerList,
|
|
|
|
req->InterfaceIndex, req->pQueryCompletionCallback, req->pQueryContext);
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* DnsQueryEx [DNSAPI.@]
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
DNS_STATUS WINAPI DnsQueryEx(DNS_QUERY_REQUEST *request, DNS_QUERY_RESULT *result, DNS_QUERY_CANCEL *cancel)
|
|
|
|
{
|
|
|
|
FIXME("(%s %p %p)\n", debugstr_query_request(request), result, cancel);
|
|
|
|
return DNS_ERROR_RCODE_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
2006-05-29 12:47:31 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* DnsQuery_A [DNSAPI.@]
|
|
|
|
*
|
|
|
|
*/
|
2007-08-04 03:20:32 +02:00
|
|
|
DNS_STATUS WINAPI DnsQuery_A( PCSTR name, WORD type, DWORD options, PVOID servers,
|
2006-05-29 12:47:31 +02:00
|
|
|
PDNS_RECORDA *result, PVOID *reserved )
|
|
|
|
{
|
|
|
|
WCHAR *nameW;
|
|
|
|
DNS_RECORDW *resultW;
|
|
|
|
DNS_STATUS status;
|
|
|
|
|
2021-03-30 10:35:27 +02:00
|
|
|
TRACE( "(%s,%s,0x%08x,%p,%p,%p)\n", debugstr_a(name), type_to_str( type ),
|
2006-05-29 12:47:31 +02:00
|
|
|
options, servers, result, reserved );
|
|
|
|
|
|
|
|
if (!name || !result)
|
|
|
|
return ERROR_INVALID_PARAMETER;
|
|
|
|
|
2021-03-30 10:35:27 +02:00
|
|
|
nameW = strdup_aw( name );
|
2006-05-29 12:47:31 +02:00
|
|
|
if (!nameW) return ERROR_NOT_ENOUGH_MEMORY;
|
|
|
|
|
2021-03-30 10:35:28 +02:00
|
|
|
status = DnsQuery_W( nameW, type, options, servers, &resultW, reserved );
|
2006-05-29 12:47:31 +02:00
|
|
|
|
|
|
|
if (status == ERROR_SUCCESS)
|
|
|
|
{
|
|
|
|
*result = (DNS_RECORDA *)DnsRecordSetCopyEx(
|
|
|
|
(DNS_RECORD *)resultW, DnsCharSetUnicode, DnsCharSetAnsi );
|
|
|
|
|
|
|
|
if (!*result) status = ERROR_NOT_ENOUGH_MEMORY;
|
|
|
|
DnsRecordListFree( (DNS_RECORD *)resultW, DnsFreeRecordList );
|
|
|
|
}
|
|
|
|
|
2007-11-28 00:11:14 +01:00
|
|
|
heap_free( nameW );
|
2006-05-29 12:47:31 +02:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* DnsQuery_UTF8 [DNSAPI.@]
|
|
|
|
*
|
|
|
|
*/
|
2007-08-04 03:20:32 +02:00
|
|
|
DNS_STATUS WINAPI DnsQuery_UTF8( PCSTR name, WORD type, DWORD options, PVOID servers,
|
2006-05-29 12:47:31 +02:00
|
|
|
PDNS_RECORDA *result, PVOID *reserved )
|
|
|
|
{
|
|
|
|
DNS_STATUS ret = DNS_ERROR_RCODE_NOT_IMPLEMENTED;
|
|
|
|
|
2021-03-30 10:35:27 +02:00
|
|
|
TRACE( "(%s,%s,0x%08x,%p,%p,%p)\n", debugstr_a(name), type_to_str( type ),
|
2006-05-29 12:47:31 +02:00
|
|
|
options, servers, result, reserved );
|
|
|
|
|
|
|
|
if (!name || !result)
|
|
|
|
return ERROR_INVALID_PARAMETER;
|
|
|
|
|
2021-03-30 10:35:28 +02:00
|
|
|
if ((ret = resolv_funcs->set_serverlist( servers ))) return ret;
|
2006-05-29 12:47:31 +02:00
|
|
|
|
2021-03-30 10:35:28 +02:00
|
|
|
ret = resolv_funcs->query( name, type, options, result );
|
2006-05-29 12:47:31 +02:00
|
|
|
|
2006-05-31 15:46:58 +02:00
|
|
|
if (ret == DNS_ERROR_RCODE_NAME_ERROR && type == DNS_TYPE_A &&
|
|
|
|
!(options & DNS_QUERY_NO_NETBT))
|
|
|
|
{
|
|
|
|
TRACE( "dns lookup failed, trying netbios query\n" );
|
2021-03-30 10:35:27 +02:00
|
|
|
ret = do_query_netbios( name, result );
|
2006-05-31 15:46:58 +02:00
|
|
|
}
|
|
|
|
|
2006-05-29 12:47:31 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* DnsQuery_W [DNSAPI.@]
|
|
|
|
*
|
|
|
|
*/
|
2007-08-04 03:20:32 +02:00
|
|
|
DNS_STATUS WINAPI DnsQuery_W( PCWSTR name, WORD type, DWORD options, PVOID servers,
|
2006-05-29 12:47:31 +02:00
|
|
|
PDNS_RECORDW *result, PVOID *reserved )
|
|
|
|
{
|
|
|
|
char *nameU;
|
|
|
|
DNS_RECORDA *resultA;
|
|
|
|
DNS_STATUS status;
|
|
|
|
|
2021-03-30 10:35:27 +02:00
|
|
|
TRACE( "(%s,%s,0x%08x,%p,%p,%p)\n", debugstr_w(name), type_to_str( type ),
|
2006-05-29 12:47:31 +02:00
|
|
|
options, servers, result, reserved );
|
|
|
|
|
|
|
|
if (!name || !result)
|
|
|
|
return ERROR_INVALID_PARAMETER;
|
|
|
|
|
2021-03-30 10:35:27 +02:00
|
|
|
nameU = strdup_wu( name );
|
2006-05-29 12:47:31 +02:00
|
|
|
if (!nameU) return ERROR_NOT_ENOUGH_MEMORY;
|
|
|
|
|
2021-03-30 10:35:28 +02:00
|
|
|
status = DnsQuery_UTF8( nameU, type, options, servers, &resultA, reserved );
|
2006-05-29 12:47:31 +02:00
|
|
|
|
|
|
|
if (status == ERROR_SUCCESS)
|
|
|
|
{
|
|
|
|
*result = (DNS_RECORDW *)DnsRecordSetCopyEx(
|
|
|
|
(DNS_RECORD *)resultA, DnsCharSetUtf8, DnsCharSetUnicode );
|
|
|
|
|
|
|
|
if (!*result) status = ERROR_NOT_ENOUGH_MEMORY;
|
|
|
|
DnsRecordListFree( (DNS_RECORD *)resultA, DnsFreeRecordList );
|
|
|
|
}
|
|
|
|
|
2007-11-28 00:11:14 +01:00
|
|
|
heap_free( nameU );
|
2006-05-29 12:47:31 +02:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2021-03-30 10:35:28 +02:00
|
|
|
static DNS_STATUS get_hostname_a( COMPUTER_NAME_FORMAT format, PSTR buffer, PDWORD len )
|
2006-05-09 10:47:06 +02:00
|
|
|
{
|
|
|
|
char name[256];
|
2018-07-20 21:45:56 +02:00
|
|
|
DWORD size = ARRAY_SIZE(name);
|
2006-05-09 10:47:06 +02:00
|
|
|
|
|
|
|
if (!GetComputerNameExA( format, name, &size ))
|
|
|
|
return DNS_ERROR_NAME_DOES_NOT_EXIST;
|
|
|
|
|
|
|
|
if (!buffer || (size = lstrlenA( name ) + 1) > *len)
|
|
|
|
{
|
|
|
|
*len = size;
|
|
|
|
return ERROR_INSUFFICIENT_BUFFER;
|
|
|
|
}
|
|
|
|
|
|
|
|
lstrcpyA( buffer, name );
|
|
|
|
return ERROR_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2021-03-30 10:35:28 +02:00
|
|
|
static DNS_STATUS get_hostname_w( COMPUTER_NAME_FORMAT format, PWSTR buffer, PDWORD len )
|
2006-05-09 10:47:06 +02:00
|
|
|
{
|
|
|
|
WCHAR name[256];
|
2018-07-20 21:45:56 +02:00
|
|
|
DWORD size = ARRAY_SIZE(name);
|
2006-05-09 10:47:06 +02:00
|
|
|
|
|
|
|
if (!GetComputerNameExW( format, name, &size ))
|
|
|
|
return DNS_ERROR_NAME_DOES_NOT_EXIST;
|
|
|
|
|
|
|
|
if (!buffer || (size = lstrlenW( name ) + 1) > *len)
|
|
|
|
{
|
|
|
|
*len = size;
|
|
|
|
return ERROR_INSUFFICIENT_BUFFER;
|
|
|
|
}
|
|
|
|
|
|
|
|
lstrcpyW( buffer, name );
|
|
|
|
return ERROR_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2021-08-03 10:20:21 +02:00
|
|
|
static DNS_STATUS get_dns_server_list( IP4_ARRAY *out, DWORD *len )
|
|
|
|
{
|
|
|
|
char buf[FIELD_OFFSET(DNS_ADDR_ARRAY, AddrArray[3])];
|
|
|
|
DNS_ADDR_ARRAY *servers = (DNS_ADDR_ARRAY *)buf;
|
|
|
|
DWORD ret, needed, i, num, array_len = sizeof(buf);
|
|
|
|
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
ret = resolv_funcs->get_serverlist( AF_INET, servers, &array_len );
|
|
|
|
if (ret != ERROR_SUCCESS && ret != ERROR_MORE_DATA) goto err;
|
|
|
|
num = (array_len - FIELD_OFFSET(DNS_ADDR_ARRAY, AddrArray[0])) / sizeof(DNS_ADDR);
|
|
|
|
needed = FIELD_OFFSET(IP4_ARRAY, AddrArray[num]);
|
|
|
|
if (!out || *len < needed)
|
|
|
|
{
|
|
|
|
*len = needed;
|
|
|
|
ret = !out ? ERROR_SUCCESS : ERROR_MORE_DATA;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
if (!ret) break;
|
|
|
|
|
|
|
|
if ((char *)servers != buf) heap_free( servers );
|
|
|
|
servers = heap_alloc( array_len );
|
|
|
|
if (!servers)
|
|
|
|
{
|
|
|
|
ret = ERROR_NOT_ENOUGH_MEMORY;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
out->AddrCount = num;
|
|
|
|
for (i = 0; i < num; i++)
|
|
|
|
out->AddrArray[i] = ((SOCKADDR_IN *)servers->AddrArray[i].MaxSa)->sin_addr.s_addr;
|
|
|
|
*len = needed;
|
|
|
|
ret = ERROR_SUCCESS;
|
|
|
|
|
|
|
|
err:
|
|
|
|
if ((char *)servers != buf) heap_free( servers );
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2006-05-09 10:47:06 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* DnsQueryConfig [DNSAPI.@]
|
|
|
|
*
|
|
|
|
*/
|
2007-09-18 00:39:58 +02:00
|
|
|
DNS_STATUS WINAPI DnsQueryConfig( DNS_CONFIG_TYPE config, DWORD flag, PCWSTR adapter,
|
2006-05-09 10:47:06 +02:00
|
|
|
PVOID reserved, PVOID buffer, PDWORD len )
|
|
|
|
{
|
|
|
|
DNS_STATUS ret = ERROR_INVALID_PARAMETER;
|
|
|
|
|
2006-09-30 12:07:06 +02:00
|
|
|
TRACE( "(%d,0x%08x,%s,%p,%p,%p)\n", config, flag, debugstr_w(adapter),
|
2006-05-09 10:47:06 +02:00
|
|
|
reserved, buffer, len );
|
|
|
|
|
|
|
|
if (!len) return ERROR_INVALID_PARAMETER;
|
|
|
|
|
|
|
|
switch (config)
|
|
|
|
{
|
|
|
|
case DnsConfigDnsServerList:
|
2021-08-03 10:20:21 +02:00
|
|
|
return get_dns_server_list( buffer, len );
|
|
|
|
|
2006-05-09 10:47:06 +02:00
|
|
|
case DnsConfigHostName_A:
|
|
|
|
case DnsConfigHostName_UTF8:
|
2021-03-30 10:35:27 +02:00
|
|
|
return get_hostname_a( ComputerNameDnsHostname, buffer, len );
|
2006-05-09 10:47:06 +02:00
|
|
|
|
|
|
|
case DnsConfigFullHostName_A:
|
|
|
|
case DnsConfigFullHostName_UTF8:
|
2021-03-30 10:35:27 +02:00
|
|
|
return get_hostname_a( ComputerNameDnsFullyQualified, buffer, len );
|
2006-05-09 10:47:06 +02:00
|
|
|
|
|
|
|
case DnsConfigPrimaryDomainName_A:
|
|
|
|
case DnsConfigPrimaryDomainName_UTF8:
|
2021-03-30 10:35:27 +02:00
|
|
|
return get_hostname_a( ComputerNameDnsDomain, buffer, len );
|
2006-05-09 10:47:06 +02:00
|
|
|
|
|
|
|
case DnsConfigHostName_W:
|
2021-03-30 10:35:27 +02:00
|
|
|
return get_hostname_w( ComputerNameDnsHostname, buffer, len );
|
2006-05-09 10:47:06 +02:00
|
|
|
|
|
|
|
case DnsConfigFullHostName_W:
|
2021-03-30 10:35:27 +02:00
|
|
|
return get_hostname_w( ComputerNameDnsFullyQualified, buffer, len );
|
2006-05-09 10:47:06 +02:00
|
|
|
|
|
|
|
case DnsConfigPrimaryDomainName_W:
|
2021-03-30 10:35:27 +02:00
|
|
|
return get_hostname_w( ComputerNameDnsDomain, buffer, len );
|
2006-05-09 10:47:06 +02:00
|
|
|
|
|
|
|
case DnsConfigAdapterDomainName_A:
|
|
|
|
case DnsConfigAdapterDomainName_W:
|
|
|
|
case DnsConfigAdapterDomainName_UTF8:
|
|
|
|
case DnsConfigAdapterInfo:
|
|
|
|
case DnsConfigPrimaryHostNameRegistrationEnabled:
|
|
|
|
case DnsConfigAdapterHostNameRegistrationEnabled:
|
|
|
|
case DnsConfigAddressRegistrationMaxCount:
|
|
|
|
FIXME( "unimplemented config type %d\n", config );
|
|
|
|
break;
|
|
|
|
|
2021-08-03 10:20:22 +02:00
|
|
|
case DnsConfigDnsServersUnspec:
|
|
|
|
return resolv_funcs->get_serverlist( AF_UNSPEC, buffer, len );
|
2021-08-03 10:20:21 +02:00
|
|
|
case DnsConfigDnsServersIpv4:
|
|
|
|
return resolv_funcs->get_serverlist( AF_INET, buffer, len );
|
2021-08-03 10:20:22 +02:00
|
|
|
case DnsConfigDnsServersIpv6:
|
|
|
|
return resolv_funcs->get_serverlist( AF_INET6, buffer, len );
|
2021-08-03 10:20:21 +02:00
|
|
|
|
2021-08-03 10:20:23 +02:00
|
|
|
case DnsConfigSearchList:
|
|
|
|
return resolv_funcs->get_searchlist( buffer, len );
|
|
|
|
|
2006-05-09 10:47:06 +02:00
|
|
|
default:
|
|
|
|
WARN( "unknown config type: %d\n", config );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|