Bug 585575 – g_socket_listener_add_inet_port()
[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_ptr_array_add (listener->priv->sockets, socket);
238
239   if (source_object)
240     g_object_set_qdata_full (G_OBJECT (socket), source_quark,
241                              g_object_ref (source_object), g_object_unref);
242
243   return TRUE;
244 }
245
246 /**
247  * g_socket_listener_add_address:
248  * @listener: a #GSocketListener
249  * @address: a #GSocketAddress
250  * @type: a #GSocketType
251  * @protocol: a #GSocketProtocol
252  * @source_object: Optional #GObject identifying this source
253  * @error: #GError for error reporting, or %NULL to ignore.
254  *
255  * Creates a socket of type @type and protocol @protocol, binds
256  * it to @address and adds it to the set of sockets we're accepting
257  * sockets from.
258  *
259  * Note that adding an IPv6 address, depending on the platform,
260  * may or may not result in a listener that also accepts IPv4
261  * connections.  For more determinstic behaviour, see
262  * g_socket_listener_add_inet_port().
263  *
264  * @source_object will be passed out in the various calls
265  * to accept to identify this particular source, which is
266  * useful if you're listening on multiple addresses and do
267  * different things depending on what address is connected to.
268  *
269  * Returns: %TRUE on success, %FALSE on error.
270  *
271  * Since: 2.22
272  */
273 gboolean
274 g_socket_listener_add_address (GSocketListener  *listener,
275                                GSocketAddress   *address,
276                                GSocketType       type,
277                                GSocketProtocol   protocol,
278                                GObject          *source_object,
279                                GError          **error)
280 {
281   GSocketFamily family;
282   GSocket *socket;
283
284   if (!check_listener (listener, error))
285     return FALSE;
286
287   family = g_socket_address_get_family (address);
288   socket = g_socket_new (family, type, protocol, error);
289   if (socket == NULL)
290     return FALSE;
291
292   g_socket_set_listen_backlog (socket, listener->priv->listen_backlog);
293
294   if (!g_socket_bind (socket, address, TRUE, error) ||
295       !g_socket_listen (socket, error) ||
296       !g_socket_listener_add_socket (listener, socket,
297                                      source_object,
298                                      error))
299     {
300       g_object_unref (socket);
301       return FALSE;
302     }
303
304   if (G_SOCKET_LISTENER_GET_CLASS (listener)->changed)
305     G_SOCKET_LISTENER_GET_CLASS (listener)->changed (listener);
306
307   return TRUE;
308 }
309
310 /**
311  * g_socket_listener_add_inet_port:
312  * @listener: a #GSocketListener
313  * @port: an IP port number (non-zero)
314  * @source_object: Optional #GObject identifying this source
315  * @error: #GError for error reporting, or %NULL to ignore.
316  *
317  * Helper function for g_socket_listener_add_address() that
318  * creates a TCP/IP socket listening on IPv4 and IPv6 (if
319  * supported) on the specified port on all interfaces.
320  *
321  * @source_object will be passed out in the various calls
322  * to accept to identify this particular source, which is
323  * useful if you're listening on multiple addresses and do
324  * different things depending on what address is connected to.
325  *
326  * Returns: %TRUE on success, %FALSE on error.
327  *
328  * Since: 2.22
329  */
330 gboolean
331 g_socket_listener_add_inet_port (GSocketListener  *listener,
332                                  guint16           port,
333                                  GObject          *source_object,
334                                  GError          **error)
335 {
336   gboolean need_ipv4_socket = TRUE;
337   GSocket *socket4 = NULL;
338   GSocket *socket6;
339
340   g_return_val_if_fail (listener != NULL, FALSE);
341   g_return_val_if_fail (port != 0, FALSE);
342
343   if (!check_listener (listener, error))
344     return FALSE;
345
346   /* first try to create an IPv6 socket */
347   socket6 = g_socket_new (G_SOCKET_FAMILY_IPV6,
348                           G_SOCKET_TYPE_STREAM,
349                           G_SOCKET_PROTOCOL_DEFAULT,
350                           NULL);
351
352   if (socket6 != NULL)
353     /* IPv6 is supported on this platform, so if we fail now it is
354      * a result of being unable to bind to our port.  Don't fail
355      * silently as a result of this!
356      */
357     {
358       GInetAddress *inet_address;
359       GSocketAddress *address;
360       gboolean result;
361
362       inet_address = g_inet_address_new_any (G_SOCKET_FAMILY_IPV6);
363       address = g_inet_socket_address_new (inet_address, port);
364       g_object_unref (inet_address);
365
366       g_socket_set_listen_backlog (socket6, listener->priv->listen_backlog);
367
368       result = g_socket_bind (socket6, address, TRUE, error) &&
369                g_socket_listen (socket6, error);
370
371       g_object_unref (address);
372
373       if (!result)
374         {
375           g_object_unref (socket6);
376
377           return FALSE;
378         }
379
380       if (source_object)
381         g_object_set_qdata_full (G_OBJECT (socket6), source_quark,
382                                  g_object_ref (source_object),
383                                  g_object_unref);
384
385       /* If this socket already speaks IPv4 then we are done. */
386       if (g_socket_speaks_ipv4 (socket6))
387         need_ipv4_socket = FALSE;
388     }
389
390   if (need_ipv4_socket)
391     /* We are here for exactly one of the following reasons:
392      *
393      *   - our platform doesn't support IPv6
394      *   - we successfully created an IPv6 socket but it's V6ONLY
395      *
396      * In either case, we need to go ahead and create an IPv4 socket
397      * and fail the call if we can't bind to it.
398      */
399     {
400       socket4 = g_socket_new (G_SOCKET_FAMILY_IPV4,
401                               G_SOCKET_TYPE_STREAM,
402                               G_SOCKET_PROTOCOL_DEFAULT,
403                               error);
404
405       if (socket4 != NULL)
406         /* IPv4 is supported on this platform, so if we fail now it is
407          * a result of being unable to bind to our port.  Don't fail
408          * silently as a result of this!
409          */
410         {
411           GInetAddress *inet_address;
412           GSocketAddress *address;
413           gboolean result;
414
415           inet_address = g_inet_address_new_any (G_SOCKET_FAMILY_IPV4);
416           address = g_inet_socket_address_new (inet_address, port);
417           g_object_unref (inet_address);
418
419           g_socket_set_listen_backlog (socket4,
420                                        listener->priv->listen_backlog);
421
422           result = g_socket_bind (socket4, address, TRUE, error) &&
423                    g_socket_listen (socket4, error);
424
425           g_object_unref (address);
426
427           if (!result)
428             {
429               g_object_unref (socket4);
430
431               if (socket6 != NULL)
432                 g_object_unref (socket6);
433
434               return FALSE;
435             }
436
437           if (source_object)
438             g_object_set_qdata_full (G_OBJECT (socket4), source_quark,
439                                      g_object_ref (source_object),
440                                      g_object_unref);
441         }
442       else
443         /* Ok.  So IPv4 is not supported on this platform.  If we
444          * succeeded at creating an IPv6 socket then that's OK, but
445          * otherwise we need to tell the user we failed.
446          */
447         {
448           if (socket6 != NULL)
449             g_clear_error (error);
450           else
451             return FALSE;
452         }
453     }
454
455   g_assert (socket6 != NULL || socket4 != NULL);
456
457   if (socket6 != NULL)
458     g_ptr_array_add (listener->priv->sockets, socket6);
459
460   if (socket4 != NULL)
461     g_ptr_array_add (listener->priv->sockets, socket4);
462
463   if (G_SOCKET_LISTENER_GET_CLASS (listener)->changed)
464     G_SOCKET_LISTENER_GET_CLASS (listener)->changed (listener);
465
466   return TRUE;
467 }
468
469 static GList *
470 add_sources (GSocketListener   *listener,
471              GSocketSourceFunc  callback,
472              gpointer           callback_data,
473              GCancellable      *cancellable,
474              GMainContext      *context)
475 {
476   GSocket *socket;
477   GSource *source;
478   GList *sources;
479   int i;
480
481   sources = NULL;
482   for (i = 0; i < listener->priv->sockets->len; i++)
483     {
484       socket = listener->priv->sockets->pdata[i];
485
486       source = g_socket_create_source (socket, G_IO_IN, cancellable);
487       g_source_set_callback (source,
488                              (GSourceFunc) callback,
489                              callback_data, NULL);
490       g_source_attach (source, context);
491
492       sources = g_list_prepend (sources, source);
493     }
494
495   return sources;
496 }
497
498 static void
499 free_sources (GList *sources)
500 {
501   GSource *source;
502   while (sources != NULL)
503     {
504       source = sources->data;
505       sources = g_list_delete_link (sources, sources);
506       g_source_destroy (source);
507       g_source_unref (source);
508     }
509 }
510
511 struct AcceptData {
512   GMainLoop *loop;
513   GSocket *socket;
514 };
515
516 static gboolean
517 accept_callback (GSocket      *socket,
518                  GIOCondition  condition,
519                  gpointer      user_data)
520 {
521   struct AcceptData *data = user_data;
522
523   data->socket = socket;
524   g_main_loop_quit (data->loop);
525
526   return TRUE;
527 }
528
529 /**
530  * g_socket_listener_accept_socket:
531  * @listener: a #GSocketListener
532  * @source_object: location where #GObject pointer will be stored, or %NULL
533  * @cancellable: optional #GCancellable object, %NULL to ignore.
534  * @error: #GError for error reporting, or %NULL to ignore.
535  *
536  * Blocks waiting for a client to connect to any of the sockets added
537  * to the listener. Returns the #GSocket that was accepted.
538  *
539  * If you want to accept the high-level #GSocketConnection, not a #GSocket,
540  * which is often the case, then you should use g_socket_listener_accept()
541  * instead.
542  *
543  * If @source_object is not %NULL it will be filled out with the source
544  * object specified when the corresponding socket or address was added
545  * to the listener.
546  *
547  * If @cancellable is not %NULL, then the operation can be cancelled by
548  * triggering the cancellable object from another thread. If the operation
549  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
550  *
551  * Returns: a #GSocket on success, %NULL on error.
552  *
553  * Since: 2.22
554  */
555 GSocket *
556 g_socket_listener_accept_socket (GSocketListener  *listener,
557                                  GObject         **source_object,
558                                  GCancellable     *cancellable,
559                                  GError          **error)
560 {
561   GSocket *accept_socket, *socket;
562
563   g_return_val_if_fail (G_IS_SOCKET_LISTENER (listener), NULL);
564
565   if (!check_listener (listener, error))
566     return NULL;
567
568   if (listener->priv->sockets->len == 1)
569     {
570       accept_socket = listener->priv->sockets->pdata[0];
571       if (!g_socket_condition_wait (accept_socket, G_IO_IN,
572                                     cancellable, error))
573         return NULL;
574     }
575   else
576     {
577       GList *sources;
578       struct AcceptData data;
579       GMainLoop *loop;
580
581       if (listener->priv->main_context == NULL)
582         listener->priv->main_context = g_main_context_new ();
583
584       loop = g_main_loop_new (listener->priv->main_context, FALSE);
585       data.loop = loop;
586       sources = add_sources (listener,
587                              accept_callback,
588                              &data,
589                              cancellable,
590                              listener->priv->main_context);
591       g_main_loop_run (loop);
592       accept_socket = data.socket;
593       free_sources (sources);
594       g_main_loop_unref (loop);
595     }
596
597   if (!(socket = g_socket_accept (accept_socket, error)))
598     return NULL;
599
600   if (source_object)
601     *source_object = g_object_get_qdata (G_OBJECT (accept_socket), source_quark);
602
603   return socket;
604 }
605
606 /**
607  * g_socket_listener_accept:
608  * @listener: a #GSocketListener
609  * @source_object: location where #GObject pointer will be stored, or %NULL
610  * @cancellable: optional #GCancellable object, %NULL to ignore.
611  * @error: #GError for error reporting, or %NULL to ignore.
612  *
613  * Blocks waiting for a client to connect to any of the sockets added
614  * to the listener. Returns a #GSocketConnection for the socket that was
615  * accepted.
616  *
617  * If @source_object is not %NULL it will be filled out with the source
618  * object specified when the corresponding socket or address was added
619  * to the listener.
620  *
621  * If @cancellable is not %NULL, then the operation can be cancelled by
622  * triggering the cancellable object from another thread. If the operation
623  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
624  *
625  * Returns: a #GSocketConnection on success, %NULL on error.
626  *
627  * Since: 2.22
628  */
629 GSocketConnection *
630 g_socket_listener_accept (GSocketListener  *listener,
631                           GObject         **source_object,
632                           GCancellable     *cancellable,
633                           GError          **error)
634 {
635   GSocketConnection *connection;
636   GSocket *socket;
637
638   socket = g_socket_listener_accept_socket (listener,
639                                             source_object,
640                                             cancellable,
641                                             error);
642   if (socket == NULL)
643     return NULL;
644
645   connection = g_socket_connection_factory_create_connection (socket);
646   g_object_unref (socket);
647
648   return connection;
649 }
650
651 struct AcceptAsyncData {
652   GSimpleAsyncResult *simple;
653   GCancellable *cancellable;
654   GList *sources;
655 };
656
657 static gboolean
658 accept_ready (GSocket      *accept_socket,
659               GIOCondition  condition,
660               gpointer      _data)
661 {
662   struct AcceptAsyncData *data = _data;
663   GError *error = NULL;
664
665   if (!g_cancellable_set_error_if_cancelled (data->cancellable,
666                                              &error))
667     {
668       GSocket *socket;
669       GObject *source_object;
670
671       socket = g_socket_accept (accept_socket, &error);
672       if (socket)
673         {
674           g_simple_async_result_set_op_res_gpointer (data->simple, socket,
675                                                      g_object_unref);
676           source_object = g_object_get_qdata (G_OBJECT (accept_socket), source_quark);
677           if (source_object)
678             g_object_set_qdata_full (G_OBJECT (data->simple),
679                                      source_quark,
680                                      g_object_ref (source_object), g_object_unref);
681         }
682     }
683
684   if (error)
685     {
686       g_simple_async_result_set_from_error (data->simple, error);
687       g_error_free (error);
688     }
689
690   g_simple_async_result_complete_in_idle (data->simple);
691   g_object_unref (data->simple);
692   free_sources (data->sources);
693   g_free (data);
694
695   return FALSE;
696 }
697
698 /**
699  * g_socket_listener_accept_socket_async:
700  * @listener: a #GSocketListener
701  * @cancellable: a #GCancellable, or %NULL
702  * @callback: a #GAsyncReadyCallback
703  * @user_data: user data for the callback
704  *
705  * This is the asynchronous version of g_socket_listener_accept_socket().
706  *
707  * When the operation is finished @callback will be
708  * called. You can then call g_socket_listener_accept_socket_finish()
709  * to get the result of the operation.
710  *
711  * Since: 2.22
712  */
713 void
714 g_socket_listener_accept_socket_async (GSocketListener     *listener,
715                                        GCancellable        *cancellable,
716                                        GAsyncReadyCallback  callback,
717                                        gpointer             user_data)
718 {
719   struct AcceptAsyncData *data;
720   GError *error = NULL;
721
722   if (!check_listener (listener, &error))
723     {
724       g_simple_async_report_gerror_in_idle (G_OBJECT (listener),
725                                             callback, user_data,
726                                             error);
727       g_error_free (error);
728       return;
729     }
730
731   data = g_new0 (struct AcceptAsyncData, 1);
732   data->simple = g_simple_async_result_new (G_OBJECT (listener),
733                                             callback, user_data,
734                                             g_socket_listener_accept_socket_async);
735   data->cancellable = cancellable;
736   data->sources = add_sources (listener,
737                                accept_ready,
738                                data,
739                                cancellable,
740                                NULL);
741 }
742
743 /**
744  * g_socket_listener_accept_socket_finish:
745  * @listener: a #GSocketListener
746  * @result: a #GAsyncResult.
747  * @source_object: Optional #GObject identifying this source
748  * @error: a #GError location to store the error occuring, or %NULL to
749  * ignore.
750  *
751  * Finishes an async accept operation. See g_socket_listener_accept_socket_async()
752  *
753  * Returns: a #GSocket on success, %NULL on error.
754  *
755  * Since: 2.22
756  */
757 GSocket *
758 g_socket_listener_accept_socket_finish (GSocketListener  *listener,
759                                         GAsyncResult     *result,
760                                         GObject         **source_object,
761                                         GError          **error)
762 {
763   GSocket *socket;
764   GSimpleAsyncResult *simple;
765
766   g_return_val_if_fail (G_IS_SOCKET_LISTENER (listener), FALSE);
767
768   simple = G_SIMPLE_ASYNC_RESULT (result);
769
770   if (g_simple_async_result_propagate_error (simple, error))
771     return NULL;
772
773   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_socket_listener_accept_socket_async);
774
775   socket = g_simple_async_result_get_op_res_gpointer (simple);
776
777   if (source_object)
778     *source_object = g_object_get_qdata (G_OBJECT (result), source_quark);
779
780   return g_object_ref (socket);
781 }
782
783 /**
784  * g_socket_listener_accept_async:
785  * @listener: a #GSocketListener
786  * @cancellable: a #GCancellable, or %NULL
787  * @callback: a #GAsyncReadyCallback
788  * @user_data: user data for the callback
789  *
790  * This is the asynchronous version of g_socket_listener_accept().
791  *
792  * When the operation is finished @callback will be
793  * called. You can then call g_socket_listener_accept_socket()
794  * to get the result of the operation.
795  *
796  * Since: 2.22
797  */
798 void
799 g_socket_listener_accept_async (GSocketListener     *listener,
800                                 GCancellable        *cancellable,
801                                 GAsyncReadyCallback  callback,
802                                 gpointer             user_data)
803 {
804   g_socket_listener_accept_socket_async (listener,
805                                          cancellable,
806                                          callback,
807                                          user_data);
808 }
809
810 /**
811  * g_socket_listener_accept_finish:
812  * @listener: a #GSocketListener
813  * @result: a #GAsyncResult.
814  * @source_object: Optional #GObject identifying this source
815  * @error: a #GError location to store the error occuring, or %NULL to
816  * ignore.
817  *
818  * Finishes an async accept operation. See g_socket_listener_accept_async()
819  *
820  * Returns: a #GSocketConnection on success, %NULL on error.
821  *
822  * Since: 2.22
823  */
824 GSocketConnection *
825 g_socket_listener_accept_finish (GSocketListener  *listener,
826                                  GAsyncResult     *result,
827                                  GObject         **source_object,
828                                  GError          **error)
829 {
830   GSocket *socket;
831   GSocketConnection *connection;
832
833   socket = g_socket_listener_accept_socket_finish (listener,
834                                                    result,
835                                                    source_object,
836                                                    error);
837   if (socket == NULL)
838     return NULL;
839
840   connection = g_socket_connection_factory_create_connection (socket);
841   g_object_unref (socket);
842   return connection;
843 }
844
845 /**
846  * g_socket_listener_set_backlog:
847  * @listener: a #GSocketListener
848  * @listen_backlog: an integer
849  *
850  * Sets the listen backlog on the sockets in the listener.
851  *
852  * See g_socket_set_listen_backlog() for details
853  *
854  * Since: 2.22
855  */
856 void
857 g_socket_listener_set_backlog (GSocketListener *listener,
858                                int              listen_backlog)
859 {
860   GSocket *socket;
861   int i;
862
863   if (listener->priv->closed)
864     return;
865
866   listener->priv->listen_backlog = listen_backlog;
867
868   for (i = 0; i < listener->priv->sockets->len; i++)
869     {
870       socket = listener->priv->sockets->pdata[i];
871       g_socket_set_listen_backlog (socket, listen_backlog);
872     }
873 }
874
875 /**
876  * g_socket_listener_close:
877  * @listener: a #GSocketListener
878  *
879  * Closes all the sockets in the listener.
880  *
881  * Since: 2.22
882  */
883 void
884 g_socket_listener_close (GSocketListener *listener)
885 {
886   GSocket *socket;
887   int i;
888
889   g_return_if_fail (G_IS_SOCKET_LISTENER (listener));
890
891   if (listener->priv->closed)
892     return;
893
894   for (i = 0; i < listener->priv->sockets->len; i++)
895     {
896       socket = listener->priv->sockets->pdata[i];
897       g_socket_close (socket, NULL);
898     }
899   listener->priv->closed = TRUE;
900 }
901
902 #define __G_SOCKET_LISTENER_C__
903 #include "gioaliasdef.c"