Remove transport's call to _dbus_authorization_do_authorization().
[platform/upstream/dbus.git] / dbus / dbus-sysdeps-win.c
index 3c46bd1..38758bd 100644 (file)
@@ -121,17 +121,123 @@ static ProcAllocateAndGetTcpExtTableFromStack lpfnAllocateAndGetTcpExTableFromSt
  * 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)
+static BOOL
+load_ex_ip_helper_procedures(void)
 {
-    HMODULE hModule = LoadLibrary ("iphlpapi.dll");
-    if (hModule == NULL)
-        return FALSE;
+  HMODULE hModule = LoadLibrary ("iphlpapi.dll");
+  if (hModule == NULL)
+    {
+      _dbus_verbose ("could not load iphlpapi.dll\n");
+      return FALSE;
+    }
 
-    lpfnAllocateAndGetTcpExTableFromStack = (ProcAllocateAndGetTcpExtTableFromStack)GetProcAddress (hModule, "AllocateAndGetTcpExTableFromStack");
-    if (lpfnAllocateAndGetTcpExTableFromStack == NULL)
-        return FALSE;
+  lpfnAllocateAndGetTcpExTableFromStack = (ProcAllocateAndGetTcpExtTableFromStack)GetProcAddress (hModule, "AllocateAndGetTcpExTableFromStack");
+  if (lpfnAllocateAndGetTcpExTableFromStack == NULL)
+    {
+      _dbus_verbose ("could not find function AllocateAndGetTcpExTableFromStack in iphlpapi.dll\n");
+      return FALSE;
+    }
+  return TRUE;
+}
 
-    return TRUE;
+/**
+ * get pid from localhost tcp connection using peer_port
+ * This function is available on WinXP >= SP3
+ * @param peer_port peers tcp port
+ * @return process id or 0 if connection has not been found
+ */
+static dbus_pid_t
+get_pid_from_extended_tcp_table(int peer_port)
+{
+  dbus_pid_t result;
+  DWORD errorCode, size, i;
+  MIB_TCPTABLE_OWNER_PID *tcp_table;
+
+  if ((errorCode =
+       GetExtendedTcpTable (NULL, &size, TRUE, AF_INET, TCP_TABLE_OWNER_PID_ALL, 0)) == ERROR_INSUFFICIENT_BUFFER)
+    {
+      tcp_table = (MIB_TCPTABLE_OWNER_PID *) dbus_malloc (size);
+      if (tcp_table == NULL)
+        {
+          _dbus_verbose ("Error allocating memory\n");
+          return 0;
+        }
+    }
+  else
+    {
+      _dbus_verbose ("unexpected error returned from GetExtendedTcpTable %d\n", errorCode);
+      return 0;
+    }
+
+  if ((errorCode = GetExtendedTcpTable (tcp_table, &size, TRUE, AF_INET, TCP_TABLE_OWNER_PID_ALL, 0)) != NO_ERROR)
+    {
+      _dbus_verbose ("Error fetching tcp table %d\n", (int)errorCode);
+      dbus_free (tcp_table);
+      return 0;
+    }
+
+  result = 0;
+  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_address == INADDR_LOOPBACK && local_port == peer_port)
+         result = p->dwOwningPid;
+    }
+
+  dbus_free (tcp_table);
+  _dbus_verbose ("got pid %d\n", (int)result);
+  return result;
+}
+
+/**
+ * get pid from localhost tcp connection using peer_port
+ * This function is available on all WinXP versions, but
+ * not in wine (at least version <= 1.6.0)
+ * @param peer_port peers tcp port
+ * @return process id or 0 if connection has not been found
+ */
+static dbus_pid_t
+get_pid_from_tcp_ex_table(int peer_port)
+{
+  dbus_pid_t result;
+  DWORD errorCode, i;
+  PMIB_TCPTABLE_EX tcp_table = NULL;
+
+  if (!load_ex_ip_helper_procedures ())
+    {
+      _dbus_verbose
+        ("Error not been able to load iphelper procedures\n");
+      return 0;
+    }
+
+  errorCode = lpfnAllocateAndGetTcpExTableFromStack (&tcp_table, TRUE, GetProcessHeap(), 0, 2);
+
+  if (errorCode != NO_ERROR)
+    {
+      _dbus_verbose
+        ("Error not been able to call AllocateAndGetTcpExTableFromStack()\n");
+      return 0;
+    }
+
+  result = 0;
+  for (i = 0; i < tcp_table->dwNumEntries; i++)
+    {
+      _MIB_TCPROW_EX *p = &tcp_table->table[i];
+      int local_port = ntohs (p->dwLocalPort);
+      int local_address = ntohl (p->dwLocalAddr);
+      if (local_address == INADDR_LOOPBACK && local_port == peer_port)
+        {
+          result = p->dwOwningPid;
+          break;
+        }
+    }
+
+  HeapFree (GetProcessHeap(), 0, tcp_table);
+  _dbus_verbose ("got pid %d\n", (int)result);
+  return result;
 }
 
 /**
@@ -147,9 +253,6 @@ _dbus_get_peer_pid_from_tcp_handle (int handle)
   int peer_port;
 
   dbus_pid_t result;
-  DWORD size;
-  MIB_TCPTABLE_OWNER_PID *tcp_table;
-  DWORD i;
   dbus_bool_t is_localhost = FALSE;
 
   getpeername (handle, (struct sockaddr *) &addr, &len);
@@ -158,7 +261,7 @@ _dbus_get_peer_pid_from_tcp_handle (int handle)
     {
       struct sockaddr_in *s = (struct sockaddr_in *) &addr;
       peer_port = ntohs (s->sin_port);
-      is_localhost = (htonl (s->sin_addr.s_addr) == INADDR_LOOPBACK);
+      is_localhost = (ntohl (s->sin_addr.s_addr) == INADDR_LOOPBACK);
     }
   else if (addr.ss_family == AF_INET6)
     {
@@ -190,75 +293,12 @@ _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;
-            }
-        }
+  _dbus_verbose ("trying to get peers pid");
 
-      if (lpBuffer)
-          HeapFree (GetProcessHeap(), 0, lpBuffer);
-
-      _dbus_verbose ("got pid %d\n", (int)result);
+  result = get_pid_from_extended_tcp_table (peer_port);
+  if (result > 0)
       return result;
-    }
-
-  if ((result =
-       GetExtendedTcpTable (NULL, &size, TRUE, AF_INET, TCP_TABLE_OWNER_PID_ALL, 0)) == ERROR_INSUFFICIENT_BUFFER)
-    {
-      tcp_table = (MIB_TCPTABLE_OWNER_PID *) dbus_malloc (size);
-      if (tcp_table == NULL)
-        {
-          _dbus_verbose ("Error allocating memory\n");
-          return 0;
-        }
-    }
-
-  if ((result = GetExtendedTcpTable (tcp_table, &size, TRUE, AF_INET, TCP_TABLE_OWNER_PID_ALL, 0)) != NO_ERROR)
-    {
-      _dbus_verbose ("Error fetching tcp table %d\n", result);
-      dbus_free (tcp_table);
-      return 0;
-    }
-
-  result = 0;
-  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_address == INADDR_LOOPBACK && local_port == peer_port)
-        result = p->dwOwningPid;
-    }
-
-  _dbus_verbose ("got pid %d\n", result);
-  dbus_free (tcp_table);
+  result = get_pid_from_tcp_ex_table (peer_port);
   return result;
 }
 
@@ -482,7 +522,7 @@ _dbus_close_socket (int        fd,
  * on exec. Should be called for all file
  * descriptors in D-Bus code.
  *
- * @param fd the file descriptor
+ * @param handle the Windows HANDLE
  */
 void
 _dbus_fd_set_close_on_exec (intptr_t handle)
@@ -498,7 +538,7 @@ _dbus_fd_set_close_on_exec (intptr_t handle)
 /**
  * Sets a file descriptor to be nonblocking.
  *
- * @param fd the file descriptor.
+ * @param handle the file descriptor.
  * @param error address of error location.
  * @returns #TRUE on success.
  */
@@ -1869,7 +1909,7 @@ again:
  * The point of the byte is that on some systems we have to
  * use sendmsg()/recvmsg() to transmit credentials.
  *
- * @param client_fd the client file descriptor
+ * @param handle the client file descriptor
  * @param credentials struct to fill with credentials of client
  * @param error location to store error code
  * @returns #TRUE on success
@@ -3378,7 +3418,7 @@ _dbus_get_is_errno_eagain_or_ewouldblock (void)
 /**
  * return the absolute path of the dbus installation 
  *
- * @param s buffer for installation path
+ * @param prefix buffer for installation path
  * @param len length of buffer
  * @returns #FALSE on failure
  */