Release 0.9.14.
[wine/multimedia.git] / dlls / dnsapi / query.c
blobbb75a56228e3131911cb1d2766028ca7f97a1914
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"
48 #include "dnsapi.h"
50 WINE_DEFAULT_DEBUG_CHANNEL(dnsapi);
52 #ifdef HAVE_RESOLV
54 static CRITICAL_SECTION resolver_cs;
55 static CRITICAL_SECTION_DEBUG resolver_cs_debug =
57 0, 0, &resolver_cs,
58 { &resolver_cs_debug.ProcessLocksList,
59 &resolver_cs_debug.ProcessLocksList },
60 0, 0, { (DWORD_PTR)(__FILE__ ": resolver_cs") }
62 static CRITICAL_SECTION resolver_cs = { &resolver_cs_debug, -1, 0, 0, 0, 0 };
64 #define LOCK_RESOLVER() do { EnterCriticalSection( &resolver_cs ); } while (0)
65 #define UNLOCK_RESOLVER() do { LeaveCriticalSection( &resolver_cs ); } while (0)
68 /* the resolver lock must be held and res_init() must have been
69 * called before calling this function.
71 static DNS_STATUS dns_get_serverlist( PIP4_ARRAY addrs, PDWORD len )
73 unsigned int i, size;
75 size = sizeof(IP4_ARRAY) + sizeof(IP4_ADDRESS) * (_res.nscount - 1);
76 if (!addrs || *len < size)
78 *len = size;
79 return ERROR_INSUFFICIENT_BUFFER;
82 addrs->AddrCount = _res.nscount;
84 for (i = 0; i < _res.nscount; i++)
85 addrs->AddrArray[i] = _res.nsaddr_list[i].sin_addr.s_addr;
87 return ERROR_SUCCESS;
90 #endif /* HAVE_RESOLV */
92 static DNS_STATUS dns_get_hostname_a( COMPUTER_NAME_FORMAT format,
93 LPSTR buffer, PDWORD len )
95 char name[256];
96 DWORD size = sizeof(name);
98 if (!GetComputerNameExA( format, name, &size ))
99 return DNS_ERROR_NAME_DOES_NOT_EXIST;
101 if (!buffer || (size = lstrlenA( name ) + 1) > *len)
103 *len = size;
104 return ERROR_INSUFFICIENT_BUFFER;
107 lstrcpyA( buffer, name );
108 return ERROR_SUCCESS;
111 static DNS_STATUS dns_get_hostname_w( COMPUTER_NAME_FORMAT format,
112 LPWSTR buffer, PDWORD len )
114 WCHAR name[256];
115 DWORD size = sizeof(name);
117 if (!GetComputerNameExW( format, name, &size ))
118 return DNS_ERROR_NAME_DOES_NOT_EXIST;
120 if (!buffer || (size = lstrlenW( name ) + 1) > *len)
122 *len = size;
123 return ERROR_INSUFFICIENT_BUFFER;
126 lstrcpyW( buffer, name );
127 return ERROR_SUCCESS;
130 /******************************************************************************
131 * DnsQueryConfig [DNSAPI.@]
134 DNS_STATUS WINAPI DnsQueryConfig( DNS_CONFIG_TYPE config, DWORD flag, PWSTR adapter,
135 PVOID reserved, PVOID buffer, PDWORD len )
137 DNS_STATUS ret = ERROR_INVALID_PARAMETER;
139 TRACE( "(%d,0x%08lx,%s,%p,%p,%p)\n", config, flag, debugstr_w(adapter),
140 reserved, buffer, len );
142 if (!len) return ERROR_INVALID_PARAMETER;
144 switch (config)
146 case DnsConfigDnsServerList:
148 #ifdef HAVE_RESOLV
149 LOCK_RESOLVER();
151 res_init();
152 ret = dns_get_serverlist( (IP4_ARRAY *)buffer, len );
154 UNLOCK_RESOLVER();
155 break;
156 #else
157 WARN( "compiled without resolver support\n" );
158 break;
159 #endif
161 case DnsConfigHostName_A:
162 case DnsConfigHostName_UTF8:
163 return dns_get_hostname_a( ComputerNameDnsHostname, buffer, len );
165 case DnsConfigFullHostName_A:
166 case DnsConfigFullHostName_UTF8:
167 return dns_get_hostname_a( ComputerNameDnsFullyQualified, buffer, len );
169 case DnsConfigPrimaryDomainName_A:
170 case DnsConfigPrimaryDomainName_UTF8:
171 return dns_get_hostname_a( ComputerNameDnsDomain, buffer, len );
173 case DnsConfigHostName_W:
174 return dns_get_hostname_w( ComputerNameDnsHostname, buffer, len );
176 case DnsConfigFullHostName_W:
177 return dns_get_hostname_w( ComputerNameDnsFullyQualified, buffer, len );
179 case DnsConfigPrimaryDomainName_W:
180 return dns_get_hostname_w( ComputerNameDnsDomain, buffer, len );
182 case DnsConfigAdapterDomainName_A:
183 case DnsConfigAdapterDomainName_W:
184 case DnsConfigAdapterDomainName_UTF8:
185 case DnsConfigSearchList:
186 case DnsConfigAdapterInfo:
187 case DnsConfigPrimaryHostNameRegistrationEnabled:
188 case DnsConfigAdapterHostNameRegistrationEnabled:
189 case DnsConfigAddressRegistrationMaxCount:
190 FIXME( "unimplemented config type %d\n", config );
191 break;
193 default:
194 WARN( "unknown config type: %d\n", config );
195 break;
197 return ret;