[kdbus] sync with kdbus (kdbus.h - commit: 5ae1ecac44cb)
[platform/upstream/glib.git] / gio / gdbusserver.c
1 /* GDBus - GLib D-Bus Library
2  *
3  * Copyright (C) 2008-2010 Red Hat, Inc.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General
16  * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
17  *
18  * Author: David Zeuthen <davidz@redhat.com>
19  */
20
21 #include "config.h"
22
23 #include <stdlib.h>
24 #include <string.h>
25 #include <errno.h>
26
27 #include "giotypes.h"
28 #include "gioerror.h"
29 #include "gdbusaddress.h"
30 #include "gdbusutils.h"
31 #include "gdbusconnection.h"
32 #include "gdbusserver.h"
33 #include "gioenumtypes.h"
34 #include "gdbusprivate.h"
35 #include "gdbusauthobserver.h"
36 #include "ginitable.h"
37 #include "gsocketservice.h"
38 #include "gthreadedsocketservice.h"
39 #include "gresolver.h"
40 #include "glib/gstdio.h"
41 #include "ginetaddress.h"
42 #include "ginetsocketaddress.h"
43 #include "ginputstream.h"
44 #include "giostream.h"
45
46 #ifdef G_OS_UNIX
47 #include <unistd.h>
48 #endif
49 #ifdef G_OS_WIN32
50 #include <io.h>
51 #endif
52
53 #ifdef G_OS_UNIX
54 #include "gunixsocketaddress.h"
55 #endif
56
57 #include "glibintl.h"
58
59 /**
60  * SECTION:gdbusserver
61  * @short_description: Helper for accepting connections
62  * @include: gio/gio.h
63  *
64  * #GDBusServer is a helper for listening to and accepting D-Bus
65  * connections. This can be used to create a new D-Bus server, allowing two
66  * peers to use the D-Bus protocol for their own specialized communication.
67  * A server instance provided in this way will not perform message routing or
68  * implement the org.freedesktop.DBus interface.
69  *
70  * To just export an object on a well-known name on a message bus, such as the
71  * session or system bus, you should instead use g_bus_own_name().
72  *
73  * An example of peer-to-peer communication with G-DBus can be found
74  * in [gdbus-example-peer.c](https://git.gnome.org/browse/glib/tree/gio/tests/gdbus-example-peer.c).
75  */
76
77 /**
78  * GDBusServer:
79  *
80  * The #GDBusServer structure contains only private data and
81  * should only be accessed using the provided API.
82  *
83  * Since: 2.26
84  */
85 struct _GDBusServer
86 {
87   /*< private >*/
88   GObject parent_instance;
89
90   GDBusServerFlags flags;
91   gchar *address;
92   gchar *guid;
93
94   guchar *nonce;
95   gchar *nonce_file;
96
97   gchar *client_address;
98
99   GSocketListener *listener;
100   gboolean is_using_listener;
101   gulong run_signal_handler_id;
102
103   /* The result of g_main_context_ref_thread_default() when the object
104    * was created (the GObject _init() function) - this is used for delivery
105    * of the :new-connection GObject signal.
106    */
107   GMainContext *main_context_at_construction;
108
109   gboolean active;
110
111   GDBusAuthObserver *authentication_observer;
112 };
113
114 typedef struct _GDBusServerClass GDBusServerClass;
115
116 /**
117  * GDBusServerClass:
118  * @new_connection: Signal class handler for the #GDBusServer::new-connection signal.
119  *
120  * Class structure for #GDBusServer.
121  *
122  * Since: 2.26
123  */
124 struct _GDBusServerClass
125 {
126   /*< private >*/
127   GObjectClass parent_class;
128
129   /*< public >*/
130   /* Signals */
131   gboolean (*new_connection) (GDBusServer      *server,
132                               GDBusConnection  *connection);
133 };
134
135 enum
136 {
137   PROP_0,
138   PROP_ADDRESS,
139   PROP_CLIENT_ADDRESS,
140   PROP_FLAGS,
141   PROP_GUID,
142   PROP_ACTIVE,
143   PROP_AUTHENTICATION_OBSERVER,
144 };
145
146 enum
147 {
148   NEW_CONNECTION_SIGNAL,
149   LAST_SIGNAL,
150 };
151
152 static guint _signals[LAST_SIGNAL] = {0};
153
154 static void initable_iface_init       (GInitableIface *initable_iface);
155
156 G_DEFINE_TYPE_WITH_CODE (GDBusServer, g_dbus_server, G_TYPE_OBJECT,
157                          G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE, initable_iface_init)
158                          );
159
160 static void
161 g_dbus_server_finalize (GObject *object)
162 {
163   GDBusServer *server = G_DBUS_SERVER (object);
164
165   if (server->authentication_observer != NULL)
166     g_object_unref (server->authentication_observer);
167
168   if (server->run_signal_handler_id > 0)
169     g_signal_handler_disconnect (server->listener, server->run_signal_handler_id);
170
171   if (server->listener != NULL)
172     g_object_unref (server->listener);
173
174   g_free (server->address);
175   g_free (server->guid);
176   g_free (server->client_address);
177   if (server->nonce != NULL)
178     {
179       memset (server->nonce, '\0', 16);
180       g_free (server->nonce);
181     }
182   /* we could unlink the nonce file but I don't
183    * think it's really worth the effort/risk
184    */
185   g_free (server->nonce_file);
186
187   g_main_context_unref (server->main_context_at_construction);
188
189   G_OBJECT_CLASS (g_dbus_server_parent_class)->finalize (object);
190 }
191
192 static void
193 g_dbus_server_get_property (GObject    *object,
194                             guint       prop_id,
195                             GValue     *value,
196                             GParamSpec *pspec)
197 {
198   GDBusServer *server = G_DBUS_SERVER (object);
199
200   switch (prop_id)
201     {
202     case PROP_FLAGS:
203       g_value_set_flags (value, server->flags);
204       break;
205
206     case PROP_GUID:
207       g_value_set_string (value, server->guid);
208       break;
209
210     case PROP_ADDRESS:
211       g_value_set_string (value, server->address);
212       break;
213
214     case PROP_CLIENT_ADDRESS:
215       g_value_set_string (value, server->client_address);
216       break;
217
218     case PROP_ACTIVE:
219       g_value_set_boolean (value, server->active);
220       break;
221
222     case PROP_AUTHENTICATION_OBSERVER:
223       g_value_set_object (value, server->authentication_observer);
224       break;
225
226     default:
227       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
228       break;
229     }
230 }
231
232 static void
233 g_dbus_server_set_property (GObject      *object,
234                             guint         prop_id,
235                             const GValue *value,
236                             GParamSpec   *pspec)
237 {
238   GDBusServer *server = G_DBUS_SERVER (object);
239
240   switch (prop_id)
241     {
242     case PROP_FLAGS:
243       server->flags = g_value_get_flags (value);
244       break;
245
246     case PROP_GUID:
247       server->guid = g_value_dup_string (value);
248       break;
249
250     case PROP_ADDRESS:
251       server->address = g_value_dup_string (value);
252       break;
253
254     case PROP_AUTHENTICATION_OBSERVER:
255       server->authentication_observer = g_value_dup_object (value);
256       break;
257
258     default:
259       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
260       break;
261     }
262 }
263
264 static void
265 g_dbus_server_class_init (GDBusServerClass *klass)
266 {
267   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
268
269   gobject_class->finalize     = g_dbus_server_finalize;
270   gobject_class->set_property = g_dbus_server_set_property;
271   gobject_class->get_property = g_dbus_server_get_property;
272
273   /**
274    * GDBusServer:flags:
275    *
276    * Flags from the #GDBusServerFlags enumeration.
277    *
278    * Since: 2.26
279    */
280   g_object_class_install_property (gobject_class,
281                                    PROP_FLAGS,
282                                    g_param_spec_flags ("flags",
283                                                        P_("Flags"),
284                                                        P_("Flags for the server"),
285                                                        G_TYPE_DBUS_SERVER_FLAGS,
286                                                        G_DBUS_SERVER_FLAGS_NONE,
287                                                        G_PARAM_READABLE |
288                                                        G_PARAM_WRITABLE |
289                                                        G_PARAM_CONSTRUCT_ONLY |
290                                                        G_PARAM_STATIC_NAME |
291                                                        G_PARAM_STATIC_BLURB |
292                                                        G_PARAM_STATIC_NICK));
293
294   /**
295    * GDBusServer:guid:
296    *
297    * The guid of the server.
298    *
299    * Since: 2.26
300    */
301   g_object_class_install_property (gobject_class,
302                                    PROP_GUID,
303                                    g_param_spec_string ("guid",
304                                                         P_("GUID"),
305                                                         P_("The guid of the server"),
306                                                         NULL,
307                                                         G_PARAM_READABLE |
308                                                         G_PARAM_WRITABLE |
309                                                         G_PARAM_CONSTRUCT_ONLY |
310                                                         G_PARAM_STATIC_NAME |
311                                                         G_PARAM_STATIC_BLURB |
312                                                         G_PARAM_STATIC_NICK));
313
314   /**
315    * GDBusServer:address:
316    *
317    * The D-Bus address to listen on.
318    *
319    * Since: 2.26
320    */
321   g_object_class_install_property (gobject_class,
322                                    PROP_ADDRESS,
323                                    g_param_spec_string ("address",
324                                                         P_("Address"),
325                                                         P_("The address to listen on"),
326                                                         NULL,
327                                                         G_PARAM_READABLE |
328                                                         G_PARAM_WRITABLE |
329                                                         G_PARAM_CONSTRUCT_ONLY |
330                                                         G_PARAM_STATIC_NAME |
331                                                         G_PARAM_STATIC_BLURB |
332                                                         G_PARAM_STATIC_NICK));
333
334   /**
335    * GDBusServer:client-address:
336    *
337    * The D-Bus address that clients can use.
338    *
339    * Since: 2.26
340    */
341   g_object_class_install_property (gobject_class,
342                                    PROP_CLIENT_ADDRESS,
343                                    g_param_spec_string ("client-address",
344                                                         P_("Client Address"),
345                                                         P_("The address clients can use"),
346                                                         NULL,
347                                                         G_PARAM_READABLE |
348                                                         G_PARAM_STATIC_NAME |
349                                                         G_PARAM_STATIC_BLURB |
350                                                         G_PARAM_STATIC_NICK));
351
352   /**
353    * GDBusServer:active:
354    *
355    * Whether the server is currently active.
356    *
357    * Since: 2.26
358    */
359   g_object_class_install_property (gobject_class,
360                                    PROP_ACTIVE,
361                                    g_param_spec_boolean ("active",
362                                                          P_("Active"),
363                                                          P_("Whether the server is currently active"),
364                                                          FALSE,
365                                                          G_PARAM_READABLE |
366                                                          G_PARAM_STATIC_NAME |
367                                                          G_PARAM_STATIC_BLURB |
368                                                          G_PARAM_STATIC_NICK));
369
370   /**
371    * GDBusServer:authentication-observer:
372    *
373    * A #GDBusAuthObserver object to assist in the authentication process or %NULL.
374    *
375    * Since: 2.26
376    */
377   g_object_class_install_property (gobject_class,
378                                    PROP_AUTHENTICATION_OBSERVER,
379                                    g_param_spec_object ("authentication-observer",
380                                                         P_("Authentication Observer"),
381                                                         P_("Object used to assist in the authentication process"),
382                                                         G_TYPE_DBUS_AUTH_OBSERVER,
383                                                         G_PARAM_READABLE |
384                                                         G_PARAM_WRITABLE |
385                                                         G_PARAM_CONSTRUCT_ONLY |
386                                                         G_PARAM_STATIC_NAME |
387                                                         G_PARAM_STATIC_BLURB |
388                                                         G_PARAM_STATIC_NICK));
389
390   /**
391    * GDBusServer::new-connection:
392    * @server: The #GDBusServer emitting the signal.
393    * @connection: A #GDBusConnection for the new connection.
394    *
395    * Emitted when a new authenticated connection has been made. Use
396    * g_dbus_connection_get_peer_credentials() to figure out what
397    * identity (if any), was authenticated.
398    *
399    * If you want to accept the connection, take a reference to the
400    * @connection object and return %TRUE. When you are done with the
401    * connection call g_dbus_connection_close() and give up your
402    * reference. Note that the other peer may disconnect at any time -
403    * a typical thing to do when accepting a connection is to listen to
404    * the #GDBusConnection::closed signal.
405    *
406    * If #GDBusServer:flags contains %G_DBUS_SERVER_FLAGS_RUN_IN_THREAD
407    * then the signal is emitted in a new thread dedicated to the
408    * connection. Otherwise the signal is emitted in the
409    * [thread-default main context][g-main-context-push-thread-default]
410    * of the thread that @server was constructed in.
411    *
412    * You are guaranteed that signal handlers for this signal runs
413    * before incoming messages on @connection are processed. This means
414    * that it's suitable to call g_dbus_connection_register_object() or
415    * similar from the signal handler.
416    *
417    * Returns: %TRUE to claim @connection, %FALSE to let other handlers
418    * run.
419    *
420    * Since: 2.26
421    */
422   _signals[NEW_CONNECTION_SIGNAL] = g_signal_new ("new-connection",
423                                                   G_TYPE_DBUS_SERVER,
424                                                   G_SIGNAL_RUN_LAST,
425                                                   G_STRUCT_OFFSET (GDBusServerClass, new_connection),
426                                                   g_signal_accumulator_true_handled,
427                                                   NULL, /* accu_data */
428                                                   NULL,
429                                                   G_TYPE_BOOLEAN,
430                                                   1,
431                                                   G_TYPE_DBUS_CONNECTION);
432 }
433
434 static void
435 g_dbus_server_init (GDBusServer *server)
436 {
437   server->main_context_at_construction = g_main_context_ref_thread_default ();
438 }
439
440 static gboolean
441 on_run (GSocketService    *service,
442         GSocketConnection *socket_connection,
443         GObject           *source_object,
444         gpointer           user_data);
445
446 /**
447  * g_dbus_server_new_sync:
448  * @address: A D-Bus address.
449  * @flags: Flags from the #GDBusServerFlags enumeration.
450  * @guid: A D-Bus GUID.
451  * @observer: (allow-none): A #GDBusAuthObserver or %NULL.
452  * @cancellable: (allow-none): A #GCancellable or %NULL.
453  * @error: Return location for server or %NULL.
454  *
455  * Creates a new D-Bus server that listens on the first address in
456  * @address that works.
457  *
458  * Once constructed, you can use g_dbus_server_get_client_address() to
459  * get a D-Bus address string that clients can use to connect.
460  *
461  * Connect to the #GDBusServer::new-connection signal to handle
462  * incoming connections.
463  *
464  * The returned #GDBusServer isn't active - you have to start it with
465  * g_dbus_server_start().
466  *
467  * #GDBusServer is used in this [example][gdbus-peer-to-peer].
468  *
469  * This is a synchronous failable constructor. See
470  * g_dbus_server_new() for the asynchronous version.
471  *
472  * Returns: A #GDBusServer or %NULL if @error is set. Free with
473  * g_object_unref().
474  *
475  * Since: 2.26
476  */
477 GDBusServer *
478 g_dbus_server_new_sync (const gchar        *address,
479                         GDBusServerFlags    flags,
480                         const gchar        *guid,
481                         GDBusAuthObserver  *observer,
482                         GCancellable       *cancellable,
483                         GError            **error)
484 {
485   GDBusServer *server;
486
487   g_return_val_if_fail (address != NULL, NULL);
488   g_return_val_if_fail (g_dbus_is_guid (guid), NULL);
489   g_return_val_if_fail (error == NULL || *error == NULL, NULL);
490
491   server = g_initable_new (G_TYPE_DBUS_SERVER,
492                            cancellable,
493                            error,
494                            "address", address,
495                            "flags", flags,
496                            "guid", guid,
497                            "authentication-observer", observer,
498                            NULL);
499
500   return server;
501 }
502
503 /**
504  * g_dbus_server_get_client_address:
505  * @server: A #GDBusServer.
506  *
507  * Gets a D-Bus address string that can be used by clients to connect
508  * to @server.
509  *
510  * Returns: A D-Bus address string. Do not free, the string is owned
511  * by @server.
512  *
513  * Since: 2.26
514  */
515 const gchar *
516 g_dbus_server_get_client_address (GDBusServer *server)
517 {
518   g_return_val_if_fail (G_IS_DBUS_SERVER (server), NULL);
519   return server->client_address;
520 }
521
522 /**
523  * g_dbus_server_get_guid:
524  * @server: A #GDBusServer.
525  *
526  * Gets the GUID for @server.
527  *
528  * Returns: A D-Bus GUID. Do not free this string, it is owned by @server.
529  *
530  * Since: 2.26
531  */
532 const gchar *
533 g_dbus_server_get_guid (GDBusServer *server)
534 {
535   g_return_val_if_fail (G_IS_DBUS_SERVER (server), NULL);
536   return server->guid;
537 }
538
539 /**
540  * g_dbus_server_get_flags:
541  * @server: A #GDBusServer.
542  *
543  * Gets the flags for @server.
544  *
545  * Returns: A set of flags from the #GDBusServerFlags enumeration.
546  *
547  * Since: 2.26
548  */
549 GDBusServerFlags
550 g_dbus_server_get_flags (GDBusServer *server)
551 {
552   g_return_val_if_fail (G_IS_DBUS_SERVER (server), G_DBUS_SERVER_FLAGS_NONE);
553   return server->flags;
554 }
555
556 /**
557  * g_dbus_server_is_active:
558  * @server: A #GDBusServer.
559  *
560  * Gets whether @server is active.
561  *
562  * Returns: %TRUE if server is active, %FALSE otherwise.
563  *
564  * Since: 2.26
565  */
566 gboolean
567 g_dbus_server_is_active (GDBusServer *server)
568 {
569   g_return_val_if_fail (G_IS_DBUS_SERVER (server), G_DBUS_SERVER_FLAGS_NONE);
570   return server->active;
571 }
572
573 /**
574  * g_dbus_server_start:
575  * @server: A #GDBusServer.
576  *
577  * Starts @server.
578  *
579  * Since: 2.26
580  */
581 void
582 g_dbus_server_start (GDBusServer *server)
583 {
584   g_return_if_fail (G_IS_DBUS_SERVER (server));
585   if (server->active)
586     return;
587   /* Right now we don't have any transport not using the listener... */
588   g_assert (server->is_using_listener);
589   g_socket_service_start (G_SOCKET_SERVICE (server->listener));
590   server->active = TRUE;
591   g_object_notify (G_OBJECT (server), "active");
592 }
593
594 /**
595  * g_dbus_server_stop:
596  * @server: A #GDBusServer.
597  *
598  * Stops @server.
599  *
600  * Since: 2.26
601  */
602 void
603 g_dbus_server_stop (GDBusServer *server)
604 {
605   g_return_if_fail (G_IS_DBUS_SERVER (server));
606   if (!server->active)
607     return;
608   /* Right now we don't have any transport not using the listener... */
609   g_assert (server->is_using_listener);
610   g_assert (server->run_signal_handler_id > 0);
611   g_signal_handler_disconnect (server->listener, server->run_signal_handler_id);
612   server->run_signal_handler_id = 0;
613   g_socket_service_stop (G_SOCKET_SERVICE (server->listener));
614   server->active = FALSE;
615   g_object_notify (G_OBJECT (server), "active");
616 }
617
618 /* ---------------------------------------------------------------------------------------------------- */
619
620 #ifdef G_OS_UNIX
621
622 static gint
623 random_ascii (void)
624 {
625   gint ret;
626   ret = g_random_int_range (0, 60);
627   if (ret < 25)
628     ret += 'A';
629   else if (ret < 50)
630     ret += 'a' - 25;
631   else
632     ret += '0' - 50;
633   return ret;
634 }
635
636 /* note that address_entry has already been validated => exactly one of path, tmpdir or abstract keys are set */
637 static gboolean
638 try_unix (GDBusServer  *server,
639           const gchar  *address_entry,
640           GHashTable   *key_value_pairs,
641           GError      **error)
642 {
643   gboolean ret;
644   const gchar *path;
645   const gchar *tmpdir;
646   const gchar *abstract;
647   GSocketAddress *address;
648
649   ret = FALSE;
650   address = NULL;
651
652   path = g_hash_table_lookup (key_value_pairs, "path");
653   tmpdir = g_hash_table_lookup (key_value_pairs, "tmpdir");
654   abstract = g_hash_table_lookup (key_value_pairs, "abstract");
655
656   if (path != NULL)
657     {
658       address = g_unix_socket_address_new (path);
659     }
660   else if (tmpdir != NULL)
661     {
662       gint n;
663       GString *s;
664       GError *local_error;
665
666     retry:
667       s = g_string_new (tmpdir);
668       g_string_append (s, "/dbus-");
669       for (n = 0; n < 8; n++)
670         g_string_append_c (s, random_ascii ());
671
672       /* prefer abstract namespace if available */
673       if (g_unix_socket_address_abstract_names_supported ())
674         address = g_unix_socket_address_new_with_type (s->str,
675                                                        -1,
676                                                        G_UNIX_SOCKET_ADDRESS_ABSTRACT);
677       else
678         address = g_unix_socket_address_new (s->str);
679       g_string_free (s, TRUE);
680
681       local_error = NULL;
682       if (!g_socket_listener_add_address (server->listener,
683                                           address,
684                                           G_SOCKET_TYPE_STREAM,
685                                           G_SOCKET_PROTOCOL_DEFAULT,
686                                           NULL, /* source_object */
687                                           NULL, /* effective_address */
688                                           &local_error))
689         {
690           if (local_error->domain == G_IO_ERROR && local_error->code == G_IO_ERROR_ADDRESS_IN_USE)
691             {
692               g_error_free (local_error);
693               goto retry;
694             }
695           g_propagate_error (error, local_error);
696           goto out;
697         }
698       ret = TRUE;
699       goto out;
700     }
701   else if (abstract != NULL)
702     {
703       if (!g_unix_socket_address_abstract_names_supported ())
704         {
705           g_set_error_literal (error,
706                                G_IO_ERROR,
707                                G_IO_ERROR_NOT_SUPPORTED,
708                                _("Abstract name space not supported"));
709           goto out;
710         }
711       address = g_unix_socket_address_new_with_type (abstract,
712                                                      -1,
713                                                      G_UNIX_SOCKET_ADDRESS_ABSTRACT);
714     }
715   else
716     {
717       g_assert_not_reached ();
718     }
719
720   if (!g_socket_listener_add_address (server->listener,
721                                       address,
722                                       G_SOCKET_TYPE_STREAM,
723                                       G_SOCKET_PROTOCOL_DEFAULT,
724                                       NULL, /* source_object */
725                                       NULL, /* effective_address */
726                                       error))
727     goto out;
728
729   ret = TRUE;
730
731  out:
732
733   if (address != NULL)
734     {
735       /* Fill out client_address if the connection attempt worked */
736       if (ret)
737         {
738           server->is_using_listener = TRUE;
739
740           switch (g_unix_socket_address_get_address_type (G_UNIX_SOCKET_ADDRESS (address)))
741             {
742             case G_UNIX_SOCKET_ADDRESS_ABSTRACT:
743               server->client_address = g_strdup_printf ("unix:abstract=%s",
744                                                         g_unix_socket_address_get_path (G_UNIX_SOCKET_ADDRESS (address)));
745               break;
746
747             case G_UNIX_SOCKET_ADDRESS_PATH:
748               server->client_address = g_strdup_printf ("unix:path=%s",
749                                                         g_unix_socket_address_get_path (G_UNIX_SOCKET_ADDRESS (address)));
750               break;
751
752             default:
753               g_assert_not_reached ();
754               break;
755             }
756         }
757       g_object_unref (address);
758     }
759   return ret;
760 }
761 #endif
762
763 /* ---------------------------------------------------------------------------------------------------- */
764
765 /* note that address_entry has already been validated =>
766  *  both host and port (guaranteed to be a number in [0, 65535]) are set (family is optional)
767  */
768 static gboolean
769 try_tcp (GDBusServer  *server,
770          const gchar  *address_entry,
771          GHashTable   *key_value_pairs,
772          gboolean      do_nonce,
773          GError      **error)
774 {
775   gboolean ret;
776   const gchar *host;
777   const gchar *port;
778   gint port_num;
779   GResolver *resolver;
780   GList *resolved_addresses;
781   GList *l;
782
783   ret = FALSE;
784   resolver = NULL;
785   resolved_addresses = NULL;
786
787   host = g_hash_table_lookup (key_value_pairs, "host");
788   port = g_hash_table_lookup (key_value_pairs, "port");
789   /* family = g_hash_table_lookup (key_value_pairs, "family"); */
790   if (g_hash_table_lookup (key_value_pairs, "noncefile") != NULL)
791     {
792       g_set_error_literal (error,
793                            G_IO_ERROR,
794                            G_IO_ERROR_INVALID_ARGUMENT,
795                            _("Cannot specify nonce file when creating a server"));
796       goto out;
797     }
798
799   if (host == NULL)
800     host = "localhost";
801   if (port == NULL)
802     port = "0";
803   port_num = strtol (port, NULL, 10);
804
805   resolver = g_resolver_get_default ();
806   resolved_addresses = g_resolver_lookup_by_name (resolver,
807                                                   host,
808                                                   NULL,
809                                                   error);
810   if (resolved_addresses == NULL)
811     goto out;
812
813   /* TODO: handle family */
814   for (l = resolved_addresses; l != NULL; l = l->next)
815     {
816       GInetAddress *address = G_INET_ADDRESS (l->data);
817       GSocketAddress *socket_address;
818       GSocketAddress *effective_address;
819
820       socket_address = g_inet_socket_address_new (address, port_num);
821       if (!g_socket_listener_add_address (server->listener,
822                                           socket_address,
823                                           G_SOCKET_TYPE_STREAM,
824                                           G_SOCKET_PROTOCOL_TCP,
825                                           NULL, /* GObject *source_object */
826                                           &effective_address,
827                                           error))
828         {
829           g_object_unref (socket_address);
830           goto out;
831         }
832       if (port_num == 0)
833         /* make sure we allocate the same port number for other listeners */
834         port_num = g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (effective_address));
835
836       g_object_unref (effective_address);
837       g_object_unref (socket_address);
838     }
839
840   if (do_nonce)
841     {
842       gint fd;
843       guint n;
844       gsize bytes_written;
845       gsize bytes_remaining;
846       char *file_escaped;
847
848       server->nonce = g_new0 (guchar, 16);
849       for (n = 0; n < 16; n++)
850         server->nonce[n] = g_random_int_range (0, 256);
851       fd = g_file_open_tmp ("gdbus-nonce-file-XXXXXX",
852                             &server->nonce_file,
853                             error);
854       if (fd == -1)
855         {
856           g_socket_listener_close (server->listener);
857           goto out;
858         }
859     again:
860       bytes_written = 0;
861       bytes_remaining = 16;
862       while (bytes_remaining > 0)
863         {
864           gssize ret;
865           ret = write (fd, server->nonce + bytes_written, bytes_remaining);
866           if (ret == -1)
867             {
868               if (errno == EINTR)
869                 goto again;
870               g_set_error (error,
871                            G_IO_ERROR,
872                            g_io_error_from_errno (errno),
873                            _("Error writing nonce file at '%s': %s"),
874                            server->nonce_file,
875                            strerror (errno));
876               goto out;
877             }
878           bytes_written += ret;
879           bytes_remaining -= ret;
880         }
881       if (!g_close (fd, error))
882         goto out;
883       file_escaped = g_uri_escape_string (server->nonce_file, "/\\", FALSE);
884       server->client_address = g_strdup_printf ("nonce-tcp:host=%s,port=%d,noncefile=%s",
885                                                 host,
886                                                 port_num,
887                                                 file_escaped);
888       g_free (file_escaped);
889     }
890   else
891     {
892       server->client_address = g_strdup_printf ("tcp:host=%s,port=%d", host, port_num);
893     }
894   server->is_using_listener = TRUE;
895   ret = TRUE;
896
897  out:
898   g_list_free_full (resolved_addresses, g_object_unref);
899   if (resolver)
900     g_object_unref (resolver);
901   return ret;
902 }
903
904 /* ---------------------------------------------------------------------------------------------------- */
905
906 typedef struct
907 {
908   GDBusServer *server;
909   GDBusConnection *connection;
910 } EmitIdleData;
911
912 static void
913 emit_idle_data_free (EmitIdleData *data)
914 {
915   g_object_unref (data->server);
916   g_object_unref (data->connection);
917   g_free (data);
918 }
919
920 static gboolean
921 emit_new_connection_in_idle (gpointer user_data)
922 {
923   EmitIdleData *data = user_data;
924   gboolean claimed;
925
926   claimed = FALSE;
927   g_signal_emit (data->server,
928                  _signals[NEW_CONNECTION_SIGNAL],
929                  0,
930                  data->connection,
931                  &claimed);
932
933   if (claimed)
934     g_dbus_connection_start_message_processing (data->connection);
935   g_object_unref (data->connection);
936
937   return FALSE;
938 }
939
940 /* Called in new thread */
941 static gboolean
942 on_run (GSocketService    *service,
943         GSocketConnection *socket_connection,
944         GObject           *source_object,
945         gpointer           user_data)
946 {
947   GDBusServer *server = G_DBUS_SERVER (user_data);
948   GDBusConnection *connection;
949   GDBusConnectionFlags connection_flags;
950
951   if (server->nonce != NULL)
952     {
953       gchar buf[16];
954       gsize bytes_read;
955
956       if (!g_input_stream_read_all (g_io_stream_get_input_stream (G_IO_STREAM (socket_connection)),
957                                     buf,
958                                     16,
959                                     &bytes_read,
960                                     NULL,  /* GCancellable */
961                                     NULL)) /* GError */
962         goto out;
963
964       if (bytes_read != 16)
965         goto out;
966
967       if (memcmp (buf, server->nonce, 16) != 0)
968         goto out;
969     }
970
971   connection_flags =
972     G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_SERVER |
973     G_DBUS_CONNECTION_FLAGS_DELAY_MESSAGE_PROCESSING;
974   if (server->flags & G_DBUS_SERVER_FLAGS_AUTHENTICATION_ALLOW_ANONYMOUS)
975     connection_flags |= G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_ALLOW_ANONYMOUS;
976
977   connection = g_dbus_connection_new_sync (G_IO_STREAM (socket_connection),
978                                            server->guid,
979                                            connection_flags,
980                                            server->authentication_observer,
981                                            NULL,  /* GCancellable */
982                                            NULL); /* GError */
983   if (connection == NULL)
984       goto out;
985
986   if (server->flags & G_DBUS_SERVER_FLAGS_RUN_IN_THREAD)
987     {
988       gboolean claimed;
989
990       claimed = FALSE;
991       g_signal_emit (server,
992                      _signals[NEW_CONNECTION_SIGNAL],
993                      0,
994                      connection,
995                      &claimed);
996       if (claimed)
997         g_dbus_connection_start_message_processing (connection);
998       g_object_unref (connection);
999     }
1000   else
1001     {
1002       GSource *idle_source;
1003       EmitIdleData *data;
1004
1005       data = g_new0 (EmitIdleData, 1);
1006       data->server = g_object_ref (server);
1007       data->connection = g_object_ref (connection);
1008
1009       idle_source = g_idle_source_new ();
1010       g_source_set_priority (idle_source, G_PRIORITY_DEFAULT);
1011       g_source_set_callback (idle_source,
1012                              emit_new_connection_in_idle,
1013                              data,
1014                              (GDestroyNotify) emit_idle_data_free);
1015       g_source_set_name (idle_source, "[gio] emit_new_connection_in_idle");
1016       g_source_attach (idle_source, server->main_context_at_construction);
1017       g_source_unref (idle_source);
1018     }
1019
1020  out:
1021   return TRUE;
1022 }
1023
1024 static gboolean
1025 initable_init (GInitable     *initable,
1026                GCancellable  *cancellable,
1027                GError       **error)
1028 {
1029   GDBusServer *server = G_DBUS_SERVER (initable);
1030   gboolean ret;
1031   guint n;
1032   gchar **addr_array;
1033   GError *last_error;
1034
1035   ret = FALSE;
1036   addr_array = NULL;
1037   last_error = NULL;
1038
1039   if (!g_dbus_is_guid (server->guid))
1040     {
1041       g_set_error (&last_error,
1042                    G_IO_ERROR,
1043                    G_IO_ERROR_INVALID_ARGUMENT,
1044                    _("The string '%s' is not a valid D-Bus GUID"),
1045                    server->guid);
1046       goto out;
1047     }
1048
1049   server->listener = G_SOCKET_LISTENER (g_threaded_socket_service_new (-1));
1050
1051   addr_array = g_strsplit (server->address, ";", 0);
1052   last_error = NULL;
1053   for (n = 0; addr_array != NULL && addr_array[n] != NULL; n++)
1054     {
1055       const gchar *address_entry = addr_array[n];
1056       GHashTable *key_value_pairs;
1057       gchar *transport_name;
1058       GError *this_error;
1059
1060       this_error = NULL;
1061       if (g_dbus_is_supported_address (address_entry,
1062                                        &this_error) &&
1063           _g_dbus_address_parse_entry (address_entry,
1064                                        &transport_name,
1065                                        &key_value_pairs,
1066                                        &this_error))
1067         {
1068
1069           if (FALSE)
1070             {
1071             }
1072 #ifdef G_OS_UNIX
1073           else if (g_strcmp0 (transport_name, "unix") == 0)
1074             ret = try_unix (server, address_entry, key_value_pairs, &this_error);
1075 #endif
1076           else if (g_strcmp0 (transport_name, "tcp") == 0)
1077             ret = try_tcp (server, address_entry, key_value_pairs, FALSE, &this_error);
1078           else if (g_strcmp0 (transport_name, "nonce-tcp") == 0)
1079             ret = try_tcp (server, address_entry, key_value_pairs, TRUE, &this_error);
1080           else
1081             g_set_error (&this_error,
1082                          G_IO_ERROR,
1083                          G_IO_ERROR_INVALID_ARGUMENT,
1084                          _("Cannot listen on unsupported transport '%s'"),
1085                          transport_name);
1086
1087           g_free (transport_name);
1088           if (key_value_pairs != NULL)
1089             g_hash_table_unref (key_value_pairs);
1090
1091           if (ret)
1092             {
1093               g_assert (this_error == NULL);
1094               goto out;
1095             }
1096         }
1097
1098       if (this_error != NULL)
1099         {
1100           if (last_error != NULL)
1101             g_error_free (last_error);
1102           last_error = this_error;
1103         }
1104     }
1105
1106  out:
1107
1108   g_strfreev (addr_array);
1109
1110   if (ret)
1111     {
1112       if (last_error != NULL)
1113         g_error_free (last_error);
1114
1115       /* Right now we don't have any transport not using the listener... */
1116       g_assert (server->is_using_listener);
1117       server->run_signal_handler_id = g_signal_connect (G_SOCKET_SERVICE (server->listener),
1118                                                         "run",
1119                                                         G_CALLBACK (on_run),
1120                                                         server);
1121     }
1122   else
1123     {
1124       g_assert (last_error != NULL);
1125       g_propagate_error (error, last_error);
1126     }
1127   return ret;
1128 }
1129
1130
1131 static void
1132 initable_iface_init (GInitableIface *initable_iface)
1133 {
1134   initable_iface->init = initable_init;
1135 }
1136
1137 /* ---------------------------------------------------------------------------------------------------- */