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-authorization.h"
32 #include "dbus-address.h"
33 #include "dbus-credentials.h"
34 #include "dbus-mainloop.h"
35 #include "dbus-message.h"
36 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
37 #include "dbus-server-debug-pipe.h"
41 * @defgroup DBusTransport DBusTransport object
42 * @ingroup DBusInternals
43 * @brief "Backend" for a DBusConnection.
45 * Types and functions related to DBusTransport. A transport is an
46 * abstraction that can send and receive data via various kinds of
47 * network connections or other IPC mechanisms.
53 * @typedef DBusTransport
55 * Opaque object representing a way message stream.
56 * DBusTransport abstracts various kinds of actual
57 * transport mechanism, such as different network protocols,
58 * or encryption schemes.
62 live_messages_notify (DBusCounter *counter,
65 DBusTransport *transport = user_data;
67 _dbus_transport_ref (transport);
70 _dbus_verbose ("Size counter value is now %d\n",
71 (int) _dbus_counter_get_size_value (counter));
72 _dbus_verbose ("Unix FD counter value is now %d\n",
73 (int) _dbus_counter_get_unix_fd_value (counter));
76 /* disable or re-enable the read watch for the transport if
79 if (transport->vtable->live_messages_changed)
81 _dbus_connection_lock (transport->connection);
82 (* transport->vtable->live_messages_changed) (transport);
83 _dbus_connection_unlock (transport->connection);
86 _dbus_transport_unref (transport);
90 * Initializes the base class members of DBusTransport. Chained up to
91 * by subclasses in their constructor. The server GUID is the
92 * globally unique ID for the server creating this connection
93 * and will be #NULL for the client side of a connection. The GUID
96 * @param transport the transport being created.
97 * @param vtable the subclass vtable.
98 * @param server_guid non-#NULL if this transport is on the server side of a connection
99 * @param address the address of the transport
100 * @returns #TRUE on success.
103 _dbus_transport_init_base (DBusTransport *transport,
104 const DBusTransportVTable *vtable,
105 const DBusString *server_guid,
106 const DBusString *address)
108 DBusMessageLoader *loader;
110 DBusAuthorization *authorization = NULL; /* non-NULL only if is_server=TRUE */
111 DBusCounter *counter;
113 DBusCredentials *creds;
115 loader = _dbus_message_loader_new ();
119 if (server_guid != NULL)
121 authorization = _dbus_authorization_new ();
122 if (authorization == NULL)
124 _dbus_message_loader_unref (loader);
125 return FALSE; /* OOM */
128 auth = _dbus_auth_server_new (server_guid, authorization);
132 auth = _dbus_auth_client_new ();
136 if (authorization != NULL)
137 _dbus_authorization_free (authorization);
138 _dbus_message_loader_unref (loader);
142 counter = _dbus_counter_new ();
145 _dbus_auth_free (auth);
146 if (authorization != NULL)
147 _dbus_authorization_free (authorization);
148 _dbus_message_loader_unref (loader);
152 creds = _dbus_credentials_new ();
155 _dbus_counter_unref (counter);
156 _dbus_auth_free (auth);
157 if (authorization != NULL)
158 _dbus_authorization_free (authorization);
159 _dbus_message_loader_unref (loader);
163 if (server_guid != NULL)
165 _dbus_assert (address == NULL);
170 _dbus_assert (address != NULL);
172 if (!_dbus_string_copy_data (address, &address_copy))
174 _dbus_credentials_unref (creds);
175 _dbus_counter_unref (counter);
176 _dbus_auth_free (auth);
177 if (authorization != NULL)
178 _dbus_authorization_free (authorization);
179 _dbus_message_loader_unref (loader);
184 transport->refcount = 1;
185 transport->vtable = vtable;
186 transport->loader = loader;
187 transport->authorization = authorization;
188 transport->auth = auth;
189 transport->live_messages = counter;
190 transport->authenticated = FALSE;
191 transport->disconnected = FALSE;
192 transport->is_server = (server_guid != NULL);
193 transport->send_credentials_pending = !transport->is_server;
194 transport->receive_credentials_pending = transport->is_server;
195 transport->address = address_copy;
197 transport->expected_guid = NULL;
199 /* Try to default to something that won't totally hose the system,
200 * but doesn't impose too much of a limitation.
202 transport->max_live_messages_size = _DBUS_ONE_MEGABYTE * 63;
204 /* On Linux RLIMIT_NOFILE defaults to 1024, so allowing 4096 fds live
205 should be more than enough */
206 transport->max_live_messages_unix_fds = 4096;
208 /* credentials read from socket if any */
209 transport->credentials = creds;
211 _dbus_counter_set_notify (transport->live_messages,
212 transport->max_live_messages_size,
213 transport->max_live_messages_unix_fds,
214 live_messages_notify,
217 if (transport->address)
218 _dbus_verbose ("Initialized transport on address %s\n", transport->address);
220 /* we can have authorization data set only in server mode */
221 _dbus_assert ((transport->is_server && transport->authorization != NULL) ||
222 (!transport->is_server && transport->authorization == NULL));
228 * Finalizes base class members of DBusTransport.
229 * Chained up to from subclass finalizers.
231 * @param transport the transport.
234 _dbus_transport_finalize_base (DBusTransport *transport)
236 if (!transport->disconnected)
237 _dbus_transport_disconnect (transport);
239 _dbus_message_loader_unref (transport->loader);
240 _dbus_auth_free (transport->auth);
241 if (transport->authorization)
242 _dbus_authorization_free (transport->authorization);
243 _dbus_counter_set_notify (transport->live_messages,
245 _dbus_counter_unref (transport->live_messages);
246 dbus_free (transport->address);
247 dbus_free (transport->expected_guid);
248 if (transport->credentials)
249 _dbus_credentials_unref (transport->credentials);
254 * Verifies if a given D-Bus address is a valid address
255 * by attempting to connect to it. If it is, returns the
256 * opened DBusTransport object. If it isn't, returns #NULL
259 * @param address the address to be checked.
260 * @param error address where an error can be returned.
261 * @returns a new transport, or #NULL on failure.
263 static DBusTransport*
264 check_address (const char *address, DBusError *error)
266 DBusAddressEntry **entries;
267 DBusTransport *transport = NULL;
270 _dbus_assert (address != NULL);
272 if (!dbus_parse_address (address, &entries, &len, error))
273 return NULL; /* not a valid address */
275 for (i = 0; i < len; i++)
277 transport = _dbus_transport_open (entries[i], error);
278 if (transport != NULL)
282 dbus_address_entries_free (entries);
287 * Creates a new transport for the "autostart" method.
288 * This creates a client-side of a transport.
290 * @param scope scope of autolaunch (Windows only)
291 * @param error address where an error can be returned.
292 * @returns a new transport, or #NULL on failure.
294 static DBusTransport*
295 _dbus_transport_new_for_autolaunch (const char *scope, DBusError *error)
298 DBusTransport *result = NULL;
300 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
302 if (!_dbus_string_init (&address))
304 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
308 if (!_dbus_get_autolaunch_address (scope, &address, error))
310 _DBUS_ASSERT_ERROR_IS_SET (error);
314 result = check_address (_dbus_string_get_const_data (&address), error);
316 _DBUS_ASSERT_ERROR_IS_SET (error);
318 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
321 _dbus_string_free (&address);
325 static DBusTransportOpenResult
326 _dbus_transport_open_autolaunch (DBusAddressEntry *entry,
327 DBusTransport **transport_p,
332 method = dbus_address_entry_get_method (entry);
333 _dbus_assert (method != NULL);
335 if (strcmp (method, "autolaunch") == 0)
337 const char *scope = dbus_address_entry_get_value (entry, "scope");
339 *transport_p = _dbus_transport_new_for_autolaunch (scope, error);
341 if (*transport_p == NULL)
343 _DBUS_ASSERT_ERROR_IS_SET (error);
344 return DBUS_TRANSPORT_OPEN_DID_NOT_CONNECT;
348 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
349 return DBUS_TRANSPORT_OPEN_OK;
354 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
355 return DBUS_TRANSPORT_OPEN_NOT_HANDLED;
359 static const struct {
360 DBusTransportOpenResult (* func) (DBusAddressEntry *entry,
361 DBusTransport **transport_p,
364 { _dbus_transport_open_socket },
365 { _dbus_transport_open_platform_specific },
366 { _dbus_transport_open_autolaunch }
367 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
368 , { _dbus_transport_open_debug_pipe }
373 * Try to open a new transport for the given address entry. (This
374 * opens a client-side-of-the-connection transport.)
376 * @param entry the address entry
377 * @param error location to store reason for failure.
378 * @returns new transport of #NULL on failure.
381 _dbus_transport_open (DBusAddressEntry *entry,
384 DBusTransport *transport;
385 const char *expected_guid_orig;
388 DBusError tmp_error = DBUS_ERROR_INIT;
390 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
393 expected_guid_orig = dbus_address_entry_get_value (entry, "guid");
394 expected_guid = _dbus_strdup (expected_guid_orig);
396 if (expected_guid_orig != NULL && expected_guid == NULL)
398 _DBUS_SET_OOM (error);
402 for (i = 0; i < (int) _DBUS_N_ELEMENTS (open_funcs); ++i)
404 DBusTransportOpenResult result;
406 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
407 result = (* open_funcs[i].func) (entry, &transport, &tmp_error);
411 case DBUS_TRANSPORT_OPEN_OK:
412 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
415 case DBUS_TRANSPORT_OPEN_NOT_HANDLED:
416 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
417 /* keep going through the loop of open funcs */
419 case DBUS_TRANSPORT_OPEN_BAD_ADDRESS:
420 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
423 case DBUS_TRANSPORT_OPEN_DID_NOT_CONNECT:
424 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
432 if (transport == NULL)
434 if (!dbus_error_is_set (&tmp_error))
435 _dbus_set_bad_address (&tmp_error,
437 "Unknown address type (examples of valid types are \"tcp\" and on UNIX \"unix\")");
439 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
440 dbus_move_error(&tmp_error, error);
441 dbus_free (expected_guid);
445 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
447 /* In the case of autostart the initial guid is NULL
448 * and the autostart transport recursively calls
449 * _dbus_open_transport wich returns a transport
450 * with a guid. That guid is the definitive one.
452 * FIXME: if more transports are added they may have
453 * an effect on the expected_guid semantics (i.e.
454 * expected_guid and transport->expected_guid may
455 * both have values). This is very unlikely though
456 * we should either throw asserts here for those
457 * corner cases or refactor the code so it is
458 * clearer on what is expected and what is not
461 transport->expected_guid = expected_guid;
468 * Increments the reference count for the transport.
470 * @param transport the transport.
471 * @returns the transport.
474 _dbus_transport_ref (DBusTransport *transport)
476 _dbus_assert (transport->refcount > 0);
478 transport->refcount += 1;
484 * Decrements the reference count for the transport.
485 * Disconnects and finalizes the transport if
486 * the reference count reaches zero.
488 * @param transport the transport.
491 _dbus_transport_unref (DBusTransport *transport)
493 _dbus_assert (transport != NULL);
494 _dbus_assert (transport->refcount > 0);
496 transport->refcount -= 1;
497 if (transport->refcount == 0)
499 _dbus_verbose ("finalizing\n");
501 _dbus_assert (transport->vtable->finalize != NULL);
503 (* transport->vtable->finalize) (transport);
508 * Closes our end of the connection to a remote application. Further
509 * attempts to use this transport will fail. Only the first call to
510 * _dbus_transport_disconnect() will have an effect.
512 * @param transport the transport.
516 _dbus_transport_disconnect (DBusTransport *transport)
518 _dbus_verbose ("start\n");
520 _dbus_assert (transport->vtable->disconnect != NULL);
522 if (transport->disconnected)
525 (* transport->vtable->disconnect) (transport);
527 transport->disconnected = TRUE;
529 _dbus_verbose ("end\n");
533 * Returns #TRUE if the transport has not been disconnected.
534 * Disconnection can result from _dbus_transport_disconnect()
535 * or because the server drops its end of the connection.
537 * @param transport the transport.
538 * @returns whether we're connected
541 _dbus_transport_get_is_connected (DBusTransport *transport)
543 return !transport->disconnected;
547 * Returns #TRUE if we have been authenticated. It will return #TRUE even if
548 * the transport is now disconnected, but was ever authenticated before
551 * This replaces the older _dbus_transport_get_is_authenticated() which
554 * @param transport the transport
555 * @returns whether we're authenticated
558 _dbus_transport_peek_is_authenticated (DBusTransport *transport)
560 return transport->authenticated;
564 * Returns #TRUE if we have been authenticated. It will return #TRUE even if
565 * the transport is now disconnected, but was ever authenticated before
568 * If we have not finished authenticating, but we have enough buffered input
569 * to finish the job, then this function will do so before it returns.
571 * This used to be called _dbus_transport_get_is_authenticated(), but that
572 * name seems inappropriate for a function with side-effects.
574 * @todo we drop connection->mutex when calling the unix_user_function,
575 * and windows_user_function, which may not be safe really.
577 * @param transport the transport
578 * @returns whether we're authenticated
581 _dbus_transport_try_to_authenticate (DBusTransport *transport)
583 if (transport->authenticated)
587 dbus_bool_t maybe_authenticated;
589 if (transport->disconnected)
592 /* paranoia ref since we call user callbacks sometimes */
593 _dbus_connection_ref_unlocked (transport->connection);
595 maybe_authenticated =
596 (!(transport->send_credentials_pending ||
597 transport->receive_credentials_pending));
599 if (maybe_authenticated)
601 switch (_dbus_auth_do_work (transport->auth))
603 case DBUS_AUTH_STATE_AUTHENTICATED:
604 /* leave as maybe_authenticated */
607 maybe_authenticated = FALSE;
611 /* If we're the client, verify the GUID
613 if (maybe_authenticated && !transport->is_server)
615 const char *server_guid;
617 server_guid = _dbus_auth_get_guid_from_server (transport->auth);
618 _dbus_assert (server_guid != NULL);
620 if (transport->expected_guid &&
621 strcmp (transport->expected_guid, server_guid) != 0)
623 _dbus_verbose ("Client expected GUID '%s' and we got '%s' from the server\n",
624 transport->expected_guid, server_guid);
625 _dbus_transport_disconnect (transport);
626 _dbus_connection_unref_unlocked (transport->connection);
631 /* If we're the server, see if we want to allow this identity to proceed.
633 if (maybe_authenticated && transport->is_server)
635 DBusCredentials *auth_identity;
637 auth_identity = _dbus_auth_get_identity (transport->auth);
638 _dbus_assert (auth_identity != NULL);
640 /* If we have an authenticated user, delegate deciding whether auth
641 * credentials are good enough to the app */
642 if (!_dbus_authorization_do_authorization (transport->authorization, auth_identity))
644 _dbus_transport_disconnect (transport);
645 maybe_authenticated = FALSE;
649 transport->authenticated = maybe_authenticated;
651 _dbus_connection_unref_unlocked (transport->connection);
652 return maybe_authenticated;
657 * See dbus_connection_get_is_anonymous().
659 * @param transport the transport
660 * @returns #TRUE if not authenticated or authenticated as anonymous
663 _dbus_transport_get_is_anonymous (DBusTransport *transport)
665 DBusCredentials *auth_identity;
667 if (!transport->authenticated)
670 auth_identity = _dbus_auth_get_identity (transport->auth);
672 if (_dbus_credentials_are_anonymous (auth_identity))
679 * Returns TRUE if the transport supports sending unix fds.
681 * @param transport the transport
682 * @returns #TRUE if TRUE it is possible to send unix fds across the transport.
685 _dbus_transport_can_pass_unix_fd(DBusTransport *transport)
687 return DBUS_TRANSPORT_CAN_SEND_UNIX_FD(transport);
691 * Gets the address of a transport. It will be
692 * #NULL for a server-side transport.
694 * @param transport the transport
695 * @returns transport's address
698 _dbus_transport_get_address (DBusTransport *transport)
700 return transport->address;
704 * Gets the id of the server we are connected to (see
705 * dbus_server_get_id()). Only works on client side.
707 * @param transport the transport
708 * @returns transport's server's id or #NULL if we are the server side
711 _dbus_transport_get_server_id (DBusTransport *transport)
713 if (transport->is_server)
715 else if (transport->authenticated)
716 return _dbus_auth_get_guid_from_server (transport->auth);
718 return transport->expected_guid;
722 * Handles a watch by reading data, writing data, or disconnecting
723 * the transport, as appropriate for the given condition.
725 * @param transport the transport.
726 * @param watch the watch.
727 * @param condition the current state of the watched file descriptor.
728 * @returns #FALSE if not enough memory to fully handle the watch
731 _dbus_transport_handle_watch (DBusTransport *transport,
733 unsigned int condition)
737 _dbus_assert (transport->vtable->handle_watch != NULL);
739 if (transport->disconnected)
742 if (dbus_watch_get_socket (watch) < 0)
744 _dbus_warn_check_failed ("Tried to handle an invalidated watch; this watch should have been removed\n");
748 _dbus_watch_sanitize_condition (watch, &condition);
750 _dbus_transport_ref (transport);
751 _dbus_watch_ref (watch);
752 retval = (* transport->vtable->handle_watch) (transport, watch, condition);
753 _dbus_watch_unref (watch);
754 _dbus_transport_unref (transport);
760 * Sets the connection using this transport. Allows the transport
761 * to add watches to the connection, queue incoming messages,
762 * and pull outgoing messages.
764 * @param transport the transport.
765 * @param connection the connection.
766 * @returns #FALSE if not enough memory
769 _dbus_transport_set_connection (DBusTransport *transport,
770 DBusConnection *connection)
772 _dbus_assert (transport->vtable->connection_set != NULL);
773 _dbus_assert (transport->connection == NULL);
775 transport->connection = connection;
776 if (transport->is_server)
777 _dbus_authorization_set_connection (transport->authorization, connection);
779 _dbus_transport_ref (transport);
780 if (!(* transport->vtable->connection_set) (transport))
781 transport->connection = NULL;
782 _dbus_transport_unref (transport);
784 return transport->connection != NULL;
788 * Get the socket file descriptor, if any.
790 * @param transport the transport
791 * @param fd_p pointer to fill in with the descriptor
792 * @returns #TRUE if a descriptor was available
795 _dbus_transport_get_socket_fd (DBusTransport *transport,
800 if (transport->vtable->get_socket_fd == NULL)
803 if (transport->disconnected)
806 _dbus_transport_ref (transport);
808 retval = (* transport->vtable->get_socket_fd) (transport,
811 _dbus_transport_unref (transport);
817 * Performs a single poll()/select() on the transport's file
818 * descriptors and then reads/writes data as appropriate,
819 * queueing incoming messages and sending outgoing messages.
820 * This is the backend for _dbus_connection_do_iteration().
821 * See _dbus_connection_do_iteration() for full details.
823 * @param transport the transport.
824 * @param flags indicates whether to read or write, and whether to block.
825 * @param timeout_milliseconds if blocking, timeout or -1 for no timeout.
828 _dbus_transport_do_iteration (DBusTransport *transport,
830 int timeout_milliseconds)
832 _dbus_assert (transport->vtable->do_iteration != NULL);
834 _dbus_verbose ("Transport iteration flags 0x%x timeout %d connected = %d\n",
835 flags, timeout_milliseconds, !transport->disconnected);
837 if ((flags & (DBUS_ITERATION_DO_WRITING |
838 DBUS_ITERATION_DO_READING)) == 0)
839 return; /* Nothing to do */
841 if (transport->disconnected)
844 _dbus_transport_ref (transport);
845 (* transport->vtable->do_iteration) (transport, flags,
846 timeout_milliseconds);
847 _dbus_transport_unref (transport);
849 _dbus_verbose ("end\n");
853 recover_unused_bytes (DBusTransport *transport)
855 if (_dbus_auth_needs_decoding (transport->auth))
857 DBusString plaintext;
858 const DBusString *encoded;
862 if (!_dbus_string_init (&plaintext))
865 _dbus_auth_get_unused_bytes (transport->auth,
868 if (!_dbus_auth_decode_data (transport->auth,
869 encoded, &plaintext))
871 _dbus_string_free (&plaintext);
875 _dbus_message_loader_get_buffer (transport->loader,
878 orig_len = _dbus_string_get_length (buffer);
880 if (!_dbus_string_move (&plaintext, 0, buffer,
883 _dbus_string_free (&plaintext);
887 _dbus_verbose (" %d unused bytes sent to message loader\n",
888 _dbus_string_get_length (buffer) -
891 _dbus_message_loader_return_buffer (transport->loader,
893 _dbus_string_get_length (buffer) -
896 _dbus_auth_delete_unused_bytes (transport->auth);
898 _dbus_string_free (&plaintext);
902 const DBusString *bytes;
905 dbus_bool_t succeeded;
907 _dbus_message_loader_get_buffer (transport->loader,
910 orig_len = _dbus_string_get_length (buffer);
912 _dbus_auth_get_unused_bytes (transport->auth,
916 if (!_dbus_string_copy (bytes, 0, buffer, _dbus_string_get_length (buffer)))
919 _dbus_verbose (" %d unused bytes sent to message loader\n",
920 _dbus_string_get_length (buffer) -
923 _dbus_message_loader_return_buffer (transport->loader,
925 _dbus_string_get_length (buffer) -
929 _dbus_auth_delete_unused_bytes (transport->auth);
937 _dbus_verbose ("Not enough memory to transfer unused bytes from auth conversation\n");
942 * Reports our current dispatch status (whether there's buffered
943 * data to be queued as messages, or not, or we need memory).
945 * @param transport the transport
946 * @returns current status
949 _dbus_transport_get_dispatch_status (DBusTransport *transport)
951 if (_dbus_counter_get_size_value (transport->live_messages) >= transport->max_live_messages_size ||
952 _dbus_counter_get_unix_fd_value (transport->live_messages) >= transport->max_live_messages_unix_fds)
953 return DBUS_DISPATCH_COMPLETE; /* complete for now */
955 if (!_dbus_transport_try_to_authenticate (transport))
957 if (_dbus_auth_do_work (transport->auth) ==
958 DBUS_AUTH_STATE_WAITING_FOR_MEMORY)
959 return DBUS_DISPATCH_NEED_MEMORY;
960 else if (!_dbus_transport_try_to_authenticate (transport))
961 return DBUS_DISPATCH_COMPLETE;
964 if (!transport->unused_bytes_recovered &&
965 !recover_unused_bytes (transport))
966 return DBUS_DISPATCH_NEED_MEMORY;
968 transport->unused_bytes_recovered = TRUE;
970 if (!_dbus_message_loader_queue_messages (transport->loader))
971 return DBUS_DISPATCH_NEED_MEMORY;
973 if (_dbus_message_loader_peek_message (transport->loader) != NULL)
974 return DBUS_DISPATCH_DATA_REMAINS;
976 return DBUS_DISPATCH_COMPLETE;
980 * Processes data we've read while handling a watch, potentially
981 * converting some of it to messages and queueing those messages on
984 * @param transport the transport
985 * @returns #TRUE if we had enough memory to queue all messages
988 _dbus_transport_queue_messages (DBusTransport *transport)
990 DBusDispatchStatus status;
993 _dbus_verbose ("_dbus_transport_queue_messages()\n");
996 /* Queue any messages */
997 while ((status = _dbus_transport_get_dispatch_status (transport)) == DBUS_DISPATCH_DATA_REMAINS)
999 DBusMessage *message;
1002 link = _dbus_message_loader_pop_message_link (transport->loader);
1003 _dbus_assert (link != NULL);
1005 message = link->data;
1007 _dbus_verbose ("queueing received message %p\n", message);
1009 if (!_dbus_message_add_counter (message, transport->live_messages))
1011 _dbus_message_loader_putback_message_link (transport->loader,
1013 status = DBUS_DISPATCH_NEED_MEMORY;
1018 /* We didn't call the notify function when we added the counter, so
1019 * catch up now. Since we have the connection's lock, it's desirable
1020 * that we bypass the notify function and call this virtual method
1022 if (transport->vtable->live_messages_changed)
1023 (* transport->vtable->live_messages_changed) (transport);
1025 /* pass ownership of link and message ref to connection */
1026 _dbus_connection_queue_received_message_link (transport->connection,
1031 if (_dbus_message_loader_get_is_corrupted (transport->loader))
1033 _dbus_verbose ("Corrupted message stream, disconnecting\n");
1034 _dbus_transport_disconnect (transport);
1037 return status != DBUS_DISPATCH_NEED_MEMORY;
1041 * See dbus_connection_set_max_message_size().
1043 * @param transport the transport
1044 * @param size the max size of a single message
1047 _dbus_transport_set_max_message_size (DBusTransport *transport,
1050 _dbus_message_loader_set_max_message_size (transport->loader, size);
1054 * See dbus_connection_set_max_message_unix_fds().
1056 * @param transport the transport
1057 * @param n the max number of unix fds of a single message
1060 _dbus_transport_set_max_message_unix_fds (DBusTransport *transport,
1063 _dbus_message_loader_set_max_message_unix_fds (transport->loader, n);
1067 * See dbus_connection_get_max_message_size().
1069 * @param transport the transport
1070 * @returns max message size
1073 _dbus_transport_get_max_message_size (DBusTransport *transport)
1075 return _dbus_message_loader_get_max_message_size (transport->loader);
1079 * See dbus_connection_get_max_message_unix_fds().
1081 * @param transport the transport
1082 * @returns max message unix fds
1085 _dbus_transport_get_max_message_unix_fds (DBusTransport *transport)
1087 return _dbus_message_loader_get_max_message_unix_fds (transport->loader);
1091 * See dbus_connection_set_max_received_size().
1093 * @param transport the transport
1094 * @param size the max size of all incoming messages
1097 _dbus_transport_set_max_received_size (DBusTransport *transport,
1100 transport->max_live_messages_size = size;
1101 _dbus_counter_set_notify (transport->live_messages,
1102 transport->max_live_messages_size,
1103 transport->max_live_messages_unix_fds,
1104 live_messages_notify,
1109 * See dbus_connection_set_max_received_unix_fds().
1111 * @param transport the transport
1112 * @param n the max unix fds of all incoming messages
1115 _dbus_transport_set_max_received_unix_fds (DBusTransport *transport,
1118 transport->max_live_messages_unix_fds = n;
1119 _dbus_counter_set_notify (transport->live_messages,
1120 transport->max_live_messages_size,
1121 transport->max_live_messages_unix_fds,
1122 live_messages_notify,
1127 * See dbus_connection_get_max_received_size().
1129 * @param transport the transport
1130 * @returns max bytes for all live messages
1133 _dbus_transport_get_max_received_size (DBusTransport *transport)
1135 return transport->max_live_messages_size;
1139 * See dbus_connection_set_max_received_unix_fds().
1141 * @param transport the transport
1142 * @returns max unix fds for all live messages
1145 _dbus_transport_get_max_received_unix_fds (DBusTransport *transport)
1147 return transport->max_live_messages_unix_fds;
1151 * See dbus_connection_get_unix_user().
1153 * @param transport the transport
1154 * @param uid return location for the user ID
1155 * @returns #TRUE if uid is filled in with a valid user ID
1158 _dbus_transport_get_unix_user (DBusTransport *transport,
1161 DBusCredentials *auth_identity;
1163 *uid = _DBUS_INT32_MAX; /* better than some root or system user in
1164 * case of bugs in the caller. Caller should
1165 * never use this value on purpose, however.
1168 if (!transport->authenticated)
1171 auth_identity = _dbus_auth_get_identity (transport->auth);
1173 if (_dbus_credentials_include (auth_identity,
1174 DBUS_CREDENTIAL_UNIX_USER_ID))
1176 *uid = _dbus_credentials_get_unix_uid (auth_identity);
1184 * See dbus_connection_get_unix_process_id().
1186 * @param transport the transport
1187 * @param pid return location for the process ID
1188 * @returns #TRUE if uid is filled in with a valid process ID
1191 _dbus_transport_get_unix_process_id (DBusTransport *transport,
1194 DBusCredentials *auth_identity;
1196 *pid = DBUS_PID_UNSET; /* Caller should never use this value on purpose,
1197 * but we set it to a safe number, INT_MAX,
1198 * just to root out possible bugs in bad callers.
1201 if (!transport->authenticated)
1204 auth_identity = _dbus_auth_get_identity (transport->auth);
1206 if (_dbus_credentials_include (auth_identity,
1207 DBUS_CREDENTIAL_UNIX_PROCESS_ID))
1209 *pid = _dbus_credentials_get_pid (auth_identity);
1217 * See dbus_connection_get_adt_audit_session_data().
1219 * @param transport the transport
1220 * @param data return location for the ADT audit data
1221 * @param data_size return length of audit data
1222 * @returns #TRUE if audit data is filled in with a valid ucred
1225 _dbus_transport_get_adt_audit_session_data (DBusTransport *transport,
1229 DBusCredentials *auth_identity;
1234 if (!transport->authenticated)
1237 auth_identity = _dbus_auth_get_identity (transport->auth);
1239 if (_dbus_credentials_include (auth_identity,
1240 DBUS_CREDENTIAL_ADT_AUDIT_DATA_ID))
1242 *data = (void *) _dbus_credentials_get_adt_audit_data (auth_identity);
1243 *data_size = _dbus_credentials_get_adt_audit_data_size (auth_identity);
1251 * See dbus_connection_set_unix_user_function().
1253 * @param transport the transport
1254 * @param function the predicate
1255 * @param data data to pass to the predicate
1256 * @param free_data_function function to free the data
1257 * @param old_data the old user data to be freed
1258 * @param old_free_data_function old free data function to free it with
1261 _dbus_transport_set_unix_user_function (DBusTransport *transport,
1262 DBusAllowUnixUserFunction function,
1264 DBusFreeFunction free_data_function,
1266 DBusFreeFunction *old_free_data_function)
1268 if (transport->is_server)
1269 _dbus_authorization_set_unix_authorization_callback (transport->authorization, function,
1270 data, free_data_function, old_data, old_free_data_function);
1274 * See dbus_connection_get_windows_user().
1276 * @param transport the transport
1277 * @param windows_sid_p return location for the user ID
1278 * @returns #TRUE if user is available; the returned value may still be #NULL if no memory to copy it
1281 _dbus_transport_get_windows_user (DBusTransport *transport,
1282 char **windows_sid_p)
1284 DBusCredentials *auth_identity;
1286 *windows_sid_p = NULL;
1288 if (!transport->authenticated)
1291 auth_identity = _dbus_auth_get_identity (transport->auth);
1293 if (_dbus_credentials_include (auth_identity,
1294 DBUS_CREDENTIAL_WINDOWS_SID))
1296 /* If no memory, we are supposed to return TRUE and set NULL */
1297 *windows_sid_p = _dbus_strdup (_dbus_credentials_get_windows_sid (auth_identity));
1306 * See dbus_connection_set_windows_user_function().
1308 * @param transport the transport
1309 * @param function the predicate
1310 * @param data data to pass to the predicate
1311 * @param free_data_function function to free the data
1312 * @param old_data the old user data to be freed
1313 * @param old_free_data_function old free data function to free it with
1317 _dbus_transport_set_windows_user_function (DBusTransport *transport,
1318 DBusAllowWindowsUserFunction function,
1320 DBusFreeFunction free_data_function,
1322 DBusFreeFunction *old_free_data_function)
1324 if (transport->is_server)
1325 _dbus_authorization_set_windows_authorization_callback (transport->authorization, function,
1326 data, free_data_function, old_data, old_free_data_function);
1330 * Sets the SASL authentication mechanisms supported by this transport.
1332 * @param transport the transport
1333 * @param mechanisms the #NULL-terminated array of mechanisms
1335 * @returns #FALSE if no memory
1338 _dbus_transport_set_auth_mechanisms (DBusTransport *transport,
1339 const char **mechanisms)
1341 return _dbus_auth_set_mechanisms (transport->auth, mechanisms);
1345 * See dbus_connection_set_allow_anonymous()
1347 * @param transport the transport
1348 * @param value #TRUE to allow anonymous connection
1351 _dbus_transport_set_allow_anonymous (DBusTransport *transport,
1354 if (transport->is_server)
1355 _dbus_authorization_set_allow_anonymous (transport->authorization, value);
1358 #ifdef DBUS_ENABLE_STATS
1360 _dbus_transport_get_stats (DBusTransport *transport,
1361 dbus_uint32_t *queue_bytes,
1362 dbus_uint32_t *queue_fds,
1363 dbus_uint32_t *peak_queue_bytes,
1364 dbus_uint32_t *peak_queue_fds)
1366 if (queue_bytes != NULL)
1367 *queue_bytes = _dbus_counter_get_size_value (transport->live_messages);
1369 if (queue_fds != NULL)
1370 *queue_fds = _dbus_counter_get_unix_fd_value (transport->live_messages);
1372 if (peak_queue_bytes != NULL)
1373 *peak_queue_bytes = _dbus_counter_get_peak_size_value (transport->live_messages);
1375 if (peak_queue_fds != NULL)
1376 *peak_queue_fds = _dbus_counter_get_peak_unix_fd_value (transport->live_messages);
1378 #endif /* DBUS_ENABLE_STATS */