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