No empty .Rs/.Re
[netbsd-mini2440.git] / external / bsd / bind / dist / lib / lwres / win32 / lwconfig.c
blob7c7bf2a0cd9f61f791b07232d610e373ceda78e7
1 /* $NetBSD$ */
3 /*
4 * Copyright (C) 2004, 2006, 2007 Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 2002 Internet Software Consortium.
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
20 /* Id: lwconfig.c,v 1.7 2007/12/14 01:40:42 marka Exp */
23 * We do this so that we may incorporate everything in the main routines
24 * so that we can take advantage of the fixes and changes made there
25 * without having to add them twice. We can then call the parse routine
26 * if there is a resolv.conf file and fetch our own data from the
27 * Windows environment otherwise.
31 * Note that on Win32 there is normally no resolv.conf since all information
32 * is stored in the registry. Therefore there is no ordering like the
33 * contents of resolv.conf. Since the "search" or "domain" keyword, on
34 * Win32 if a search list is found it is used, otherwise the domain name
35 * is used since they are mutually exclusive. The search list can be entered
36 * in the DNS tab of the "Advanced TCP/IP settings" window under the same place
37 * that you add your nameserver list.
40 #define lwres_conf_parse generic_lwres_conf_parse
41 #include "../lwconfig.c"
42 #undef lwres_conf_parse
44 #include <iphlpapi.h>
46 #define TCPIP_SUBKEY \
47 "SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters"
49 void
50 get_win32_searchlist(lwres_context_t *ctx) {
51 HKEY hKey;
52 BOOL keyFound = TRUE;
53 char searchlist[MAX_PATH];
54 DWORD searchlen = MAX_PATH;
55 char *cp;
56 lwres_conf_t *confdata;
58 REQUIRE(ctx != NULL);
59 confdata = &ctx->confdata;
61 memset(searchlist, 0, MAX_PATH);
62 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, TCPIP_SUBKEY, 0, KEY_READ, &hKey)
63 != ERROR_SUCCESS)
64 keyFound = FALSE;
66 if (keyFound == TRUE) {
67 /* Get the named directory */
68 if (RegQueryValueEx(hKey, "SearchList", NULL, NULL,
69 (LPBYTE)searchlist, &searchlen) != ERROR_SUCCESS)
70 keyFound = FALSE;
71 RegCloseKey(hKey);
74 confdata->searchnxt = 0;
75 cp = strtok((char *)searchlist, ", \0");
76 while (cp != NULL) {
77 if (confdata->searchnxt == LWRES_CONFMAXSEARCH)
78 break;
79 if (strlen(cp) <= MAX_PATH && strlen(cp) > 0) {
80 confdata->search[confdata->searchnxt] = lwres_strdup(ctx, cp);
81 if (confdata->search[confdata->searchnxt] != NULL)
82 confdata->searchnxt++;
84 cp = strtok(NULL, ", \0");
88 lwres_result_t
89 lwres_conf_parse(lwres_context_t *ctx, const char *filename) {
90 lwres_result_t ret = LWRES_R_SUCCESS;
91 lwres_result_t res;
92 lwres_conf_t *confdata;
93 FIXED_INFO * FixedInfo;
94 ULONG BufLen = sizeof(FIXED_INFO);
95 DWORD dwRetVal;
96 IP_ADDR_STRING *pIPAddr;
98 REQUIRE(ctx != NULL);
99 confdata = &ctx->confdata;
100 REQUIRE(confdata != NULL);
102 /* Use the resolver if there is one */
103 ret = generic_lwres_conf_parse(ctx, filename);
104 if ((ret != LWRES_R_NOTFOUND && ret != LWRES_R_SUCCESS) ||
105 (ret == LWRES_R_SUCCESS && confdata->nsnext > 0))
106 return (ret);
109 * We didn't get any nameservers so we need to do this ourselves
111 FixedInfo = (FIXED_INFO *) GlobalAlloc(GPTR, BufLen);
112 dwRetVal = GetNetworkParams(FixedInfo, &BufLen);
113 if (dwRetVal == ERROR_BUFFER_OVERFLOW) {
114 GlobalFree(FixedInfo);
115 FixedInfo = GlobalAlloc(GPTR, BufLen);
116 dwRetVal = GetNetworkParams(FixedInfo, &BufLen);
118 if (dwRetVal != ERROR_SUCCESS) {
119 GlobalFree(FixedInfo);
120 return (LWRES_R_FAILURE);
123 /* Get the search list from the registry */
124 get_win32_searchlist(ctx);
126 /* Use only if there is no search list */
127 if (confdata->searchnxt == 0 && strlen(FixedInfo->DomainName) > 0) {
128 confdata->domainname = lwres_strdup(ctx, FixedInfo->DomainName);
129 if (confdata->domainname == NULL) {
130 GlobalFree(FixedInfo);
131 return (LWRES_R_FAILURE);
133 } else
134 confdata->domainname = NULL;
136 /* Get the list of nameservers */
137 pIPAddr = &FixedInfo->DnsServerList;
138 while (pIPAddr) {
139 if (confdata->nsnext >= LWRES_CONFMAXNAMESERVERS)
140 break;
142 res = lwres_create_addr(pIPAddr->IpAddress.String,
143 &confdata->nameservers[confdata->nsnext++], 1);
144 if (res != LWRES_R_SUCCESS) {
145 GlobalFree(FixedInfo);
146 return (res);
148 pIPAddr = pIPAddr ->Next;
151 GlobalFree(FixedInfo);
152 return (LWRES_R_SUCCESS);