From e5315890fdd3aa1189a78810d06f6feba9e374c8 Mon Sep 17 00:00:00 2001 From: Daniel Collins Date: Sat, 25 Jan 2014 23:33:11 +0000 Subject: [PATCH] Use GetTickCount64() for connect timeouts where available. --- src/ipxwrapper.c | 29 ++++++++++++++++++++++++++++- src/ipxwrapper.h | 3 ++- src/winsock.c | 6 ++---- 3 files changed, 32 insertions(+), 6 deletions(-) diff --git a/src/ipxwrapper.c b/src/ipxwrapper.c index 7abbf36..04636e2 100644 --- a/src/ipxwrapper.c +++ b/src/ipxwrapper.c @@ -1,5 +1,5 @@ /* ipxwrapper - Library functions - * Copyright (C) 2008-2013 Daniel Collins + * Copyright (C) 2008-2014 Daniel Collins * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 as published by @@ -48,6 +48,9 @@ main_config_t main_config; static CRITICAL_SECTION sockets_cs; +typedef ULONGLONG WINAPI (*GetTickCount64_t)(void); +static HMODULE kernel32 = NULL; + static void init_cs(CRITICAL_SECTION *cs) { if(!InitializeCriticalSectionAndSpinCount(cs, 0x80000000)) @@ -121,6 +124,12 @@ BOOL WINAPI DllMain(HINSTANCE me, DWORD why, LPVOID res) unload_dlls(); log_close(); + + if(kernel32) + { + FreeLibrary(kernel32); + kernel32 = NULL; + } } return TRUE; @@ -157,3 +166,21 @@ void unlock_sockets(void) { LeaveCriticalSection(&sockets_cs); } + +uint64_t get_ticks(void) +{ + static GetTickCount64_t GetTickCount64 = NULL; + + if(!kernel32 && (kernel32 = LoadLibrary("kernel32.dll"))) + { + GetTickCount64 = (GetTickCount64_t)(GetProcAddress(kernel32, "GetTickCount64")); + } + + if(GetTickCount64) + { + return GetTickCount64(); + } + else{ + return GetTickCount(); + } +} diff --git a/src/ipxwrapper.h b/src/ipxwrapper.h index 05fa88e..cbf8767 100644 --- a/src/ipxwrapper.h +++ b/src/ipxwrapper.h @@ -1,5 +1,5 @@ /* ipxwrapper - Library header - * Copyright (C) 2008-2013 Daniel Collins + * Copyright (C) 2008-2014 Daniel Collins * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 as published by @@ -131,6 +131,7 @@ extern main_config_t main_config; ipx_socket *get_socket(SOCKET sockfd); void lock_sockets(void); void unlock_sockets(void); +uint64_t get_ticks(void); void add_self_to_firewall(void); diff --git a/src/winsock.c b/src/winsock.c index 33369a4..1b7e247 100644 --- a/src/winsock.c +++ b/src/winsock.c @@ -1635,13 +1635,11 @@ static int _connect_spx(ipx_socket *sock, struct sockaddr_ipx *ipxaddr) * * BUG: Batch may time out or wait (effectively) forever if the * batch is sent just before the system tick count rolls over. - * - * TODO: Use GetTickCount64() if available. */ - DWORD wait_until = GetTickCount() + (IPX_CONNECT_TIMEOUT / IPX_CONNECT_TRIES) * 1000; + uint64_t wait_until = get_ticks() + (IPX_CONNECT_TIMEOUT / IPX_CONNECT_TRIES) * 1000; - for(DWORD now; (now = GetTickCount()) < wait_until;) + for(uint64_t now; (now = get_ticks()) < wait_until;) { /* Release the socket table in case the remote address * in question is in the same process and we block the