2003-04-24 Havoc Pennington <hp@redhat.com>
[platform/upstream/dbus.git] / dbus / dbus-transport.c
index d074480..f7b110a 100644 (file)
@@ -213,6 +213,8 @@ _dbus_transport_open (const char     *address,
 {
   DBusTransport *transport;
   DBusAddressEntry **entries;
+  DBusError tmp_error;
+  DBusError first_error;
   int len, i;
   const char *address_problem_type;
   const char *address_problem_field;
@@ -223,15 +225,21 @@ _dbus_transport_open (const char     *address,
   if (!dbus_parse_address (address, &entries, &len, error))
     return NULL;
 
+  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
+  
   transport = NULL;
   address_problem_type = NULL;
   address_problem_field = NULL;
   address_problem_other = NULL;
-  
+
+  dbus_error_init (&tmp_error);
+  dbus_error_init (&first_error);
   for (i = 0; i < len; i++)
     {
-      const char *method = dbus_address_entry_get_method (entries[i]);
+      const char *method;
 
+      method = dbus_address_entry_get_method (entries[i]);
+      
       if (strcmp (method, "unix") == 0)
        {
          const char *path = dbus_address_entry_get_value (entries[i], "path");
@@ -250,7 +258,7 @@ _dbus_transport_open (const char     *address,
               goto bad_address;
             }
 
-          transport = _dbus_transport_new_for_domain_socket (path, error);
+          transport = _dbus_transport_new_for_domain_socket (path, &tmp_error);
        }
       else if (strcmp (method, "tcp") == 0)
        {
@@ -277,7 +285,7 @@ _dbus_transport_open (const char     *address,
               goto bad_address;
             }
           
-         transport = _dbus_transport_new_for_tcp_socket (host, lport, error);
+         transport = _dbus_transport_new_for_tcp_socket (host, lport, &tmp_error);
        }
 #ifdef DBUS_BUILD_TESTS
       else if (strcmp (method, "debug") == 0)
@@ -290,8 +298,8 @@ _dbus_transport_open (const char     *address,
               address_problem_field = "name";
               goto bad_address;
             }
-
-         transport = _dbus_transport_debug_client_new (name, error);
+          
+          transport = _dbus_transport_debug_client_new (name, &tmp_error);
        }
       else if (strcmp (method, "debug-pipe") == 0)
        {
@@ -303,8 +311,8 @@ _dbus_transport_open (const char     *address,
               address_problem_field = "name";
               goto bad_address;
             }
-
-         transport = _dbus_transport_debug_pipe_new (name, error);
+          
+         transport = _dbus_transport_debug_pipe_new (name, &tmp_error);
        }
 #endif
       else
@@ -314,9 +322,29 @@ _dbus_transport_open (const char     *address,
         }
 
       if (transport)
-       break;    
+       break;
+
+      _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
+      
+      if (i == 0)
+        dbus_move_error (&tmp_error, &first_error);
+      else
+        dbus_error_free (&tmp_error);
     }
 
+  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
+  _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
+  
+  if (transport == NULL)
+    {
+      _DBUS_ASSERT_ERROR_IS_SET (&first_error);
+      dbus_move_error (&first_error, error);
+    }
+  else
+    {
+      dbus_error_free (&first_error);
+    }
+  
   dbus_address_entries_free (entries);
   return transport;
 
@@ -431,9 +459,21 @@ _dbus_transport_get_is_authenticated (DBusTransport *transport)
       
       maybe_authenticated =
         (!(transport->send_credentials_pending ||
-           transport->receive_credentials_pending)) &&
-        _dbus_auth_do_work (transport->auth) == DBUS_AUTH_STATE_AUTHENTICATED;
+           transport->receive_credentials_pending));
 
+      if (maybe_authenticated)
+        {
+          switch (_dbus_auth_do_work (transport->auth))
+            {
+            case DBUS_AUTH_STATE_AUTHENTICATED:
+            case DBUS_AUTH_STATE_AUTHENTICATED_WITH_UNUSED_BYTES:
+              /* leave as maybe_authenticated */
+              break;
+            default:
+              maybe_authenticated = FALSE;
+            }
+        }
+      
       /* If we've authenticated as some identity, check that the auth
        * identity is the same as our own identity.  In the future, we
        * may have API allowing applications to specify how this is
@@ -455,14 +495,15 @@ _dbus_transport_get_is_authenticated (DBusTransport *transport)
                                                       auth_identity.uid,
                                                       transport->unix_user_data))
                 {
-                  _dbus_verbose ("Client UID %d was rejected, disconnecting\n",
+                  _dbus_verbose ("Client UID "DBUS_UID_FORMAT
+                                 " was rejected, disconnecting\n",
                                  auth_identity.uid);
                   _dbus_transport_disconnect (transport);
                   return FALSE;
                 }
               else
                 {
-                  _dbus_verbose ("Client UID %d authorized\n", auth_identity.uid);
+                  _dbus_verbose ("Client UID "DBUS_UID_FORMAT" authorized\n", auth_identity.uid);
                 }
             }
           else
@@ -474,14 +515,16 @@ _dbus_transport_get_is_authenticated (DBusTransport *transport)
               if (!_dbus_credentials_match (&our_identity,
                                             &auth_identity))
                 {
-                  _dbus_verbose ("Client authorized as UID %d but our UID is %d, disconnecting\n",
+                  _dbus_verbose ("Client authorized as UID "DBUS_UID_FORMAT
+                                 " but our UID is "DBUS_UID_FORMAT", disconnecting\n",
                                  auth_identity.uid, our_identity.uid);
                   _dbus_transport_disconnect (transport);
                   return FALSE;
                 }
               else
                 {
-                  _dbus_verbose ("Client authorized as UID %d matching our UID %d\n",
+                  _dbus_verbose ("Client authorized as UID "DBUS_UID_FORMAT
+                                 " matching our UID "DBUS_UID_FORMAT"\n",
                                  auth_identity.uid, our_identity.uid);
                 }
             }
@@ -737,18 +780,18 @@ _dbus_transport_get_dispatch_status (DBusTransport *transport)
 
   if (!_dbus_transport_get_is_authenticated (transport))
     {
-      switch (_dbus_auth_do_work (transport->auth))
-        {
-        case DBUS_AUTH_STATE_WAITING_FOR_MEMORY:
-          return DBUS_DISPATCH_NEED_MEMORY;
-        case DBUS_AUTH_STATE_AUTHENTICATED_WITH_UNUSED_BYTES:
-          if (!recover_unused_bytes (transport))
-            return DBUS_DISPATCH_NEED_MEMORY;
-          break;
-        default:
-          break;
-        }
+      if (_dbus_auth_do_work (transport->auth) ==
+          DBUS_AUTH_STATE_WAITING_FOR_MEMORY)
+        return DBUS_DISPATCH_NEED_MEMORY;
+      else
+        return DBUS_DISPATCH_COMPLETE;
     }
+
+  if (!transport->unused_bytes_recovered &&
+      !recover_unused_bytes (transport))
+    return DBUS_DISPATCH_NEED_MEMORY;
+
+  transport->unused_bytes_recovered = TRUE;
   
   if (!_dbus_message_loader_queue_messages (transport->loader))
     return DBUS_DISPATCH_NEED_MEMORY;
@@ -771,6 +814,8 @@ dbus_bool_t
 _dbus_transport_queue_messages (DBusTransport *transport)
 {
   DBusDispatchStatus status;
+
+  _dbus_verbose ("_dbus_transport_queue_messages()\n");
   
   /* Queue any messages */
   while ((status = _dbus_transport_get_dispatch_status (transport)) == DBUS_DISPATCH_DATA_REMAINS)
@@ -785,11 +830,19 @@ _dbus_transport_queue_messages (DBusTransport *transport)
       
       _dbus_verbose ("queueing received message %p\n", message);
 
-      _dbus_message_add_size_counter (message, transport->live_messages_size);
-
-      /* pass ownership of link and message ref to connection */
-      _dbus_connection_queue_received_message_link (transport->connection,
-                                                    link);
+      if (!_dbus_message_add_size_counter (message, transport->live_messages_size))
+        {
+          _dbus_message_loader_putback_message_link (transport->loader,
+                                                     link);
+          status = DBUS_DISPATCH_NEED_MEMORY;
+          break;
+        }
+      else
+        {
+          /* pass ownership of link and message ref to connection */
+          _dbus_connection_queue_received_message_link (transport->connection,
+                                                        link);
+        }
     }
 
   if (_dbus_message_loader_get_is_corrupted (transport->loader))
@@ -827,14 +880,14 @@ _dbus_transport_get_max_message_size (DBusTransport  *transport)
 }
 
 /**
- * See dbus_connection_set_max_live_messages_size().
+ * See dbus_connection_set_max_received_size().
  *
  * @param transport the transport
  * @param size the max size of all incoming messages
  */
 void
-_dbus_transport_set_max_live_messages_size (DBusTransport  *transport,
-                                            long            size)
+_dbus_transport_set_max_received_size (DBusTransport  *transport,
+                                       long            size)
 {
   transport->max_live_messages_size = size;
   _dbus_counter_set_notify (transport->live_messages_size,
@@ -845,13 +898,13 @@ _dbus_transport_set_max_live_messages_size (DBusTransport  *transport,
 
 
 /**
- * See dbus_connection_get_max_live_messages_size().
+ * See dbus_connection_get_max_received_size().
  *
  * @param transport the transport
  * @returns max bytes for all live messages
  */
 long
-_dbus_transport_get_max_live_messages_size (DBusTransport  *transport)
+_dbus_transport_get_max_received_size (DBusTransport  *transport)
 {
   return transport->max_live_messages_size;
 }