GApplication: allow handles_commandline and service
[platform/upstream/glib.git] / gio / gsocketconnection.c
1 /* GIO - GLib Input, Output and Streaming Library
2  *
3  * Copyright © 2008 Christian Kellner, Samuel Cormier-Iijima
4  *           © 2008 codethink
5  * Copyright © 2009 Red Hat, Inc
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General
18  * Public License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20  * Boston, MA 02111-1307, USA.
21  *
22  * Authors: Christian Kellner <gicmo@gnome.org>
23  *          Samuel Cormier-Iijima <sciyoshi@gmail.com>
24  *          Ryan Lortie <desrt@desrt.ca>
25  *          Alexander Larsson <alexl@redhat.com>
26  */
27
28 #include "config.h"
29
30 #include "gsocketconnection.h"
31
32 #include "gsocketoutputstream.h"
33 #include "gsocketinputstream.h"
34 #include "gioprivate.h"
35 #include <gio/giostream.h>
36 #include <gio/gtask.h>
37 #include "gunixconnection.h"
38 #include "gtcpconnection.h"
39 #include "glibintl.h"
40
41
42 /**
43  * SECTION:gsocketconnection
44  * @short_description: A socket connection
45  * @include: gio/gio.h
46  * @see_also: #GIOStream, #GSocketClient, #GSocketListener
47  *
48  * #GSocketConnection is a #GIOStream for a connected socket. They
49  * can be created either by #GSocketClient when connecting to a host,
50  * or by #GSocketListener when accepting a new client.
51  *
52  * The type of the #GSocketConnection object returned from these calls
53  * depends on the type of the underlying socket that is in use. For
54  * instance, for a TCP/IP connection it will be a #GTcpConnection.
55  *
56  * Choosing what type of object to construct is done with the socket
57  * connection factory, and it is possible for 3rd parties to register
58  * custom socket connection types for specific combination of socket
59  * family/type/protocol using g_socket_connection_factory_register_type().
60  *
61  * Since: 2.22
62  */
63
64 enum
65 {
66   PROP_NONE,
67   PROP_SOCKET,
68 };
69
70 struct _GSocketConnectionPrivate
71 {
72   GSocket       *socket;
73   GInputStream  *input_stream;
74   GOutputStream *output_stream;
75
76   GSocketAddress *cached_remote_address;
77
78   gboolean       in_dispose;
79 };
80
81 static gboolean g_socket_connection_close         (GIOStream            *stream,
82                                                    GCancellable         *cancellable,
83                                                    GError              **error);
84 static void     g_socket_connection_close_async   (GIOStream            *stream,
85                                                    int                   io_priority,
86                                                    GCancellable         *cancellable,
87                                                    GAsyncReadyCallback   callback,
88                                                    gpointer              user_data);
89 static gboolean g_socket_connection_close_finish  (GIOStream            *stream,
90                                                    GAsyncResult         *result,
91                                                    GError              **error);
92
93 G_DEFINE_TYPE_WITH_PRIVATE (GSocketConnection, g_socket_connection, G_TYPE_IO_STREAM)
94
95 static GInputStream *
96 g_socket_connection_get_input_stream (GIOStream *io_stream)
97 {
98   GSocketConnection *connection = G_SOCKET_CONNECTION (io_stream);
99
100   if (connection->priv->input_stream == NULL)
101     connection->priv->input_stream = (GInputStream *)
102       _g_socket_input_stream_new (connection->priv->socket);
103
104   return connection->priv->input_stream;
105 }
106
107 static GOutputStream *
108 g_socket_connection_get_output_stream (GIOStream *io_stream)
109 {
110   GSocketConnection *connection = G_SOCKET_CONNECTION (io_stream);
111
112   if (connection->priv->output_stream == NULL)
113     connection->priv->output_stream = (GOutputStream *)
114       _g_socket_output_stream_new (connection->priv->socket);
115
116   return connection->priv->output_stream;
117 }
118
119 /**
120  * g_socket_connection_is_connected:
121  * @connection: a #GSocketConnection
122  *
123  * Checks if @connection is connected. This is equivalent to calling
124  * g_socket_is_connected() on @connection's underlying #GSocket.
125  *
126  * Returns: whether @connection is connected
127  *
128  * Since: 2.32
129  */
130 gboolean
131 g_socket_connection_is_connected (GSocketConnection  *connection)
132 {
133   return g_socket_is_connected (connection->priv->socket);
134 }
135
136 /**
137  * g_socket_connection_connect:
138  * @connection: a #GSocketConnection
139  * @address: a #GSocketAddress specifying the remote address.
140  * @cancellable: (allow-none): a %GCancellable or %NULL
141  * @error: #GError for error reporting, or %NULL to ignore.
142  *
143  * Connect @connection to the specified remote address.
144  *
145  * Returns: %TRUE if the connection succeeded, %FALSE on error
146  *
147  * Since: 2.32
148  */
149 gboolean
150 g_socket_connection_connect (GSocketConnection  *connection,
151                              GSocketAddress     *address,
152                              GCancellable       *cancellable,
153                              GError            **error)
154 {
155   g_return_val_if_fail (G_IS_SOCKET_CONNECTION (connection), FALSE);
156   g_return_val_if_fail (G_IS_SOCKET_ADDRESS (address), FALSE);
157
158   return g_socket_connect (connection->priv->socket, address,
159                            cancellable, error);
160 }
161
162 static gboolean g_socket_connection_connect_callback (GSocket      *socket,
163                                                       GIOCondition  condition,
164                                                       gpointer      user_data);
165
166 /**
167  * g_socket_connection_connect_async:
168  * @connection: a #GSocketConnection
169  * @address: a #GSocketAddress specifying the remote address.
170  * @cancellable: (allow-none): a %GCancellable or %NULL
171  * @callback: (scope async): a #GAsyncReadyCallback
172  * @user_data: (closure): user data for the callback
173  *
174  * Asynchronously connect @connection to the specified remote address.
175  *
176  * This clears the #GSocket:blocking flag on @connection's underlying
177  * socket if it is currently set.
178  *
179  * Use g_socket_connection_connect_finish() to retrieve the result.
180  *
181  * Since: 2.32
182  */
183 void
184 g_socket_connection_connect_async (GSocketConnection   *connection,
185                                    GSocketAddress      *address,
186                                    GCancellable        *cancellable,
187                                    GAsyncReadyCallback  callback,
188                                    gpointer             user_data)
189 {
190   GTask *task;
191   GError *tmp_error = NULL;
192
193   g_return_if_fail (G_IS_SOCKET_CONNECTION (connection));
194   g_return_if_fail (G_IS_SOCKET_ADDRESS (address));
195
196   task = g_task_new (connection, cancellable, callback, user_data);
197
198   g_socket_set_blocking (connection->priv->socket, FALSE);
199
200   if (g_socket_connect (connection->priv->socket, address,
201                         cancellable, &tmp_error))
202     {
203       g_task_return_boolean (task, TRUE);
204       g_object_unref (task);
205     }
206   else if (g_error_matches (tmp_error, G_IO_ERROR, G_IO_ERROR_PENDING))
207     {
208       GSource *source;
209
210       g_error_free (tmp_error);
211       source = g_socket_create_source (connection->priv->socket,
212                                        G_IO_OUT, cancellable);
213       g_task_attach_source (task, source,
214                             (GSourceFunc) g_socket_connection_connect_callback);
215       g_source_unref (source);
216     }
217   else
218     {
219       g_task_return_error (task, tmp_error);
220       g_object_unref (task);
221     }
222 }
223
224 static gboolean
225 g_socket_connection_connect_callback (GSocket      *socket,
226                                       GIOCondition  condition,
227                                       gpointer      user_data)
228 {
229   GTask *task = user_data;
230   GSocketConnection *connection = g_task_get_source_object (task);
231   GError *error = NULL;
232
233   if (g_socket_check_connect_result (connection->priv->socket, &error))
234     g_task_return_boolean (task, TRUE);
235   else
236     g_task_return_error (task, error);
237
238   g_object_unref (task);
239   return FALSE;
240 }
241
242 /**
243  * g_socket_connection_connect_finish:
244  * @connection: a #GSocketConnection
245  * @result: the #GAsyncResult
246  * @error: #GError for error reporting, or %NULL to ignore.
247  *
248  * Gets the result of a g_socket_connection_connect_async() call.
249  *
250  * Returns: %TRUE if the connection succeeded, %FALSE on error
251  *
252  * Since: 2.32
253  */
254 gboolean
255 g_socket_connection_connect_finish (GSocketConnection  *connection,
256                                     GAsyncResult       *result,
257                                     GError            **error)
258 {
259   g_return_val_if_fail (G_IS_SOCKET_CONNECTION (connection), FALSE);
260   g_return_val_if_fail (g_task_is_valid (result, connection), FALSE);
261
262   return g_task_propagate_boolean (G_TASK (result), error);
263 }
264
265 /**
266  * g_socket_connection_get_socket:
267  * @connection: a #GSocketConnection
268  *
269  * Gets the underlying #GSocket object of the connection.
270  * This can be useful if you want to do something unusual on it
271  * not supported by the #GSocketConnection APIs.
272  *
273  * Returns: (transfer none): a #GSocketAddress or %NULL on error.
274  *
275  * Since: 2.22
276  */
277 GSocket *
278 g_socket_connection_get_socket (GSocketConnection *connection)
279 {
280   g_return_val_if_fail (G_IS_SOCKET_CONNECTION (connection), NULL);
281
282   return connection->priv->socket;
283 }
284
285 /**
286  * g_socket_connection_get_local_address:
287  * @connection: a #GSocketConnection
288  * @error: #GError for error reporting, or %NULL to ignore.
289  *
290  * Try to get the local address of a socket connection.
291  *
292  * Returns: (transfer full): a #GSocketAddress or %NULL on error.
293  *     Free the returned object with g_object_unref().
294  *
295  * Since: 2.22
296  */
297 GSocketAddress *
298 g_socket_connection_get_local_address (GSocketConnection  *connection,
299                                        GError            **error)
300 {
301   return g_socket_get_local_address (connection->priv->socket, error);
302 }
303
304 /**
305  * g_socket_connection_get_remote_address:
306  * @connection: a #GSocketConnection
307  * @error: #GError for error reporting, or %NULL to ignore.
308  *
309  * Try to get the remote address of a socket connection.
310  *
311  * Since GLib 2.40, when used with g_socket_client_connect() or
312  * g_socket_client_connect_async(), during emission of
313  * %G_SOCKET_CLIENT_CONNECTING, this function will return the remote
314  * address that will be used for the connection.  This allows
315  * applications to print e.g. "Connecting to example.com
316  * (10.42.77.3)...".
317  *
318  * Returns: (transfer full): a #GSocketAddress or %NULL on error.
319  *     Free the returned object with g_object_unref().
320  *
321  * Since: 2.22
322  */
323 GSocketAddress *
324 g_socket_connection_get_remote_address (GSocketConnection  *connection,
325                                         GError            **error)
326 {
327   if (!g_socket_is_connected (connection->priv->socket))
328     {
329       return connection->priv->cached_remote_address ?
330         g_object_ref (connection->priv->cached_remote_address) : NULL;
331     }
332   return g_socket_get_remote_address (connection->priv->socket, error);
333 }
334
335 /* Private API allowing applications to retrieve the resolved address
336  * now, before we start connecting.
337  *
338  * https://bugzilla.gnome.org/show_bug.cgi?id=712547
339  */
340 void
341 g_socket_connection_set_cached_remote_address (GSocketConnection *connection,
342                                                GSocketAddress    *address)
343 {
344   g_clear_object (&connection->priv->cached_remote_address);
345   connection->priv->cached_remote_address = address ? g_object_ref (address) : NULL;
346 }
347
348 static void
349 g_socket_connection_get_property (GObject    *object,
350                                   guint       prop_id,
351                                   GValue     *value,
352                                   GParamSpec *pspec)
353 {
354   GSocketConnection *connection = G_SOCKET_CONNECTION (object);
355
356   switch (prop_id)
357     {
358      case PROP_SOCKET:
359       g_value_set_object (value, connection->priv->socket);
360       break;
361
362      default:
363       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
364     }
365 }
366
367 static void
368 g_socket_connection_set_property (GObject      *object,
369                                   guint         prop_id,
370                                   const GValue *value,
371                                   GParamSpec   *pspec)
372 {
373   GSocketConnection *connection = G_SOCKET_CONNECTION (object);
374
375   switch (prop_id)
376     {
377      case PROP_SOCKET:
378       connection->priv->socket = G_SOCKET (g_value_dup_object (value));
379       break;
380
381      default:
382       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
383     }
384 }
385
386 static void
387 g_socket_connection_constructed (GObject *object)
388 {
389   GSocketConnection *connection = G_SOCKET_CONNECTION (object);
390
391   g_assert (connection->priv->socket != NULL);
392 }
393
394 static void
395 g_socket_connection_dispose (GObject *object)
396 {
397   GSocketConnection *connection = G_SOCKET_CONNECTION (object);
398
399   connection->priv->in_dispose = TRUE;
400
401   g_clear_object (&connection->priv->cached_remote_address);
402
403   G_OBJECT_CLASS (g_socket_connection_parent_class)
404     ->dispose (object);
405
406   connection->priv->in_dispose = FALSE;
407 }
408
409 static void
410 g_socket_connection_finalize (GObject *object)
411 {
412   GSocketConnection *connection = G_SOCKET_CONNECTION (object);
413
414   if (connection->priv->input_stream)
415     g_object_unref (connection->priv->input_stream);
416
417   if (connection->priv->output_stream)
418     g_object_unref (connection->priv->output_stream);
419
420   g_object_unref (connection->priv->socket);
421
422   G_OBJECT_CLASS (g_socket_connection_parent_class)
423     ->finalize (object);
424 }
425
426 static void
427 g_socket_connection_class_init (GSocketConnectionClass *klass)
428 {
429   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
430   GIOStreamClass *stream_class = G_IO_STREAM_CLASS (klass);
431
432   gobject_class->set_property = g_socket_connection_set_property;
433   gobject_class->get_property = g_socket_connection_get_property;
434   gobject_class->constructed = g_socket_connection_constructed;
435   gobject_class->finalize = g_socket_connection_finalize;
436   gobject_class->dispose = g_socket_connection_dispose;
437
438   stream_class->get_input_stream = g_socket_connection_get_input_stream;
439   stream_class->get_output_stream = g_socket_connection_get_output_stream;
440   stream_class->close_fn = g_socket_connection_close;
441   stream_class->close_async = g_socket_connection_close_async;
442   stream_class->close_finish = g_socket_connection_close_finish;
443
444   g_object_class_install_property (gobject_class,
445                                    PROP_SOCKET,
446                                    g_param_spec_object ("socket",
447                                                         P_("Socket"),
448                                                         P_("The underlying GSocket"),
449                                                         G_TYPE_SOCKET,
450                                                         G_PARAM_CONSTRUCT_ONLY |
451                                                         G_PARAM_READWRITE |
452                                                         G_PARAM_STATIC_STRINGS));
453 }
454
455 static void
456 g_socket_connection_init (GSocketConnection *connection)
457 {
458   connection->priv = g_socket_connection_get_instance_private (connection);
459 }
460
461 static gboolean
462 g_socket_connection_close (GIOStream     *stream,
463                            GCancellable  *cancellable,
464                            GError       **error)
465 {
466   GSocketConnection *connection = G_SOCKET_CONNECTION (stream);
467
468   if (connection->priv->output_stream)
469     g_output_stream_close (connection->priv->output_stream,
470                            cancellable, NULL);
471   if (connection->priv->input_stream)
472     g_input_stream_close (connection->priv->input_stream,
473                           cancellable, NULL);
474
475   /* Don't close the underlying socket if this is being called
476    * as part of dispose(); when destroying the GSocketConnection,
477    * we only want to close the socket if we're holding the last
478    * reference on it, and in that case it will close itself when
479    * we unref it in finalize().
480    */
481   if (connection->priv->in_dispose)
482     return TRUE;
483
484   return g_socket_close (connection->priv->socket, error);
485 }
486
487
488 static void
489 g_socket_connection_close_async (GIOStream           *stream,
490                                  int                  io_priority,
491                                  GCancellable        *cancellable,
492                                  GAsyncReadyCallback  callback,
493                                  gpointer             user_data)
494 {
495   GTask *task;
496   GIOStreamClass *class;
497   GError *error;
498
499   class = G_IO_STREAM_GET_CLASS (stream);
500
501   task = g_task_new (stream, cancellable, callback, user_data);
502
503   /* socket close is not blocked, just do it! */
504   error = NULL;
505   if (class->close_fn &&
506       !class->close_fn (stream, cancellable, &error))
507     g_task_return_error (task, error);
508   else
509     g_task_return_boolean (task, TRUE);
510
511   g_object_unref (task);
512 }
513
514 static gboolean
515 g_socket_connection_close_finish (GIOStream     *stream,
516                                   GAsyncResult  *result,
517                                   GError       **error)
518 {
519   return g_task_propagate_boolean (G_TASK (result), error);
520 }
521
522 typedef struct {
523   GSocketFamily socket_family;
524   GSocketType socket_type;
525   int protocol;
526   GType implementation;
527 } ConnectionFactory;
528
529 static guint
530 connection_factory_hash (gconstpointer key)
531 {
532   const ConnectionFactory *factory = key;
533   guint h;
534
535   h = factory->socket_family ^ (factory->socket_type << 4) ^ (factory->protocol << 8);
536   /* This is likely to be small, so spread over whole
537      hash space to get some distribution */
538   h = h ^ (h << 8) ^ (h << 16) ^ (h << 24);
539
540   return h;
541 }
542
543 static gboolean
544 connection_factory_equal (gconstpointer _a,
545                           gconstpointer _b)
546 {
547   const ConnectionFactory *a = _a;
548   const ConnectionFactory *b = _b;
549
550   if (a->socket_family != b->socket_family)
551     return FALSE;
552
553   if (a->socket_type != b->socket_type)
554     return FALSE;
555
556   if (a->protocol != b->protocol)
557     return FALSE;
558
559   return TRUE;
560 }
561
562 static GHashTable *connection_factories = NULL;
563 G_LOCK_DEFINE_STATIC(connection_factories);
564
565 /**
566  * g_socket_connection_factory_register_type:
567  * @g_type: a #GType, inheriting from %G_TYPE_SOCKET_CONNECTION
568  * @family: a #GSocketFamily
569  * @type: a #GSocketType
570  * @protocol: a protocol id
571  *
572  * Looks up the #GType to be used when creating socket connections on
573  * sockets with the specified @family, @type and @protocol.
574  *
575  * If no type is registered, the #GSocketConnection base type is returned.
576  *
577  * Since: 2.22
578  */
579 void
580 g_socket_connection_factory_register_type (GType         g_type,
581                                            GSocketFamily family,
582                                            GSocketType   type,
583                                            gint          protocol)
584 {
585   ConnectionFactory *factory;
586
587   g_return_if_fail (g_type_is_a (g_type, G_TYPE_SOCKET_CONNECTION));
588
589   G_LOCK (connection_factories);
590
591   if (connection_factories == NULL)
592     connection_factories = g_hash_table_new_full (connection_factory_hash,
593                                                   connection_factory_equal,
594                                                   (GDestroyNotify)g_free,
595                                                   NULL);
596
597   factory = g_new0 (ConnectionFactory, 1);
598   factory->socket_family = family;
599   factory->socket_type = type;
600   factory->protocol = protocol;
601   factory->implementation = g_type;
602
603   g_hash_table_insert (connection_factories,
604                        factory, factory);
605
606   G_UNLOCK (connection_factories);
607 }
608
609 static void
610 init_builtin_types (void)
611 {
612 #ifndef G_OS_WIN32
613   g_type_ensure (G_TYPE_UNIX_CONNECTION);
614 #endif
615   g_type_ensure (G_TYPE_TCP_CONNECTION);
616 }
617
618 /**
619  * g_socket_connection_factory_lookup_type:
620  * @family: a #GSocketFamily
621  * @type: a #GSocketType
622  * @protocol_id: a protocol id
623  *
624  * Looks up the #GType to be used when creating socket connections on
625  * sockets with the specified @family, @type and @protocol_id.
626  *
627  * If no type is registered, the #GSocketConnection base type is returned.
628  *
629  * Returns: a #GType
630  *
631  * Since: 2.22
632  */
633 GType
634 g_socket_connection_factory_lookup_type (GSocketFamily family,
635                                          GSocketType   type,
636                                          gint          protocol_id)
637 {
638   ConnectionFactory *factory, key;
639   GType g_type;
640
641   init_builtin_types ();
642
643   G_LOCK (connection_factories);
644
645   g_type = G_TYPE_SOCKET_CONNECTION;
646
647   if (connection_factories)
648     {
649       key.socket_family = family;
650       key.socket_type = type;
651       key.protocol = protocol_id;
652
653       factory = g_hash_table_lookup (connection_factories, &key);
654       if (factory)
655         g_type = factory->implementation;
656     }
657
658   G_UNLOCK (connection_factories);
659
660   return g_type;
661 }
662
663 /**
664  * g_socket_connection_factory_create_connection:
665  * @socket: a #GSocket
666  *
667  * Creates a #GSocketConnection subclass of the right type for
668  * @socket.
669  *
670  * Returns: (transfer full): a #GSocketConnection
671  *
672  * Since: 2.22
673  */
674 GSocketConnection *
675 g_socket_connection_factory_create_connection (GSocket *socket)
676 {
677   GType type;
678
679   type = g_socket_connection_factory_lookup_type (g_socket_get_family (socket),
680                                                   g_socket_get_socket_type (socket),
681                                                   g_socket_get_protocol (socket));
682   return g_object_new (type, "socket", socket, NULL);
683 }