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