2 * Copyright (C) 2004, 2006, 2007 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 2002 Internet Software Consortium.
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 * PERFORMANCE OF THIS SOFTWARE.
18 /* $Id: lwconfig.c,v 1.7 2007/12/14 01:40:42 marka Exp $ */
21 * We do this so that we may incorporate everything in the main routines
22 * so that we can take advantage of the fixes and changes made there
23 * without having to add them twice. We can then call the parse routine
24 * if there is a resolv.conf file and fetch our own data from the
25 * Windows environment otherwise.
29 * Note that on Win32 there is normally no resolv.conf since all information
30 * is stored in the registry. Therefore there is no ordering like the
31 * contents of resolv.conf. Since the "search" or "domain" keyword, on
32 * Win32 if a search list is found it is used, otherwise the domain name
33 * is used since they are mutually exclusive. The search list can be entered
34 * in the DNS tab of the "Advanced TCP/IP settings" window under the same place
35 * that you add your nameserver list.
38 #define lwres_conf_parse generic_lwres_conf_parse
39 #include "../lwconfig.c"
40 #undef lwres_conf_parse
44 #define TCPIP_SUBKEY \
45 "SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters"
48 get_win32_searchlist(lwres_context_t *ctx) {
51 char searchlist[MAX_PATH];
52 DWORD searchlen = MAX_PATH;
54 lwres_conf_t *confdata;
57 confdata = &ctx->confdata;
59 memset(searchlist, 0, MAX_PATH);
60 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, TCPIP_SUBKEY, 0, KEY_READ, &hKey)
64 if (keyFound == TRUE) {
65 /* Get the named directory */
66 if (RegQueryValueEx(hKey, "SearchList", NULL, NULL,
67 (LPBYTE)searchlist, &searchlen) != ERROR_SUCCESS)
72 confdata->searchnxt = 0;
73 cp = strtok((char *)searchlist, ", \0");
75 if (confdata->searchnxt == LWRES_CONFMAXSEARCH)
77 if (strlen(cp) <= MAX_PATH && strlen(cp) > 0) {
78 confdata->search[confdata->searchnxt] = lwres_strdup(ctx, cp);
79 if (confdata->search[confdata->searchnxt] != NULL)
80 confdata->searchnxt++;
82 cp = strtok(NULL, ", \0");
87 lwres_conf_parse(lwres_context_t *ctx, const char *filename) {
88 lwres_result_t ret = LWRES_R_SUCCESS;
90 lwres_conf_t *confdata;
91 FIXED_INFO * FixedInfo;
92 ULONG BufLen = sizeof(FIXED_INFO);
94 IP_ADDR_STRING *pIPAddr;
97 confdata = &ctx->confdata;
98 REQUIRE(confdata != NULL);
100 /* Use the resolver if there is one */
101 ret = generic_lwres_conf_parse(ctx, filename);
102 if ((ret != LWRES_R_NOTFOUND && ret != LWRES_R_SUCCESS) ||
103 (ret == LWRES_R_SUCCESS && confdata->nsnext > 0))
107 * We didn't get any nameservers so we need to do this ourselves
109 FixedInfo = (FIXED_INFO *) GlobalAlloc(GPTR, BufLen);
110 dwRetVal = GetNetworkParams(FixedInfo, &BufLen);
111 if (dwRetVal == ERROR_BUFFER_OVERFLOW) {
112 GlobalFree(FixedInfo);
113 FixedInfo = GlobalAlloc(GPTR, BufLen);
114 dwRetVal = GetNetworkParams(FixedInfo, &BufLen);
116 if (dwRetVal != ERROR_SUCCESS) {
117 GlobalFree(FixedInfo);
118 return (LWRES_R_FAILURE);
121 /* Get the search list from the registry */
122 get_win32_searchlist(ctx);
124 /* Use only if there is no search list */
125 if (confdata->searchnxt == 0 && strlen(FixedInfo->DomainName) > 0) {
126 confdata->domainname = lwres_strdup(ctx, FixedInfo->DomainName);
127 if (confdata->domainname == NULL) {
128 GlobalFree(FixedInfo);
129 return (LWRES_R_FAILURE);
132 confdata->domainname = NULL;
134 /* Get the list of nameservers */
135 pIPAddr = &FixedInfo->DnsServerList;
137 if (confdata->nsnext >= LWRES_CONFMAXNAMESERVERS)
140 res = lwres_create_addr(pIPAddr->IpAddress.String,
141 &confdata->nameservers[confdata->nsnext++], 1);
142 if (res != LWRES_R_SUCCESS) {
143 GlobalFree(FixedInfo);
146 pIPAddr = pIPAddr ->Next;
149 GlobalFree(FixedInfo);
150 return (LWRES_R_SUCCESS);