2 * reslist.c - get nameservers from windows *
4 * ircd-ratbox related changes are as follows
6 * Copyright (C) 2008 Aaron Sethman <androsyn@ratbox.org>
7 * Copyright (C) 2008-2012 ircd-ratbox development team
9 * pretty much all of this was yanked from c-ares ares_init.c here is the original
10 * header from there --
12 * Id: ares_init.c,v 1.72 2008-05-15 00:00:19 yangtse Exp $
13 * Copyright 1998 by the Massachusetts Institute of Technology.
14 * Copyright (C) 2007-2008 by Daniel Stenberg
16 * Permission to use, copy, modify, and distribute this
17 * software and its documentation for any purpose and without
18 * fee is hereby granted, provided that the above copyright
19 * notice appear in all copies and that both that copyright
20 * notice and this permission notice appear in supporting
21 * documentation, and that the name of M.I.T. not be used in
22 * advertising or publicity pertaining to distribution of the
23 * software without specific, written prior permission.
24 * M.I.T. makes no representations about the suitability of
25 * this software for any purpose. It is provided "as is"
26 * without express or implied warranty.
37 const char *get_windows_nameservers(void);
40 #define INADDR_NONE ((unsigned int) 0xffffffff)
41 #endif /* INADDR_NONE */
43 #define IS_NT() ((int)GetVersion() > 0)
44 #define WIN_NS_9X "System\\CurrentControlSet\\Services\\VxD\\MSTCP"
45 #define WIN_NS_NT_KEY "System\\CurrentControlSet\\Services\\Tcpip\\Parameters"
46 #define NAMESERVER "NameServer"
47 #define DHCPNAMESERVER "DhcpNameServer"
48 #define DATABASEPATH "DatabasePath"
49 #define WIN_PATH_HOSTS "\\hosts"
52 get_iphlpapi_dns_info(char *ret_buf
, size_t ret_size
)
54 FIXED_INFO
*fi
= alloca(sizeof(*fi
));
55 DWORD size
= sizeof(*fi
);
56 typedef DWORD(WINAPI
* get_net_param_func
) (FIXED_INFO
*, DWORD
*);
57 get_net_param_func xxGetNetworkParams
; /* available only on Win-98/2000+ */
59 IP_ADDR_STRING
*ipAddr
;
62 size_t ip_size
= sizeof("255.255.255.255,") - 1;
63 size_t left
= ret_size
;
70 handle
= LoadLibrary("iphlpapi.dll");
74 xxGetNetworkParams
= (get_net_param_func
) GetProcAddress(handle
, "GetNetworkParams");
75 if(!xxGetNetworkParams
)
78 res
= (*xxGetNetworkParams
) (fi
, &size
);
79 if((res
!= ERROR_BUFFER_OVERFLOW
) && (res
!= ERROR_SUCCESS
))
83 if(!fi
|| (*xxGetNetworkParams
) (fi
, &size
) != ERROR_SUCCESS
)
88 printf("Host Name: %s\n", fi
->HostName
);
89 printf("Domain Name: %s\n", fi
->DomainName
);
90 printf("DNS Servers:\n" " %s (primary)\n", fi
->DnsServerList
.IpAddress
.String
);
92 if(strlen(fi
->DnsServerList
.IpAddress
.String
) > 0 &&
93 inet_addr(fi
->DnsServerList
.IpAddress
.String
) != INADDR_NONE
&& left
> ip_size
)
95 ret
+= sprintf(ret
, "%s,", fi
->DnsServerList
.IpAddress
.String
);
96 left
-= ret
- ret_buf
;
100 for(i
= 0, ipAddr
= fi
->DnsServerList
.Next
; ipAddr
&& left
> ip_size
;
101 ipAddr
= ipAddr
->Next
, i
++)
103 if(inet_addr(ipAddr
->IpAddress
.String
) != INADDR_NONE
)
105 ret
+= sprintf(ret
, "%s,", ipAddr
->IpAddress
.String
);
106 left
-= ret
- ret_buf
;
110 printf(" %s (secondary %d)\n", ipAddr
->IpAddress
.String
, i
+ 1);
117 if(debug
&& left
<= ip_size
)
118 printf("Too many nameservers. Truncating to %d addressess", count
);
125 * Warning: returns a dynamically allocated buffer, the user MUST
126 * use free() / rb_free() if the function returns 1
129 get_res_nt(HKEY hKey
, const char *subkey
, char **obuf
)
131 /* Test for the size we need */
135 result
= RegQueryValueEx(hKey
, subkey
, 0, NULL
, NULL
, &size
);
136 if((result
!= ERROR_SUCCESS
&& result
!= ERROR_MORE_DATA
) || !size
)
138 *obuf
= rb_malloc(size
+ 1);
142 if(RegQueryValueEx(hKey
, subkey
, 0, NULL
, (LPBYTE
) * obuf
, &size
) != ERROR_SUCCESS
)
156 get_res_interfaces_nt(HKEY hKey
, const char *subkey
, char **obuf
)
158 char enumbuf
[39]; /* GUIDs are 38 chars + 1 for NULL */
159 DWORD enum_size
= 39;
163 while(RegEnumKeyEx(hKey
, idx
++, enumbuf
, &enum_size
, 0,
164 NULL
, NULL
, NULL
) != ERROR_NO_MORE_ITEMS
)
169 if(RegOpenKeyEx(hKey
, enumbuf
, 0, KEY_QUERY_VALUE
, &hVal
) != ERROR_SUCCESS
)
171 rc
= get_res_nt(hVal
, subkey
, obuf
);
180 get_windows_nameservers(void)
183 NameServer info via IPHLPAPI (IP helper API):
184 GetNetworkParams() should be the trusted source for this.
185 Available in Win-98/2000 and later. If that fail, fall-back to
186 registry information.
190 On Windows 9X, the DNS server can be found in:
191 HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\VxD\MSTCP\NameServer
193 On Windows NT/2000/XP/2003:
194 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\NameServer
196 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\DhcpNameServer
198 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\{AdapterID}\
201 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\{AdapterID}\
204 static char namelist
[512];
211 memset(&namelist
, 0, sizeof(namelist
));
212 if(get_iphlpapi_dns_info(namelist
, sizeof(namelist
)) > 0)
219 if(RegOpenKeyEx(HKEY_LOCAL_MACHINE
, WIN_NS_NT_KEY
, 0,
220 KEY_READ
, &mykey
) == ERROR_SUCCESS
)
222 RegOpenKeyEx(mykey
, "Interfaces", 0,
223 KEY_QUERY_VALUE
| KEY_ENUMERATE_SUB_KEYS
, &subkey
);
224 if(get_res_nt(mykey
, NAMESERVER
, &line
))
226 rb_strlcpy(namelist
, line
, sizeof(namelist
));
229 else if(get_res_nt(mykey
, DHCPNAMESERVER
, &line
))
231 rb_strlcpy(namelist
, line
, sizeof(namelist
));
234 /* Try the interfaces */
235 else if(get_res_interfaces_nt(subkey
, NAMESERVER
, &line
))
237 rb_strlcpy(namelist
, line
, sizeof(namelist
));
240 else if(get_res_interfaces_nt(subkey
, DHCPNAMESERVER
, &line
))
242 rb_strlcpy(namelist
, line
, sizeof(namelist
));
251 if(RegOpenKeyEx(HKEY_LOCAL_MACHINE
, WIN_NS_9X
, 0,
252 KEY_READ
, &mykey
) == ERROR_SUCCESS
)
254 if((result
= RegQueryValueEx(mykey
, NAMESERVER
, NULL
, &data_type
,
255 NULL
, &bytes
)) == ERROR_SUCCESS
||
256 result
== ERROR_MORE_DATA
)
260 line
= (char *)rb_malloc(bytes
+ 1);
261 if(RegQueryValueEx(mykey
, NAMESERVER
, NULL
, &data_type
,
262 (unsigned char *)line
, &bytes
) ==
265 rb_strlcpy(namelist
, line
, sizeof(namelist
));
273 if(strlen(namelist
) > 0)