Add doc to load_ex_ip_helper_procedures().
[platform/upstream/dbus.git] / dbus / dbus-sysdeps-win.c
index 72ccb93..3c46bd1 100644 (file)
@@ -104,6 +104,36 @@ _dbus_win_set_errno (int err)
 #endif
 }
 
+static BOOL is_winxp_sp3_or_lower();
+
+/*
+ * _MIB_TCPROW_EX and friends are not available in system headers
+ *  and are mapped to attribute identical ...OWNER_PID typedefs.
+ */
+typedef MIB_TCPROW_OWNER_PID _MIB_TCPROW_EX;
+typedef MIB_TCPTABLE_OWNER_PID MIB_TCPTABLE_EX;
+typedef PMIB_TCPTABLE_OWNER_PID PMIB_TCPTABLE_EX;
+typedef DWORD (WINAPI *ProcAllocateAndGetTcpExtTableFromStack)(PMIB_TCPTABLE_EX*,BOOL,HANDLE,DWORD,DWORD);
+static ProcAllocateAndGetTcpExtTableFromStack lpfnAllocateAndGetTcpExTableFromStack = NULL;
+
+/**
+ * AllocateAndGetTcpExTableFromStack() is undocumented and not exported,
+ * but is the only way to do this in older XP versions.
+ * @return true if the procedures could be loaded
+ */
+static BOOL load_ex_ip_helper_procedures(void)
+{
+    HMODULE hModule = LoadLibrary ("iphlpapi.dll");
+    if (hModule == NULL)
+        return FALSE;
+
+    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 +190,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;
+        }
+
+      result = 0;
+      for (dwSize = 0; dwSize < lpBuffer->dwNumEntries; dwSize++)
+        {
+          int local_port = ntohs (lpBuffer->table[dwSize].dwLocalPort);
+          int local_address = ntohl (lpBuffer->table[dwSize].dwLocalAddr);
+          if (local_address == INADDR_LOOPBACK && local_port == peer_port)
+            {
+              result = lpBuffer->table[dwSize].dwOwningPid;
+              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)
     {
@@ -182,8 +250,10 @@ _dbus_get_peer_pid_from_tcp_handle (int handle)
   for (i = 0; i < tcp_table->dwNumEntries; i++)
     {
       MIB_TCPROW_OWNER_PID *p = &tcp_table->table[i];
+      int local_address = ntohl (p->dwLocalAddr);
       int local_port = ntohs (p->dwLocalPort);
-      if (p->dwState == MIB_TCP_STATE_ESTAB && local_port == peer_port)
+      if (p->dwState == MIB_TCP_STATE_ESTAB
+          && local_address == INADDR_LOOPBACK && local_port == peer_port)
         result = p->dwOwningPid;
     }
 
@@ -832,6 +902,38 @@ _dbus_pid_for_log (void)
 }
 
 #ifndef DBUS_WINCE
+
+static BOOL is_winxp_sp3_or_lower()
+{
+   OSVERSIONINFOEX osvi;
+   DWORDLONG dwlConditionMask = 0;
+   int op=VER_LESS_EQUAL;
+
+   // Initialize the OSVERSIONINFOEX structure.
+
+   ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
+   osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
+   osvi.dwMajorVersion = 5;
+   osvi.dwMinorVersion = 1;
+   osvi.wServicePackMajor = 3;
+   osvi.wServicePackMinor = 0;
+
+   // Initialize the condition mask.
+
+   VER_SET_CONDITION( dwlConditionMask, VER_MAJORVERSION, op );
+   VER_SET_CONDITION( dwlConditionMask, VER_MINORVERSION, op );
+   VER_SET_CONDITION( dwlConditionMask, VER_SERVICEPACKMAJOR, op );
+   VER_SET_CONDITION( dwlConditionMask, VER_SERVICEPACKMINOR, op );
+
+   // Perform the test.
+
+   return VerifyVersionInfo(
+      &osvi,
+      VER_MAJORVERSION | VER_MINORVERSION |
+      VER_SERVICEPACKMAJOR | VER_SERVICEPACKMINOR,
+      dwlConditionMask);
+}
+
 /** Gets our SID
  * @param sid points to sid buffer, need to be freed with LocalFree()
  * @param process_id the process id for which the sid should be returned
@@ -845,7 +947,8 @@ _dbus_getsid(char **sid, dbus_pid_t process_id)
   DWORD n;
   PSID psid;
   int retval = FALSE;
-  HANDLE process_handle = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, process_id);
+
+  HANDLE process_handle = OpenProcess(is_winxp_sp3_or_lower() ? PROCESS_QUERY_INFORMATION : PROCESS_QUERY_LIMITED_INFORMATION, FALSE, process_id);
 
   if (!OpenProcessToken (process_handle, TOKEN_QUERY, &process_token))
     {
@@ -879,7 +982,7 @@ failed:
   if (process_token != INVALID_HANDLE_VALUE)
     CloseHandle (process_token);
 
-  _dbus_verbose("_dbus_getsid() returns %d\n",retval);
+  _dbus_verbose("_dbus_getsid() got '%s' and returns %d\n", *sid, retval);
   return retval;
 }
 #endif
@@ -2221,7 +2324,7 @@ _dbus_replace_install_prefix (const char *configure_time_path)
 #endif
 }
 
-#if !defined (DBUS_DISABLE_ASSERTS) || defined(DBUS_BUILD_TESTS)
+#if !defined (DBUS_DISABLE_ASSERT) || defined(DBUS_ENABLE_EMBEDDED_TESTS)
 
 #if defined(_MSC_VER) || defined(DBUS_WINCE)
 # ifdef BACKTRACES
@@ -3457,7 +3560,7 @@ _dbus_append_keyring_directory_for_credentials (DBusString      *directory,
       _dbus_string_append(&homedir,homepath);
     }
   
-#ifdef DBUS_BUILD_TESTS
+#ifdef DBUS_ENABLE_EMBEDDED_TESTS
   {
     const char *override;