2002-12-26 Havoc Pennington <hp@pobox.com>
[platform/upstream/dbus.git] / dbus / dbus-transport.c
1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-transport.c DBusTransport object (internal to D-BUS implementation)
3  *
4  * Copyright (C) 2002  Red Hat Inc.
5  *
6  * Licensed under the Academic Free License version 1.2
7  * 
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.
12  *
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.
17  * 
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
21  *
22  */
23
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
30 /**
31  * @defgroup DBusTransport DBusTransport object
32  * @ingroup  DBusInternals
33  * @brief "Backend" for a DBusConnection.
34  *
35  * Types and functions related to DBusTransport.  A transport is an
36  * abstraction that can send and receive data via various kinds of
37  * network connections or other IPC mechanisms.
38  * 
39  * @{
40  */
41
42 /**
43  * @typedef DBusTransport
44  *
45  * Opaque object representing a way message stream.
46  * DBusTransport abstracts various kinds of actual
47  * transport mechanism, such as different network protocols,
48  * or encryption schemes.
49  */
50
51 /**
52  * Refs a transport and associated connection for reentrancy.
53  *
54  * @todo this macro reflects a design mistake, which is that the
55  * transport has a pointer to its connection. Ownership should move in
56  * only one direction; the connection should push/pull from the
57  * transport, rather than vice versa. Then the connection would take
58  * care of referencing itself when needed.
59  */
60 #define DBUS_TRANSPORT_HOLD_REF(t) \
61   _dbus_transport_ref (t); if ((t)->connection) dbus_connection_ref ((t)->connection)
62
63 /**
64  * Inverse of DBUS_TRANSPORT_HOLD_REF().
65  */
66 #define DBUS_TRANSPORT_RELEASE_REF(t) \
67   if ((t)->connection) dbus_connection_unref ((t)->connection); _dbus_transport_unref (t)
68
69
70 /**
71  * Initializes the base class members of DBusTransport.
72  * Chained up to by subclasses in their constructor.
73  *
74  * @param transport the transport being created.
75  * @param vtable the subclass vtable.
76  * @param server #TRUE if this transport is on the server side of a connection
77  * @returns #TRUE on success.
78  */
79 dbus_bool_t
80 _dbus_transport_init_base (DBusTransport             *transport,
81                            const DBusTransportVTable *vtable,
82                            dbus_bool_t                server)
83 {
84   DBusMessageLoader *loader;
85   DBusAuth *auth;
86   
87   loader = _dbus_message_loader_new ();
88   if (loader == NULL)
89     return FALSE;
90
91   if (server)
92     auth = _dbus_auth_server_new ();
93   else
94     auth = _dbus_auth_client_new ();
95   if (auth == NULL)
96     {
97       _dbus_message_loader_unref (loader);
98       return FALSE;
99     }
100   
101   transport->refcount = 1;
102   transport->vtable = vtable;
103   transport->loader = loader;
104   transport->auth = auth;
105   transport->authenticated = FALSE;
106   transport->messages_need_sending = FALSE;
107   transport->disconnected = FALSE;
108   
109   return TRUE;
110 }
111
112 /**
113  * Finalizes base class members of DBusTransport.
114  * Chained up to from subclass finalizers.
115  *
116  * @param transport the transport.
117  */
118 void
119 _dbus_transport_finalize_base (DBusTransport *transport)
120 {
121   if (!transport->disconnected)
122     _dbus_transport_disconnect (transport);
123
124   _dbus_message_loader_unref (transport->loader);
125   _dbus_auth_unref (transport->auth);
126 }
127
128 /**
129  * Opens a new transport for the given address.  (This opens a
130  * client-side-of-the-connection transport.)
131  *
132  * @todo right now the address is just a Unix domain socket path.
133  * 
134  * @param address the address.
135  * @param result location to store reason for failure.
136  * @returns new transport of #NULL on failure.
137  */
138 DBusTransport*
139 _dbus_transport_open (const char     *address,
140                       DBusResultCode *result)
141 {
142   DBusTransport *transport;
143   
144   /* FIXME parse the address - whatever format
145    * we decide addresses are in - and find the
146    * appropriate transport.
147    */
148
149   /* Pretend it's just a unix domain socket name for now */
150   transport = _dbus_transport_new_for_domain_socket (address,
151                                                      FALSE,
152                                                      result);
153   
154   return transport;
155 }
156
157 /**
158  * Increments the reference count for the transport.
159  *
160  * @param transport the transport.
161  */
162 void
163 _dbus_transport_ref (DBusTransport *transport)
164 {
165   transport->refcount += 1;
166 }
167
168 /**
169  * Decrements the reference count for the transport.
170  * Disconnects and finalizes the transport if
171  * the reference count reaches zero.
172  *
173  * @param transport the transport.
174  */
175 void
176 _dbus_transport_unref (DBusTransport *transport)
177 {
178   _dbus_assert (transport != NULL);
179   _dbus_assert (transport->refcount > 0);
180
181   transport->refcount -= 1;
182   if (transport->refcount == 0)
183     {
184       _dbus_assert (transport->vtable->finalize != NULL);
185       
186       (* transport->vtable->finalize) (transport);
187     }
188 }
189
190 /**
191  * Closes our end of the connection to a remote application. Further
192  * attempts to use this transport will fail. Only the first call to
193  * _dbus_transport_disconnect() will have an effect.
194  *
195  * @param transport the transport.
196  * 
197  */
198 void
199 _dbus_transport_disconnect (DBusTransport *transport)
200 {
201   _dbus_assert (transport->vtable->disconnect != NULL);
202
203   if (transport->disconnected)
204     return;
205
206   DBUS_TRANSPORT_HOLD_REF (transport);
207   (* transport->vtable->disconnect) (transport);
208
209   transport->disconnected = TRUE;
210   DBUS_TRANSPORT_RELEASE_REF (transport);
211 }
212
213 /**
214  * Returns #TRUE if the transport has not been disconnected.
215  * Disconnection can result from _dbus_transport_disconnect()
216  * or because the server drops its end of the connection.
217  *
218  * @param transport the transport.
219  * @returns whether we're connected
220  */
221 dbus_bool_t
222 _dbus_transport_get_is_connected (DBusTransport *transport)
223 {
224   return !transport->disconnected;
225 }
226
227 /**
228  * Returns #TRUE if we have been authenticated.  Will return #TRUE
229  * even if the transport is disconnected.
230  *
231  * @param transport the transport
232  * @returns whether we're authenticated
233  */
234 dbus_bool_t
235 _dbus_transport_get_is_authenticated (DBusTransport *transport)
236 {  
237   if (transport->authenticated)
238     return TRUE;
239   else
240     {
241       transport->authenticated =
242         _dbus_auth_do_work (transport->auth) == DBUS_AUTH_STATE_AUTHENTICATED;
243
244       return transport->authenticated;
245     }
246 }
247
248 /**
249  * Handles a watch by reading data, writing data, or disconnecting
250  * the transport, as appropriate for the given condition.
251  *
252  * @param transport the transport.
253  * @param watch the watch.
254  * @param condition the current state of the watched file descriptor.
255  */
256 void
257 _dbus_transport_handle_watch (DBusTransport           *transport,
258                               DBusWatch               *watch,
259                               unsigned int             condition)
260 {
261   _dbus_assert (transport->vtable->handle_watch != NULL);
262
263   if (transport->disconnected)
264     {
265       _dbus_connection_transport_error (transport->connection,
266                                         DBUS_RESULT_DISCONNECTED);
267       return;
268     }
269
270   if (dbus_watch_get_fd (watch) < 0)
271     {
272       _dbus_warn ("Tried to handle an invalidated watch; this watch should have been removed\n");
273       return;
274     }
275   
276   _dbus_watch_sanitize_condition (watch, &condition);
277
278   DBUS_TRANSPORT_HOLD_REF (transport);
279   _dbus_watch_ref (watch);
280   (* transport->vtable->handle_watch) (transport, watch, condition);
281   _dbus_watch_unref (watch);
282   DBUS_TRANSPORT_RELEASE_REF (transport);
283 }
284
285 /**
286  * Sets the connection using this transport. Allows the transport
287  * to add watches to the connection, queue incoming messages,
288  * and pull outgoing messages.
289  *
290  * @param transport the transport.
291  * @param connection the connection.
292  */
293 void
294 _dbus_transport_set_connection (DBusTransport  *transport,
295                                 DBusConnection *connection)
296 {
297   _dbus_assert (transport->vtable->connection_set != NULL);
298   _dbus_assert (transport->connection == NULL);
299   
300   transport->connection = connection;
301
302   DBUS_TRANSPORT_HOLD_REF (transport);
303   (* transport->vtable->connection_set) (transport);
304   DBUS_TRANSPORT_RELEASE_REF (transport);
305 }
306
307 /**
308  * Notifies the transport when the outgoing message queue goes from
309  * empty to non-empty or vice versa. Typically causes the transport to
310  * add or remove its DBUS_WATCH_WRITABLE watch.
311  *
312  * @param transport the transport.
313  * @param queue_length the length of the outgoing message queue.
314  *
315  */
316 void
317 _dbus_transport_messages_pending (DBusTransport  *transport,
318                                   int             queue_length)
319 {
320   _dbus_assert (transport->vtable->messages_pending != NULL);
321
322   if (transport->disconnected)
323     {
324       _dbus_connection_transport_error (transport->connection,
325                                         DBUS_RESULT_DISCONNECTED);
326       return;
327     }
328
329   transport->messages_need_sending = queue_length > 0;
330
331   DBUS_TRANSPORT_HOLD_REF (transport);
332   (* transport->vtable->messages_pending) (transport,
333                                            queue_length);
334   DBUS_TRANSPORT_RELEASE_REF (transport);
335 }
336
337 /**
338  * Performs a single poll()/select() on the transport's file
339  * descriptors and then reads/writes data as appropriate,
340  * queueing incoming messages and sending outgoing messages.
341  * This is the backend for _dbus_connection_do_iteration().
342  * See _dbus_connection_do_iteration() for full details.
343  *
344  * @param transport the transport.
345  * @param flags indicates whether to read or write, and whether to block.
346  * @param timeout_milliseconds if blocking, timeout or -1 for no timeout.
347  */
348 void
349 _dbus_transport_do_iteration (DBusTransport  *transport,
350                               unsigned int    flags,
351                               int             timeout_milliseconds)
352 {
353   _dbus_assert (transport->vtable->do_iteration != NULL);
354
355   if ((flags & (DBUS_ITERATION_DO_WRITING |
356                 DBUS_ITERATION_DO_READING)) == 0)
357     return; /* Nothing to do */
358
359   if (transport->disconnected)
360     {
361       _dbus_connection_transport_error (transport->connection,
362                                         DBUS_RESULT_DISCONNECTED);
363       return;
364     }
365
366   DBUS_TRANSPORT_HOLD_REF (transport);
367   (* transport->vtable->do_iteration) (transport, flags,
368                                        timeout_milliseconds);
369   DBUS_TRANSPORT_RELEASE_REF (transport);
370 }
371
372 /** @} */