forgot to remove a header
[platform/upstream/dbus.git] / dbus / dbus-server.c
1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-server.c DBusServer object
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-server.h"
25 #include "dbus-server-unix.h"
26 #ifdef DBUS_BUILD_TESTS
27 #include "dbus-server-debug.h"
28 #endif
29 #include "dbus-address.h"
30
31 /**
32  * @defgroup DBusServer DBusServer
33  * @ingroup  DBus
34  * @brief Server that listens for new connections.
35  *
36  * Types and functions related to DBusServer.
37  * A DBusServer represents a server that other applications
38  * can connect to. Each connection from another application
39  * is represented by a DBusConnection.
40  */
41
42 /**
43  * @defgroup DBusServerInternals DBusServer implementation details
44  * @ingroup  DBusInternals
45  * @brief Implementation details of DBusServer
46  *
47  * @{
48  */
49
50 /**
51  * Initializes the members of the DBusServer base class.
52  * Chained up to by subclass constructors.
53  *
54  * @param server the server.
55  * @param vtable the vtable for the subclass.
56  * @returns #TRUE on success.
57  */
58 dbus_bool_t
59 _dbus_server_init_base (DBusServer             *server,
60                         const DBusServerVTable *vtable)
61 {
62   server->vtable = vtable;
63   server->refcount = 1;
64
65   server->watches = _dbus_watch_list_new ();
66   if (server->watches == NULL)
67     return FALSE;
68
69   server->timeouts = _dbus_timeout_list_new ();
70   if (server->timeouts == NULL)
71     {
72       _dbus_watch_list_free (server->watches);
73       server->watches = NULL;
74       return FALSE;
75     }
76   
77   server->connection_counter = _dbus_counter_new ();
78   if (server->connection_counter == NULL)
79     {
80       _dbus_watch_list_free (server->watches);
81       server->watches = NULL;
82       _dbus_timeout_list_free (server->timeouts);
83       server->timeouts = NULL;
84       
85       return FALSE;
86     }
87
88   server->max_connections = 256; /* same as an X server, seems like a nice default */
89   
90   return TRUE;
91 }
92
93 /**
94  * Finalizes the members of the DBusServer base class.
95  * Chained up to by subclass finalizers.
96  *
97  * @param server the server.
98  */
99 void
100 _dbus_server_finalize_base (DBusServer *server)
101 {
102   dbus_server_set_new_connection_function (server, NULL, NULL, NULL);
103
104   if (!server->disconnected)
105     dbus_server_disconnect (server);
106
107   _dbus_watch_list_free (server->watches);
108   _dbus_timeout_list_free (server->timeouts);
109   _dbus_counter_unref (server->connection_counter);
110 }
111
112 /**
113  * Adds a watch for this server, chaining out to application-provided
114  * watch handlers.
115  *
116  * @param server the server.
117  * @param watch the watch to add.
118  */
119 dbus_bool_t
120 _dbus_server_add_watch (DBusServer *server,
121                         DBusWatch  *watch)
122 {
123   return _dbus_watch_list_add_watch (server->watches, watch);
124 }
125
126 /**
127  * Removes a watch previously added with _dbus_server_remove_watch().
128  *
129  * @param server the server.
130  * @param watch the watch to remove.
131  */
132 void
133 _dbus_server_remove_watch  (DBusServer *server,
134                             DBusWatch  *watch)
135 {
136   _dbus_watch_list_remove_watch (server->watches, watch);
137 }
138
139 /**
140  * Adds a timeout for this server, chaining out to application-provided
141  * timeout handlers.
142  *
143  * @param server the server.
144  * @param timeout the timeout to add.
145  */
146 dbus_bool_t
147 _dbus_server_add_timeout (DBusServer  *server,
148                           DBusTimeout *timeout)
149 {
150   return _dbus_timeout_list_add_timeout (server->timeouts, timeout);
151 }
152
153 /**
154  * Removes a timeout previously added with _dbus_server_add_timeout().
155  *
156  * @param server the server.
157  * @param timeout the timeout to remove.
158  */
159 void
160 _dbus_server_remove_timeout (DBusServer  *server,
161                              DBusTimeout *timeout)
162 {
163   _dbus_timeout_list_remove_timeout (server->timeouts, timeout);  
164 }
165
166 /** @} */
167
168 /**
169  * @addtogroup DBusServer
170  *
171  * @{
172  */
173
174
175 /**
176  * @typedef DBusServer
177  *
178  * An opaque object representing a server that listens for
179  * connections from other applications. Each time a connection
180  * is made, a new DBusConnection is created and made available
181  * via an application-provided DBusNewConnectionFunction.
182  * The DBusNewConnectionFunction is provided with
183  * dbus_server_set_new_connection_function().
184  * 
185  */
186
187 /**
188  * Listens for new connections on the given address.
189  * Returns #NULL if listening fails for any reason.
190  * Otherwise returns a new #DBusServer.
191  * dbus_server_set_new_connection_function() and
192  * dbus_server_set_watch_functions() should be called
193  * immediately to render the server fully functional.
194  *
195  * @todo error messages on bad address could really be better.
196  * DBusResultCode is a bit limiting here.
197  *
198  * @param address the address of this server.
199  * @param result location to store rationale for failure.
200  * @returns a new DBusServer, or #NULL on failure.
201  * 
202  */
203 DBusServer*
204 dbus_server_listen (const char     *address,
205                     DBusResultCode *result)
206 {
207   DBusServer *server;
208   DBusAddressEntry **entries;
209   int len, i;
210   
211   if (!dbus_parse_address (address, &entries, &len, result))
212     return NULL;
213
214   server = NULL;
215   
216   for (i = 0; i < len; i++)
217     {
218       const char *method = dbus_address_entry_get_method (entries[i]);
219
220       if (strcmp (method, "unix") == 0)
221         {
222           const char *path = dbus_address_entry_get_value (entries[i], "path");
223
224           if (path == NULL)
225             goto bad_address;
226
227           server = _dbus_server_new_for_domain_socket (path, result);
228
229           if (server)
230             break;
231         }
232       else if (strcmp (method, "tcp") == 0)
233         {
234           const char *host = dbus_address_entry_get_value (entries[i], "host");
235           const char *port = dbus_address_entry_get_value (entries[i], "port");
236           DBusString  str;
237           long lport;
238           dbus_bool_t sresult;
239           
240           if (port == NULL)
241             goto bad_address;
242
243           _dbus_string_init_const (&str, port);
244           sresult = _dbus_string_parse_int (&str, 0, &lport, NULL);
245           _dbus_string_free (&str);
246           
247           if (sresult == FALSE || lport <= 0 || lport > 65535)
248             goto bad_address;
249           
250           server = _dbus_server_new_for_tcp_socket (host, lport, result);
251
252           if (server)
253             break;
254         }
255 #ifdef DBUS_BUILD_TESTS
256       else if (strcmp (method, "debug") == 0)
257         {
258           const char *name = dbus_address_entry_get_value (entries[i], "name");
259
260           if (name == NULL)
261             goto bad_address;
262
263           server = _dbus_server_debug_new (name, result);
264
265           if (server)
266             break;
267         }
268 #endif
269       else
270         goto bad_address;
271     }
272   
273   dbus_address_entries_free (entries);
274   return server;
275
276  bad_address:
277   dbus_address_entries_free (entries);
278   dbus_set_result (result, DBUS_RESULT_BAD_ADDRESS);
279
280   return NULL;
281 }
282
283 /**
284  * Increments the reference count of a DBusServer.
285  *
286  * @param server the server.
287  */
288 void
289 dbus_server_ref (DBusServer *server)
290 {
291   server->refcount += 1;
292 }
293
294 /**
295  * Decrements the reference count of a DBusServer.  Finalizes the
296  * server if the reference count reaches zero. The server connection
297  * will be closed as with dbus_server_disconnect() when the server is
298  * finalized.
299  *
300  * @param server the server.
301  */
302 void
303 dbus_server_unref (DBusServer *server)
304 {
305   _dbus_assert (server != NULL);
306   _dbus_assert (server->refcount > 0);
307
308   server->refcount -= 1;
309   if (server->refcount == 0)
310     {
311       _dbus_assert (server->vtable->finalize != NULL);
312       
313       (* server->vtable->finalize) (server);
314     }
315 }
316
317 /**
318  * Releases the server's address and stops listening for
319  * new clients. If called more than once, only the first
320  * call has an effect. Does not modify the server's
321  * reference count.
322  * 
323  * @param server the server.
324  */
325 void
326 dbus_server_disconnect (DBusServer *server)
327 {
328   _dbus_assert (server->vtable->disconnect != NULL);
329
330   if (server->disconnected)
331     return;
332   
333   (* server->vtable->disconnect) (server);
334   server->disconnected = TRUE;
335 }
336
337 /**
338  * Returns #TRUE if the server is still listening for new connections.
339  *
340  * @param server the server.
341  */
342 dbus_bool_t
343 dbus_server_get_is_connected (DBusServer *server)
344 {
345   return !server->disconnected;
346 }
347
348 /**
349  * Sets a function to be used for handling new connections.  The given
350  * function is passed each new connection as the connection is
351  * created. If the new connection function increments the connection's
352  * reference count, the connection will stay alive. Otherwise, the
353  * connection will be unreferenced and closed.
354  *
355  * @param server the server.
356  * @param function a function to handle new connections.
357  * @param data data to pass to the new connection handler.
358  * @param free_data_function function to free the data.
359  */
360 void
361 dbus_server_set_new_connection_function (DBusServer                *server,
362                                          DBusNewConnectionFunction  function,
363                                          void                      *data,
364                                          DBusFreeFunction           free_data_function)
365 {
366   if (server->new_connection_free_data_function != NULL)
367     (* server->new_connection_free_data_function) (server->new_connection_data);
368   
369   server->new_connection_function = function;
370   server->new_connection_data = data;
371   server->new_connection_free_data_function = free_data_function;
372 }
373
374 /**
375  * Sets the watch functions for the connection. These functions are
376  * responsible for making the application's main loop aware of file
377  * descriptors that need to be monitored for events.
378  *
379  * This function behaves exactly like dbus_connection_set_watch_functions();
380  * see the documentation for that routine.
381  *
382  * @param server the server.
383  * @param add_function function to begin monitoring a new descriptor.
384  * @param remove_function function to stop monitoring a descriptor.
385  * @param data data to pass to add_function and remove_function.
386  * @param free_data_function function to be called to free the data.
387  */
388 void
389 dbus_server_set_watch_functions (DBusServer              *server,
390                                  DBusAddWatchFunction     add_function,
391                                  DBusRemoveWatchFunction  remove_function,
392                                  void                    *data,
393                                  DBusFreeFunction         free_data_function)
394 {
395   _dbus_watch_list_set_functions (server->watches,
396                                   add_function,
397                                   remove_function,
398                                   data,
399                                   free_data_function);
400 }
401
402 /**
403  * Sets the timeout functions for the connection. These functions are
404  * responsible for making the application's main loop aware of timeouts.
405  *
406  * This function behaves exactly like dbus_connection_set_timeout_functions();
407  * see the documentation for that routine.
408  *
409  * @param server the server.
410  * @param add_function function to add a timeout.
411  * @param remove_function function to remove a timeout.
412  * @param data data to pass to add_function and remove_function.
413  * @param free_data_function function to be called to free the data.
414  */
415 void
416 dbus_server_set_timeout_functions (DBusServer                *server,
417                                    DBusAddTimeoutFunction     add_function,
418                                    DBusRemoveTimeoutFunction  remove_function,
419                                    void                      *data,
420                                    DBusFreeFunction           free_data_function)
421 {
422   _dbus_timeout_list_set_functions (server->timeouts,
423                                     add_function, remove_function,
424                                     data, free_data_function); 
425 }
426
427 /**
428  * Called to notify the server when a previously-added watch
429  * is ready for reading or writing, or has an exception such
430  * as a hangup.
431  *
432  * @param server the server.
433  * @param watch the watch.
434  * @param condition the current condition of the file descriptors being watched.
435  */
436 void
437 dbus_server_handle_watch (DBusServer              *server,
438                           DBusWatch               *watch,
439                           unsigned int             condition)
440 {
441   _dbus_assert (server->vtable->handle_watch != NULL);
442
443   _dbus_watch_sanitize_condition (watch, &condition);
444   
445   (* server->vtable->handle_watch) (server, watch, condition);
446 }
447
448 /**
449  * Sets the maximum number of connections that can be open at one
450  * time for this server. If the maximum is reached, and another
451  * client tries to connect, then the oldest unauthenticated client
452  * will be dropped. If no unauthenticated client exists, then
453  * the new connection will be refused.
454  *
455  * If the maximum is set to a number lower than the current
456  * number of connections, no current connections are
457  * disconnected.
458  *
459  * @todo honoring max_connections has not been implemented
460  * yet. The only real work involved is keeping a list
461  * of live connections on the DBusServer so the oldest
462  * unauthenticated client can be located when required.
463  * 
464  * @todo for a systemwide daemon, we need a max number of connections
465  * per user, since any user can authenticate a bunch of connections
466  * and create a DOS.
467  *
468  * @todo a single process might listen on multiple mechanisms
469  * (multiple DBusServer) and might want the max connections
470  * value to span all those servers. Should consider
471  * changing the API accordingly, though I'm inclined to
472  * punt this to the app that wants to do it instead of
473  * putting it in the library.
474  * 
475  * @param server the server
476  * @param max_connections maximum number of connections allowed
477  */
478 void
479 dbus_server_set_max_connections (DBusServer *server,
480                                  int         max_connections)
481 {
482   server->max_connections = max_connections;
483 }
484
485 /**
486  * Gets the maximum number of connections that can be active
487  * at a time for this server.
488  *
489  * @param server the server
490  * @returns maximum number of connections at once
491  */
492 int
493 dbus_server_get_max_connections (DBusServer *server)
494 {
495   return server->max_connections;
496 }
497
498 /**
499  * Gets the number of #DBusConnection to this server that
500  * have not yet been finalized. i.e. all #DBusConnection that
501  * were passed to #DBusNewConnectionFunction and have not yet been
502  * finalized will count in this total.
503  *
504  * @param server the server
505  * @returns the number of connections
506  */
507 int
508 dbus_server_get_n_connections (DBusServer *server)
509 {
510   return _dbus_counter_get_value (server->connection_counter);
511 }
512
513 /** @} */
514