1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* dbus-transport.c DBusTransport object (internal to D-Bus implementation)
4 * Copyright (C) 2002, 2003 Red Hat Inc.
6 * Licensed under the Academic Free License version 2.1
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 #include "dbus-transport-protected.h"
26 #include "dbus-transport-unix.h"
27 #include "dbus-transport-socket.h"
28 #include "dbus-connection-internal.h"
29 #include "dbus-watch.h"
30 #include "dbus-auth.h"
31 #include "dbus-address.h"
32 #include "dbus-credentials.h"
33 #include "dbus-mainloop.h"
34 #include "dbus-message.h"
35 #ifdef DBUS_BUILD_TESTS
36 #include "dbus-server-debug-pipe.h"
40 * @defgroup DBusTransport DBusTransport object
41 * @ingroup DBusInternals
42 * @brief "Backend" for a DBusConnection.
44 * Types and functions related to DBusTransport. A transport is an
45 * abstraction that can send and receive data via various kinds of
46 * network connections or other IPC mechanisms.
52 * @typedef DBusTransport
54 * Opaque object representing a way message stream.
55 * DBusTransport abstracts various kinds of actual
56 * transport mechanism, such as different network protocols,
57 * or encryption schemes.
61 live_messages_notify (DBusCounter *counter,
64 DBusTransport *transport = user_data;
66 _dbus_transport_ref (transport);
69 _dbus_verbose ("Size counter value is now %d\n",
70 (int) _dbus_counter_get_size_value (counter));
71 _dbus_verbose ("Unix FD counter value is now %d\n",
72 (int) _dbus_counter_get_unix_fd_value (counter));
75 /* disable or re-enable the read watch for the transport if
78 if (transport->vtable->live_messages_changed)
80 _dbus_connection_lock (transport->connection);
81 (* transport->vtable->live_messages_changed) (transport);
82 _dbus_connection_unlock (transport->connection);
85 _dbus_transport_unref (transport);
89 * Initializes the base class members of DBusTransport. Chained up to
90 * by subclasses in their constructor. The server GUID is the
91 * globally unique ID for the server creating this connection
92 * and will be #NULL for the client side of a connection. The GUID
95 * @param transport the transport being created.
96 * @param vtable the subclass vtable.
97 * @param server_guid non-#NULL if this transport is on the server side of a connection
98 * @param address the address of the transport
99 * @returns #TRUE on success.
102 _dbus_transport_init_base (DBusTransport *transport,
103 const DBusTransportVTable *vtable,
104 const DBusString *server_guid,
105 const DBusString *address)
107 DBusMessageLoader *loader;
109 DBusCounter *counter;
111 DBusCredentials *creds;
113 loader = _dbus_message_loader_new ();
118 auth = _dbus_auth_server_new (server_guid);
120 auth = _dbus_auth_client_new ();
123 _dbus_message_loader_unref (loader);
127 counter = _dbus_counter_new ();
130 _dbus_auth_unref (auth);
131 _dbus_message_loader_unref (loader);
135 creds = _dbus_credentials_new ();
138 _dbus_counter_unref (counter);
139 _dbus_auth_unref (auth);
140 _dbus_message_loader_unref (loader);
146 _dbus_assert (address == NULL);
151 _dbus_assert (address != NULL);
153 if (!_dbus_string_copy_data (address, &address_copy))
155 _dbus_credentials_unref (creds);
156 _dbus_counter_unref (counter);
157 _dbus_auth_unref (auth);
158 _dbus_message_loader_unref (loader);
163 transport->refcount = 1;
164 transport->vtable = vtable;
165 transport->loader = loader;
166 transport->auth = auth;
167 transport->live_messages = counter;
168 transport->authenticated = FALSE;
169 transport->disconnected = FALSE;
170 transport->is_server = (server_guid != NULL);
171 transport->send_credentials_pending = !transport->is_server;
172 transport->receive_credentials_pending = transport->is_server;
173 transport->address = address_copy;
175 transport->unix_user_function = NULL;
176 transport->unix_user_data = NULL;
177 transport->free_unix_user_data = NULL;
179 transport->windows_user_function = NULL;
180 transport->windows_user_data = NULL;
181 transport->free_windows_user_data = NULL;
183 transport->expected_guid = NULL;
185 /* Try to default to something that won't totally hose the system,
186 * but doesn't impose too much of a limitation.
188 transport->max_live_messages_size = _DBUS_ONE_MEGABYTE * 63;
190 /* On Linux RLIMIT_NOFILE defaults to 1024, so allowing 4096 fds live
191 should be more than enough */
192 transport->max_live_messages_unix_fds = 4096;
194 /* credentials read from socket if any */
195 transport->credentials = creds;
197 _dbus_counter_set_notify (transport->live_messages,
198 transport->max_live_messages_size,
199 transport->max_live_messages_unix_fds,
200 live_messages_notify,
203 if (transport->address)
204 _dbus_verbose ("Initialized transport on address %s\n", transport->address);
210 * Finalizes base class members of DBusTransport.
211 * Chained up to from subclass finalizers.
213 * @param transport the transport.
216 _dbus_transport_finalize_base (DBusTransport *transport)
218 if (!transport->disconnected)
219 _dbus_transport_disconnect (transport);
221 if (transport->free_unix_user_data != NULL)
222 (* transport->free_unix_user_data) (transport->unix_user_data);
224 if (transport->free_windows_user_data != NULL)
225 (* transport->free_windows_user_data) (transport->windows_user_data);
227 _dbus_message_loader_unref (transport->loader);
228 _dbus_auth_unref (transport->auth);
229 _dbus_counter_set_notify (transport->live_messages,
231 _dbus_counter_unref (transport->live_messages);
232 dbus_free (transport->address);
233 dbus_free (transport->expected_guid);
234 if (transport->credentials)
235 _dbus_credentials_unref (transport->credentials);
240 * Verifies if a given D-Bus address is a valid address
241 * by attempting to connect to it. If it is, returns the
242 * opened DBusTransport object. If it isn't, returns #NULL
245 * @param error address where an error can be returned.
246 * @returns a new transport, or #NULL on failure.
248 static DBusTransport*
249 check_address (const char *address, DBusError *error)
251 DBusAddressEntry **entries;
252 DBusTransport *transport = NULL;
255 _dbus_assert (address != NULL);
256 _dbus_assert (*address != '\0');
258 if (!dbus_parse_address (address, &entries, &len, error))
259 return NULL; /* not a valid address */
261 for (i = 0; i < len; i++)
263 transport = _dbus_transport_open (entries[i], error);
264 if (transport != NULL)
268 dbus_address_entries_free (entries);
273 * Creates a new transport for the "autostart" method.
274 * This creates a client-side of a transport.
276 * @param error address where an error can be returned.
277 * @returns a new transport, or #NULL on failure.
279 static DBusTransport*
280 _dbus_transport_new_for_autolaunch (const char *scope, DBusError *error)
283 DBusTransport *result = NULL;
285 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
287 if (!_dbus_string_init (&address))
289 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
293 if (!_dbus_get_autolaunch_address (scope, &address, error))
295 _DBUS_ASSERT_ERROR_IS_SET (error);
299 result = check_address (_dbus_string_get_const_data (&address), error);
301 _DBUS_ASSERT_ERROR_IS_SET (error);
303 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
306 _dbus_string_free (&address);
310 static DBusTransportOpenResult
311 _dbus_transport_open_autolaunch (DBusAddressEntry *entry,
312 DBusTransport **transport_p,
317 method = dbus_address_entry_get_method (entry);
318 _dbus_assert (method != NULL);
320 if (strcmp (method, "autolaunch") == 0)
322 const char *scope = dbus_address_entry_get_value (entry, "scope");
324 *transport_p = _dbus_transport_new_for_autolaunch (scope, error);
326 if (*transport_p == NULL)
328 _DBUS_ASSERT_ERROR_IS_SET (error);
329 return DBUS_TRANSPORT_OPEN_DID_NOT_CONNECT;
333 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
334 return DBUS_TRANSPORT_OPEN_OK;
339 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
340 return DBUS_TRANSPORT_OPEN_NOT_HANDLED;
344 static const struct {
345 DBusTransportOpenResult (* func) (DBusAddressEntry *entry,
346 DBusTransport **transport_p,
349 { _dbus_transport_open_socket },
350 { _dbus_transport_open_platform_specific },
351 { _dbus_transport_open_autolaunch }
352 #ifdef DBUS_BUILD_TESTS
353 , { _dbus_transport_open_debug_pipe }
358 * Try to open a new transport for the given address entry. (This
359 * opens a client-side-of-the-connection transport.)
361 * @param entry the address entry
362 * @param error location to store reason for failure.
363 * @returns new transport of #NULL on failure.
366 _dbus_transport_open (DBusAddressEntry *entry,
369 DBusTransport *transport;
370 const char *expected_guid_orig;
373 DBusError tmp_error = DBUS_ERROR_INIT;
375 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
378 expected_guid_orig = dbus_address_entry_get_value (entry, "guid");
379 expected_guid = _dbus_strdup (expected_guid_orig);
381 if (expected_guid_orig != NULL && expected_guid == NULL)
383 _DBUS_SET_OOM (error);
387 for (i = 0; i < (int) _DBUS_N_ELEMENTS (open_funcs); ++i)
389 DBusTransportOpenResult result;
391 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
392 result = (* open_funcs[i].func) (entry, &transport, &tmp_error);
396 case DBUS_TRANSPORT_OPEN_OK:
397 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
400 case DBUS_TRANSPORT_OPEN_NOT_HANDLED:
401 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
402 /* keep going through the loop of open funcs */
404 case DBUS_TRANSPORT_OPEN_BAD_ADDRESS:
405 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
408 case DBUS_TRANSPORT_OPEN_DID_NOT_CONNECT:
409 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
417 if (transport == NULL)
419 if (!dbus_error_is_set (&tmp_error))
420 _dbus_set_bad_address (&tmp_error,
422 "Unknown address type (examples of valid types are \"tcp\" and on UNIX \"unix\")");
424 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
425 dbus_move_error(&tmp_error, error);
426 dbus_free (expected_guid);
430 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
432 /* In the case of autostart the initial guid is NULL
433 * and the autostart transport recursively calls
434 * _dbus_open_transport wich returns a transport
435 * with a guid. That guid is the definitive one.
437 * FIXME: if more transports are added they may have
438 * an effect on the expected_guid semantics (i.e.
439 * expected_guid and transport->expected_guid may
440 * both have values). This is very unlikely though
441 * we should either throw asserts here for those
442 * corner cases or refactor the code so it is
443 * clearer on what is expected and what is not
446 transport->expected_guid = expected_guid;
453 * Increments the reference count for the transport.
455 * @param transport the transport.
456 * @returns the transport.
459 _dbus_transport_ref (DBusTransport *transport)
461 _dbus_assert (transport->refcount > 0);
463 transport->refcount += 1;
469 * Decrements the reference count for the transport.
470 * Disconnects and finalizes the transport if
471 * the reference count reaches zero.
473 * @param transport the transport.
476 _dbus_transport_unref (DBusTransport *transport)
478 _dbus_assert (transport != NULL);
479 _dbus_assert (transport->refcount > 0);
481 transport->refcount -= 1;
482 if (transport->refcount == 0)
484 _dbus_verbose ("finalizing\n");
486 _dbus_assert (transport->vtable->finalize != NULL);
488 (* transport->vtable->finalize) (transport);
493 * Closes our end of the connection to a remote application. Further
494 * attempts to use this transport will fail. Only the first call to
495 * _dbus_transport_disconnect() will have an effect.
497 * @param transport the transport.
501 _dbus_transport_disconnect (DBusTransport *transport)
503 _dbus_verbose ("start\n");
505 _dbus_assert (transport->vtable->disconnect != NULL);
507 if (transport->disconnected)
510 (* transport->vtable->disconnect) (transport);
512 transport->disconnected = TRUE;
514 _dbus_verbose ("end\n");
518 * Returns #TRUE if the transport has not been disconnected.
519 * Disconnection can result from _dbus_transport_disconnect()
520 * or because the server drops its end of the connection.
522 * @param transport the transport.
523 * @returns whether we're connected
526 _dbus_transport_get_is_connected (DBusTransport *transport)
528 return !transport->disconnected;
532 auth_via_unix_user_function (DBusTransport *transport)
534 DBusCredentials *auth_identity;
536 DBusConnection *connection;
537 DBusAllowUnixUserFunction unix_user_function;
538 void *unix_user_data;
541 /* Dropping the lock here probably isn't that safe. */
543 auth_identity = _dbus_auth_get_identity (transport->auth);
544 _dbus_assert (auth_identity != NULL);
546 connection = transport->connection;
547 unix_user_function = transport->unix_user_function;
548 unix_user_data = transport->unix_user_data;
549 uid = _dbus_credentials_get_unix_uid (auth_identity);
551 _dbus_verbose ("unlock\n");
552 _dbus_connection_unlock (connection);
554 allow = (* unix_user_function) (connection,
558 _dbus_verbose ("lock post unix user function\n");
559 _dbus_connection_lock (connection);
563 _dbus_verbose ("Client UID "DBUS_UID_FORMAT" authorized\n", uid);
567 _dbus_verbose ("Client UID "DBUS_UID_FORMAT
568 " was rejected, disconnecting\n",
569 _dbus_credentials_get_unix_uid (auth_identity));
570 _dbus_transport_disconnect (transport);
577 auth_via_windows_user_function (DBusTransport *transport)
579 DBusCredentials *auth_identity;
581 DBusConnection *connection;
582 DBusAllowWindowsUserFunction windows_user_function;
583 void *windows_user_data;
586 /* Dropping the lock here probably isn't that safe. */
588 auth_identity = _dbus_auth_get_identity (transport->auth);
589 _dbus_assert (auth_identity != NULL);
591 connection = transport->connection;
592 windows_user_function = transport->windows_user_function;
593 windows_user_data = transport->unix_user_data;
594 windows_sid = _dbus_strdup (_dbus_credentials_get_windows_sid (auth_identity));
596 if (windows_sid == NULL)
602 _dbus_verbose ("unlock\n");
603 _dbus_connection_unlock (connection);
605 allow = (* windows_user_function) (connection,
609 _dbus_verbose ("lock post windows user function\n");
610 _dbus_connection_lock (connection);
614 _dbus_verbose ("Client SID '%s' authorized\n", windows_sid);
618 _dbus_verbose ("Client SID '%s' was rejected, disconnecting\n",
619 _dbus_credentials_get_windows_sid (auth_identity));
620 _dbus_transport_disconnect (transport);
627 auth_via_default_rules (DBusTransport *transport)
629 DBusCredentials *auth_identity;
630 DBusCredentials *our_identity;
633 auth_identity = _dbus_auth_get_identity (transport->auth);
634 _dbus_assert (auth_identity != NULL);
636 /* By default, connection is allowed if the client is 1) root or 2)
637 * has the same UID as us or 3) anonymous is allowed.
640 our_identity = _dbus_credentials_new_from_current_process ();
641 if (our_identity == NULL)
647 if (transport->allow_anonymous ||
648 _dbus_credentials_get_unix_uid (auth_identity) == 0 ||
649 _dbus_credentials_same_user (our_identity,
652 if (_dbus_credentials_include(our_identity,DBUS_CREDENTIAL_WINDOWS_SID))
653 _dbus_verbose ("Client authorized as SID '%s'"
654 "matching our SID '%s'\n",
655 _dbus_credentials_get_windows_sid(auth_identity),
656 _dbus_credentials_get_windows_sid(our_identity));
658 _dbus_verbose ("Client authorized as UID "DBUS_UID_FORMAT
659 " matching our UID "DBUS_UID_FORMAT"\n",
660 _dbus_credentials_get_unix_uid(auth_identity),
661 _dbus_credentials_get_unix_uid(our_identity));
662 /* We have authenticated! */
667 if (_dbus_credentials_include(our_identity,DBUS_CREDENTIAL_WINDOWS_SID))
668 _dbus_verbose ("Client authorized as SID '%s'"
669 " but our SID is '%s', disconnecting\n",
670 (_dbus_credentials_get_windows_sid(auth_identity) ?
671 _dbus_credentials_get_windows_sid(auth_identity) : "<null>"),
672 (_dbus_credentials_get_windows_sid(our_identity) ?
673 _dbus_credentials_get_windows_sid(our_identity) : "<null>"));
675 _dbus_verbose ("Client authorized as UID "DBUS_UID_FORMAT
676 " but our UID is "DBUS_UID_FORMAT", disconnecting\n",
677 _dbus_credentials_get_unix_uid(auth_identity),
678 _dbus_credentials_get_unix_uid(our_identity));
679 _dbus_transport_disconnect (transport);
683 _dbus_credentials_unref (our_identity);
690 * Returns #TRUE if we have been authenticated. Will return #TRUE
691 * even if the transport is disconnected.
693 * @todo we drop connection->mutex when calling the unix_user_function,
694 * and windows_user_function, which may not be safe really.
696 * @param transport the transport
697 * @returns whether we're authenticated
700 _dbus_transport_get_is_authenticated (DBusTransport *transport)
702 if (transport->authenticated)
706 dbus_bool_t maybe_authenticated;
708 if (transport->disconnected)
711 /* paranoia ref since we call user callbacks sometimes */
712 _dbus_connection_ref_unlocked (transport->connection);
714 maybe_authenticated =
715 (!(transport->send_credentials_pending ||
716 transport->receive_credentials_pending));
718 if (maybe_authenticated)
720 switch (_dbus_auth_do_work (transport->auth))
722 case DBUS_AUTH_STATE_AUTHENTICATED:
723 /* leave as maybe_authenticated */
726 maybe_authenticated = FALSE;
730 /* If we're the client, verify the GUID
732 if (maybe_authenticated && !transport->is_server)
734 const char *server_guid;
736 server_guid = _dbus_auth_get_guid_from_server (transport->auth);
737 _dbus_assert (server_guid != NULL);
739 if (transport->expected_guid &&
740 strcmp (transport->expected_guid, server_guid) != 0)
742 _dbus_verbose ("Client expected GUID '%s' and we got '%s' from the server\n",
743 transport->expected_guid, server_guid);
744 _dbus_transport_disconnect (transport);
745 _dbus_connection_unref_unlocked (transport->connection);
750 /* If we're the server, see if we want to allow this identity to proceed.
752 if (maybe_authenticated && transport->is_server)
755 DBusCredentials *auth_identity;
757 auth_identity = _dbus_auth_get_identity (transport->auth);
758 _dbus_assert (auth_identity != NULL);
760 /* If we have an auth'd user and a user function, delegate
761 * deciding whether auth credentials are good enough to the
762 * app; otherwise, use our default decision process.
764 if (transport->unix_user_function != NULL &&
765 _dbus_credentials_include (auth_identity, DBUS_CREDENTIAL_UNIX_USER_ID))
767 allow = auth_via_unix_user_function (transport);
769 else if (transport->windows_user_function != NULL &&
770 _dbus_credentials_include (auth_identity, DBUS_CREDENTIAL_WINDOWS_SID))
772 allow = auth_via_windows_user_function (transport);
776 allow = auth_via_default_rules (transport);
780 maybe_authenticated = FALSE;
783 transport->authenticated = maybe_authenticated;
785 _dbus_connection_unref_unlocked (transport->connection);
786 return maybe_authenticated;
791 * See dbus_connection_get_is_anonymous().
793 * @param transport the transport
794 * @returns #TRUE if not authenticated or authenticated as anonymous
797 _dbus_transport_get_is_anonymous (DBusTransport *transport)
799 DBusCredentials *auth_identity;
801 if (!transport->authenticated)
804 auth_identity = _dbus_auth_get_identity (transport->auth);
806 if (_dbus_credentials_are_anonymous (auth_identity))
813 * Returns TRUE if the transport supports sending unix fds.
815 * @param transport the transport
816 * @returns #TRUE if TRUE it is possible to send unix fds across the transport.
819 _dbus_transport_can_pass_unix_fd(DBusTransport *transport)
821 return DBUS_TRANSPORT_CAN_SEND_UNIX_FD(transport);
825 * Gets the address of a transport. It will be
826 * #NULL for a server-side transport.
828 * @param transport the transport
829 * @returns transport's address
832 _dbus_transport_get_address (DBusTransport *transport)
834 return transport->address;
838 * Gets the id of the server we are connected to (see
839 * dbus_server_get_id()). Only works on client side.
841 * @param transport the transport
842 * @returns transport's server's id or #NULL if we are the server side
845 _dbus_transport_get_server_id (DBusTransport *transport)
847 if (transport->is_server)
849 else if (transport->authenticated)
850 return _dbus_auth_get_guid_from_server (transport->auth);
852 return transport->expected_guid;
856 * Handles a watch by reading data, writing data, or disconnecting
857 * the transport, as appropriate for the given condition.
859 * @param transport the transport.
860 * @param watch the watch.
861 * @param condition the current state of the watched file descriptor.
862 * @returns #FALSE if not enough memory to fully handle the watch
865 _dbus_transport_handle_watch (DBusTransport *transport,
867 unsigned int condition)
871 _dbus_assert (transport->vtable->handle_watch != NULL);
873 if (transport->disconnected)
876 if (dbus_watch_get_socket (watch) < 0)
878 _dbus_warn_check_failed ("Tried to handle an invalidated watch; this watch should have been removed\n");
882 _dbus_watch_sanitize_condition (watch, &condition);
884 _dbus_transport_ref (transport);
885 _dbus_watch_ref (watch);
886 retval = (* transport->vtable->handle_watch) (transport, watch, condition);
887 _dbus_watch_unref (watch);
888 _dbus_transport_unref (transport);
894 * Sets the connection using this transport. Allows the transport
895 * to add watches to the connection, queue incoming messages,
896 * and pull outgoing messages.
898 * @param transport the transport.
899 * @param connection the connection.
900 * @returns #FALSE if not enough memory
903 _dbus_transport_set_connection (DBusTransport *transport,
904 DBusConnection *connection)
906 _dbus_assert (transport->vtable->connection_set != NULL);
907 _dbus_assert (transport->connection == NULL);
909 transport->connection = connection;
911 _dbus_transport_ref (transport);
912 if (!(* transport->vtable->connection_set) (transport))
913 transport->connection = NULL;
914 _dbus_transport_unref (transport);
916 return transport->connection != NULL;
920 * Get the socket file descriptor, if any.
922 * @param transport the transport
923 * @param fd_p pointer to fill in with the descriptor
924 * @returns #TRUE if a descriptor was available
927 _dbus_transport_get_socket_fd (DBusTransport *transport,
932 if (transport->vtable->get_socket_fd == NULL)
935 if (transport->disconnected)
938 _dbus_transport_ref (transport);
940 retval = (* transport->vtable->get_socket_fd) (transport,
943 _dbus_transport_unref (transport);
949 * Performs a single poll()/select() on the transport's file
950 * descriptors and then reads/writes data as appropriate,
951 * queueing incoming messages and sending outgoing messages.
952 * This is the backend for _dbus_connection_do_iteration().
953 * See _dbus_connection_do_iteration() for full details.
955 * @param transport the transport.
956 * @param flags indicates whether to read or write, and whether to block.
957 * @param timeout_milliseconds if blocking, timeout or -1 for no timeout.
960 _dbus_transport_do_iteration (DBusTransport *transport,
962 int timeout_milliseconds)
964 _dbus_assert (transport->vtable->do_iteration != NULL);
966 _dbus_verbose ("Transport iteration flags 0x%x timeout %d connected = %d\n",
967 flags, timeout_milliseconds, !transport->disconnected);
969 if ((flags & (DBUS_ITERATION_DO_WRITING |
970 DBUS_ITERATION_DO_READING)) == 0)
971 return; /* Nothing to do */
973 if (transport->disconnected)
976 _dbus_transport_ref (transport);
977 (* transport->vtable->do_iteration) (transport, flags,
978 timeout_milliseconds);
979 _dbus_transport_unref (transport);
981 _dbus_verbose ("end\n");
985 recover_unused_bytes (DBusTransport *transport)
987 if (_dbus_auth_needs_decoding (transport->auth))
989 DBusString plaintext;
990 const DBusString *encoded;
994 if (!_dbus_string_init (&plaintext))
997 _dbus_auth_get_unused_bytes (transport->auth,
1000 if (!_dbus_auth_decode_data (transport->auth,
1001 encoded, &plaintext))
1003 _dbus_string_free (&plaintext);
1007 _dbus_message_loader_get_buffer (transport->loader,
1010 orig_len = _dbus_string_get_length (buffer);
1012 if (!_dbus_string_move (&plaintext, 0, buffer,
1015 _dbus_string_free (&plaintext);
1019 _dbus_verbose (" %d unused bytes sent to message loader\n",
1020 _dbus_string_get_length (buffer) -
1023 _dbus_message_loader_return_buffer (transport->loader,
1025 _dbus_string_get_length (buffer) -
1028 _dbus_auth_delete_unused_bytes (transport->auth);
1030 _dbus_string_free (&plaintext);
1034 const DBusString *bytes;
1037 dbus_bool_t succeeded;
1039 _dbus_message_loader_get_buffer (transport->loader,
1042 orig_len = _dbus_string_get_length (buffer);
1044 _dbus_auth_get_unused_bytes (transport->auth,
1048 if (!_dbus_string_copy (bytes, 0, buffer, _dbus_string_get_length (buffer)))
1051 _dbus_verbose (" %d unused bytes sent to message loader\n",
1052 _dbus_string_get_length (buffer) -
1055 _dbus_message_loader_return_buffer (transport->loader,
1057 _dbus_string_get_length (buffer) -
1061 _dbus_auth_delete_unused_bytes (transport->auth);
1069 _dbus_verbose ("Not enough memory to transfer unused bytes from auth conversation\n");
1074 * Reports our current dispatch status (whether there's buffered
1075 * data to be queued as messages, or not, or we need memory).
1077 * @param transport the transport
1078 * @returns current status
1081 _dbus_transport_get_dispatch_status (DBusTransport *transport)
1083 if (_dbus_counter_get_size_value (transport->live_messages) >= transport->max_live_messages_size ||
1084 _dbus_counter_get_unix_fd_value (transport->live_messages) >= transport->max_live_messages_unix_fds)
1085 return DBUS_DISPATCH_COMPLETE; /* complete for now */
1087 if (!_dbus_transport_get_is_authenticated (transport))
1089 if (_dbus_auth_do_work (transport->auth) ==
1090 DBUS_AUTH_STATE_WAITING_FOR_MEMORY)
1091 return DBUS_DISPATCH_NEED_MEMORY;
1092 else if (!_dbus_transport_get_is_authenticated (transport))
1093 return DBUS_DISPATCH_COMPLETE;
1096 if (!transport->unused_bytes_recovered &&
1097 !recover_unused_bytes (transport))
1098 return DBUS_DISPATCH_NEED_MEMORY;
1100 transport->unused_bytes_recovered = TRUE;
1102 if (!_dbus_message_loader_queue_messages (transport->loader))
1103 return DBUS_DISPATCH_NEED_MEMORY;
1105 if (_dbus_message_loader_peek_message (transport->loader) != NULL)
1106 return DBUS_DISPATCH_DATA_REMAINS;
1108 return DBUS_DISPATCH_COMPLETE;
1112 * Processes data we've read while handling a watch, potentially
1113 * converting some of it to messages and queueing those messages on
1116 * @param transport the transport
1117 * @returns #TRUE if we had enough memory to queue all messages
1120 _dbus_transport_queue_messages (DBusTransport *transport)
1122 DBusDispatchStatus status;
1125 _dbus_verbose ("_dbus_transport_queue_messages()\n");
1128 /* Queue any messages */
1129 while ((status = _dbus_transport_get_dispatch_status (transport)) == DBUS_DISPATCH_DATA_REMAINS)
1131 DBusMessage *message;
1134 link = _dbus_message_loader_pop_message_link (transport->loader);
1135 _dbus_assert (link != NULL);
1137 message = link->data;
1139 _dbus_verbose ("queueing received message %p\n", message);
1141 if (!_dbus_message_add_counter (message, transport->live_messages))
1143 _dbus_message_loader_putback_message_link (transport->loader,
1145 status = DBUS_DISPATCH_NEED_MEMORY;
1150 /* We didn't call the notify function when we added the counter, so
1151 * catch up now. Since we have the connection's lock, it's desirable
1152 * that we bypass the notify function and call this virtual method
1154 if (transport->vtable->live_messages_changed)
1155 (* transport->vtable->live_messages_changed) (transport);
1157 /* pass ownership of link and message ref to connection */
1158 _dbus_connection_queue_received_message_link (transport->connection,
1163 if (_dbus_message_loader_get_is_corrupted (transport->loader))
1165 _dbus_verbose ("Corrupted message stream, disconnecting\n");
1166 _dbus_transport_disconnect (transport);
1169 return status != DBUS_DISPATCH_NEED_MEMORY;
1173 * See dbus_connection_set_max_message_size().
1175 * @param transport the transport
1176 * @param size the max size of a single message
1179 _dbus_transport_set_max_message_size (DBusTransport *transport,
1182 _dbus_message_loader_set_max_message_size (transport->loader, size);
1186 * See dbus_connection_set_max_message_unix_fds().
1188 * @param transport the transport
1189 * @param n the max number of unix fds of a single message
1192 _dbus_transport_set_max_message_unix_fds (DBusTransport *transport,
1195 _dbus_message_loader_set_max_message_unix_fds (transport->loader, n);
1199 * See dbus_connection_get_max_message_size().
1201 * @param transport the transport
1202 * @returns max message size
1205 _dbus_transport_get_max_message_size (DBusTransport *transport)
1207 return _dbus_message_loader_get_max_message_size (transport->loader);
1211 * See dbus_connection_get_max_message_unix_fds().
1213 * @param transport the transport
1214 * @returns max message unix fds
1217 _dbus_transport_get_max_message_unix_fds (DBusTransport *transport)
1219 return _dbus_message_loader_get_max_message_unix_fds (transport->loader);
1223 * See dbus_connection_set_max_received_size().
1225 * @param transport the transport
1226 * @param size the max size of all incoming messages
1229 _dbus_transport_set_max_received_size (DBusTransport *transport,
1232 transport->max_live_messages_size = size;
1233 _dbus_counter_set_notify (transport->live_messages,
1234 transport->max_live_messages_size,
1235 transport->max_live_messages_unix_fds,
1236 live_messages_notify,
1241 * See dbus_connection_set_max_received_unix_fds().
1243 * @param transport the transport
1244 * @param n the max unix fds of all incoming messages
1247 _dbus_transport_set_max_received_unix_fds (DBusTransport *transport,
1250 transport->max_live_messages_unix_fds = n;
1251 _dbus_counter_set_notify (transport->live_messages,
1252 transport->max_live_messages_size,
1253 transport->max_live_messages_unix_fds,
1254 live_messages_notify,
1259 * See dbus_connection_get_max_received_size().
1261 * @param transport the transport
1262 * @returns max bytes for all live messages
1265 _dbus_transport_get_max_received_size (DBusTransport *transport)
1267 return transport->max_live_messages_size;
1271 * See dbus_connection_set_max_received_unix_fds().
1273 * @param transport the transport
1274 * @returns max unix fds for all live messages
1277 _dbus_transport_get_max_received_unix_fds (DBusTransport *transport)
1279 return transport->max_live_messages_unix_fds;
1283 * See dbus_connection_get_unix_user().
1285 * @param transport the transport
1286 * @param uid return location for the user ID
1287 * @returns #TRUE if uid is filled in with a valid user ID
1290 _dbus_transport_get_unix_user (DBusTransport *transport,
1293 DBusCredentials *auth_identity;
1295 *uid = _DBUS_INT32_MAX; /* better than some root or system user in
1296 * case of bugs in the caller. Caller should
1297 * never use this value on purpose, however.
1300 if (!transport->authenticated)
1303 auth_identity = _dbus_auth_get_identity (transport->auth);
1305 if (_dbus_credentials_include (auth_identity,
1306 DBUS_CREDENTIAL_UNIX_USER_ID))
1308 *uid = _dbus_credentials_get_unix_uid (auth_identity);
1316 * See dbus_connection_get_unix_process_id().
1318 * @param transport the transport
1319 * @param pid return location for the process ID
1320 * @returns #TRUE if uid is filled in with a valid process ID
1323 _dbus_transport_get_unix_process_id (DBusTransport *transport,
1326 DBusCredentials *auth_identity;
1328 *pid = DBUS_PID_UNSET; /* Caller should never use this value on purpose,
1329 * but we set it to a safe number, INT_MAX,
1330 * just to root out possible bugs in bad callers.
1333 if (!transport->authenticated)
1336 auth_identity = _dbus_auth_get_identity (transport->auth);
1338 if (_dbus_credentials_include (auth_identity,
1339 DBUS_CREDENTIAL_UNIX_PROCESS_ID))
1341 *pid = _dbus_credentials_get_unix_pid (auth_identity);
1349 * See dbus_connection_get_adt_audit_session_data().
1351 * @param transport the transport
1352 * @param data return location for the ADT audit data
1353 * @param data_size return length of audit data
1354 * @returns #TRUE if audit data is filled in with a valid ucred
1357 _dbus_transport_get_adt_audit_session_data (DBusTransport *transport,
1361 DBusCredentials *auth_identity;
1366 if (!transport->authenticated)
1369 auth_identity = _dbus_auth_get_identity (transport->auth);
1371 if (_dbus_credentials_include (auth_identity,
1372 DBUS_CREDENTIAL_ADT_AUDIT_DATA_ID))
1374 *data = (void *) _dbus_credentials_get_adt_audit_data (auth_identity);
1375 *data_size = _dbus_credentials_get_adt_audit_data_size (auth_identity);
1383 * See dbus_connection_set_unix_user_function().
1385 * @param transport the transport
1386 * @param function the predicate
1387 * @param data data to pass to the predicate
1388 * @param free_data_function function to free the data
1389 * @param old_data the old user data to be freed
1390 * @param old_free_data_function old free data function to free it with
1393 _dbus_transport_set_unix_user_function (DBusTransport *transport,
1394 DBusAllowUnixUserFunction function,
1396 DBusFreeFunction free_data_function,
1398 DBusFreeFunction *old_free_data_function)
1400 *old_data = transport->unix_user_data;
1401 *old_free_data_function = transport->free_unix_user_data;
1403 transport->unix_user_function = function;
1404 transport->unix_user_data = data;
1405 transport->free_unix_user_data = free_data_function;
1409 * See dbus_connection_get_windows_user().
1411 * @param transport the transport
1412 * @param windows_sid_p return location for the user ID
1413 * @returns #TRUE if user is available; the returned value may still be #NULL if no memory to copy it
1416 _dbus_transport_get_windows_user (DBusTransport *transport,
1417 char **windows_sid_p)
1419 DBusCredentials *auth_identity;
1421 *windows_sid_p = NULL;
1423 if (!transport->authenticated)
1426 auth_identity = _dbus_auth_get_identity (transport->auth);
1428 if (_dbus_credentials_include (auth_identity,
1429 DBUS_CREDENTIAL_WINDOWS_SID))
1431 /* If no memory, we are supposed to return TRUE and set NULL */
1432 *windows_sid_p = _dbus_strdup (_dbus_credentials_get_windows_sid (auth_identity));
1441 * See dbus_connection_set_windows_user_function().
1443 * @param transport the transport
1444 * @param function the predicate
1445 * @param data data to pass to the predicate
1446 * @param free_data_function function to free the data
1447 * @param old_data the old user data to be freed
1448 * @param old_free_data_function old free data function to free it with
1452 _dbus_transport_set_windows_user_function (DBusTransport *transport,
1453 DBusAllowWindowsUserFunction function,
1455 DBusFreeFunction free_data_function,
1457 DBusFreeFunction *old_free_data_function)
1459 *old_data = transport->windows_user_data;
1460 *old_free_data_function = transport->free_windows_user_data;
1462 transport->windows_user_function = function;
1463 transport->windows_user_data = data;
1464 transport->free_windows_user_data = free_data_function;
1468 * Sets the SASL authentication mechanisms supported by this transport.
1470 * @param transport the transport
1471 * @param mechanisms the #NULL-terminated array of mechanisms
1473 * @returns #FALSE if no memory
1476 _dbus_transport_set_auth_mechanisms (DBusTransport *transport,
1477 const char **mechanisms)
1479 return _dbus_auth_set_mechanisms (transport->auth, mechanisms);
1483 * See dbus_connection_set_allow_anonymous()
1485 * @param transport the transport
1486 * @param value #TRUE to allow anonymous connection
1489 _dbus_transport_set_allow_anonymous (DBusTransport *transport,
1492 transport->allow_anonymous = value != FALSE;
1495 #ifdef DBUS_ENABLE_STATS
1497 _dbus_transport_get_stats (DBusTransport *transport,
1498 dbus_uint32_t *queue_bytes,
1499 dbus_uint32_t *queue_fds,
1500 dbus_uint32_t *peak_queue_bytes,
1501 dbus_uint32_t *peak_queue_fds)
1503 if (queue_bytes != NULL)
1504 *queue_bytes = _dbus_counter_get_size_value (transport->live_messages);
1506 if (queue_fds != NULL)
1507 *queue_fds = _dbus_counter_get_unix_fd_value (transport->live_messages);
1509 if (peak_queue_bytes != NULL)
1510 *peak_queue_bytes = _dbus_counter_get_peak_size_value (transport->live_messages);
1512 if (peak_queue_fds != NULL)
1513 *peak_queue_fds = _dbus_counter_get_peak_unix_fd_value (transport->live_messages);
1515 #endif /* DBUS_ENABLE_STATS */