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