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 * SPDX-License-Identifier: LGPL-2.1-or-later
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General
20 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
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/gioenumtypes.h>
32 #include <gio/gtask.h>
33 #include <gio/gcancellable.h>
34 #include <gio/gsocketaddress.h>
35 #include <gio/ginetaddress.h>
36 #include <gio/gioerror.h>
37 #include <gio/gsocket.h>
38 #include <gio/gsocketconnection.h>
39 #include <gio/ginetsocketaddress.h>
40 #include "glibintl.h"
41 #include "gmarshal-internal.h"
42
43
44 /**
45 * GSocketListener:
46 *
47 * A `GSocketListener` is an object that keeps track of a set
48 * of server sockets and helps you accept sockets from any of the
49 * socket, either sync or async.
50 *
51 * Add addresses and ports to listen on using
52 * [method@Gio.SocketListener.add_address] and
53 * [method@Gio.SocketListener.add_inet_port]. These will be listened on until
54 * [method@Gio.SocketListener.close] is called. Dropping your final reference to
55 * the `GSocketListener` will not cause [method@Gio.SocketListener.close] to be
56 * called implicitly, as some references to the `GSocketListener` may be held
57 * internally.
58 *
59 * If you want to implement a network server, also look at
60 * [class@Gio.SocketService] and [class@Gio.ThreadedSocketService] which are
61 * subclasses of `GSocketListener` that make this even easier.
62 *
63 * Since: 2.22
64 */
65
66 enum
67 {
68 PROP_0,
69 PROP_LISTEN_BACKLOG
70 };
71
72 enum
73 {
74 EVENT,
75 LAST_SIGNAL
76 };
77
78 static guint signals[LAST_SIGNAL] = { 0 };
79
80 static GQuark source_quark = 0;
81
82 struct _GSocketListenerPrivate
83 {
84 GPtrArray *sockets;
85 GMainContext *main_context;
86 int listen_backlog;
87 guint closed : 1;
88 };
89
90 G_DEFINE_TYPE_WITH_PRIVATE (GSocketListener, g_socket_listener, G_TYPE_OBJECT)
91
92 static void
93 g_socket_listener_finalize (GObject *object)
94 {
95 GSocketListener *listener = G_SOCKET_LISTENER (object);
96
97 if (listener->priv->main_context)
98 g_main_context_unref (listener->priv->main_context);
99
100 /* Do not explicitly close the sockets. Instead, let them close themselves if
101 * their final reference is dropped, but keep them open if a reference is
102 * held externally to the GSocketListener (which is possible if
103 * g_socket_listener_add_socket() was used).
104 */
105 g_ptr_array_free (listener->priv->sockets, TRUE);
106
107 G_OBJECT_CLASS (g_socket_listener_parent_class)
108 ->finalize (object);
109 }
110
111 static void
112 g_socket_listener_get_property (GObject *object,
113 guint prop_id,
114 GValue *value,
115 GParamSpec *pspec)
116 {
117 GSocketListener *listener = G_SOCKET_LISTENER (object);
118
119 switch (prop_id)
120 {
121 case PROP_LISTEN_BACKLOG:
122 g_value_set_int (value, listener->priv->listen_backlog);
123 break;
124
125 default:
126 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
127 }
128 }
129
130 static void
131 g_socket_listener_set_property (GObject *object,
132 guint prop_id,
133 const GValue *value,
134 GParamSpec *pspec)
135 {
136 GSocketListener *listener = G_SOCKET_LISTENER (object);
137
138 switch (prop_id)
139 {
140 case PROP_LISTEN_BACKLOG:
141 g_socket_listener_set_backlog (listener, g_value_get_int (value));
142 break;
143
144 default:
145 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
146 }
147 }
148
149 static void
150 g_socket_listener_class_init (GSocketListenerClass *klass)
151 {
152 GObjectClass *gobject_class G_GNUC_UNUSED = G_OBJECT_CLASS (klass);
153
154 gobject_class->finalize = g_socket_listener_finalize;
155 gobject_class->set_property = g_socket_listener_set_property;
156 gobject_class->get_property = g_socket_listener_get_property;
157
158 /**
159 * GSocketListener:listen-backlog:
160 *
161 * The number of outstanding connections in the listen queue.
162 *
163 * Since: 2.22
164 */
165 g_object_class_install_property (gobject_class, PROP_LISTEN_BACKLOG,
166 g_param_spec_int ("listen-backlog", NULL, NULL,
167 0,
168 2000,
169 10,
170 G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
171
172 /**
173 * GSocketListener::event:
174 * @listener: the #GSocketListener
175 * @event: the event that is occurring
176 * @socket: the #GSocket the event is occurring on
177 *
178 * Emitted when @listener's activity on @socket changes state.
179 * Note that when @listener is used to listen on both IPv4 and
180 * IPv6, a separate set of signals will be emitted for each, and
181 * the order they happen in is undefined.
182 *
183 * Since: 2.46
184 */
185 signals[EVENT] =
186 g_signal_new (I_("event"),
187 G_TYPE_FROM_CLASS (gobject_class),
188 G_SIGNAL_RUN_LAST,
189 G_STRUCT_OFFSET (GSocketListenerClass, event),
190 NULL, NULL,
191 _g_cclosure_marshal_VOID__ENUM_OBJECT,
192 G_TYPE_NONE, 2,
193 G_TYPE_SOCKET_LISTENER_EVENT,
194 G_TYPE_SOCKET);
195 g_signal_set_va_marshaller (signals[EVENT],
196 G_TYPE_FROM_CLASS (gobject_class),
197 _g_cclosure_marshal_VOID__ENUM_OBJECTv);
198
199 source_quark = g_quark_from_static_string ("g-socket-listener-source");
200 }
201
202 static void
203 g_socket_listener_init (GSocketListener *listener)
204 {
205 listener->priv = g_socket_listener_get_instance_private (listener);
206 listener->priv->sockets =
207 g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref);
208 listener->priv->listen_backlog = 10;
209 }
210
211 /**
212 * g_socket_listener_new:
213 *
214 * Creates a new #GSocketListener with no sockets to listen for.
215 * New listeners can be added with e.g. g_socket_listener_add_address()
216 * or g_socket_listener_add_inet_port().
217 *
218 * Returns: a new #GSocketListener.
219 *
220 * Since: 2.22
221 */
222 GSocketListener *
223 g_socket_listener_new (void)
224 {
225 return g_object_new (G_TYPE_SOCKET_LISTENER, NULL);
226 }
227
228 static gboolean
229 check_listener (GSocketListener *listener,
230 GError **error)
231 {
232 if (listener->priv->closed)
233 {
234 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
235 _("Listener is already closed"));
236 return FALSE;
237 }
238
239 return TRUE;
240 }
241
242 /**
243 * g_socket_listener_add_socket:
244 * @listener: a #GSocketListener
245 * @socket: a listening #GSocket
246 * @source_object: (nullable): Optional #GObject identifying this source
247 * @error: #GError for error reporting, or %NULL to ignore.
248 *
249 * Adds @socket to the set of sockets that we try to accept
250 * new clients from. The socket must be bound to a local
251 * address and listened to.
252 *
253 * @source_object will be passed out in the various calls
254 * to accept to identify this particular source, which is
255 * useful if you're listening on multiple addresses and do
256 * different things depending on what address is connected to.
257 *
258 * The @socket will not be automatically closed when the @listener is finalized
259 * unless the listener held the final reference to the socket. Before GLib 2.42,
260 * the @socket was automatically closed on finalization of the @listener, even
261 * if references to it were held elsewhere.
262 *
263 * Returns: %TRUE on success, %FALSE on error.
264 *
265 * Since: 2.22
266 */
267 gboolean
268 g_socket_listener_add_socket (GSocketListener *listener,
269 GSocket *socket,
270 GObject *source_object,
271 GError **error)
272 {
273 if (!check_listener (listener, error))
274 return FALSE;
275
276 /* TODO: Check that socket it is bound & not closed? */
277
278 if (g_socket_is_closed (socket))
279 {
280 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
281 _("Added socket is closed"));
282 return FALSE;
283 }
284
285 g_object_ref (socket);
286 g_ptr_array_add (listener->priv->sockets, socket);
287
288 if (source_object)
289 g_object_set_qdata_full (G_OBJECT (socket), source_quark,
290 g_object_ref (source_object), g_object_unref);
291
292
293 if (G_SOCKET_LISTENER_GET_CLASS (listener)->changed)
294 G_SOCKET_LISTENER_GET_CLASS (listener)->changed (listener);
295
296 return TRUE;
297 }
298
299 /**
300 * g_socket_listener_add_address:
301 * @listener: a #GSocketListener
302 * @address: a #GSocketAddress
303 * @type: a #GSocketType
304 * @protocol: a #GSocketProtocol
305 * @source_object: (nullable): Optional #GObject identifying this source
306 * @effective_address: (out) (optional): location to store the address that was bound to, or %NULL.
307 * @error: #GError for error reporting, or %NULL to ignore.
308 *
309 * Creates a socket of type @type and protocol @protocol, binds
310 * it to @address and adds it to the set of sockets we're accepting
311 * sockets from.
312 *
313 * Note that adding an IPv6 address, depending on the platform,
314 * may or may not result in a listener that also accepts IPv4
315 * connections. For more deterministic behavior, see
316 * g_socket_listener_add_inet_port().
317 *
318 * @source_object will be passed out in the various calls
319 * to accept to identify this particular source, which is
320 * useful if you're listening on multiple addresses and do
321 * different things depending on what address is connected to.
322 *
323 * If successful and @effective_address is non-%NULL then it will
324 * be set to the address that the binding actually occurred at. This
325 * is helpful for determining the port number that was used for when
326 * requesting a binding to port 0 (ie: "any port"). This address, if
327 * requested, belongs to the caller and must be freed.
328 *
329 * Call g_socket_listener_close() to stop listening on @address; this will not
330 * be done automatically when you drop your final reference to @listener, as
331 * references may be held internally.
332 *
333 * Returns: %TRUE on success, %FALSE on error.
334 *
335 * Since: 2.22
336 */
337 gboolean
338 g_socket_listener_add_address (GSocketListener *listener,
339 GSocketAddress *address,
340 GSocketType type,
341 GSocketProtocol protocol,
342 GObject *source_object,
343 GSocketAddress **effective_address,
344 GError **error)
345 {
346 GSocketAddress *local_address;
347 GSocketFamily family;
348 GSocket *socket;
349
350 if (!check_listener (listener, error))
351 return FALSE;
352
353 family = g_socket_address_get_family (address);
354 socket = g_socket_new (family, type, protocol, error);
355 if (socket == NULL)
356 return FALSE;
357
358 g_socket_set_listen_backlog (socket, listener->priv->listen_backlog);
359
360 g_signal_emit (listener, signals[EVENT], 0,
361 G_SOCKET_LISTENER_BINDING, socket);
362
363 if (!g_socket_bind (socket, address, TRUE, error))
364 {
365 g_object_unref (socket);
366 return FALSE;
367 }
368
369 g_signal_emit (listener, signals[EVENT], 0,
370 G_SOCKET_LISTENER_BOUND, socket);
371 g_signal_emit (listener, signals[EVENT], 0,
372 G_SOCKET_LISTENER_LISTENING, socket);
373
374 if (!g_socket_listen (socket, error))
375 {
376 g_object_unref (socket);
377 return FALSE;
378 }
379
380 g_signal_emit (listener, signals[EVENT], 0,
381 G_SOCKET_LISTENER_LISTENED, socket);
382
383 local_address = NULL;
384 if (effective_address)
385 {
386 local_address = g_socket_get_local_address (socket, error);
387 if (local_address == NULL)
388 {
389 g_object_unref (socket);
390 return FALSE;
391 }
392 }
393
394 if (!g_socket_listener_add_socket (listener, socket,
395 source_object,
396 error))
397 {
398 if (local_address)
399 g_object_unref (local_address);
400 g_object_unref (socket);
401 return FALSE;
402 }
403
404 if (effective_address)
405 *effective_address = local_address;
406
407 g_object_unref (socket); /* add_socket refs this */
408
409 return TRUE;
410 }
411
412 /**
413 * g_socket_listener_add_inet_port:
414 * @listener: a #GSocketListener
415 * @port: an IP port number (non-zero)
416 * @source_object: (nullable): Optional #GObject identifying this source
417 * @error: #GError for error reporting, or %NULL to ignore.
418 *
419 * Helper function for g_socket_listener_add_address() that
420 * creates a TCP/IP socket listening on IPv4 and IPv6 (if
421 * supported) on the specified port on all interfaces.
422 *
423 * @source_object will be passed out in the various calls
424 * to accept to identify this particular source, which is
425 * useful if you're listening on multiple addresses and do
426 * different things depending on what address is connected to.
427 *
428 * Call g_socket_listener_close() to stop listening on @port; this will not
429 * be done automatically when you drop your final reference to @listener, as
430 * references may be held internally.
431 *
432 * Returns: %TRUE on success, %FALSE on error.
433 *
434 * Since: 2.22
435 */
436 gboolean
437 g_socket_listener_add_inet_port (GSocketListener *listener,
438 guint16 port,
439 GObject *source_object,
440 GError **error)
441 {
442 gboolean need_ipv4_socket = TRUE;
443 GSocket *socket4 = NULL;
444 GSocket *socket6;
445
446 g_return_val_if_fail (listener != NULL, FALSE);
447 g_return_val_if_fail (port != 0, FALSE);
448
449 if (!check_listener (listener, error))
450 return FALSE;
451
452 /* first try to create an IPv6 socket */
453 socket6 = g_socket_new (G_SOCKET_FAMILY_IPV6,
454 G_SOCKET_TYPE_STREAM,
455 G_SOCKET_PROTOCOL_DEFAULT,
456 NULL);
457
458 if (socket6 != NULL)
459 /* IPv6 is supported on this platform, so if we fail now it is
460 * a result of being unable to bind to our port. Don't fail
461 * silently as a result of this!
462 */
463 {
464 GInetAddress *inet_address;
465 GSocketAddress *address;
466
467 inet_address = g_inet_address_new_any (G_SOCKET_FAMILY_IPV6);
468 address = g_inet_socket_address_new (inet_address, port);
469 g_object_unref (inet_address);
470
471 g_socket_set_listen_backlog (socket6, listener->priv->listen_backlog);
472
473 g_signal_emit (listener, signals[EVENT], 0,
474 G_SOCKET_LISTENER_BINDING, socket6);
475
476 if (!g_socket_bind (socket6, address, TRUE, error))
477 {
478 g_object_unref (address);
479 g_object_unref (socket6);
480 return FALSE;
481 }
482
483 g_object_unref (address);
484
485 g_signal_emit (listener, signals[EVENT], 0,
486 G_SOCKET_LISTENER_BOUND, socket6);
487 g_signal_emit (listener, signals[EVENT], 0,
488 G_SOCKET_LISTENER_LISTENING, socket6);
489
490 if (!g_socket_listen (socket6, error))
491 {
492 g_object_unref (socket6);
493 return FALSE;
494 }
495
496 g_signal_emit (listener, signals[EVENT], 0,
497 G_SOCKET_LISTENER_LISTENED, socket6);
498
499 if (source_object)
500 g_object_set_qdata_full (G_OBJECT (socket6), source_quark,
501 g_object_ref (source_object),
502 g_object_unref);
503
504 /* If this socket already speaks IPv4 then we are done. */
505 if (g_socket_speaks_ipv4 (socket6))
506 need_ipv4_socket = FALSE;
507 }
508
509 if (need_ipv4_socket)
510 /* We are here for exactly one of the following reasons:
511 *
512 * - our platform doesn't support IPv6
513 * - we successfully created an IPv6 socket but it's V6ONLY
514 *
515 * In either case, we need to go ahead and create an IPv4 socket
516 * and fail the call if we can't bind to it.
517 */
518 {
519 socket4 = g_socket_new (G_SOCKET_FAMILY_IPV4,
520 G_SOCKET_TYPE_STREAM,
521 G_SOCKET_PROTOCOL_DEFAULT,
522 error);
523
524 if (socket4 != NULL)
525 /* IPv4 is supported on this platform, so if we fail now it is
526 * a result of being unable to bind to our port. Don't fail
527 * silently as a result of this!
528 */
529 {
530 GInetAddress *inet_address;
531 GSocketAddress *address;
532
533 inet_address = g_inet_address_new_any (G_SOCKET_FAMILY_IPV4);
534 address = g_inet_socket_address_new (inet_address, port);
535 g_object_unref (inet_address);
536
537 g_socket_set_listen_backlog (socket4,
538 listener->priv->listen_backlog);
539
540 g_signal_emit (listener, signals[EVENT], 0,
541 G_SOCKET_LISTENER_BINDING, socket4);
542
543 if (!g_socket_bind (socket4, address, TRUE, error))
544 {
545 g_object_unref (address);
546 g_object_unref (socket4);
547 if (socket6 != NULL)
548 g_object_unref (socket6);
549
550 return FALSE;
551 }
552
553 g_object_unref (address);
554
555 g_signal_emit (listener, signals[EVENT], 0,
556 G_SOCKET_LISTENER_BOUND, socket4);
557 g_signal_emit (listener, signals[EVENT], 0,
558 G_SOCKET_LISTENER_LISTENING, socket4);
559
560 if (!g_socket_listen (socket4, error))
561 {
562 g_object_unref (socket4);
563 if (socket6 != NULL)
564 g_object_unref (socket6);
565
566 return FALSE;
567 }
568
569 g_signal_emit (listener, signals[EVENT], 0,
570 G_SOCKET_LISTENER_LISTENED, socket4);
571
572 if (source_object)
573 g_object_set_qdata_full (G_OBJECT (socket4), source_quark,
574 g_object_ref (source_object),
575 g_object_unref);
576 }
577 else
578 /* Ok. So IPv4 is not supported on this platform. If we
579 * succeeded at creating an IPv6 socket then that's OK, but
580 * otherwise we need to tell the user we failed.
581 */
582 {
583 if (socket6 != NULL)
584 g_clear_error (error);
585 else
586 return FALSE;
587 }
588 }
589
590 g_assert (socket6 != NULL || socket4 != NULL);
591
592 if (socket6 != NULL)
593 g_ptr_array_add (listener->priv->sockets, socket6);
594
595 if (socket4 != NULL)
596 g_ptr_array_add (listener->priv->sockets, socket4);
597
598 if (G_SOCKET_LISTENER_GET_CLASS (listener)->changed)
599 G_SOCKET_LISTENER_GET_CLASS (listener)->changed (listener);
600
601 return TRUE;
602 }
603
604 static GList *
605 add_sources (GSocketListener *listener,
606 GSocketSourceFunc callback,
607 gpointer callback_data,
608 GCancellable *cancellable,
609 GMainContext *context)
610 {
611 GSocket *socket;
612 GSource *source;
613 GList *sources;
614 guint i;
615
616 sources = NULL;
617 for (i = 0; i < listener->priv->sockets->len; i++)
618 {
619 socket = listener->priv->sockets->pdata[i];
620
621 source = g_socket_create_source (socket, G_IO_IN, cancellable);
622 g_source_set_callback (source,
623 (GSourceFunc) callback,
624 callback_data, NULL);
625 g_source_attach (source, context);
626
627 sources = g_list_prepend (sources, source);
628 }
629
630 return sources;
631 }
632
633 static void
634 free_sources (GList *sources)
635 {
636 GSource *source;
637 while (sources != NULL)
638 {
639 source = sources->data;
640 sources = g_list_delete_link (sources, sources);
641 g_source_destroy (source);
642 g_source_unref (source);
643 }
644 }
645
646 struct AcceptData {
647 GMainLoop *loop;
648 GSocket *socket;
649 };
650
651 static gboolean
652 accept_callback (GSocket *socket,
653 GIOCondition condition,
654 gpointer user_data)
655 {
656 struct AcceptData *data = user_data;
657
658 data->socket = socket;
659 g_main_loop_quit (data->loop);
660
661 return TRUE;
662 }
663
664 /**
665 * g_socket_listener_accept_socket:
666 * @listener: a #GSocketListener
667 * @source_object: (out) (transfer none) (optional) (nullable): location where #GObject pointer will be stored, or %NULL.
668 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
669 * @error: #GError for error reporting, or %NULL to ignore.
670 *
671 * Blocks waiting for a client to connect to any of the sockets added
672 * to the listener. Returns the #GSocket that was accepted.
673 *
674 * If you want to accept the high-level #GSocketConnection, not a #GSocket,
675 * which is often the case, then you should use g_socket_listener_accept()
676 * instead.
677 *
678 * If @source_object is not %NULL it will be filled out with the source
679 * object specified when the corresponding socket or address was added
680 * to the listener.
681 *
682 * If @cancellable is not %NULL, then the operation can be cancelled by
683 * triggering the cancellable object from another thread. If the operation
684 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
685 *
686 * Returns: (transfer full): a #GSocket on success, %NULL on error.
687 *
688 * Since: 2.22
689 */
690 GSocket *
691 g_socket_listener_accept_socket (GSocketListener *listener,
692 GObject **source_object,
693 GCancellable *cancellable,
694 GError **error)
695 {
696 GSocket *accept_socket, *socket;
697
698 g_return_val_if_fail (G_IS_SOCKET_LISTENER (listener), NULL);
699
700 if (!check_listener (listener, error))
701 return NULL;
702
703 if (listener->priv->sockets->len == 1)
704 {
705 accept_socket = listener->priv->sockets->pdata[0];
706 if (!g_socket_condition_wait (accept_socket, G_IO_IN,
707 cancellable, error))
708 return NULL;
709 }
710 else
711 {
712 GList *sources;
713 struct AcceptData data;
714 GMainLoop *loop;
715
716 if (listener->priv->main_context == NULL)
717 listener->priv->main_context = g_main_context_new ();
718
719 loop = g_main_loop_new (listener->priv->main_context, FALSE);
720 data.loop = loop;
721 sources = add_sources (listener,
722 accept_callback,
723 &data,
724 cancellable,
725 listener->priv->main_context);
726 g_main_loop_run (loop);
727 accept_socket = data.socket;
728 free_sources (sources);
729 g_main_loop_unref (loop);
730 }
731
732 if (!(socket = g_socket_accept (accept_socket, cancellable, error)))
733 return NULL;
734
735 if (source_object)
736 *source_object = g_object_get_qdata (G_OBJECT (accept_socket), source_quark);
737
738 return socket;
739 }
740
741 /**
742 * g_socket_listener_accept:
743 * @listener: a #GSocketListener
744 * @source_object: (out) (transfer none) (optional) (nullable): location where #GObject pointer will be stored, or %NULL
745 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
746 * @error: #GError for error reporting, or %NULL to ignore.
747 *
748 * Blocks waiting for a client to connect to any of the sockets added
749 * to the listener. Returns a #GSocketConnection for the socket that was
750 * accepted.
751 *
752 * If @source_object is not %NULL it will be filled out with the source
753 * object specified when the corresponding socket or address was added
754 * to the listener.
755 *
756 * If @cancellable is not %NULL, then the operation can be cancelled by
757 * triggering the cancellable object from another thread. If the operation
758 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
759 *
760 * Returns: (transfer full): a #GSocketConnection on success, %NULL on error.
761 *
762 * Since: 2.22
763 */
764 GSocketConnection *
765 g_socket_listener_accept (GSocketListener *listener,
766 GObject **source_object,
767 GCancellable *cancellable,
768 GError **error)
769 {
770 GSocketConnection *connection;
771 GSocket *socket;
772
773 socket = g_socket_listener_accept_socket (listener,
774 source_object,
775 cancellable,
776 error);
777 if (socket == NULL)
778 return NULL;
779
780 connection = g_socket_connection_factory_create_connection (socket);
781 g_object_unref (socket);
782
783 return connection;
784 }
785
786 typedef struct
787 {
788 GList *sources; /* (element-type GSource) */
789 gboolean returned_yet;
790 } AcceptSocketAsyncData;
791
792 static void
793 accept_socket_async_data_free (AcceptSocketAsyncData *data)
794 {
795 free_sources (data->sources);
796 g_free (data);
797 }
798
799 static gboolean
800 accept_ready (GSocket *accept_socket,
801 GIOCondition condition,
802 gpointer user_data)
803 {
804 GTask *task = user_data;
805 GError *error = NULL;
806 GSocket *socket;
807 GObject *source_object;
808 AcceptSocketAsyncData *data = g_task_get_task_data (task);
809
810 /* Don’t call g_task_return_*() multiple times if we have multiple incoming
811 * connections in the same #GMainContext iteration. */
812 if (data->returned_yet)
813 return G_SOURCE_REMOVE;
814
815 socket = g_socket_accept (accept_socket, g_task_get_cancellable (task), &error);
816 if (socket)
817 {
818 source_object = g_object_get_qdata (G_OBJECT (accept_socket), source_quark);
819 if (source_object)
820 g_object_set_qdata_full (G_OBJECT (task),
821 source_quark,
822 g_object_ref (source_object), g_object_unref);
823 g_task_return_pointer (task, socket, g_object_unref);
824 }
825 else
826 {
827 g_task_return_error (task, error);
828 }
829
830 data->returned_yet = TRUE;
831 g_object_unref (task);
832
833 return G_SOURCE_REMOVE;
834 }
835
836 /**
837 * g_socket_listener_accept_socket_async:
838 * @listener: a #GSocketListener
839 * @cancellable: (nullable): a #GCancellable, or %NULL
840 * @callback: (scope async): a #GAsyncReadyCallback
841 * @user_data: user data for the callback
842 *
843 * This is the asynchronous version of g_socket_listener_accept_socket().
844 *
845 * When the operation is finished @callback will be
846 * called. You can then call g_socket_listener_accept_socket_finish()
847 * to get the result of the operation.
848 *
849 * Since: 2.22
850 */
851 void
852 g_socket_listener_accept_socket_async (GSocketListener *listener,
853 GCancellable *cancellable,
854 GAsyncReadyCallback callback,
855 gpointer user_data)
856 {
857 GTask *task;
858 GError *error = NULL;
859 AcceptSocketAsyncData *data = NULL;
860
861 task = g_task_new (listener, cancellable, callback, user_data);
862 g_task_set_source_tag (task, g_socket_listener_accept_socket_async);
863
864 if (!check_listener (listener, &error))
865 {
866 g_task_return_error (task, error);
867 g_object_unref (task);
868 return;
869 }
870
871 data = g_new0 (AcceptSocketAsyncData, 1);
872 data->returned_yet = FALSE;
873 data->sources = add_sources (listener,
874 accept_ready,
875 task,
876 cancellable,
877 g_main_context_get_thread_default ());
878 g_task_set_task_data (task, g_steal_pointer (&data),
879 (GDestroyNotify) accept_socket_async_data_free);
880 }
881
882 /**
883 * g_socket_listener_accept_socket_finish:
884 * @listener: a #GSocketListener
885 * @result: a #GAsyncResult.
886 * @source_object: (out) (transfer none) (optional) (nullable): Optional #GObject identifying this source
887 * @error: a #GError location to store the error occurring, or %NULL to
888 * ignore.
889 *
890 * Finishes an async accept operation. See g_socket_listener_accept_socket_async()
891 *
892 * Returns: (transfer full): a #GSocket on success, %NULL on error.
893 *
894 * Since: 2.22
895 */
896 GSocket *
897 g_socket_listener_accept_socket_finish (GSocketListener *listener,
898 GAsyncResult *result,
899 GObject **source_object,
900 GError **error)
901 {
902 g_return_val_if_fail (G_IS_SOCKET_LISTENER (listener), NULL);
903 g_return_val_if_fail (g_task_is_valid (result, listener), NULL);
904
905 if (source_object)
906 *source_object = g_object_get_qdata (G_OBJECT (result), source_quark);
907
908 return g_task_propagate_pointer (G_TASK (result), error);
909 }
910
911 /**
912 * g_socket_listener_accept_async:
913 * @listener: a #GSocketListener
914 * @cancellable: (nullable): a #GCancellable, or %NULL
915 * @callback: (scope async): a #GAsyncReadyCallback
916 * @user_data: user data for the callback
917 *
918 * This is the asynchronous version of g_socket_listener_accept().
919 *
920 * When the operation is finished @callback will be
921 * called. You can then call g_socket_listener_accept_finish()
922 * to get the result of the operation.
923 *
924 * Since: 2.22
925 */
926 void
927 g_socket_listener_accept_async (GSocketListener *listener,
928 GCancellable *cancellable,
929 GAsyncReadyCallback callback,
930 gpointer user_data)
931 {
932 g_socket_listener_accept_socket_async (listener,
933 cancellable,
934 callback,
935 user_data);
936 }
937
938 /**
939 * g_socket_listener_accept_finish:
940 * @listener: a #GSocketListener
941 * @result: a #GAsyncResult.
942 * @source_object: (out) (transfer none) (optional) (nullable): Optional #GObject identifying this source
943 * @error: a #GError location to store the error occurring, or %NULL to
944 * ignore.
945 *
946 * Finishes an async accept operation. See g_socket_listener_accept_async()
947 *
948 * Returns: (transfer full): a #GSocketConnection on success, %NULL on error.
949 *
950 * Since: 2.22
951 */
952 GSocketConnection *
953 g_socket_listener_accept_finish (GSocketListener *listener,
954 GAsyncResult *result,
955 GObject **source_object,
956 GError **error)
957 {
958 GSocket *socket;
959 GSocketConnection *connection;
960
961 socket = g_socket_listener_accept_socket_finish (listener,
962 result,
963 source_object,
964 error);
965 if (socket == NULL)
966 return NULL;
967
968 connection = g_socket_connection_factory_create_connection (socket);
969 g_object_unref (socket);
970 return connection;
971 }
972
973 /**
974 * g_socket_listener_set_backlog:
975 * @listener: a #GSocketListener
976 * @listen_backlog: an integer
977 *
978 * Sets the listen backlog on the sockets in the listener. This must be called
979 * before adding any sockets, addresses or ports to the #GSocketListener (for
980 * example, by calling g_socket_listener_add_inet_port()) to be effective.
981 *
982 * See g_socket_set_listen_backlog() for details
983 *
984 * Since: 2.22
985 */
986 void
987 g_socket_listener_set_backlog (GSocketListener *listener,
988 int listen_backlog)
989 {
990 GSocket *socket;
991 guint i;
992
993 if (listener->priv->closed)
994 return;
995
996 listener->priv->listen_backlog = listen_backlog;
997
998 for (i = 0; i < listener->priv->sockets->len; i++)
999 {
1000 socket = listener->priv->sockets->pdata[i];
1001 g_socket_set_listen_backlog (socket, listen_backlog);
1002 }
1003 }
1004
1005 /**
1006 * g_socket_listener_close:
1007 * @listener: a #GSocketListener
1008 *
1009 * Closes all the sockets in the listener.
1010 *
1011 * Since: 2.22
1012 */
1013 void
1014 g_socket_listener_close (GSocketListener *listener)
1015 {
1016 GSocket *socket;
1017 guint i;
1018
1019 g_return_if_fail (G_IS_SOCKET_LISTENER (listener));
1020
1021 if (listener->priv->closed)
1022 return;
1023
1024 for (i = 0; i < listener->priv->sockets->len; i++)
1025 {
1026 socket = listener->priv->sockets->pdata[i];
1027 g_socket_close (socket, NULL);
1028 }
1029 listener->priv->closed = TRUE;
1030 }
1031
1032 /**
1033 * g_socket_listener_add_any_inet_port:
1034 * @listener: a #GSocketListener
1035 * @source_object: (nullable): Optional #GObject identifying this source
1036 * @error: a #GError location to store the error occurring, or %NULL to
1037 * ignore.
1038 *
1039 * Listens for TCP connections on any available port number for both
1040 * IPv6 and IPv4 (if each is available).
1041 *
1042 * This is useful if you need to have a socket for incoming connections
1043 * but don't care about the specific port number.
1044 *
1045 * @source_object will be passed out in the various calls
1046 * to accept to identify this particular source, which is
1047 * useful if you're listening on multiple addresses and do
1048 * different things depending on what address is connected to.
1049 *
1050 * Returns: the port number, or 0 in case of failure.
1051 *
1052 * Since: 2.24
1053 **/
1054 guint16
1055 g_socket_listener_add_any_inet_port (GSocketListener *listener,
1056 GObject *source_object,
1057 GError **error)
1058 {
1059 GSList *sockets_to_close = NULL;
1060 guint16 candidate_port = 0;
1061 GSocket *socket6 = NULL;
1062 GSocket *socket4 = NULL;
1063 gint attempts = 37;
1064
1065 /*
1066 * multi-step process:
1067 * - first, create an IPv6 socket.
1068 * - if that fails, create an IPv4 socket and bind it to port 0 and
1069 * that's it. no retries if that fails (why would it?).
1070 * - if our IPv6 socket also speaks IPv4 then we are done.
1071 * - if not, then we need to create a IPv4 socket with the same port
1072 * number. this might fail, of course. so we try this a bunch of
1073 * times -- leaving the old IPv6 sockets open so that we get a
1074 * different port number to try each time.
1075 * - if all that fails then just give up.
1076 */
1077
1078 while (attempts--)
1079 {
1080 GInetAddress *inet_address;
1081 GSocketAddress *address;
1082 gboolean result;
1083
1084 g_assert (socket6 == NULL);
1085 socket6 = g_socket_new (G_SOCKET_FAMILY_IPV6,
1086 G_SOCKET_TYPE_STREAM,
1087 G_SOCKET_PROTOCOL_DEFAULT,
1088 NULL);
1089
1090 if (socket6 != NULL)
1091 {
1092 inet_address = g_inet_address_new_any (G_SOCKET_FAMILY_IPV6);
1093 address = g_inet_socket_address_new (inet_address, 0);
1094 g_object_unref (inet_address);
1095
1096 g_signal_emit (listener, signals[EVENT], 0,
1097 G_SOCKET_LISTENER_BINDING, socket6);
1098
1099 result = g_socket_bind (socket6, address, TRUE, error);
1100 g_object_unref (address);
1101
1102 if (!result ||
1103 !(address = g_socket_get_local_address (socket6, error)))
1104 {
1105 g_object_unref (socket6);
1106 socket6 = NULL;
1107 break;
1108 }
1109
1110 g_signal_emit (listener, signals[EVENT], 0,
1111 G_SOCKET_LISTENER_BOUND, socket6);
1112
1113 g_assert (G_IS_INET_SOCKET_ADDRESS (address));
1114 candidate_port =
1115 g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (address));
1116 g_assert (candidate_port != 0);
1117 g_object_unref (address);
1118
1119 if (g_socket_speaks_ipv4 (socket6))
1120 break;
1121 }
1122
1123 g_assert (socket4 == NULL);
1124 socket4 = g_socket_new (G_SOCKET_FAMILY_IPV4,
1125 G_SOCKET_TYPE_STREAM,
1126 G_SOCKET_PROTOCOL_DEFAULT,
1127 socket6 ? NULL : error);
1128
1129 if (socket4 == NULL)
1130 /* IPv4 not supported.
1131 * if IPv6 is supported then candidate_port will be non-zero
1132 * (and the error parameter above will have been NULL)
1133 * if IPv6 is unsupported then candidate_port will be zero
1134 * (and error will have been set by the above call)
1135 */
1136 break;
1137
1138 inet_address = g_inet_address_new_any (G_SOCKET_FAMILY_IPV4);
1139 address = g_inet_socket_address_new (inet_address, candidate_port);
1140 g_object_unref (inet_address);
1141
1142 g_signal_emit (listener, signals[EVENT], 0,
1143 G_SOCKET_LISTENER_BINDING, socket4);
1144
1145 /* a note on the 'error' clause below:
1146 *
1147 * if candidate_port is 0 then we report the error right away
1148 * since it is strange that this binding would fail at all.
1149 * otherwise, we ignore the error message (ie: NULL).
1150 *
1151 * the exception to this rule is the last time through the loop
1152 * (ie: attempts == 0) in which case we want to set the error
1153 * because failure here means that the entire call will fail and
1154 * we need something to show to the user.
1155 *
1156 * an english summary of the situation: "if we gave a candidate
1157 * port number AND we have more attempts to try, then ignore the
1158 * error for now".
1159 */
1160 result = g_socket_bind (socket4, address, TRUE,
1161 (candidate_port && attempts) ? NULL : error);
1162 g_object_unref (address);
1163
1164 if (candidate_port)
1165 {
1166 g_assert (socket6 != NULL);
1167
1168 if (result)
1169 /* got our candidate port successfully */
1170 {
1171 g_signal_emit (listener, signals[EVENT], 0,
1172 G_SOCKET_LISTENER_BOUND, socket4);
1173 break;
1174 }
1175 else
1176 /* we failed to bind to the specified port. try again. */
1177 {
1178 g_object_unref (socket4);
1179 socket4 = NULL;
1180
1181 /* keep this open so we get a different port number */
1182 sockets_to_close = g_slist_prepend (sockets_to_close,
1183 socket6);
1184 candidate_port = 0;
1185 socket6 = NULL;
1186 }
1187 }
1188 else
1189 /* we didn't tell it a port. this means two things.
1190 * - if we failed, then something really bad happened.
1191 * - if we succeeded, then we need to find out the port number.
1192 */
1193 {
1194 g_assert (socket6 == NULL);
1195
1196 if (!result ||
1197 !(address = g_socket_get_local_address (socket4, error)))
1198 {
1199 g_object_unref (socket4);
1200 socket4 = NULL;
1201 break;
1202 }
1203
1204 g_signal_emit (listener, signals[EVENT], 0,
1205 G_SOCKET_LISTENER_BOUND, socket4);
1206
1207 g_assert (G_IS_INET_SOCKET_ADDRESS (address));
1208 candidate_port =
1209 g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (address));
1210 g_assert (candidate_port != 0);
1211 g_object_unref (address);
1212 break;
1213 }
1214 }
1215
1216 /* should only be non-zero if we have a socket */
1217 g_assert ((candidate_port != 0) == (socket4 || socket6));
1218
1219 while (sockets_to_close)
1220 {
1221 g_object_unref (sockets_to_close->data);
1222 sockets_to_close = g_slist_delete_link (sockets_to_close,
1223 sockets_to_close);
1224 }
1225
1226 /* now we actually listen() the sockets and add them to the listener */
1227 if (socket6 != NULL)
1228 {
1229 g_socket_set_listen_backlog (socket6, listener->priv->listen_backlog);
1230
1231 g_signal_emit (listener, signals[EVENT], 0,
1232 G_SOCKET_LISTENER_LISTENING, socket6);
1233
1234 if (!g_socket_listen (socket6, error))
1235 {
1236 g_object_unref (socket6);
1237 if (socket4)
1238 g_object_unref (socket4);
1239
1240 return 0;
1241 }
1242
1243 g_signal_emit (listener, signals[EVENT], 0,
1244 G_SOCKET_LISTENER_LISTENED, socket6);
1245
1246 if (source_object)
1247 g_object_set_qdata_full (G_OBJECT (socket6), source_quark,
1248 g_object_ref (source_object),
1249 g_object_unref);
1250
1251 g_ptr_array_add (listener->priv->sockets, socket6);
1252 }
1253
1254 if (socket4 != NULL)
1255 {
1256 g_socket_set_listen_backlog (socket4, listener->priv->listen_backlog);
1257
1258 g_signal_emit (listener, signals[EVENT], 0,
1259 G_SOCKET_LISTENER_LISTENING, socket4);
1260
1261 if (!g_socket_listen (socket4, error))
1262 {
1263 g_object_unref (socket4);
1264 if (socket6)
1265 g_object_unref (socket6);
1266
1267 return 0;
1268 }
1269
1270 g_signal_emit (listener, signals[EVENT], 0,
1271 G_SOCKET_LISTENER_LISTENED, socket4);
1272
1273 if (source_object)
1274 g_object_set_qdata_full (G_OBJECT (socket4), source_quark,
1275 g_object_ref (source_object),
1276 g_object_unref);
1277
1278 g_ptr_array_add (listener->priv->sockets, socket4);
1279 }
1280
1281 if ((socket4 != NULL || socket6 != NULL) &&
1282 G_SOCKET_LISTENER_GET_CLASS (listener)->changed)
1283 G_SOCKET_LISTENER_GET_CLASS (listener)->changed (listener);
1284
1285 return candidate_port;
1286 }