1 /* -*- mode: C; c-file-style: "gnu" -*- */
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 1.2
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "dbus-transport-protected.h"
25 #include "dbus-transport-unix.h"
26 #include "dbus-connection-internal.h"
27 #include "dbus-watch.h"
28 #include "dbus-auth.h"
29 #include "dbus-address.h"
30 #ifdef DBUS_BUILD_TESTS
31 #include "dbus-transport-debug.h"
32 #include "dbus-server-debug-pipe.h"
36 * @defgroup DBusTransport DBusTransport object
37 * @ingroup DBusInternals
38 * @brief "Backend" for a DBusConnection.
40 * Types and functions related to DBusTransport. A transport is an
41 * abstraction that can send and receive data via various kinds of
42 * network connections or other IPC mechanisms.
48 * @typedef DBusTransport
50 * Opaque object representing a way message stream.
51 * DBusTransport abstracts various kinds of actual
52 * transport mechanism, such as different network protocols,
53 * or encryption schemes.
57 live_messages_size_notify (DBusCounter *counter,
60 DBusTransport *transport = user_data;
62 _dbus_transport_ref (transport);
65 _dbus_verbose ("Counter value is now %d\n",
66 (int) _dbus_counter_get_value (counter));
69 /* disable or re-enable the read watch for the transport if
72 if (* transport->vtable->live_messages_changed)
73 (* transport->vtable->live_messages_changed) (transport);
75 _dbus_transport_unref (transport);
79 * Initializes the base class members of DBusTransport.
80 * Chained up to by subclasses in their constructor.
82 * @param transport the transport being created.
83 * @param vtable the subclass vtable.
84 * @param server #TRUE if this transport is on the server side of a connection
85 * @returns #TRUE on success.
88 _dbus_transport_init_base (DBusTransport *transport,
89 const DBusTransportVTable *vtable,
92 DBusMessageLoader *loader;
96 loader = _dbus_message_loader_new ();
101 auth = _dbus_auth_server_new ();
103 auth = _dbus_auth_client_new ();
106 _dbus_message_loader_unref (loader);
110 counter = _dbus_counter_new ();
113 _dbus_auth_unref (auth);
114 _dbus_message_loader_unref (loader);
118 transport->refcount = 1;
119 transport->vtable = vtable;
120 transport->loader = loader;
121 transport->auth = auth;
122 transport->live_messages_size = counter;
123 transport->authenticated = FALSE;
124 transport->messages_need_sending = FALSE;
125 transport->disconnected = FALSE;
126 transport->send_credentials_pending = !server;
127 transport->receive_credentials_pending = server;
128 transport->is_server = server;
130 /* Try to default to something that won't totally hose the system,
131 * but doesn't impose too much of a limitation.
133 transport->max_live_messages_size = _DBUS_ONE_MEGABYTE * 63;
135 transport->credentials.pid = -1;
136 transport->credentials.uid = -1;
137 transport->credentials.gid = -1;
139 _dbus_counter_set_notify (transport->live_messages_size,
140 transport->max_live_messages_size,
141 live_messages_size_notify,
148 * Finalizes base class members of DBusTransport.
149 * Chained up to from subclass finalizers.
151 * @param transport the transport.
154 _dbus_transport_finalize_base (DBusTransport *transport)
156 if (!transport->disconnected)
157 _dbus_transport_disconnect (transport);
159 _dbus_message_loader_unref (transport->loader);
160 _dbus_auth_unref (transport->auth);
161 _dbus_counter_set_notify (transport->live_messages_size,
163 _dbus_counter_unref (transport->live_messages_size);
167 * Opens a new transport for the given address. (This opens a
168 * client-side-of-the-connection transport.)
170 * @todo error messages on bad address could really be better.
171 * DBusResultCode is a bit limiting here.
173 * @param address the address.
174 * @param result location to store reason for failure.
175 * @returns new transport of #NULL on failure.
178 _dbus_transport_open (const char *address,
179 DBusResultCode *result)
181 DBusTransport *transport;
182 DBusAddressEntry **entries;
185 if (!dbus_parse_address (address, &entries, &len, result))
190 for (i = 0; i < len; i++)
192 const char *method = dbus_address_entry_get_method (entries[i]);
194 if (strcmp (method, "unix") == 0)
196 const char *path = dbus_address_entry_get_value (entries[i], "path");
201 transport = _dbus_transport_new_for_domain_socket (path, FALSE, result);
203 else if (strcmp (method, "tcp") == 0)
205 const char *host = dbus_address_entry_get_value (entries[i], "host");
206 const char *port = dbus_address_entry_get_value (entries[i], "port");
214 _dbus_string_init_const (&str, port);
215 sresult = _dbus_string_parse_int (&str, 0, &lport, NULL);
216 _dbus_string_free (&str);
218 if (sresult == FALSE || lport <= 0 || lport > 65535)
221 transport = _dbus_transport_new_for_tcp_socket (host, lport, FALSE, result);
223 #ifdef DBUS_BUILD_TESTS
224 else if (strcmp (method, "debug") == 0)
226 const char *name = dbus_address_entry_get_value (entries[i], "name");
231 transport = _dbus_transport_debug_client_new (name, result);
233 else if (strcmp (method, "debug-pipe") == 0)
235 const char *name = dbus_address_entry_get_value (entries[i], "name");
240 transport = _dbus_transport_debug_pipe_new (name, result);
250 dbus_address_entries_free (entries);
254 dbus_address_entries_free (entries);
255 dbus_set_result (result, DBUS_RESULT_BAD_ADDRESS);
261 * Increments the reference count for the transport.
263 * @param transport the transport.
266 _dbus_transport_ref (DBusTransport *transport)
268 transport->refcount += 1;
272 * Decrements the reference count for the transport.
273 * Disconnects and finalizes the transport if
274 * the reference count reaches zero.
276 * @param transport the transport.
279 _dbus_transport_unref (DBusTransport *transport)
281 _dbus_assert (transport != NULL);
282 _dbus_assert (transport->refcount > 0);
284 transport->refcount -= 1;
285 if (transport->refcount == 0)
287 _dbus_assert (transport->vtable->finalize != NULL);
289 (* transport->vtable->finalize) (transport);
294 * Closes our end of the connection to a remote application. Further
295 * attempts to use this transport will fail. Only the first call to
296 * _dbus_transport_disconnect() will have an effect.
298 * @param transport the transport.
302 _dbus_transport_disconnect (DBusTransport *transport)
304 _dbus_assert (transport->vtable->disconnect != NULL);
306 if (transport->disconnected)
309 _dbus_transport_ref (transport);
310 (* transport->vtable->disconnect) (transport);
312 transport->disconnected = TRUE;
314 _dbus_connection_notify_disconnected (transport->connection);
316 _dbus_transport_unref (transport);
320 * Returns #TRUE if the transport has not been disconnected.
321 * Disconnection can result from _dbus_transport_disconnect()
322 * or because the server drops its end of the connection.
324 * @param transport the transport.
325 * @returns whether we're connected
328 _dbus_transport_get_is_connected (DBusTransport *transport)
330 return !transport->disconnected;
334 * Returns #TRUE if we have been authenticated. Will return #TRUE
335 * even if the transport is disconnected.
337 * @param transport the transport
338 * @returns whether we're authenticated
341 _dbus_transport_get_is_authenticated (DBusTransport *transport)
343 if (transport->authenticated)
347 if (transport->disconnected)
350 transport->authenticated =
351 (!(transport->send_credentials_pending ||
352 transport->receive_credentials_pending)) &&
353 _dbus_auth_do_work (transport->auth) == DBUS_AUTH_STATE_AUTHENTICATED;
355 /* If we've authenticated as some identity, check that the auth
356 * identity is the same as our own identity. In the future, we
357 * may have API allowing applications to specify how this is
358 * done, for example they may allow connection as any identity,
359 * but then impose restrictions on certain identities.
360 * Or they may give certain identities extra privileges.
363 if (transport->authenticated && transport->is_server)
365 DBusCredentials auth_identity;
366 DBusCredentials our_identity;
368 _dbus_credentials_from_current_process (&our_identity);
369 _dbus_auth_get_identity (transport->auth, &auth_identity);
371 if (!_dbus_credentials_match (&our_identity,
374 _dbus_verbose ("Client authorized as UID %d but our UID is %d, disconnecting\n",
375 auth_identity.uid, our_identity.uid);
376 _dbus_transport_disconnect (transport);
381 _dbus_verbose ("Client authorized as UID %d matching our UID %d\n",
382 auth_identity.uid, our_identity.uid);
386 return transport->authenticated;
391 * Handles a watch by reading data, writing data, or disconnecting
392 * the transport, as appropriate for the given condition.
394 * @param transport the transport.
395 * @param watch the watch.
396 * @param condition the current state of the watched file descriptor.
399 _dbus_transport_handle_watch (DBusTransport *transport,
401 unsigned int condition)
403 _dbus_assert (transport->vtable->handle_watch != NULL);
405 if (transport->disconnected)
408 if (dbus_watch_get_fd (watch) < 0)
410 _dbus_warn ("Tried to handle an invalidated watch; this watch should have been removed\n");
414 _dbus_watch_sanitize_condition (watch, &condition);
416 _dbus_transport_ref (transport);
417 _dbus_watch_ref (watch);
418 (* transport->vtable->handle_watch) (transport, watch, condition);
419 _dbus_watch_unref (watch);
420 _dbus_transport_unref (transport);
424 * Sets the connection using this transport. Allows the transport
425 * to add watches to the connection, queue incoming messages,
426 * and pull outgoing messages.
428 * @param transport the transport.
429 * @param connection the connection.
430 * @returns #FALSE if not enough memory
433 _dbus_transport_set_connection (DBusTransport *transport,
434 DBusConnection *connection)
436 _dbus_assert (transport->vtable->connection_set != NULL);
437 _dbus_assert (transport->connection == NULL);
439 transport->connection = connection;
441 _dbus_transport_ref (transport);
442 if (!(* transport->vtable->connection_set) (transport))
443 transport->connection = NULL;
444 _dbus_transport_unref (transport);
446 return transport->connection != NULL;
450 * Notifies the transport when the outgoing message queue goes from
451 * empty to non-empty or vice versa. Typically causes the transport to
452 * add or remove its DBUS_WATCH_WRITABLE watch.
454 * @param transport the transport.
455 * @param queue_length the length of the outgoing message queue.
459 _dbus_transport_messages_pending (DBusTransport *transport,
462 _dbus_assert (transport->vtable->messages_pending != NULL);
464 if (transport->disconnected)
467 transport->messages_need_sending = queue_length > 0;
469 _dbus_transport_ref (transport);
470 (* transport->vtable->messages_pending) (transport,
472 _dbus_transport_unref (transport);
476 * Performs a single poll()/select() on the transport's file
477 * descriptors and then reads/writes data as appropriate,
478 * queueing incoming messages and sending outgoing messages.
479 * This is the backend for _dbus_connection_do_iteration().
480 * See _dbus_connection_do_iteration() for full details.
482 * @param transport the transport.
483 * @param flags indicates whether to read or write, and whether to block.
484 * @param timeout_milliseconds if blocking, timeout or -1 for no timeout.
487 _dbus_transport_do_iteration (DBusTransport *transport,
489 int timeout_milliseconds)
491 _dbus_assert (transport->vtable->do_iteration != NULL);
493 _dbus_verbose ("Transport iteration flags 0x%x timeout %d connected = %d\n",
494 flags, timeout_milliseconds, !transport->disconnected);
496 if ((flags & (DBUS_ITERATION_DO_WRITING |
497 DBUS_ITERATION_DO_READING)) == 0)
498 return; /* Nothing to do */
500 if (transport->disconnected)
503 _dbus_transport_ref (transport);
504 (* transport->vtable->do_iteration) (transport, flags,
505 timeout_milliseconds);
506 _dbus_transport_unref (transport);
510 * Reports our current dispatch status (whether there's buffered
511 * data to be queued as messages, or not, or we need memory).
513 * @param transport the transport
514 * @returns current status
517 _dbus_transport_get_dispatch_status (DBusTransport *transport)
519 if (_dbus_counter_get_value (transport->live_messages_size) >= transport->max_live_messages_size)
520 return DBUS_DISPATCH_COMPLETE; /* complete for now */
522 if (!_dbus_message_loader_queue_messages (transport->loader))
523 return DBUS_DISPATCH_NEED_MEMORY;
525 if (_dbus_message_loader_peek_message (transport->loader) != NULL)
526 return DBUS_DISPATCH_DATA_REMAINS;
528 return DBUS_DISPATCH_COMPLETE;
532 * Processes data we've read while handling a watch, potentially
533 * converting some of it to messages and queueing those messages on
536 * @param transport the transport
537 * @returns #TRUE if we had enough memory to queue all messages
540 _dbus_transport_queue_messages (DBusTransport *transport)
542 DBusDispatchStatus status;
544 /* Queue any messages */
545 while ((status = _dbus_transport_get_dispatch_status (transport)) == DBUS_DISPATCH_DATA_REMAINS)
547 DBusMessage *message;
550 link = _dbus_message_loader_pop_message_link (transport->loader);
551 _dbus_assert (link != NULL);
553 message = link->data;
555 _dbus_verbose ("queueing received message %p\n", message);
557 _dbus_message_add_size_counter (message, transport->live_messages_size);
559 /* pass ownership of link and message ref to connection */
560 _dbus_connection_queue_received_message_link (transport->connection,
564 if (_dbus_message_loader_get_is_corrupted (transport->loader))
566 _dbus_verbose ("Corrupted message stream, disconnecting\n");
567 _dbus_transport_disconnect (transport);
570 return status != DBUS_DISPATCH_NEED_MEMORY;
574 * See dbus_connection_set_max_message_size().
576 * @param transport the transport
577 * @param size the max size of a single message
580 _dbus_transport_set_max_message_size (DBusTransport *transport,
583 _dbus_message_loader_set_max_message_size (transport->loader, size);
587 * See dbus_connection_get_max_message_size().
589 * @param transport the transport
590 * @returns max message size
593 _dbus_transport_get_max_message_size (DBusTransport *transport)
595 return _dbus_message_loader_get_max_message_size (transport->loader);
599 * See dbus_connection_set_max_live_messages_size().
601 * @param transport the transport
602 * @param size the max size of all incoming messages
605 _dbus_transport_set_max_live_messages_size (DBusTransport *transport,
608 transport->max_live_messages_size = size;
609 _dbus_counter_set_notify (transport->live_messages_size,
610 transport->max_live_messages_size,
611 live_messages_size_notify,
617 * See dbus_connection_get_max_live_messages_size().
619 * @param transport the transport
620 * @returns max bytes for all live messages
623 _dbus_transport_get_max_live_messages_size (DBusTransport *transport)
625 return transport->max_live_messages_size;