-
Notifications
You must be signed in to change notification settings - Fork 166
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Retrieve DNS server address error in win32 platform #176
Comments
Thanks. |
But the first ip is not necessarily the best nameserver, 蛋疼 |
Thanks. |
On linux the default behaviour is to to try the second (and the third, see |
Sorry for my poor English. Following is code of function static int ns_get_ip_address_of_nameserver(char *name, size_t name_len) {
int ret = 0;
#ifdef _WIN32
int i;
LONG err;
HKEY hKey, hSub;
char subkey[512], dhcpns[512], ns[512], value[128], *key =
"SYSTEM\\ControlSet001\\Services\\Tcpip\\Parameters\\Interfaces";
if ((err = RegOpenKey(HKEY_LOCAL_MACHINE,
key, &hKey)) != ERROR_SUCCESS) {
fprintf(stderr, "cannot open reg key %s: %d\n", key, err);
ret--;
} else {
for (ret--, i = 0; RegEnumKey(hKey, i, subkey,
sizeof(subkey)) == ERROR_SUCCESS; i++) {
DWORD type, len = sizeof(value);
if (RegOpenKey(hKey, subkey, &hSub) == ERROR_SUCCESS &&
(RegQueryValueEx(hSub, "NameServer", 0,
&type, value, &len) == ERROR_SUCCESS ||
RegQueryValueEx(hSub, "DhcpNameServer", 0,
&type, value, &len) == ERROR_SUCCESS)) {
strncpy(name, value, name_len);
ret++;
RegCloseKey(hSub);
break;
}
}
RegCloseKey(hKey);
}
#else
/* linux version */
#endif /* _WIN32 */
return ret;
} looking at line that's the point. |
1) If the registry value is empty - check the next interface. 2) Return proper address from ns_get_ip_address_of_nameserver, not just the IP (confusing naming is confusing). While I'm here, remove ret--++ madness and make it obviously return exactly what the documentation says.
When testing
example/http_client
,type command
./http_client http://www.microsoft.com/
got right response in Linux(Ubuntu 14.04) but nothing printed in Windows platform(Windows 7 x64).With some efforts, I found function
ns_get_ip_address_of_nameserver()
try to retrieve item under registryHKLM\\SYSTEM\\ControlSet001\\Services\\Tcpip\\Parameters\\Interfaces\
, and copy first item'sNameServer
field as the address of DNS server.But when got too many interface items(as I shown below) and the NameServer field is an empty string, or the NameServer string contains more than one IP address(seperated by comma token), we cannot retrieve the right DNS server address we want.
The text was updated successfully, but these errors were encountered: