From eeae9c2cddb8eff5becb0d3b1368837e4ec1532f Mon Sep 17 00:00:00 2001 From: Ralf Habacker Date: Wed, 7 Aug 2013 20:47:55 +0200 Subject: [PATCH] Fixed bug of unsupported GetExtendedTcpTable() table option on Windows XP SP2 and earlier. Bug: https://bugs.freedesktop.org/show_bug.cgi?id=66060 --- dbus/dbus-sysdeps-win.c | 84 +++++++++++++++++++++++++++++++++++++++++++++++ 1 Datei geändert, 84 Zeilen hinzugefügt(+) diff --git a/dbus/dbus-sysdeps-win.c b/dbus/dbus-sysdeps-win.c index 9df1c2d..b99e35f 100644 --- a/dbus/dbus-sysdeps-win.c +++ b/dbus/dbus-sysdeps-win.c @@ -104,6 +104,52 @@ _dbus_win_set_errno (int err) #endif } +static BOOL is_winxp_sp3_or_lower(); + +typedef struct _MIB_TCPROW_EX +{ + DWORD dwState; // MIB_TCP_STATE_* + DWORD dwLocalAddr; + DWORD dwLocalPort; + DWORD dwRemoteAddr; + DWORD dwRemotePort; + DWORD dwProcessId; +} MIB_TCPROW_EX, *PMIB_TCPROW_EX; + +typedef struct _MIB_TCPTABLE_EX +{ + DWORD dwNumEntries; + MIB_TCPROW_EX table[ANY_SIZE]; +} MIB_TCPTABLE_EX, *PMIB_TCPTABLE_EX; + +/* +DWORD +WINAPI +AllocateAndGetTcpExTableFromStack( + OUT PMIB_TCPTABLE_EX *pTcpTableEx, + IN BOOL bOrder, + IN HANDLE hAllocHeap, + IN DWORD dwAllocFlags, + IN DWORD dwProtocolVersion; // 2 - TCP, 23 - TCPv6 (size of *pTcpTableEx must be 56!) +); +*/ +typedef DWORD (WINAPI *ProcAllocateAndGetTcpExtTableFromStack)(PMIB_TCPTABLE_EX*,BOOL,HANDLE,DWORD,DWORD); +static ProcAllocateAndGetTcpExtTableFromStack lpfnAllocateAndGetTcpExTableFromStack = NULL; + +static BOOL load_ex_ip_helper_procedures(void) +{ + HMODULE hModule = LoadLibrary ("iphlpapi.dll"); + if (hModule == NULL) + return FALSE; + + // XP and later + lpfnAllocateAndGetTcpExTableFromStack = (ProcAllocateAndGetTcpExtTableFromStack)GetProcAddress (hModule, "AllocateAndGetTcpExTableFromStack"); + if (lpfnAllocateAndGetTcpExTableFromStack == NULL) + return FALSE; + + return TRUE; +} + /** * @brief return peer process id from tcp handle for localhost connections * @param handle tcp socket descriptor @@ -160,6 +206,44 @@ _dbus_get_peer_pid_from_tcp_handle (int handle) return 0; } + if (is_winxp_sp3_or_lower ()) + { + DWORD errorCode, dwSize; + PMIB_TCPTABLE_EX lpBuffer = NULL; + + if (!load_ex_ip_helper_procedures ()) + { + _dbus_verbose + ("Error not been able to load iphelper procedures\n"); + return 0; + } + + errorCode = lpfnAllocateAndGetTcpExTableFromStack (&lpBuffer, TRUE, GetProcessHeap(), 0, 2); + + if (errorCode != NO_ERROR) + { + _dbus_verbose + ("Error not been able to call AllocateAndGetTcpExTableFromStack()\n"); + return 0; + } + + for (dwSize = 0; dwSize < lpBuffer->dwNumEntries; dwSize++) + { + int local_port = ntohs (lpBuffer->table[dwSize].dwLocalPort); + if (local_port == peer_port) + { + result = lpBuffer->table[dwSize].dwProcessId; + break; + } + } + + if (lpBuffer) + HeapFree (GetProcessHeap(), 0, lpBuffer); + + _dbus_verbose ("got pid %d\n", (int)result); + return result; + } + if ((result = GetExtendedTcpTable (NULL, &size, TRUE, AF_INET, TCP_TABLE_OWNER_PID_ALL, 0)) == ERROR_INSUFFICIENT_BUFFER) { -- 1.7.10.4