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 #ifdef DBUS_BUILD_TESTS
30 #include "dbus-transport-debug.h"
34 * @defgroup DBusTransport DBusTransport object
35 * @ingroup DBusInternals
36 * @brief "Backend" for a DBusConnection.
38 * Types and functions related to DBusTransport. A transport is an
39 * abstraction that can send and receive data via various kinds of
40 * network connections or other IPC mechanisms.
46 * @typedef DBusTransport
48 * Opaque object representing a way message stream.
49 * DBusTransport abstracts various kinds of actual
50 * transport mechanism, such as different network protocols,
51 * or encryption schemes.
55 * Refs a transport and associated connection for reentrancy.
57 * @todo this macro reflects a design mistake, which is that the
58 * transport has a pointer to its connection. Ownership should move in
59 * only one direction; the connection should push/pull from the
60 * transport, rather than vice versa. Then the connection would take
61 * care of referencing itself when needed.
63 #define DBUS_TRANSPORT_HOLD_REF(t) \
64 _dbus_transport_ref (t); if ((t)->connection) dbus_connection_ref ((t)->connection)
67 * Inverse of DBUS_TRANSPORT_HOLD_REF().
69 #define DBUS_TRANSPORT_RELEASE_REF(t) \
70 if ((t)->connection) dbus_connection_unref ((t)->connection); _dbus_transport_unref (t)
73 live_messages_size_notify (DBusCounter *counter,
76 DBusTransport *transport = user_data;
78 DBUS_TRANSPORT_HOLD_REF (transport);
81 _dbus_verbose ("Counter value is now %d\n",
82 (int) _dbus_counter_get_value (counter));
85 /* disable or re-enable the read watch for the transport if
88 if (* transport->vtable->live_messages_changed)
89 (* transport->vtable->live_messages_changed) (transport);
91 DBUS_TRANSPORT_RELEASE_REF (transport);
95 * Initializes the base class members of DBusTransport.
96 * Chained up to by subclasses in their constructor.
98 * @param transport the transport being created.
99 * @param vtable the subclass vtable.
100 * @param server #TRUE if this transport is on the server side of a connection
101 * @returns #TRUE on success.
104 _dbus_transport_init_base (DBusTransport *transport,
105 const DBusTransportVTable *vtable,
108 DBusMessageLoader *loader;
110 DBusCounter *counter;
112 loader = _dbus_message_loader_new ();
117 auth = _dbus_auth_server_new ();
119 auth = _dbus_auth_client_new ();
122 _dbus_message_loader_unref (loader);
126 counter = _dbus_counter_new ();
129 _dbus_auth_unref (auth);
130 _dbus_message_loader_unref (loader);
134 transport->refcount = 1;
135 transport->vtable = vtable;
136 transport->loader = loader;
137 transport->auth = auth;
138 transport->live_messages_size = counter;
139 transport->authenticated = FALSE;
140 transport->messages_need_sending = FALSE;
141 transport->disconnected = FALSE;
142 transport->send_credentials_pending = !server;
143 transport->receive_credentials_pending = server;
144 transport->is_server = server;
146 /* Try to default to something that won't totally hose the system,
147 * but doesn't impose too much of a limitation.
149 transport->max_live_messages_size = _DBUS_ONE_MEGABYTE * 63;
151 transport->credentials.pid = -1;
152 transport->credentials.uid = -1;
153 transport->credentials.gid = -1;
155 _dbus_counter_set_notify (transport->live_messages_size,
156 transport->max_live_messages_size,
157 live_messages_size_notify,
164 * Finalizes base class members of DBusTransport.
165 * Chained up to from subclass finalizers.
167 * @param transport the transport.
170 _dbus_transport_finalize_base (DBusTransport *transport)
172 if (!transport->disconnected)
173 _dbus_transport_disconnect (transport);
175 _dbus_message_loader_unref (transport->loader);
176 _dbus_auth_unref (transport->auth);
177 _dbus_counter_set_notify (transport->live_messages_size,
179 _dbus_counter_unref (transport->live_messages_size);
183 * Opens a new transport for the given address. (This opens a
184 * client-side-of-the-connection transport.)
186 * @todo right now the address is just a Unix domain socket path.
188 * @param address the address.
189 * @param result location to store reason for failure.
190 * @returns new transport of #NULL on failure.
193 _dbus_transport_open (const char *address,
194 DBusResultCode *result)
196 DBusTransport *transport;
198 /* FIXME parse the address - whatever format
199 * we decide addresses are in - and find the
200 * appropriate transport.
204 /* Pretend it's just a unix domain socket name for now */
205 transport = _dbus_transport_new_for_domain_socket (address,
209 transport = _dbus_transport_debug_client_new (address,
216 * Increments the reference count for the transport.
218 * @param transport the transport.
221 _dbus_transport_ref (DBusTransport *transport)
223 transport->refcount += 1;
227 * Decrements the reference count for the transport.
228 * Disconnects and finalizes the transport if
229 * the reference count reaches zero.
231 * @param transport the transport.
234 _dbus_transport_unref (DBusTransport *transport)
236 _dbus_assert (transport != NULL);
237 _dbus_assert (transport->refcount > 0);
239 transport->refcount -= 1;
240 if (transport->refcount == 0)
242 _dbus_assert (transport->vtable->finalize != NULL);
244 (* transport->vtable->finalize) (transport);
249 * Closes our end of the connection to a remote application. Further
250 * attempts to use this transport will fail. Only the first call to
251 * _dbus_transport_disconnect() will have an effect.
253 * @param transport the transport.
257 _dbus_transport_disconnect (DBusTransport *transport)
259 _dbus_assert (transport->vtable->disconnect != NULL);
261 if (transport->disconnected)
264 DBUS_TRANSPORT_HOLD_REF (transport);
265 (* transport->vtable->disconnect) (transport);
267 transport->disconnected = TRUE;
269 _dbus_connection_notify_disconnected (transport->connection);
271 DBUS_TRANSPORT_RELEASE_REF (transport);
275 * Returns #TRUE if the transport has not been disconnected.
276 * Disconnection can result from _dbus_transport_disconnect()
277 * or because the server drops its end of the connection.
279 * @param transport the transport.
280 * @returns whether we're connected
283 _dbus_transport_get_is_connected (DBusTransport *transport)
285 return !transport->disconnected;
289 * Returns #TRUE if we have been authenticated. Will return #TRUE
290 * even if the transport is disconnected.
292 * @param transport the transport
293 * @returns whether we're authenticated
296 _dbus_transport_get_is_authenticated (DBusTransport *transport)
298 if (transport->authenticated)
302 if (transport->disconnected)
305 transport->authenticated =
306 (!(transport->send_credentials_pending ||
307 transport->receive_credentials_pending)) &&
308 _dbus_auth_do_work (transport->auth) == DBUS_AUTH_STATE_AUTHENTICATED;
310 /* If we've authenticated as some identity, check that the auth
311 * identity is the same as our own identity. In the future, we
312 * may have API allowing applications to specify how this is
313 * done, for example they may allow connection as any identity,
314 * but then impose restrictions on certain identities.
315 * Or they may give certain identities extra privileges.
318 if (transport->authenticated && transport->is_server)
320 DBusCredentials auth_identity;
321 DBusCredentials our_identity;
323 _dbus_credentials_from_current_process (&our_identity);
324 _dbus_auth_get_identity (transport->auth, &auth_identity);
326 if (!_dbus_credentials_match (&our_identity,
329 _dbus_verbose ("Client authorized as UID %d but our UID is %d, disconnecting\n",
330 auth_identity.uid, our_identity.uid);
331 _dbus_transport_disconnect (transport);
336 _dbus_verbose ("Client authorized as UID %d matching our UID %d\n",
337 auth_identity.uid, our_identity.uid);
341 return transport->authenticated;
346 * Handles a watch by reading data, writing data, or disconnecting
347 * the transport, as appropriate for the given condition.
349 * @param transport the transport.
350 * @param watch the watch.
351 * @param condition the current state of the watched file descriptor.
354 _dbus_transport_handle_watch (DBusTransport *transport,
356 unsigned int condition)
358 _dbus_assert (transport->vtable->handle_watch != NULL);
360 if (transport->disconnected)
363 if (dbus_watch_get_fd (watch) < 0)
365 _dbus_warn ("Tried to handle an invalidated watch; this watch should have been removed\n");
369 _dbus_watch_sanitize_condition (watch, &condition);
371 DBUS_TRANSPORT_HOLD_REF (transport);
372 _dbus_watch_ref (watch);
373 (* transport->vtable->handle_watch) (transport, watch, condition);
374 _dbus_watch_unref (watch);
375 DBUS_TRANSPORT_RELEASE_REF (transport);
379 * Sets the connection using this transport. Allows the transport
380 * to add watches to the connection, queue incoming messages,
381 * and pull outgoing messages.
383 * @param transport the transport.
384 * @param connection the connection.
387 _dbus_transport_set_connection (DBusTransport *transport,
388 DBusConnection *connection)
390 _dbus_assert (transport->vtable->connection_set != NULL);
391 _dbus_assert (transport->connection == NULL);
393 transport->connection = connection;
395 DBUS_TRANSPORT_HOLD_REF (transport);
396 (* transport->vtable->connection_set) (transport);
397 DBUS_TRANSPORT_RELEASE_REF (transport);
401 * Notifies the transport when the outgoing message queue goes from
402 * empty to non-empty or vice versa. Typically causes the transport to
403 * add or remove its DBUS_WATCH_WRITABLE watch.
405 * @param transport the transport.
406 * @param queue_length the length of the outgoing message queue.
410 _dbus_transport_messages_pending (DBusTransport *transport,
413 _dbus_assert (transport->vtable->messages_pending != NULL);
415 if (transport->disconnected)
418 transport->messages_need_sending = queue_length > 0;
420 DBUS_TRANSPORT_HOLD_REF (transport);
421 (* transport->vtable->messages_pending) (transport,
423 DBUS_TRANSPORT_RELEASE_REF (transport);
427 * Performs a single poll()/select() on the transport's file
428 * descriptors and then reads/writes data as appropriate,
429 * queueing incoming messages and sending outgoing messages.
430 * This is the backend for _dbus_connection_do_iteration().
431 * See _dbus_connection_do_iteration() for full details.
433 * @param transport the transport.
434 * @param flags indicates whether to read or write, and whether to block.
435 * @param timeout_milliseconds if blocking, timeout or -1 for no timeout.
438 _dbus_transport_do_iteration (DBusTransport *transport,
440 int timeout_milliseconds)
442 _dbus_assert (transport->vtable->do_iteration != NULL);
444 if ((flags & (DBUS_ITERATION_DO_WRITING |
445 DBUS_ITERATION_DO_READING)) == 0)
446 return; /* Nothing to do */
448 if (transport->disconnected)
451 DBUS_TRANSPORT_HOLD_REF (transport);
452 (* transport->vtable->do_iteration) (transport, flags,
453 timeout_milliseconds);
454 DBUS_TRANSPORT_RELEASE_REF (transport);
458 * See dbus_connection_set_max_message_size().
460 * @param transport the transport
461 * @param size the max size of a single message
464 _dbus_transport_set_max_message_size (DBusTransport *transport,
467 _dbus_message_loader_set_max_message_size (transport->loader, size);
471 * See dbus_connection_get_max_message_size().
473 * @param transport the transport
474 * @returns max message size
477 _dbus_transport_get_max_message_size (DBusTransport *transport)
479 return _dbus_message_loader_get_max_message_size (transport->loader);
483 * See dbus_connection_set_max_live_messages_size().
485 * @param transport the transport
486 * @param size the max size of all incoming messages
489 _dbus_transport_set_max_live_messages_size (DBusTransport *transport,
492 transport->max_live_messages_size = size;
493 _dbus_counter_set_notify (transport->live_messages_size,
494 transport->max_live_messages_size,
495 live_messages_size_notify,
501 * See dbus_connection_get_max_live_messages_size().
503 * @param transport the transport
504 * @returns max bytes for all live messages
507 _dbus_transport_get_max_live_messages_size (DBusTransport *transport)
509 return transport->max_live_messages_size;