1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright © 2008 Christian Kellner, Samuel Cormier-Iijima
4 * Copyright © 2009 codethink
5 * Copyright © 2009 Red Hat, Inc
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.
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.
17 * You should have received a copy of the GNU Lesser General
18 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
20 * Authors: Christian Kellner <gicmo@gnome.org>
21 * Samuel Cormier-Iijima <sciyoshi@gmail.com>
22 * Ryan Lortie <desrt@desrt.ca>
23 * Alexander Larsson <alexl@redhat.com>
27 #include "gsocketlistener.h"
29 #include <gio/gtask.h>
30 #include <gio/gcancellable.h>
31 #include <gio/gsocketaddress.h>
32 #include <gio/ginetaddress.h>
33 #include <gio/gioerror.h>
34 #include <gio/gsocket.h>
35 #include <gio/gsocketconnection.h>
36 #include <gio/ginetsocketaddress.h>
41 * SECTION:gsocketlistener
42 * @title: GSocketListener
43 * @short_description: Helper for accepting network client connections
45 * @see_also: #GThreadedSocketService, #GSocketService.
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.
51 * If you want to implement a network server, also look at #GSocketService
52 * and #GThreadedSocketService which are subclass of #GSocketListener
53 * that makes this even easier.
65 static GQuark source_quark = 0;
67 struct _GSocketListenerPrivate
70 GMainContext *main_context;
75 G_DEFINE_TYPE_WITH_PRIVATE (GSocketListener, g_socket_listener, G_TYPE_OBJECT)
78 g_socket_listener_finalize (GObject *object)
80 GSocketListener *listener = G_SOCKET_LISTENER (object);
82 if (listener->priv->main_context)
83 g_main_context_unref (listener->priv->main_context);
85 /* Do not explicitly close the sockets. Instead, let them close themselves if
86 * their final reference is dropped, but keep them open if a reference is
87 * held externally to the GSocketListener (which is possible if
88 * g_socket_listener_add_socket() was used).
90 g_ptr_array_free (listener->priv->sockets, TRUE);
92 G_OBJECT_CLASS (g_socket_listener_parent_class)
97 g_socket_listener_get_property (GObject *object,
102 GSocketListener *listener = G_SOCKET_LISTENER (object);
106 case PROP_LISTEN_BACKLOG:
107 g_value_set_int (value, listener->priv->listen_backlog);
111 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
116 g_socket_listener_set_property (GObject *object,
121 GSocketListener *listener = G_SOCKET_LISTENER (object);
125 case PROP_LISTEN_BACKLOG:
126 g_socket_listener_set_backlog (listener, g_value_get_int (value));
130 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
136 g_socket_listener_class_init (GSocketListenerClass *klass)
138 GObjectClass *gobject_class G_GNUC_UNUSED = G_OBJECT_CLASS (klass);
140 gobject_class->finalize = g_socket_listener_finalize;
141 gobject_class->set_property = g_socket_listener_set_property;
142 gobject_class->get_property = g_socket_listener_get_property;
143 g_object_class_install_property (gobject_class, PROP_LISTEN_BACKLOG,
144 g_param_spec_int ("listen-backlog",
145 P_("Listen backlog"),
146 P_("outstanding connections in the listen queue"),
150 G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
152 source_quark = g_quark_from_static_string ("g-socket-listener-source");
156 g_socket_listener_init (GSocketListener *listener)
158 listener->priv = g_socket_listener_get_instance_private (listener);
159 listener->priv->sockets =
160 g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref);
161 listener->priv->listen_backlog = 10;
165 * g_socket_listener_new:
167 * Creates a new #GSocketListener with no sockets to listen for.
168 * New listeners can be added with e.g. g_socket_listener_add_address()
169 * or g_socket_listener_add_inet_port().
171 * Returns: a new #GSocketListener.
176 g_socket_listener_new (void)
178 return g_object_new (G_TYPE_SOCKET_LISTENER, NULL);
182 check_listener (GSocketListener *listener,
185 if (listener->priv->closed)
187 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
188 _("Listener is already closed"));
196 * g_socket_listener_add_socket:
197 * @listener: a #GSocketListener
198 * @socket: a listening #GSocket
199 * @source_object: (allow-none): Optional #GObject identifying this source
200 * @error: #GError for error reporting, or %NULL to ignore.
202 * Adds @socket to the set of sockets that we try to accept
203 * new clients from. The socket must be bound to a local
204 * address and listened to.
206 * @source_object will be passed out in the various calls
207 * to accept to identify this particular source, which is
208 * useful if you're listening on multiple addresses and do
209 * different things depending on what address is connected to.
211 * The @socket will not be automatically closed when the @listener is finalized
212 * unless the listener held the final reference to the socket. Before GLib 2.42,
213 * the @socket was automatically closed on finalization of the @listener, even
214 * if references to it were held elsewhere.
216 * Returns: %TRUE on success, %FALSE on error.
221 g_socket_listener_add_socket (GSocketListener *listener,
223 GObject *source_object,
226 if (!check_listener (listener, error))
229 /* TODO: Check that socket it is bound & not closed? */
231 if (g_socket_is_closed (socket))
233 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
234 _("Added socket is closed"));
238 g_object_ref (socket);
239 g_ptr_array_add (listener->priv->sockets, socket);
242 g_object_set_qdata_full (G_OBJECT (socket), source_quark,
243 g_object_ref (source_object), g_object_unref);
246 if (G_SOCKET_LISTENER_GET_CLASS (listener)->changed)
247 G_SOCKET_LISTENER_GET_CLASS (listener)->changed (listener);
253 * g_socket_listener_add_address:
254 * @listener: a #GSocketListener
255 * @address: a #GSocketAddress
256 * @type: a #GSocketType
257 * @protocol: a #GSocketProtocol
258 * @source_object: (allow-none): Optional #GObject identifying this source
259 * @effective_address: (out) (allow-none): location to store the address that was bound to, or %NULL.
260 * @error: #GError for error reporting, or %NULL to ignore.
262 * Creates a socket of type @type and protocol @protocol, binds
263 * it to @address and adds it to the set of sockets we're accepting
266 * Note that adding an IPv6 address, depending on the platform,
267 * may or may not result in a listener that also accepts IPv4
268 * connections. For more deterministic behavior, see
269 * g_socket_listener_add_inet_port().
271 * @source_object will be passed out in the various calls
272 * to accept to identify this particular source, which is
273 * useful if you're listening on multiple addresses and do
274 * different things depending on what address is connected to.
276 * If successful and @effective_address is non-%NULL then it will
277 * be set to the address that the binding actually occurred at. This
278 * is helpful for determining the port number that was used for when
279 * requesting a binding to port 0 (ie: "any port"). This address, if
280 * requested, belongs to the caller and must be freed.
282 * Returns: %TRUE on success, %FALSE on error.
287 g_socket_listener_add_address (GSocketListener *listener,
288 GSocketAddress *address,
290 GSocketProtocol protocol,
291 GObject *source_object,
292 GSocketAddress **effective_address,
295 GSocketAddress *local_address;
296 GSocketFamily family;
299 if (!check_listener (listener, error))
302 family = g_socket_address_get_family (address);
303 socket = g_socket_new (family, type, protocol, error);
307 g_socket_set_listen_backlog (socket, listener->priv->listen_backlog);
309 if (!g_socket_bind (socket, address, TRUE, error) ||
310 !g_socket_listen (socket, error))
312 g_object_unref (socket);
316 local_address = NULL;
317 if (effective_address)
319 local_address = g_socket_get_local_address (socket, error);
320 if (local_address == NULL)
322 g_object_unref (socket);
327 if (!g_socket_listener_add_socket (listener, socket,
332 g_object_unref (local_address);
333 g_object_unref (socket);
337 if (effective_address)
338 *effective_address = local_address;
340 g_object_unref (socket); /* add_socket refs this */
346 * g_socket_listener_add_inet_port:
347 * @listener: a #GSocketListener
348 * @port: an IP port number (non-zero)
349 * @source_object: (allow-none): Optional #GObject identifying this source
350 * @error: #GError for error reporting, or %NULL to ignore.
352 * Helper function for g_socket_listener_add_address() that
353 * creates a TCP/IP socket listening on IPv4 and IPv6 (if
354 * supported) on the specified port on all interfaces.
356 * @source_object will be passed out in the various calls
357 * to accept to identify this particular source, which is
358 * useful if you're listening on multiple addresses and do
359 * different things depending on what address is connected to.
361 * Returns: %TRUE on success, %FALSE on error.
366 g_socket_listener_add_inet_port (GSocketListener *listener,
368 GObject *source_object,
371 gboolean need_ipv4_socket = TRUE;
372 GSocket *socket4 = NULL;
375 g_return_val_if_fail (listener != NULL, FALSE);
376 g_return_val_if_fail (port != 0, FALSE);
378 if (!check_listener (listener, error))
381 /* first try to create an IPv6 socket */
382 socket6 = g_socket_new (G_SOCKET_FAMILY_IPV6,
383 G_SOCKET_TYPE_STREAM,
384 G_SOCKET_PROTOCOL_DEFAULT,
388 /* IPv6 is supported on this platform, so if we fail now it is
389 * a result of being unable to bind to our port. Don't fail
390 * silently as a result of this!
393 GInetAddress *inet_address;
394 GSocketAddress *address;
397 inet_address = g_inet_address_new_any (G_SOCKET_FAMILY_IPV6);
398 address = g_inet_socket_address_new (inet_address, port);
399 g_object_unref (inet_address);
401 g_socket_set_listen_backlog (socket6, listener->priv->listen_backlog);
403 result = g_socket_bind (socket6, address, TRUE, error) &&
404 g_socket_listen (socket6, error);
406 g_object_unref (address);
410 g_object_unref (socket6);
416 g_object_set_qdata_full (G_OBJECT (socket6), source_quark,
417 g_object_ref (source_object),
420 /* If this socket already speaks IPv4 then we are done. */
421 if (g_socket_speaks_ipv4 (socket6))
422 need_ipv4_socket = FALSE;
425 if (need_ipv4_socket)
426 /* We are here for exactly one of the following reasons:
428 * - our platform doesn't support IPv6
429 * - we successfully created an IPv6 socket but it's V6ONLY
431 * In either case, we need to go ahead and create an IPv4 socket
432 * and fail the call if we can't bind to it.
435 socket4 = g_socket_new (G_SOCKET_FAMILY_IPV4,
436 G_SOCKET_TYPE_STREAM,
437 G_SOCKET_PROTOCOL_DEFAULT,
441 /* IPv4 is supported on this platform, so if we fail now it is
442 * a result of being unable to bind to our port. Don't fail
443 * silently as a result of this!
446 GInetAddress *inet_address;
447 GSocketAddress *address;
450 inet_address = g_inet_address_new_any (G_SOCKET_FAMILY_IPV4);
451 address = g_inet_socket_address_new (inet_address, port);
452 g_object_unref (inet_address);
454 g_socket_set_listen_backlog (socket4,
455 listener->priv->listen_backlog);
457 result = g_socket_bind (socket4, address, TRUE, error) &&
458 g_socket_listen (socket4, error);
460 g_object_unref (address);
464 g_object_unref (socket4);
467 g_object_unref (socket6);
473 g_object_set_qdata_full (G_OBJECT (socket4), source_quark,
474 g_object_ref (source_object),
478 /* Ok. So IPv4 is not supported on this platform. If we
479 * succeeded at creating an IPv6 socket then that's OK, but
480 * otherwise we need to tell the user we failed.
484 g_clear_error (error);
490 g_assert (socket6 != NULL || socket4 != NULL);
493 g_ptr_array_add (listener->priv->sockets, socket6);
496 g_ptr_array_add (listener->priv->sockets, socket4);
498 if (G_SOCKET_LISTENER_GET_CLASS (listener)->changed)
499 G_SOCKET_LISTENER_GET_CLASS (listener)->changed (listener);
505 add_sources (GSocketListener *listener,
506 GSocketSourceFunc callback,
507 gpointer callback_data,
508 GCancellable *cancellable,
509 GMainContext *context)
517 for (i = 0; i < listener->priv->sockets->len; i++)
519 socket = listener->priv->sockets->pdata[i];
521 source = g_socket_create_source (socket, G_IO_IN, cancellable);
522 g_source_set_callback (source,
523 (GSourceFunc) callback,
524 callback_data, NULL);
525 g_source_attach (source, context);
527 sources = g_list_prepend (sources, source);
534 free_sources (GList *sources)
537 while (sources != NULL)
539 source = sources->data;
540 sources = g_list_delete_link (sources, sources);
541 g_source_destroy (source);
542 g_source_unref (source);
552 accept_callback (GSocket *socket,
553 GIOCondition condition,
556 struct AcceptData *data = user_data;
558 data->socket = socket;
559 g_main_loop_quit (data->loop);
565 * g_socket_listener_accept_socket:
566 * @listener: a #GSocketListener
567 * @source_object: (out) (transfer none) (allow-none): location where #GObject pointer will be stored, or %NULL.
568 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
569 * @error: #GError for error reporting, or %NULL to ignore.
571 * Blocks waiting for a client to connect to any of the sockets added
572 * to the listener. Returns the #GSocket that was accepted.
574 * If you want to accept the high-level #GSocketConnection, not a #GSocket,
575 * which is often the case, then you should use g_socket_listener_accept()
578 * If @source_object is not %NULL it will be filled out with the source
579 * object specified when the corresponding socket or address was added
582 * If @cancellable is not %NULL, then the operation can be cancelled by
583 * triggering the cancellable object from another thread. If the operation
584 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
586 * Returns: (transfer full): a #GSocket on success, %NULL on error.
591 g_socket_listener_accept_socket (GSocketListener *listener,
592 GObject **source_object,
593 GCancellable *cancellable,
596 GSocket *accept_socket, *socket;
598 g_return_val_if_fail (G_IS_SOCKET_LISTENER (listener), NULL);
600 if (!check_listener (listener, error))
603 if (listener->priv->sockets->len == 1)
605 accept_socket = listener->priv->sockets->pdata[0];
606 if (!g_socket_condition_wait (accept_socket, G_IO_IN,
613 struct AcceptData data;
616 if (listener->priv->main_context == NULL)
617 listener->priv->main_context = g_main_context_new ();
619 loop = g_main_loop_new (listener->priv->main_context, FALSE);
621 sources = add_sources (listener,
625 listener->priv->main_context);
626 g_main_loop_run (loop);
627 accept_socket = data.socket;
628 free_sources (sources);
629 g_main_loop_unref (loop);
632 if (!(socket = g_socket_accept (accept_socket, cancellable, error)))
636 *source_object = g_object_get_qdata (G_OBJECT (accept_socket), source_quark);
642 * g_socket_listener_accept:
643 * @listener: a #GSocketListener
644 * @source_object: (out) (transfer none) (allow-none): location where #GObject pointer will be stored, or %NULL
645 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
646 * @error: #GError for error reporting, or %NULL to ignore.
648 * Blocks waiting for a client to connect to any of the sockets added
649 * to the listener. Returns a #GSocketConnection for the socket that was
652 * If @source_object is not %NULL it will be filled out with the source
653 * object specified when the corresponding socket or address was added
656 * If @cancellable is not %NULL, then the operation can be cancelled by
657 * triggering the cancellable object from another thread. If the operation
658 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
660 * Returns: (transfer full): a #GSocketConnection on success, %NULL on error.
665 g_socket_listener_accept (GSocketListener *listener,
666 GObject **source_object,
667 GCancellable *cancellable,
670 GSocketConnection *connection;
673 socket = g_socket_listener_accept_socket (listener,
680 connection = g_socket_connection_factory_create_connection (socket);
681 g_object_unref (socket);
687 accept_ready (GSocket *accept_socket,
688 GIOCondition condition,
691 GTask *task = user_data;
692 GError *error = NULL;
694 GObject *source_object;
696 socket = g_socket_accept (accept_socket, g_task_get_cancellable (task), &error);
699 source_object = g_object_get_qdata (G_OBJECT (accept_socket), source_quark);
701 g_object_set_qdata_full (G_OBJECT (task),
703 g_object_ref (source_object), g_object_unref);
704 g_task_return_pointer (task, socket, g_object_unref);
708 g_task_return_error (task, error);
711 g_object_unref (task);
716 * g_socket_listener_accept_socket_async:
717 * @listener: a #GSocketListener
718 * @cancellable: (allow-none): a #GCancellable, or %NULL
719 * @callback: (scope async): a #GAsyncReadyCallback
720 * @user_data: (closure): user data for the callback
722 * This is the asynchronous version of g_socket_listener_accept_socket().
724 * When the operation is finished @callback will be
725 * called. You can then call g_socket_listener_accept_socket_finish()
726 * to get the result of the operation.
731 g_socket_listener_accept_socket_async (GSocketListener *listener,
732 GCancellable *cancellable,
733 GAsyncReadyCallback callback,
738 GError *error = NULL;
740 task = g_task_new (listener, cancellable, callback, user_data);
742 if (!check_listener (listener, &error))
744 g_task_return_error (task, error);
745 g_object_unref (task);
749 sources = add_sources (listener,
753 g_main_context_get_thread_default ());
754 g_task_set_task_data (task, sources, (GDestroyNotify) free_sources);
758 * g_socket_listener_accept_socket_finish:
759 * @listener: a #GSocketListener
760 * @result: a #GAsyncResult.
761 * @source_object: (out) (transfer none) (allow-none): Optional #GObject identifying this source
762 * @error: a #GError location to store the error occurring, or %NULL to
765 * Finishes an async accept operation. See g_socket_listener_accept_socket_async()
767 * Returns: (transfer full): a #GSocket on success, %NULL on error.
772 g_socket_listener_accept_socket_finish (GSocketListener *listener,
773 GAsyncResult *result,
774 GObject **source_object,
777 g_return_val_if_fail (G_IS_SOCKET_LISTENER (listener), NULL);
778 g_return_val_if_fail (g_task_is_valid (result, listener), NULL);
781 *source_object = g_object_get_qdata (G_OBJECT (result), source_quark);
783 return g_task_propagate_pointer (G_TASK (result), error);
787 * g_socket_listener_accept_async:
788 * @listener: a #GSocketListener
789 * @cancellable: (allow-none): a #GCancellable, or %NULL
790 * @callback: (scope async): a #GAsyncReadyCallback
791 * @user_data: (closure): user data for the callback
793 * This is the asynchronous version of g_socket_listener_accept().
795 * When the operation is finished @callback will be
796 * called. You can then call g_socket_listener_accept_socket()
797 * to get the result of the operation.
802 g_socket_listener_accept_async (GSocketListener *listener,
803 GCancellable *cancellable,
804 GAsyncReadyCallback callback,
807 g_socket_listener_accept_socket_async (listener,
814 * g_socket_listener_accept_finish:
815 * @listener: a #GSocketListener
816 * @result: a #GAsyncResult.
817 * @source_object: (out) (transfer none) (allow-none): Optional #GObject identifying this source
818 * @error: a #GError location to store the error occurring, or %NULL to
821 * Finishes an async accept operation. See g_socket_listener_accept_async()
823 * Returns: (transfer full): a #GSocketConnection on success, %NULL on error.
828 g_socket_listener_accept_finish (GSocketListener *listener,
829 GAsyncResult *result,
830 GObject **source_object,
834 GSocketConnection *connection;
836 socket = g_socket_listener_accept_socket_finish (listener,
843 connection = g_socket_connection_factory_create_connection (socket);
844 g_object_unref (socket);
849 * g_socket_listener_set_backlog:
850 * @listener: a #GSocketListener
851 * @listen_backlog: an integer
853 * Sets the listen backlog on the sockets in the listener.
855 * See g_socket_set_listen_backlog() for details
860 g_socket_listener_set_backlog (GSocketListener *listener,
866 if (listener->priv->closed)
869 listener->priv->listen_backlog = listen_backlog;
871 for (i = 0; i < listener->priv->sockets->len; i++)
873 socket = listener->priv->sockets->pdata[i];
874 g_socket_set_listen_backlog (socket, listen_backlog);
879 * g_socket_listener_close:
880 * @listener: a #GSocketListener
882 * Closes all the sockets in the listener.
887 g_socket_listener_close (GSocketListener *listener)
892 g_return_if_fail (G_IS_SOCKET_LISTENER (listener));
894 if (listener->priv->closed)
897 for (i = 0; i < listener->priv->sockets->len; i++)
899 socket = listener->priv->sockets->pdata[i];
900 g_socket_close (socket, NULL);
902 listener->priv->closed = TRUE;
906 * g_socket_listener_add_any_inet_port:
907 * @listener: a #GSocketListener
908 * @source_object: (allow-none): Optional #GObject identifying this source
909 * @error: a #GError location to store the error occurring, or %NULL to
912 * Listens for TCP connections on any available port number for both
913 * IPv6 and IPv4 (if each is available).
915 * This is useful if you need to have a socket for incoming connections
916 * but don't care about the specific port number.
918 * @source_object will be passed out in the various calls
919 * to accept to identify this particular source, which is
920 * useful if you're listening on multiple addresses and do
921 * different things depending on what address is connected to.
923 * Returns: the port number, or 0 in case of failure.
928 g_socket_listener_add_any_inet_port (GSocketListener *listener,
929 GObject *source_object,
932 GSList *sockets_to_close = NULL;
933 guint16 candidate_port = 0;
934 GSocket *socket6 = NULL;
935 GSocket *socket4 = NULL;
939 * multi-step process:
940 * - first, create an IPv6 socket.
941 * - if that fails, create an IPv4 socket and bind it to port 0 and
942 * that's it. no retries if that fails (why would it?).
943 * - if our IPv6 socket also speaks IPv4 then we are done.
944 * - if not, then we need to create a IPv4 socket with the same port
945 * number. this might fail, of course. so we try this a bunch of
946 * times -- leaving the old IPv6 sockets open so that we get a
947 * different port number to try each time.
948 * - if all that fails then just give up.
953 GInetAddress *inet_address;
954 GSocketAddress *address;
957 g_assert (socket6 == NULL);
958 socket6 = g_socket_new (G_SOCKET_FAMILY_IPV6,
959 G_SOCKET_TYPE_STREAM,
960 G_SOCKET_PROTOCOL_DEFAULT,
965 inet_address = g_inet_address_new_any (G_SOCKET_FAMILY_IPV6);
966 address = g_inet_socket_address_new (inet_address, 0);
967 g_object_unref (inet_address);
968 result = g_socket_bind (socket6, address, TRUE, error);
969 g_object_unref (address);
972 !(address = g_socket_get_local_address (socket6, error)))
974 g_object_unref (socket6);
979 g_assert (G_IS_INET_SOCKET_ADDRESS (address));
981 g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (address));
982 g_assert (candidate_port != 0);
983 g_object_unref (address);
985 if (g_socket_speaks_ipv4 (socket6))
989 g_assert (socket4 == NULL);
990 socket4 = g_socket_new (G_SOCKET_FAMILY_IPV4,
991 G_SOCKET_TYPE_STREAM,
992 G_SOCKET_PROTOCOL_DEFAULT,
993 socket6 ? NULL : error);
996 /* IPv4 not supported.
997 * if IPv6 is supported then candidate_port will be non-zero
998 * (and the error parameter above will have been NULL)
999 * if IPv6 is unsupported then candidate_port will be zero
1000 * (and error will have been set by the above call)
1004 inet_address = g_inet_address_new_any (G_SOCKET_FAMILY_IPV4);
1005 address = g_inet_socket_address_new (inet_address, candidate_port);
1006 g_object_unref (inet_address);
1007 /* a note on the 'error' clause below:
1009 * if candidate_port is 0 then we report the error right away
1010 * since it is strange that this binding would fail at all.
1011 * otherwise, we ignore the error message (ie: NULL).
1013 * the exception to this rule is the last time through the loop
1014 * (ie: attempts == 0) in which case we want to set the error
1015 * because failure here means that the entire call will fail and
1016 * we need something to show to the user.
1018 * an english summary of the situation: "if we gave a candidate
1019 * port number AND we have more attempts to try, then ignore the
1022 result = g_socket_bind (socket4, address, TRUE,
1023 (candidate_port && attempts) ? NULL : error);
1024 g_object_unref (address);
1028 g_assert (socket6 != NULL);
1031 /* got our candidate port successfully */
1035 /* we failed to bind to the specified port. try again. */
1037 g_object_unref (socket4);
1040 /* keep this open so we get a different port number */
1041 sockets_to_close = g_slist_prepend (sockets_to_close,
1048 /* we didn't tell it a port. this means two things.
1049 * - if we failed, then something really bad happened.
1050 * - if we succeeded, then we need to find out the port number.
1053 g_assert (socket6 == NULL);
1056 !(address = g_socket_get_local_address (socket4, error)))
1058 g_object_unref (socket4);
1063 g_assert (G_IS_INET_SOCKET_ADDRESS (address));
1065 g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (address));
1066 g_assert (candidate_port != 0);
1067 g_object_unref (address);
1072 /* should only be non-zero if we have a socket */
1073 g_assert ((candidate_port != 0) == (socket4 || socket6));
1075 while (sockets_to_close)
1077 g_object_unref (sockets_to_close->data);
1078 sockets_to_close = g_slist_delete_link (sockets_to_close,
1082 /* now we actually listen() the sockets and add them to the listener */
1083 if (socket6 != NULL)
1085 g_socket_set_listen_backlog (socket6, listener->priv->listen_backlog);
1086 if (!g_socket_listen (socket6, error))
1088 g_object_unref (socket6);
1090 g_object_unref (socket4);
1096 g_object_set_qdata_full (G_OBJECT (socket6), source_quark,
1097 g_object_ref (source_object),
1100 g_ptr_array_add (listener->priv->sockets, socket6);
1103 if (socket4 != NULL)
1105 g_socket_set_listen_backlog (socket4, listener->priv->listen_backlog);
1106 if (!g_socket_listen (socket4, error))
1108 g_object_unref (socket4);
1110 g_object_unref (socket6);
1116 g_object_set_qdata_full (G_OBJECT (socket4), source_quark,
1117 g_object_ref (source_object),
1120 g_ptr_array_add (listener->priv->sockets, socket4);
1123 if ((socket4 != NULL || socket6 != NULL) &&
1124 G_SOCKET_LISTENER_GET_CLASS (listener)->changed)
1125 G_SOCKET_LISTENER_GET_CLASS (listener)->changed (listener);
1127 return candidate_port;