winemac: Add proper locking in macdrv_surface_set_region().
[wine/multimedia.git] / dlls / dnsapi / query.c
blobb1d80dd8f009c0892578112c5cad2d97c825dad0
1 /*
2 * DNS support
4 * Copyright (C) 2006 Hans Leidekker
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #include "wine/port.h"
23 #include "wine/debug.h"
25 #include <stdarg.h>
26 #include <string.h>
27 #include <sys/types.h>
29 #ifdef HAVE_NETINET_IN_H
30 # include <netinet/in.h>
31 #endif
32 #ifdef HAVE_ARPA_NAMESER_H
33 # include <arpa/nameser.h>
34 #endif
35 #ifdef HAVE_RESOLV_H
36 # include <resolv.h>
37 #endif
38 #ifdef HAVE_NETDB_H
39 # include <netdb.h>
40 #endif
42 #include "windef.h"
43 #include "winbase.h"
44 #include "winerror.h"
45 #include "winnls.h"
46 #include "windns.h"
47 #include "nb30.h"
49 #include "dnsapi.h"
51 WINE_DEFAULT_DEBUG_CHANNEL(dnsapi);
53 #ifdef HAVE_RESOLV
55 /* call res_init() just once because of a bug in Mac OS X 10.4 */
56 /* call once per thread on systems that have per-thread _res */
57 static void initialise_resolver( void )
59 if ((_res.options & RES_INIT) == 0)
60 res_init();
63 static const char *dns_section_to_str( ns_sect section )
65 switch (section)
67 case ns_s_qd: return "Question";
68 case ns_s_an: return "Answer";
69 case ns_s_ns: return "Authority";
70 case ns_s_ar: return "Additional";
71 default:
73 static char tmp[5];
74 FIXME( "unknown section: 0x%02x\n", section );
75 sprintf( tmp, "0x%02x", section );
76 return tmp;
81 static unsigned long dns_map_options( DWORD options )
83 unsigned long ret = 0;
85 if (options == DNS_QUERY_STANDARD)
86 return RES_DEFAULT;
88 if (options & DNS_QUERY_ACCEPT_TRUNCATED_RESPONSE)
89 ret |= RES_IGNTC;
90 if (options & DNS_QUERY_USE_TCP_ONLY)
91 ret |= RES_USEVC;
92 if (options & DNS_QUERY_NO_RECURSION)
93 ret &= ~RES_RECURSE;
94 if (options & DNS_QUERY_NO_LOCAL_NAME)
95 ret &= ~RES_DNSRCH;
96 if (options & DNS_QUERY_NO_HOSTS_FILE)
97 ret |= RES_NOALIASES;
98 if (options & DNS_QUERY_TREAT_AS_FQDN)
99 ret &= ~RES_DEFNAMES;
101 if (options & DNS_QUERY_DONT_RESET_TTL_VALUES)
102 FIXME( "option DNS_QUERY_DONT_RESET_TTL_VALUES not implemented\n" );
103 if (options & DNS_QUERY_RESERVED)
104 FIXME( "option DNS_QUERY_RESERVED not implemented\n" );
105 if (options & DNS_QUERY_WIRE_ONLY)
106 FIXME( "option DNS_QUERY_WIRE_ONLY not implemented\n" );
107 if (options & DNS_QUERY_NO_WIRE_QUERY)
108 FIXME( "option DNS_QUERY_NO_WIRE_QUERY not implemented\n" );
109 if (options & DNS_QUERY_BYPASS_CACHE)
110 FIXME( "option DNS_QUERY_BYPASS_CACHE not implemented\n" );
111 if (options & DNS_QUERY_RETURN_MESSAGE)
112 FIXME( "option DNS_QUERY_RETURN_MESSAGE not implemented\n" );
114 if (options & DNS_QUERY_NO_NETBT)
115 TRACE( "netbios query disabled\n" );
117 return ret;
120 static DNS_STATUS dns_map_error( int error )
122 switch (error)
124 case ns_r_noerror: return ERROR_SUCCESS;
125 case ns_r_formerr: return DNS_ERROR_RCODE_FORMAT_ERROR;
126 case ns_r_servfail: return DNS_ERROR_RCODE_SERVER_FAILURE;
127 case ns_r_nxdomain: return DNS_ERROR_RCODE_NAME_ERROR;
128 case ns_r_notimpl: return DNS_ERROR_RCODE_NOT_IMPLEMENTED;
129 case ns_r_refused: return DNS_ERROR_RCODE_REFUSED;
130 case ns_r_yxdomain: return DNS_ERROR_RCODE_YXDOMAIN;
131 case ns_r_yxrrset: return DNS_ERROR_RCODE_YXRRSET;
132 case ns_r_nxrrset: return DNS_ERROR_RCODE_NXRRSET;
133 case ns_r_notauth: return DNS_ERROR_RCODE_NOTAUTH;
134 case ns_r_notzone: return DNS_ERROR_RCODE_NOTZONE;
135 default:
136 FIXME( "unmapped error code: %d\n", error );
137 return DNS_ERROR_RCODE_NOT_IMPLEMENTED;
141 static DNS_STATUS dns_map_h_errno( int error )
143 switch (error)
145 case NO_DATA:
146 case HOST_NOT_FOUND: return DNS_ERROR_RCODE_NAME_ERROR;
147 case TRY_AGAIN: return DNS_ERROR_RCODE_SERVER_FAILURE;
148 case NO_RECOVERY: return DNS_ERROR_RCODE_REFUSED;
149 case NETDB_INTERNAL: return DNS_ERROR_RCODE;
150 default:
151 FIXME( "unmapped error code: %d\n", error );
152 return DNS_ERROR_RCODE_NOT_IMPLEMENTED;
156 static char *dns_dname_from_msg( ns_msg msg, const unsigned char *pos )
158 int len;
159 char *str, dname[NS_MAXDNAME] = ".";
161 /* returns *compressed* length, ignore it */
162 dns_ns_name_uncompress( ns_msg_base(msg), ns_msg_end(msg), pos, dname, sizeof(dname) );
164 len = strlen( dname );
165 str = heap_alloc( len + 1 );
166 if (str) strcpy( str, dname );
167 return str;
170 static char *dns_str_from_rdata( const unsigned char *rdata )
172 char *str;
173 unsigned int len = rdata[0];
175 str = heap_alloc( len + 1 );
176 if (str)
178 memcpy( str, ++rdata, len );
179 str[len] = '\0';
181 return str;
184 static unsigned int dns_get_record_size( const ns_rr *rr )
186 const unsigned char *pos = rr->rdata;
187 unsigned int num = 0, size = sizeof(DNS_RECORDA);
189 switch (rr->type)
191 case ns_t_key:
193 pos += sizeof(WORD) + sizeof(BYTE) + sizeof(BYTE);
194 size += rr->rdata + rr->rdlength - pos - 1;
195 break;
197 case ns_t_sig:
199 pos += sizeof(PCHAR) + sizeof(WORD) + 2 * sizeof(BYTE);
200 pos += 3 * sizeof(DWORD) + 2 * sizeof(WORD);
201 size += rr->rdata + rr->rdlength - pos - 1;
202 break;
204 case ns_t_hinfo:
205 case ns_t_isdn:
206 case ns_t_txt:
207 case ns_t_x25:
209 while (pos[0] && pos < rr->rdata + rr->rdlength)
211 num++;
212 pos += pos[0] + 1;
214 size += (num - 1) * sizeof(PCHAR);
215 break;
217 case ns_t_null:
219 size += rr->rdlength - 1;
220 break;
222 case ns_t_nxt:
223 case ns_t_wks:
224 case 0xff01: /* WINS */
226 FIXME( "unhandled type: %s\n", dns_type_to_str( rr->type ) );
227 break;
229 default:
230 break;
232 return size;
235 static DNS_STATUS dns_copy_rdata( ns_msg msg, const ns_rr *rr, DNS_RECORDA *r, WORD *dlen )
237 DNS_STATUS ret = ERROR_SUCCESS;
238 const unsigned char *pos = rr->rdata;
239 unsigned int i, size;
241 switch (rr->type)
243 case ns_t_a:
245 r->Data.A.IpAddress = *(const DWORD *)pos;
246 *dlen = sizeof(DNS_A_DATA);
247 break;
249 case ns_t_aaaa:
251 for (i = 0; i < sizeof(IP6_ADDRESS)/sizeof(DWORD); i++)
253 r->Data.AAAA.Ip6Address.IP6Dword[i] = *(const DWORD *)pos;
254 pos += sizeof(DWORD);
257 *dlen = sizeof(DNS_AAAA_DATA);
258 break;
260 case ns_t_key:
262 /* FIXME: byte order? */
263 r->Data.KEY.wFlags = *(const WORD *)pos; pos += sizeof(WORD);
264 r->Data.KEY.chProtocol = *pos++;
265 r->Data.KEY.chAlgorithm = *pos++;
267 size = rr->rdata + rr->rdlength - pos;
269 for (i = 0; i < size; i++)
270 r->Data.KEY.Key[i] = *pos++;
272 *dlen = sizeof(DNS_KEY_DATA) + (size - 1) * sizeof(BYTE);
273 break;
275 case ns_t_rp:
276 case ns_t_minfo:
278 r->Data.MINFO.pNameMailbox = dns_dname_from_msg( msg, pos );
279 if (!r->Data.MINFO.pNameMailbox) return ERROR_NOT_ENOUGH_MEMORY;
281 if (dns_ns_name_skip( &pos, ns_msg_end( msg ) ) < 0)
282 return DNS_ERROR_BAD_PACKET;
284 r->Data.MINFO.pNameErrorsMailbox = dns_dname_from_msg( msg, pos );
285 if (!r->Data.MINFO.pNameErrorsMailbox)
287 heap_free( r->Data.MINFO.pNameMailbox );
288 return ERROR_NOT_ENOUGH_MEMORY;
291 *dlen = sizeof(DNS_MINFO_DATAA);
292 break;
294 case ns_t_afsdb:
295 case ns_t_rt:
296 case ns_t_mx:
298 r->Data.MX.wPreference = ntohs( *(const WORD *)pos );
299 r->Data.MX.pNameExchange = dns_dname_from_msg( msg, pos + sizeof(WORD) );
300 if (!r->Data.MX.pNameExchange) return ERROR_NOT_ENOUGH_MEMORY;
302 *dlen = sizeof(DNS_MX_DATAA);
303 break;
305 case ns_t_null:
307 r->Data.Null.dwByteCount = rr->rdlength;
308 memcpy( r->Data.Null.Data, rr->rdata, rr->rdlength );
310 *dlen = sizeof(DNS_NULL_DATA) + rr->rdlength - 1;
311 break;
313 case ns_t_cname:
314 case ns_t_ns:
315 case ns_t_mb:
316 case ns_t_md:
317 case ns_t_mf:
318 case ns_t_mg:
319 case ns_t_mr:
320 case ns_t_ptr:
322 r->Data.PTR.pNameHost = dns_dname_from_msg( msg, pos );
323 if (!r->Data.PTR.pNameHost) return ERROR_NOT_ENOUGH_MEMORY;
325 *dlen = sizeof(DNS_PTR_DATAA);
326 break;
328 case ns_t_sig:
330 r->Data.SIG.pNameSigner = dns_dname_from_msg( msg, pos );
331 if (!r->Data.SIG.pNameSigner) return ERROR_NOT_ENOUGH_MEMORY;
333 if (dns_ns_name_skip( &pos, ns_msg_end( msg ) ) < 0)
334 return DNS_ERROR_BAD_PACKET;
336 /* FIXME: byte order? */
337 r->Data.SIG.wTypeCovered = *(const WORD *)pos; pos += sizeof(WORD);
338 r->Data.SIG.chAlgorithm = *pos++;
339 r->Data.SIG.chLabelCount = *pos++;
340 r->Data.SIG.dwOriginalTtl = *(const DWORD *)pos; pos += sizeof(DWORD);
341 r->Data.SIG.dwExpiration = *(const DWORD *)pos; pos += sizeof(DWORD);
342 r->Data.SIG.dwTimeSigned = *(const DWORD *)pos; pos += sizeof(DWORD);
343 r->Data.SIG.wKeyTag = *(const WORD *)pos;
345 size = rr->rdata + rr->rdlength - pos;
347 for (i = 0; i < size; i++)
348 r->Data.SIG.Signature[i] = *pos++;
350 *dlen = sizeof(DNS_SIG_DATAA) + (size - 1) * sizeof(BYTE);
351 break;
353 case ns_t_soa:
355 r->Data.SOA.pNamePrimaryServer = dns_dname_from_msg( msg, pos );
356 if (!r->Data.SOA.pNamePrimaryServer) return ERROR_NOT_ENOUGH_MEMORY;
358 if (dns_ns_name_skip( &pos, ns_msg_end( msg ) ) < 0)
359 return DNS_ERROR_BAD_PACKET;
361 r->Data.SOA.pNameAdministrator = dns_dname_from_msg( msg, pos );
362 if (!r->Data.SOA.pNameAdministrator)
364 heap_free( r->Data.SOA.pNamePrimaryServer );
365 return ERROR_NOT_ENOUGH_MEMORY;
368 if (dns_ns_name_skip( &pos, ns_msg_end( msg ) ) < 0)
369 return DNS_ERROR_BAD_PACKET;
371 r->Data.SOA.dwSerialNo = ntohl( *(const DWORD *)pos ); pos += sizeof(DWORD);
372 r->Data.SOA.dwRefresh = ntohl( *(const DWORD *)pos ); pos += sizeof(DWORD);
373 r->Data.SOA.dwRetry = ntohl( *(const DWORD *)pos ); pos += sizeof(DWORD);
374 r->Data.SOA.dwExpire = ntohl( *(const DWORD *)pos ); pos += sizeof(DWORD);
375 r->Data.SOA.dwDefaultTtl = ntohl( *(const DWORD *)pos ); pos += sizeof(DWORD);
377 *dlen = sizeof(DNS_SOA_DATAA);
378 break;
380 case ns_t_srv:
382 r->Data.SRV.wPriority = ntohs( *(const WORD *)pos ); pos += sizeof(WORD);
383 r->Data.SRV.wWeight = ntohs( *(const WORD *)pos ); pos += sizeof(WORD);
384 r->Data.SRV.wPort = ntohs( *(const WORD *)pos ); pos += sizeof(WORD);
386 r->Data.SRV.pNameTarget = dns_dname_from_msg( msg, pos );
387 if (!r->Data.SRV.pNameTarget) return ERROR_NOT_ENOUGH_MEMORY;
389 *dlen = sizeof(DNS_SRV_DATAA);
390 break;
392 case ns_t_hinfo:
393 case ns_t_isdn:
394 case ns_t_x25:
395 case ns_t_txt:
397 i = 0;
398 while (pos[0] && pos < rr->rdata + rr->rdlength)
400 r->Data.TXT.pStringArray[i] = dns_str_from_rdata( pos );
401 if (!r->Data.TXT.pStringArray[i])
403 while (i > 0) heap_free( r->Data.TXT.pStringArray[--i] );
404 return ERROR_NOT_ENOUGH_MEMORY;
406 i++;
407 pos += pos[0] + 1;
409 r->Data.TXT.dwStringCount = i;
410 *dlen = sizeof(DNS_TXT_DATAA) + (i - 1) * sizeof(PCHAR);
411 break;
413 case ns_t_atma:
414 case ns_t_loc:
415 case ns_t_nxt:
416 case ns_t_tsig:
417 case ns_t_wks:
418 case 0x00f9: /* TKEY */
419 case 0xff01: /* WINS */
420 case 0xff02: /* WINSR */
421 default:
422 FIXME( "unhandled type: %s\n", dns_type_to_str( rr->type ) );
423 return DNS_ERROR_RCODE_NOT_IMPLEMENTED;
426 return ret;
429 static DNS_STATUS dns_copy_record( ns_msg msg, ns_sect section,
430 unsigned short num, DNS_RECORDA **recp )
432 DNS_STATUS ret;
433 DNS_RECORDA *record;
434 WORD dlen;
435 ns_rr rr;
437 if (dns_ns_parserr( &msg, section, num, &rr ) < 0)
438 return DNS_ERROR_BAD_PACKET;
440 if (!(record = heap_alloc_zero( dns_get_record_size( &rr ) )))
441 return ERROR_NOT_ENOUGH_MEMORY;
443 record->pName = dns_strdup_u( rr.name );
444 if (!record->pName)
446 heap_free( record );
447 return ERROR_NOT_ENOUGH_MEMORY;
450 record->wType = rr.type;
451 record->Flags.S.Section = section;
452 record->Flags.S.CharSet = DnsCharSetUtf8;
453 record->dwTtl = rr.ttl;
455 if ((ret = dns_copy_rdata( msg, &rr, record, &dlen )))
457 heap_free( record->pName );
458 heap_free( record );
459 return ret;
461 record->wDataLength = dlen;
462 *recp = record;
464 TRACE( "found %s record in %s section\n",
465 dns_type_to_str( rr.type ), dns_section_to_str( section ) );
466 return ERROR_SUCCESS;
469 #define DEFAULT_TTL 1200
471 static DNS_STATUS dns_do_query_netbios( PCSTR name, DNS_RECORDA **recp )
473 NCB ncb;
474 UCHAR ret;
475 DNS_RRSET rrset;
476 FIND_NAME_BUFFER *buffer;
477 FIND_NAME_HEADER *header;
478 DNS_RECORDA *record = NULL;
479 unsigned int i, len;
480 DNS_STATUS status = ERROR_INVALID_NAME;
482 len = strlen( name );
483 if (len >= NCBNAMSZ) return DNS_ERROR_RCODE_NAME_ERROR;
485 DNS_RRSET_INIT( rrset );
487 memset( &ncb, 0, sizeof(ncb) );
488 ncb.ncb_command = NCBFINDNAME;
490 memset( ncb.ncb_callname, ' ', sizeof(ncb.ncb_callname) );
491 memcpy( ncb.ncb_callname, name, len );
492 ncb.ncb_callname[NCBNAMSZ - 1] = '\0';
494 ret = Netbios( &ncb );
495 if (ret != NRC_GOODRET) return ERROR_INVALID_NAME;
497 header = (FIND_NAME_HEADER *)ncb.ncb_buffer;
498 buffer = (FIND_NAME_BUFFER *)((char *)header + sizeof(FIND_NAME_HEADER));
500 for (i = 0; i < header->node_count; i++)
502 record = heap_alloc_zero( sizeof(DNS_RECORDA) );
503 if (!record)
505 status = ERROR_NOT_ENOUGH_MEMORY;
506 goto exit;
508 else
510 record->pName = dns_strdup_u( name );
511 if (!record->pName)
513 status = ERROR_NOT_ENOUGH_MEMORY;
514 goto exit;
517 record->wType = DNS_TYPE_A;
518 record->Flags.S.Section = DnsSectionAnswer;
519 record->Flags.S.CharSet = DnsCharSetUtf8;
520 record->dwTtl = DEFAULT_TTL;
522 /* FIXME: network byte order? */
523 record->Data.A.IpAddress = *(DWORD *)((char *)buffer[i].destination_addr + 2);
525 DNS_RRSET_ADD( rrset, (DNS_RECORD *)record );
528 status = ERROR_SUCCESS;
530 exit:
531 DNS_RRSET_TERMINATE( rrset );
533 if (status != ERROR_SUCCESS)
534 DnsRecordListFree( rrset.pFirstRR, DnsFreeRecordList );
535 else
536 *recp = (DNS_RECORDA *)rrset.pFirstRR;
538 return status;
541 /* res_init() must have been called before calling these three functions.
543 static DNS_STATUS dns_set_serverlist( const IP4_ARRAY *addrs )
545 int i;
547 if (addrs->AddrCount > MAXNS)
549 WARN( "too many servers: %d only using the first: %d\n",
550 addrs->AddrCount, MAXNS );
551 _res.nscount = MAXNS;
553 else _res.nscount = addrs->AddrCount;
555 for (i = 0; i < _res.nscount; i++)
556 _res.nsaddr_list[i].sin_addr.s_addr = addrs->AddrArray[i];
558 return ERROR_SUCCESS;
561 static DNS_STATUS dns_get_serverlist( PIP4_ARRAY addrs, PDWORD len )
563 unsigned int size;
564 int i;
566 size = FIELD_OFFSET(IP4_ARRAY, AddrArray[_res.nscount]);
567 if (!addrs || *len < size)
569 *len = size;
570 return ERROR_INSUFFICIENT_BUFFER;
573 addrs->AddrCount = _res.nscount;
575 for (i = 0; i < _res.nscount; i++)
576 addrs->AddrArray[i] = _res.nsaddr_list[i].sin_addr.s_addr;
578 return ERROR_SUCCESS;
581 #define DNS_MAX_PACKET_SIZE 4096
582 static DNS_STATUS dns_do_query( PCSTR name, WORD type, DWORD options, PDNS_RECORDA *result )
584 DNS_STATUS ret = DNS_ERROR_RCODE_NOT_IMPLEMENTED;
586 unsigned int i, num;
587 unsigned char answer[DNS_MAX_PACKET_SIZE];
588 ns_sect sections[] = { ns_s_an, ns_s_ar };
589 ns_msg msg;
591 DNS_RECORDA *record = NULL;
592 DNS_RRSET rrset;
593 int len;
595 DNS_RRSET_INIT( rrset );
597 len = res_query( name, ns_c_in, type, answer, sizeof(answer) );
598 if (len < 0)
600 ret = dns_map_h_errno( h_errno );
601 goto exit;
604 if (dns_ns_initparse( answer, len, &msg ) < 0)
606 ret = DNS_ERROR_BAD_PACKET;
607 goto exit;
610 #define RCODE_MASK 0x0f
611 if ((msg._flags & RCODE_MASK) != ns_r_noerror)
613 ret = dns_map_error( msg._flags & RCODE_MASK );
614 goto exit;
617 for (i = 0; i < sizeof(sections)/sizeof(sections[0]); i++)
619 for (num = 0; num < ns_msg_count( msg, sections[i] ); num++)
621 ret = dns_copy_record( msg, sections[i], num, &record );
622 if (ret != ERROR_SUCCESS) goto exit;
624 DNS_RRSET_ADD( rrset, (DNS_RECORD *)record );
628 exit:
629 DNS_RRSET_TERMINATE( rrset );
631 if (ret != ERROR_SUCCESS)
632 DnsRecordListFree( rrset.pFirstRR, DnsFreeRecordList );
633 else
634 *result = (DNS_RECORDA *)rrset.pFirstRR;
636 return ret;
639 #endif /* HAVE_RESOLV */
641 /******************************************************************************
642 * DnsQuery_A [DNSAPI.@]
645 DNS_STATUS WINAPI DnsQuery_A( PCSTR name, WORD type, DWORD options, PVOID servers,
646 PDNS_RECORDA *result, PVOID *reserved )
648 WCHAR *nameW;
649 DNS_RECORDW *resultW;
650 DNS_STATUS status;
652 TRACE( "(%s,%s,0x%08x,%p,%p,%p)\n", debugstr_a(name), dns_type_to_str( type ),
653 options, servers, result, reserved );
655 if (!name || !result)
656 return ERROR_INVALID_PARAMETER;
658 nameW = dns_strdup_aw( name );
659 if (!nameW) return ERROR_NOT_ENOUGH_MEMORY;
661 status = DnsQuery_W( nameW, type, options, servers, &resultW, reserved );
663 if (status == ERROR_SUCCESS)
665 *result = (DNS_RECORDA *)DnsRecordSetCopyEx(
666 (DNS_RECORD *)resultW, DnsCharSetUnicode, DnsCharSetAnsi );
668 if (!*result) status = ERROR_NOT_ENOUGH_MEMORY;
669 DnsRecordListFree( (DNS_RECORD *)resultW, DnsFreeRecordList );
672 heap_free( nameW );
673 return status;
676 /******************************************************************************
677 * DnsQuery_UTF8 [DNSAPI.@]
680 DNS_STATUS WINAPI DnsQuery_UTF8( PCSTR name, WORD type, DWORD options, PVOID servers,
681 PDNS_RECORDA *result, PVOID *reserved )
683 DNS_STATUS ret = DNS_ERROR_RCODE_NOT_IMPLEMENTED;
684 #ifdef HAVE_RESOLV
686 TRACE( "(%s,%s,0x%08x,%p,%p,%p)\n", debugstr_a(name), dns_type_to_str( type ),
687 options, servers, result, reserved );
689 if (!name || !result)
690 return ERROR_INVALID_PARAMETER;
692 initialise_resolver();
693 _res.options |= dns_map_options( options );
695 if (servers && (ret = dns_set_serverlist( servers )))
696 return ret;
698 ret = dns_do_query( name, type, options, result );
700 if (ret == DNS_ERROR_RCODE_NAME_ERROR && type == DNS_TYPE_A &&
701 !(options & DNS_QUERY_NO_NETBT))
703 TRACE( "dns lookup failed, trying netbios query\n" );
704 ret = dns_do_query_netbios( name, result );
707 #endif
708 return ret;
711 /******************************************************************************
712 * DnsQuery_W [DNSAPI.@]
715 DNS_STATUS WINAPI DnsQuery_W( PCWSTR name, WORD type, DWORD options, PVOID servers,
716 PDNS_RECORDW *result, PVOID *reserved )
718 char *nameU;
719 DNS_RECORDA *resultA;
720 DNS_STATUS status;
722 TRACE( "(%s,%s,0x%08x,%p,%p,%p)\n", debugstr_w(name), dns_type_to_str( type ),
723 options, servers, result, reserved );
725 if (!name || !result)
726 return ERROR_INVALID_PARAMETER;
728 nameU = dns_strdup_wu( name );
729 if (!nameU) return ERROR_NOT_ENOUGH_MEMORY;
731 status = DnsQuery_UTF8( nameU, type, options, servers, &resultA, reserved );
733 if (status == ERROR_SUCCESS)
735 *result = (DNS_RECORDW *)DnsRecordSetCopyEx(
736 (DNS_RECORD *)resultA, DnsCharSetUtf8, DnsCharSetUnicode );
738 if (!*result) status = ERROR_NOT_ENOUGH_MEMORY;
739 DnsRecordListFree( (DNS_RECORD *)resultA, DnsFreeRecordList );
742 heap_free( nameU );
743 return status;
746 static DNS_STATUS dns_get_hostname_a( COMPUTER_NAME_FORMAT format,
747 PSTR buffer, PDWORD len )
749 char name[256];
750 DWORD size = sizeof(name)/sizeof(name[0]);
752 if (!GetComputerNameExA( format, name, &size ))
753 return DNS_ERROR_NAME_DOES_NOT_EXIST;
755 if (!buffer || (size = lstrlenA( name ) + 1) > *len)
757 *len = size;
758 return ERROR_INSUFFICIENT_BUFFER;
761 lstrcpyA( buffer, name );
762 return ERROR_SUCCESS;
765 static DNS_STATUS dns_get_hostname_w( COMPUTER_NAME_FORMAT format,
766 PWSTR buffer, PDWORD len )
768 WCHAR name[256];
769 DWORD size = sizeof(name)/sizeof(name[0]);
771 if (!GetComputerNameExW( format, name, &size ))
772 return DNS_ERROR_NAME_DOES_NOT_EXIST;
774 if (!buffer || (size = lstrlenW( name ) + 1) > *len)
776 *len = size;
777 return ERROR_INSUFFICIENT_BUFFER;
780 lstrcpyW( buffer, name );
781 return ERROR_SUCCESS;
784 /******************************************************************************
785 * DnsQueryConfig [DNSAPI.@]
788 DNS_STATUS WINAPI DnsQueryConfig( DNS_CONFIG_TYPE config, DWORD flag, PCWSTR adapter,
789 PVOID reserved, PVOID buffer, PDWORD len )
791 DNS_STATUS ret = ERROR_INVALID_PARAMETER;
793 TRACE( "(%d,0x%08x,%s,%p,%p,%p)\n", config, flag, debugstr_w(adapter),
794 reserved, buffer, len );
796 if (!len) return ERROR_INVALID_PARAMETER;
798 switch (config)
800 case DnsConfigDnsServerList:
802 #ifdef HAVE_RESOLV
803 initialise_resolver();
804 ret = dns_get_serverlist( buffer, len );
805 break;
806 #else
807 WARN( "compiled without resolver support\n" );
808 break;
809 #endif
811 case DnsConfigHostName_A:
812 case DnsConfigHostName_UTF8:
813 return dns_get_hostname_a( ComputerNameDnsHostname, buffer, len );
815 case DnsConfigFullHostName_A:
816 case DnsConfigFullHostName_UTF8:
817 return dns_get_hostname_a( ComputerNameDnsFullyQualified, buffer, len );
819 case DnsConfigPrimaryDomainName_A:
820 case DnsConfigPrimaryDomainName_UTF8:
821 return dns_get_hostname_a( ComputerNameDnsDomain, buffer, len );
823 case DnsConfigHostName_W:
824 return dns_get_hostname_w( ComputerNameDnsHostname, buffer, len );
826 case DnsConfigFullHostName_W:
827 return dns_get_hostname_w( ComputerNameDnsFullyQualified, buffer, len );
829 case DnsConfigPrimaryDomainName_W:
830 return dns_get_hostname_w( ComputerNameDnsDomain, buffer, len );
832 case DnsConfigAdapterDomainName_A:
833 case DnsConfigAdapterDomainName_W:
834 case DnsConfigAdapterDomainName_UTF8:
835 case DnsConfigSearchList:
836 case DnsConfigAdapterInfo:
837 case DnsConfigPrimaryHostNameRegistrationEnabled:
838 case DnsConfigAdapterHostNameRegistrationEnabled:
839 case DnsConfigAddressRegistrationMaxCount:
840 FIXME( "unimplemented config type %d\n", config );
841 break;
843 default:
844 WARN( "unknown config type: %d\n", config );
845 break;
847 return ret;