From bd066ab8efa0d43ef002954f4587a195a15ac460 Mon Sep 17 00:00:00 2001 From: Yang Tse Date: Wed, 23 Mar 2011 20:53:28 +0100 Subject: [PATCH] build: find out windows platform using GetVersionEx() --- ares_gethostbyaddr.c | 12 ++++++++++-- ares_gethostbyname.c | 12 ++++++++++-- ares_init.c | 7 +++++-- ares_private.h | 15 ++++++++++++++- windows_port.c | 39 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 78 insertions(+), 7 deletions(-) diff --git a/ares_gethostbyaddr.c b/ares_gethostbyaddr.c index 0de2cf2..2945c4b 100644 --- a/ares_gethostbyaddr.c +++ b/ares_gethostbyaddr.c @@ -186,7 +186,13 @@ static int file_lookup(struct ares_addr *addr, struct hostent **host) #ifdef WIN32 char PATH_HOSTS[MAX_PATH]; - if (IS_NT()) { + win_platform platform; + + PATH_HOSTS[0] = '\0'; + + platform = getplatform(); + + if (platform == WIN_NT) { char tmp[MAX_PATH]; HKEY hkeyHosts; @@ -200,8 +206,10 @@ static int file_lookup(struct ares_addr *addr, struct hostent **host) RegCloseKey(hkeyHosts); } } - else + else if (platform == WIN_9X) GetWindowsDirectory(PATH_HOSTS, MAX_PATH); + else + return ARES_ENOTFOUND; strcat(PATH_HOSTS, WIN_PATH_HOSTS); diff --git a/ares_gethostbyname.c b/ares_gethostbyname.c index 4469ffe..6599c08 100644 --- a/ares_gethostbyname.c +++ b/ares_gethostbyname.c @@ -344,7 +344,13 @@ static int file_lookup(const char *name, int family, struct hostent **host) #ifdef WIN32 char PATH_HOSTS[MAX_PATH]; - if (IS_NT()) { + win_platform platform; + + PATH_HOSTS[0] = '\0'; + + platform = getplatform(); + + if (platform == WIN_NT) { char tmp[MAX_PATH]; HKEY hkeyHosts; @@ -358,8 +364,10 @@ static int file_lookup(const char *name, int family, struct hostent **host) RegCloseKey(hkeyHosts); } } - else + else if (platform == WIN_9X) GetWindowsDirectory(PATH_HOSTS, MAX_PATH); + else + return ARES_ENOTFOUND; strcat(PATH_HOSTS, WIN_PATH_HOSTS); diff --git a/ares_init.c b/ares_init.c index 8aaaa2f..108238e 100644 --- a/ares_init.c +++ b/ares_init.c @@ -704,6 +704,7 @@ DhcpNameServer DWORD bytes; DWORD result; char buf[256]; + win_platform platform; if (channel->nservers > -1) /* don't override ARES_OPT_SERVER */ return ARES_SUCCESS; @@ -715,7 +716,9 @@ DhcpNameServer goto okay; } - if (IS_NT()) + platform = getplatform(); + + if (platform == WIN_NT) { if (RegOpenKeyEx( HKEY_LOCAL_MACHINE, WIN_NS_NT_KEY, 0, @@ -749,7 +752,7 @@ DhcpNameServer RegCloseKey(mykey); } } - else + else if (platform == WIN_9X) { if (RegOpenKeyEx( HKEY_LOCAL_MACHINE, WIN_NS_9X, 0, diff --git a/ares_private.h b/ares_private.h index 01a79b6..78f3a1c 100644 --- a/ares_private.h +++ b/ares_private.h @@ -52,7 +52,6 @@ #if defined(WIN32) && !defined(WATT32) -#define IS_NT() ((int)GetVersion() > 0) #define WIN_NS_9X "System\\CurrentControlSet\\Services\\VxD\\MSTCP" #define WIN_NS_NT_KEY "System\\CurrentControlSet\\Services\\Tcpip\\Parameters" #define NAMESERVER "NameServer" @@ -345,6 +344,20 @@ long ares__tvdiff(struct timeval t1, struct timeval t2); (c)->sock_state_cb((c)->sock_state_cb_data, (s), (r), (w)); \ } while (0) +#if (defined(WIN32) || defined(WATT32)) && !defined(MSDOS) + +typedef enum { + WIN_UNKNOWN, + WIN_3X, + WIN_9X, + WIN_NT, + WIN_CE +} win_platform; + +win_platform getplatform(void); + +#endif + #ifdef CURLDEBUG /* This is low-level hard-hacking memory leak tracking and similar. Using the libcurl lowlevel code from within library is ugly and only works when diff --git a/windows_port.c b/windows_port.c index 03acd1c..6a48f08 100644 --- a/windows_port.c +++ b/windows_port.c @@ -1,5 +1,6 @@ #include "ares_setup.h" +#include "ares_private.h" /* only do the following on windows */ @@ -19,4 +20,42 @@ WINAPI DllMain (HINSTANCE hnd, DWORD reason, LPVOID reserved) } #endif +#define V_PLATFORM_WIN32s 0 +#define V_PLATFORM_WIN32_WINDOWS 1 +#define V_PLATFORM_WIN32_NT 2 +#define V_PLATFORM_WIN32_CE 3 + +win_platform getplatform(void) +{ + OSVERSIONINFOEX OsvEx; + + memset(&OsvEx, 0, sizeof(OsvEx)); + OsvEx.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX); + if (!GetVersionEx((void *)&OsvEx)) + { + memset(&OsvEx, 0, sizeof(OsvEx)); + OsvEx.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); + if (!GetVersionEx((void *)&OsvEx)) + return WIN_UNKNOWN; + } + + switch(OsvEx.dwPlatformId) + { + case V_PLATFORM_WIN32s: + return WIN_3X; + + case V_PLATFORM_WIN32_WINDOWS: + return WIN_9X; + + case V_PLATFORM_WIN32_NT: + return WIN_NT; + + case V_PLATFORM_WIN32_CE: + return WIN_CE; + + default: + return WIN_UNKNOWN; + } +} + #endif /* WIN32 builds only */ -- 2.7.4