Ref the passed in socket in g_socket_listener_add_socket (#585599)
[platform/upstream/glib.git] / gio / gsocketlistener.c
1 /* GIO - GLib Input, Output and Streaming Library
2  *
3  * Copyright © 2008 Christian Kellner, Samuel Cormier-Iijima
4  * Copyright © 2009 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 #include "gsocketlistener.h"
30
31 #include <gio/gsimpleasyncresult.h>
32 #include <gio/gcancellable.h>
33 #include <gio/gsocketaddress.h>
34 #include <gio/ginetaddress.h>
35 #include <gio/gioerror.h>
36 #include <gio/gsocket.h>
37 #include <gio/gsocketconnection.h>
38 #include <gio/ginetsocketaddress.h>
39 #include "glibintl.h"
40
41 #include "gioalias.h"
42
43 /**
44  * SECTION: gsocketlistener
45  * @title: GSocketListener
46  * @short_description: Helper for accepting network client connections
47  * @see_also: #GThreadedSocketService, #GSocketService.
48  *
49  * A #GSocketListener is an object that keeps track of a set
50  * of server sockets and helps you accept sockets from any of the
51  * socket, either sync or async.
52  *
53  * If you want to implement a network server, also look at #GSocketService
54  * and #GThreadedSocketService which are subclass of #GSocketListener
55  * that makes this even easier.
56  *
57  * Since: 2.22
58  */
59
60 G_DEFINE_TYPE (GSocketListener, g_socket_listener, G_TYPE_OBJECT);
61
62 enum
63 {
64   PROP_0,
65   PROP_LISTEN_BACKLOG
66 };
67
68
69 static GQuark source_quark = 0;
70
71 struct _GSocketListenerPrivate
72 {
73   GPtrArray           *sockets;
74   GMainContext        *main_context;
75   int                 listen_backlog;
76   guint               closed : 1;
77 };
78
79 static void
80 g_socket_listener_finalize (GObject *object)
81 {
82   GSocketListener *listener = G_SOCKET_LISTENER (object);
83
84   if (listener->priv->main_context)
85     g_main_context_unref (listener->priv->main_context);
86
87   if (!listener->priv->closed)
88     g_socket_listener_close (listener);
89
90   g_ptr_array_free (listener->priv->sockets, TRUE);
91
92   G_OBJECT_CLASS (g_socket_listener_parent_class)
93     ->finalize (object);
94 }
95
96 static void
97 g_socket_listener_get_property (GObject    *object,
98                                 guint       prop_id,
99                                 GValue     *value,
100                                 GParamSpec *pspec)
101 {
102   GSocketListener *listener = G_SOCKET_LISTENER (object);
103
104   switch (prop_id)
105     {
106       case PROP_LISTEN_BACKLOG:
107         g_value_set_int (value, listener->priv->listen_backlog);
108         break;
109
110       default:
111         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
112     }
113 }
114
115 static void
116 g_socket_listener_set_property (GObject      *object,
117                                 guint         prop_id,
118                                 const GValue *value,
119                                 GParamSpec   *pspec)
120 {
121   GSocketListener *listener = G_SOCKET_LISTENER (object);
122
123   switch (prop_id)
124     {
125       case PROP_LISTEN_BACKLOG:
126         g_socket_listener_set_backlog (listener, g_value_get_int (value));
127         break;
128
129       default:
130         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
131     }
132 }
133
134
135 static void
136 g_socket_listener_class_init (GSocketListenerClass *klass)
137 {
138   GObjectClass *gobject_class G_GNUC_UNUSED = G_OBJECT_CLASS (klass);
139
140   g_type_class_add_private (klass, sizeof (GSocketListenerPrivate));
141
142   gobject_class->finalize = g_socket_listener_finalize;
143   gobject_class->set_property = g_socket_listener_set_property;
144   gobject_class->get_property = g_socket_listener_get_property;
145   g_object_class_install_property (gobject_class, PROP_LISTEN_BACKLOG,
146                                    g_param_spec_int ("listen-backlog",
147                                                      P_("Listen backlog"),
148                                                      P_("outstanding connections in the listen queue"),
149                                                      0,
150                                                      2000,
151                                                      10,
152                                                      G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
153
154   source_quark = g_quark_from_static_string ("g-socket-listener-source");
155 }
156
157 static void
158 g_socket_listener_init (GSocketListener *listener)
159 {
160   listener->priv = G_TYPE_INSTANCE_GET_PRIVATE (listener,
161                                                 G_TYPE_SOCKET_LISTENER,
162                                                 GSocketListenerPrivate);
163   listener->priv->sockets =
164     g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref);
165   listener->priv->listen_backlog = 10;
166 }
167
168 /**
169  * g_socket_listener_new:
170  *
171  * Creates a new #GSocketListener with no sockets to listen for.
172  * New listeners can be added with e.g. g_socket_listener_add_address()
173  * or g_socket_listener_add_inet_port().
174  *
175  * Returns: a new #GSocketListener.
176  *
177  * Since: 2.22
178  */
179 GSocketListener *
180 g_socket_listener_new (void)
181 {
182   return g_object_new (G_TYPE_SOCKET_LISTENER, NULL);
183 }
184
185 static gboolean
186 check_listener (GSocketListener *listener,
187                 GError **error)
188 {
189   if (listener->priv->closed)
190     {
191       g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
192                            _("Listener is already closed"));
193       return FALSE;
194     }
195
196   return TRUE;
197 }
198
199 /**
200  * g_socket_listener_add_socket:
201  * @listener: a #GSocketListener
202  * @socket: a listening #GSocket
203  * @source_object: Optional #GObject identifying this source
204  * @error: #GError for error reporting, or %NULL to ignore.
205  *
206  * Adds @socket to the set of sockets that we try to accept
207  * new clients from. The socket must be bound to a local
208  * address and listened to.
209  *
210  * @source_object will be passed out in the various calls
211  * to accept to identify this particular source, which is
212  * useful if you're listening on multiple addresses and do
213  * different things depending on what address is connected to.
214  *
215  * Returns: %TRUE on success, %FALSE on error.
216  *
217  * Since: 2.22
218  */
219 gboolean
220 g_socket_listener_add_socket (GSocketListener  *listener,
221                               GSocket          *socket,
222                               GObject          *source_object,
223                               GError          **error)
224 {
225   if (!check_listener (listener, error))
226     return FALSE;
227
228   /* TODO: Check that socket it is bound & not closed? */
229
230   if (g_socket_is_closed (socket))
231     {
232       g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
233                            _("Added socket is closed"));
234       return FALSE;
235     }
236
237   g_object_ref (socket);
238   g_ptr_array_add (listener->priv->sockets, socket);
239
240   if (source_object)
241     g_object_set_qdata_full (G_OBJECT (socket), source_quark,
242                              g_object_ref (source_object), g_object_unref);
243
244   return TRUE;
245 }
246
247 /**
248  * g_socket_listener_add_address:
249  * @listener: a #GSocketListener
250  * @address: a #GSocketAddress
251  * @type: a #GSocketType
252  * @protocol: a #GSocketProtocol
253  * @source_object: Optional #GObject identifying this source
254  * @effective_address: location to store the address that was bound to, or %NULL.
255  * @error: #GError for error reporting, or %NULL to ignore.
256  *
257  * Creates a socket of type @type and protocol @protocol, binds
258  * it to @address and adds it to the set of sockets we're accepting
259  * sockets from.
260  *
261  * Note that adding an IPv6 address, depending on the platform,
262  * may or may not result in a listener that also accepts IPv4
263  * connections.  For more determinstic behaviour, see
264  * g_socket_listener_add_inet_port().
265  *
266  * @source_object will be passed out in the various calls
267  * to accept to identify this particular source, which is
268  * useful if you're listening on multiple addresses and do
269  * different things depending on what address is connected to.
270  *
271  * If successful and @effective_address is non-%NULL then it will
272  * be set to the address that the binding actually occured at.  This
273  * is helpful for determining the port number that was used for when
274  * requesting a binding to port 0 (ie: "any port").  This address, if
275  * requested, belongs to the caller and must be freed.
276  *
277  * Returns: %TRUE on success, %FALSE on error.
278  *
279  * Since: 2.22
280  */
281 gboolean
282 g_socket_listener_add_address (GSocketListener  *listener,
283                                GSocketAddress   *address,
284                                GSocketType       type,
285                                GSocketProtocol   protocol,
286                                GObject          *source_object,
287                                GSocketAddress  **effective_address,
288                                GError          **error)
289 {
290   GSocketAddress *local_address;
291   GSocketFamily family;
292   GSocket *socket;
293
294   if (!check_listener (listener, error))
295     return FALSE;
296
297   family = g_socket_address_get_family (address);
298   socket = g_socket_new (family, type, protocol, error);
299   if (socket == NULL)
300     return FALSE;
301
302   g_socket_set_listen_backlog (socket, listener->priv->listen_backlog);
303
304   if (!g_socket_bind (socket, address, TRUE, error) ||
305       !g_socket_listen (socket, error))
306     {
307       g_object_unref (socket);
308       return FALSE;
309     }
310
311   local_address = NULL;
312   if (effective_address)
313     {
314       local_address = g_socket_get_local_address (socket, error);
315       if (local_address == NULL)
316         {
317           g_object_unref (socket);
318           return FALSE;
319         }
320     }
321
322   if (!g_socket_listener_add_socket (listener, socket,
323                                      source_object,
324                                      error))
325     {
326       if (local_address)
327         g_object_unref (local_address);
328       g_object_unref (socket);
329       return FALSE;
330     }
331
332   if (effective_address)
333     *effective_address = local_address;
334
335   g_object_unref (socket); /* add_socket refs this */
336
337   if (G_SOCKET_LISTENER_GET_CLASS (listener)->changed)
338     G_SOCKET_LISTENER_GET_CLASS (listener)->changed (listener);
339
340   return TRUE;
341 }
342
343 /**
344  * g_socket_listener_add_inet_port:
345  * @listener: a #GSocketListener
346  * @port: an IP port number (non-zero)
347  * @source_object: Optional #GObject identifying this source
348  * @error: #GError for error reporting, or %NULL to ignore.
349  *
350  * Helper function for g_socket_listener_add_address() that
351  * creates a TCP/IP socket listening on IPv4 and IPv6 (if
352  * supported) on the specified port on all interfaces.
353  *
354  * @source_object will be passed out in the various calls
355  * to accept to identify this particular source, which is
356  * useful if you're listening on multiple addresses and do
357  * different things depending on what address is connected to.
358  *
359  * Returns: %TRUE on success, %FALSE on error.
360  *
361  * Since: 2.22
362  */
363 gboolean
364 g_socket_listener_add_inet_port (GSocketListener  *listener,
365                                  guint16           port,
366                                  GObject          *source_object,
367                                  GError          **error)
368 {
369   gboolean need_ipv4_socket = TRUE;
370   GSocket *socket4 = NULL;
371   GSocket *socket6;
372
373   g_return_val_if_fail (listener != NULL, FALSE);
374   g_return_val_if_fail (port != 0, FALSE);
375
376   if (!check_listener (listener, error))
377     return FALSE;
378
379   /* first try to create an IPv6 socket */
380   socket6 = g_socket_new (G_SOCKET_FAMILY_IPV6,
381                           G_SOCKET_TYPE_STREAM,
382                           G_SOCKET_PROTOCOL_DEFAULT,
383                           NULL);
384
385   if (socket6 != NULL)
386     /* IPv6 is supported on this platform, so if we fail now it is
387      * a result of being unable to bind to our port.  Don't fail
388      * silently as a result of this!
389      */
390     {
391       GInetAddress *inet_address;
392       GSocketAddress *address;
393       gboolean result;
394
395       inet_address = g_inet_address_new_any (G_SOCKET_FAMILY_IPV6);
396       address = g_inet_socket_address_new (inet_address, port);
397       g_object_unref (inet_address);
398
399       g_socket_set_listen_backlog (socket6, listener->priv->listen_backlog);
400
401       result = g_socket_bind (socket6, address, TRUE, error) &&
402                g_socket_listen (socket6, error);
403
404       g_object_unref (address);
405
406       if (!result)
407         {
408           g_object_unref (socket6);
409
410           return FALSE;
411         }
412
413       if (source_object)
414         g_object_set_qdata_full (G_OBJECT (socket6), source_quark,
415                                  g_object_ref (source_object),
416                                  g_object_unref);
417
418       /* If this socket already speaks IPv4 then we are done. */
419       if (g_socket_speaks_ipv4 (socket6))
420         need_ipv4_socket = FALSE;
421     }
422
423   if (need_ipv4_socket)
424     /* We are here for exactly one of the following reasons:
425      *
426      *   - our platform doesn't support IPv6
427      *   - we successfully created an IPv6 socket but it's V6ONLY
428      *
429      * In either case, we need to go ahead and create an IPv4 socket
430      * and fail the call if we can't bind to it.
431      */
432     {
433       socket4 = g_socket_new (G_SOCKET_FAMILY_IPV4,
434                               G_SOCKET_TYPE_STREAM,
435                               G_SOCKET_PROTOCOL_DEFAULT,
436                               error);
437
438       if (socket4 != NULL)
439         /* IPv4 is supported on this platform, so if we fail now it is
440          * a result of being unable to bind to our port.  Don't fail
441          * silently as a result of this!
442          */
443         {
444           GInetAddress *inet_address;
445           GSocketAddress *address;
446           gboolean result;
447
448           inet_address = g_inet_address_new_any (G_SOCKET_FAMILY_IPV4);
449           address = g_inet_socket_address_new (inet_address, port);
450           g_object_unref (inet_address);
451
452           g_socket_set_listen_backlog (socket4,
453                                        listener->priv->listen_backlog);
454
455           result = g_socket_bind (socket4, address, TRUE, error) &&
456                    g_socket_listen (socket4, error);
457
458           g_object_unref (address);
459
460           if (!result)
461             {
462               g_object_unref (socket4);
463
464               if (socket6 != NULL)
465                 g_object_unref (socket6);
466
467               return FALSE;
468             }
469
470           if (source_object)
471             g_object_set_qdata_full (G_OBJECT (socket4), source_quark,
472                                      g_object_ref (source_object),
473                                      g_object_unref);
474         }
475       else
476         /* Ok.  So IPv4 is not supported on this platform.  If we
477          * succeeded at creating an IPv6 socket then that's OK, but
478          * otherwise we need to tell the user we failed.
479          */
480         {
481           if (socket6 != NULL)
482             g_clear_error (error);
483           else
484             return FALSE;
485         }
486     }
487
488   g_assert (socket6 != NULL || socket4 != NULL);
489
490   if (socket6 != NULL)
491     g_ptr_array_add (listener->priv->sockets, socket6);
492
493   if (socket4 != NULL)
494     g_ptr_array_add (listener->priv->sockets, socket4);
495
496   if (G_SOCKET_LISTENER_GET_CLASS (listener)->changed)
497     G_SOCKET_LISTENER_GET_CLASS (listener)->changed (listener);
498
499   return TRUE;
500 }
501
502 static GList *
503 add_sources (GSocketListener   *listener,
504              GSocketSourceFunc  callback,
505              gpointer           callback_data,
506              GCancellable      *cancellable,
507              GMainContext      *context)
508 {
509   GSocket *socket;
510   GSource *source;
511   GList *sources;
512   int i;
513
514   sources = NULL;
515   for (i = 0; i < listener->priv->sockets->len; i++)
516     {
517       socket = listener->priv->sockets->pdata[i];
518
519       source = g_socket_create_source (socket, G_IO_IN, cancellable);
520       g_source_set_callback (source,
521                              (GSourceFunc) callback,
522                              callback_data, NULL);
523       g_source_attach (source, context);
524
525       sources = g_list_prepend (sources, source);
526     }
527
528   return sources;
529 }
530
531 static void
532 free_sources (GList *sources)
533 {
534   GSource *source;
535   while (sources != NULL)
536     {
537       source = sources->data;
538       sources = g_list_delete_link (sources, sources);
539       g_source_destroy (source);
540       g_source_unref (source);
541     }
542 }
543
544 struct AcceptData {
545   GMainLoop *loop;
546   GSocket *socket;
547 };
548
549 static gboolean
550 accept_callback (GSocket      *socket,
551                  GIOCondition  condition,
552                  gpointer      user_data)
553 {
554   struct AcceptData *data = user_data;
555
556   data->socket = socket;
557   g_main_loop_quit (data->loop);
558
559   return TRUE;
560 }
561
562 /**
563  * g_socket_listener_accept_socket:
564  * @listener: a #GSocketListener
565  * @source_object: location where #GObject pointer will be stored, or %NULL
566  * @cancellable: optional #GCancellable object, %NULL to ignore.
567  * @error: #GError for error reporting, or %NULL to ignore.
568  *
569  * Blocks waiting for a client to connect to any of the sockets added
570  * to the listener. Returns the #GSocket that was accepted.
571  *
572  * If you want to accept the high-level #GSocketConnection, not a #GSocket,
573  * which is often the case, then you should use g_socket_listener_accept()
574  * instead.
575  *
576  * If @source_object is not %NULL it will be filled out with the source
577  * object specified when the corresponding socket or address was added
578  * to the listener.
579  *
580  * If @cancellable is not %NULL, then the operation can be cancelled by
581  * triggering the cancellable object from another thread. If the operation
582  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
583  *
584  * Returns: a #GSocket on success, %NULL on error.
585  *
586  * Since: 2.22
587  */
588 GSocket *
589 g_socket_listener_accept_socket (GSocketListener  *listener,
590                                  GObject         **source_object,
591                                  GCancellable     *cancellable,
592                                  GError          **error)
593 {
594   GSocket *accept_socket, *socket;
595
596   g_return_val_if_fail (G_IS_SOCKET_LISTENER (listener), NULL);
597
598   if (!check_listener (listener, error))
599     return NULL;
600
601   if (listener->priv->sockets->len == 1)
602     {
603       accept_socket = listener->priv->sockets->pdata[0];
604       if (!g_socket_condition_wait (accept_socket, G_IO_IN,
605                                     cancellable, error))
606         return NULL;
607     }
608   else
609     {
610       GList *sources;
611       struct AcceptData data;
612       GMainLoop *loop;
613
614       if (listener->priv->main_context == NULL)
615         listener->priv->main_context = g_main_context_new ();
616
617       loop = g_main_loop_new (listener->priv->main_context, FALSE);
618       data.loop = loop;
619       sources = add_sources (listener,
620                              accept_callback,
621                              &data,
622                              cancellable,
623                              listener->priv->main_context);
624       g_main_loop_run (loop);
625       accept_socket = data.socket;
626       free_sources (sources);
627       g_main_loop_unref (loop);
628     }
629
630   if (!(socket = g_socket_accept (accept_socket, error)))
631     return NULL;
632
633   if (source_object)
634     *source_object = g_object_get_qdata (G_OBJECT (accept_socket), source_quark);
635
636   return socket;
637 }
638
639 /**
640  * g_socket_listener_accept:
641  * @listener: a #GSocketListener
642  * @source_object: location where #GObject pointer will be stored, or %NULL
643  * @cancellable: optional #GCancellable object, %NULL to ignore.
644  * @error: #GError for error reporting, or %NULL to ignore.
645  *
646  * Blocks waiting for a client to connect to any of the sockets added
647  * to the listener. Returns a #GSocketConnection for the socket that was
648  * accepted.
649  *
650  * If @source_object is not %NULL it will be filled out with the source
651  * object specified when the corresponding socket or address was added
652  * to the listener.
653  *
654  * If @cancellable is not %NULL, then the operation can be cancelled by
655  * triggering the cancellable object from another thread. If the operation
656  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
657  *
658  * Returns: a #GSocketConnection on success, %NULL on error.
659  *
660  * Since: 2.22
661  */
662 GSocketConnection *
663 g_socket_listener_accept (GSocketListener  *listener,
664                           GObject         **source_object,
665                           GCancellable     *cancellable,
666                           GError          **error)
667 {
668   GSocketConnection *connection;
669   GSocket *socket;
670
671   socket = g_socket_listener_accept_socket (listener,
672                                             source_object,
673                                             cancellable,
674                                             error);
675   if (socket == NULL)
676     return NULL;
677
678   connection = g_socket_connection_factory_create_connection (socket);
679   g_object_unref (socket);
680
681   return connection;
682 }
683
684 struct AcceptAsyncData {
685   GSimpleAsyncResult *simple;
686   GCancellable *cancellable;
687   GList *sources;
688 };
689
690 static gboolean
691 accept_ready (GSocket      *accept_socket,
692               GIOCondition  condition,
693               gpointer      _data)
694 {
695   struct AcceptAsyncData *data = _data;
696   GError *error = NULL;
697
698   if (!g_cancellable_set_error_if_cancelled (data->cancellable,
699                                              &error))
700     {
701       GSocket *socket;
702       GObject *source_object;
703
704       socket = g_socket_accept (accept_socket, &error);
705       if (socket)
706         {
707           g_simple_async_result_set_op_res_gpointer (data->simple, socket,
708                                                      g_object_unref);
709           source_object = g_object_get_qdata (G_OBJECT (accept_socket), source_quark);
710           if (source_object)
711             g_object_set_qdata_full (G_OBJECT (data->simple),
712                                      source_quark,
713                                      g_object_ref (source_object), g_object_unref);
714         }
715     }
716
717   if (error)
718     {
719       g_simple_async_result_set_from_error (data->simple, error);
720       g_error_free (error);
721     }
722
723   g_simple_async_result_complete_in_idle (data->simple);
724   g_object_unref (data->simple);
725   free_sources (data->sources);
726   g_free (data);
727
728   return FALSE;
729 }
730
731 /**
732  * g_socket_listener_accept_socket_async:
733  * @listener: a #GSocketListener
734  * @cancellable: a #GCancellable, or %NULL
735  * @callback: a #GAsyncReadyCallback
736  * @user_data: user data for the callback
737  *
738  * This is the asynchronous version of g_socket_listener_accept_socket().
739  *
740  * When the operation is finished @callback will be
741  * called. You can then call g_socket_listener_accept_socket_finish()
742  * to get the result of the operation.
743  *
744  * Since: 2.22
745  */
746 void
747 g_socket_listener_accept_socket_async (GSocketListener     *listener,
748                                        GCancellable        *cancellable,
749                                        GAsyncReadyCallback  callback,
750                                        gpointer             user_data)
751 {
752   struct AcceptAsyncData *data;
753   GError *error = NULL;
754
755   if (!check_listener (listener, &error))
756     {
757       g_simple_async_report_gerror_in_idle (G_OBJECT (listener),
758                                             callback, user_data,
759                                             error);
760       g_error_free (error);
761       return;
762     }
763
764   data = g_new0 (struct AcceptAsyncData, 1);
765   data->simple = g_simple_async_result_new (G_OBJECT (listener),
766                                             callback, user_data,
767                                             g_socket_listener_accept_socket_async);
768   data->cancellable = cancellable;
769   data->sources = add_sources (listener,
770                                accept_ready,
771                                data,
772                                cancellable,
773                                NULL);
774 }
775
776 /**
777  * g_socket_listener_accept_socket_finish:
778  * @listener: a #GSocketListener
779  * @result: a #GAsyncResult.
780  * @source_object: Optional #GObject identifying this source
781  * @error: a #GError location to store the error occuring, or %NULL to
782  * ignore.
783  *
784  * Finishes an async accept operation. See g_socket_listener_accept_socket_async()
785  *
786  * Returns: a #GSocket on success, %NULL on error.
787  *
788  * Since: 2.22
789  */
790 GSocket *
791 g_socket_listener_accept_socket_finish (GSocketListener  *listener,
792                                         GAsyncResult     *result,
793                                         GObject         **source_object,
794                                         GError          **error)
795 {
796   GSocket *socket;
797   GSimpleAsyncResult *simple;
798
799   g_return_val_if_fail (G_IS_SOCKET_LISTENER (listener), FALSE);
800
801   simple = G_SIMPLE_ASYNC_RESULT (result);
802
803   if (g_simple_async_result_propagate_error (simple, error))
804     return NULL;
805
806   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_socket_listener_accept_socket_async);
807
808   socket = g_simple_async_result_get_op_res_gpointer (simple);
809
810   if (source_object)
811     *source_object = g_object_get_qdata (G_OBJECT (result), source_quark);
812
813   return g_object_ref (socket);
814 }
815
816 /**
817  * g_socket_listener_accept_async:
818  * @listener: a #GSocketListener
819  * @cancellable: a #GCancellable, or %NULL
820  * @callback: a #GAsyncReadyCallback
821  * @user_data: user data for the callback
822  *
823  * This is the asynchronous version of g_socket_listener_accept().
824  *
825  * When the operation is finished @callback will be
826  * called. You can then call g_socket_listener_accept_socket()
827  * to get the result of the operation.
828  *
829  * Since: 2.22
830  */
831 void
832 g_socket_listener_accept_async (GSocketListener     *listener,
833                                 GCancellable        *cancellable,
834                                 GAsyncReadyCallback  callback,
835                                 gpointer             user_data)
836 {
837   g_socket_listener_accept_socket_async (listener,
838                                          cancellable,
839                                          callback,
840                                          user_data);
841 }
842
843 /**
844  * g_socket_listener_accept_finish:
845  * @listener: a #GSocketListener
846  * @result: a #GAsyncResult.
847  * @source_object: Optional #GObject identifying this source
848  * @error: a #GError location to store the error occuring, or %NULL to
849  * ignore.
850  *
851  * Finishes an async accept operation. See g_socket_listener_accept_async()
852  *
853  * Returns: a #GSocketConnection on success, %NULL on error.
854  *
855  * Since: 2.22
856  */
857 GSocketConnection *
858 g_socket_listener_accept_finish (GSocketListener  *listener,
859                                  GAsyncResult     *result,
860                                  GObject         **source_object,
861                                  GError          **error)
862 {
863   GSocket *socket;
864   GSocketConnection *connection;
865
866   socket = g_socket_listener_accept_socket_finish (listener,
867                                                    result,
868                                                    source_object,
869                                                    error);
870   if (socket == NULL)
871     return NULL;
872
873   connection = g_socket_connection_factory_create_connection (socket);
874   g_object_unref (socket);
875   return connection;
876 }
877
878 /**
879  * g_socket_listener_set_backlog:
880  * @listener: a #GSocketListener
881  * @listen_backlog: an integer
882  *
883  * Sets the listen backlog on the sockets in the listener.
884  *
885  * See g_socket_set_listen_backlog() for details
886  *
887  * Since: 2.22
888  */
889 void
890 g_socket_listener_set_backlog (GSocketListener *listener,
891                                int              listen_backlog)
892 {
893   GSocket *socket;
894   int i;
895
896   if (listener->priv->closed)
897     return;
898
899   listener->priv->listen_backlog = listen_backlog;
900
901   for (i = 0; i < listener->priv->sockets->len; i++)
902     {
903       socket = listener->priv->sockets->pdata[i];
904       g_socket_set_listen_backlog (socket, listen_backlog);
905     }
906 }
907
908 /**
909  * g_socket_listener_close:
910  * @listener: a #GSocketListener
911  *
912  * Closes all the sockets in the listener.
913  *
914  * Since: 2.22
915  */
916 void
917 g_socket_listener_close (GSocketListener *listener)
918 {
919   GSocket *socket;
920   int i;
921
922   g_return_if_fail (G_IS_SOCKET_LISTENER (listener));
923
924   if (listener->priv->closed)
925     return;
926
927   for (i = 0; i < listener->priv->sockets->len; i++)
928     {
929       socket = listener->priv->sockets->pdata[i];
930       g_socket_close (socket, NULL);
931     }
932   listener->priv->closed = TRUE;
933 }
934
935 #define __G_SOCKET_LISTENER_C__
936 #include "gioaliasdef.c"