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