| 
									
										
										
										
											2008-12-29 13:38:28 -05:00
										 |  |  | /* GIO - GLib Input, Output and Streaming Library
 | 
					
						
							| 
									
										
										
										
											2018-10-22 13:36:27 -04:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2008-12-29 13:38:28 -05:00
										 |  |  |  * Copyright (C) 2008 Red Hat, Inc. | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2022-05-18 09:12:45 +01:00
										 |  |  |  * SPDX-License-Identifier: LGPL-2.1-or-later | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2008-12-29 13:38:28 -05:00
										 |  |  |  * This library is free software; you can redistribute it and/or | 
					
						
							|  |  |  |  * modify it under the terms of the GNU Lesser General Public | 
					
						
							|  |  |  |  * License as published by the Free Software Foundation; either | 
					
						
							| 
									
										
										
										
											2017-05-27 18:21:30 +02:00
										 |  |  |  * version 2.1 of the License, or (at your option) any later version. | 
					
						
							| 
									
										
										
										
											2008-12-29 13:38:28 -05:00
										 |  |  |  * | 
					
						
							|  |  |  |  * This library is distributed in the hope that it will be useful, | 
					
						
							|  |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU | 
					
						
							|  |  |  |  * Lesser General Public License for more details. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * You should have received a copy of the GNU Lesser General | 
					
						
							| 
									
										
										
										
											2014-01-23 12:58:29 +01:00
										 |  |  |  * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
 | 
					
						
							| 
									
										
										
										
											2008-12-29 13:38:28 -05:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "config.h"
 | 
					
						
							|  |  |  | #include "gsocketaddressenumerator.h"
 | 
					
						
							|  |  |  | #include "glibintl.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-08-02 15:48:22 -04:00
										 |  |  | #include "gtask.h"
 | 
					
						
							| 
									
										
										
										
											2008-12-29 13:38:28 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-28 13:10:55 +00:00
										 |  |  | /**
 | 
					
						
							| 
									
										
										
										
											2023-11-02 16:23:37 +00:00
										 |  |  |  * GSocketAddressEnumerator: | 
					
						
							| 
									
										
										
										
											2018-11-28 13:10:55 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2023-11-02 16:23:37 +00:00
										 |  |  |  * `GSocketAddressEnumerator` is an enumerator type for | 
					
						
							|  |  |  |  * [class@Gio.SocketAddress] instances. It is returned by enumeration functions | 
					
						
							|  |  |  |  * such as [method@Gio.SocketConnectable.enumerate], which returns a | 
					
						
							|  |  |  |  * `GSocketAddressEnumerator` to list each [class@Gio.SocketAddress] which could | 
					
						
							|  |  |  |  * be used to connect to that [iface@Gio.SocketConnectable]. | 
					
						
							| 
									
										
										
										
											2018-11-28 13:10:55 +00:00
										 |  |  |  * | 
					
						
							|  |  |  |  * Enumeration is typically a blocking operation, so the asynchronous methods | 
					
						
							| 
									
										
										
										
											2023-11-02 16:23:37 +00:00
										 |  |  |  * [method@Gio.SocketAddressEnumerator.next_async] and | 
					
						
							|  |  |  |  * [method@Gio.SocketAddressEnumerator.next_finish] should be used where | 
					
						
							|  |  |  |  * possible. | 
					
						
							| 
									
										
										
										
											2018-11-28 13:10:55 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2023-11-02 16:23:37 +00:00
										 |  |  |  * Each `GSocketAddressEnumerator` can only be enumerated once. Once | 
					
						
							|  |  |  |  * [method@Gio.SocketAddressEnumerator.next] has returned `NULL`, further | 
					
						
							|  |  |  |  * enumeration with that `GSocketAddressEnumerator` is not possible, and it can | 
					
						
							| 
									
										
										
										
											2018-11-28 13:10:55 +00:00
										 |  |  |  * be unreffed. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2008-12-29 13:38:28 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-17 11:54:02 +01:00
										 |  |  | G_DEFINE_ABSTRACT_TYPE (GSocketAddressEnumerator, g_socket_address_enumerator, G_TYPE_OBJECT) | 
					
						
							| 
									
										
										
										
											2008-12-29 13:38:28 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | static void            g_socket_address_enumerator_real_next_async  (GSocketAddressEnumerator  *enumerator, | 
					
						
							|  |  |  | 								     GCancellable              *cancellable, | 
					
						
							|  |  |  | 								     GAsyncReadyCallback        callback, | 
					
						
							|  |  |  | 								     gpointer                   user_data); | 
					
						
							|  |  |  | static GSocketAddress *g_socket_address_enumerator_real_next_finish (GSocketAddressEnumerator  *enumerator, | 
					
						
							|  |  |  | 								     GAsyncResult              *result, | 
					
						
							|  |  |  | 								     GError                   **error); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void | 
					
						
							|  |  |  | g_socket_address_enumerator_init (GSocketAddressEnumerator *enumerator) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void | 
					
						
							|  |  |  | g_socket_address_enumerator_class_init (GSocketAddressEnumeratorClass *enumerator_class) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   enumerator_class->next_async = g_socket_address_enumerator_real_next_async; | 
					
						
							|  |  |  |   enumerator_class->next_finish = g_socket_address_enumerator_real_next_finish; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  * g_socket_address_enumerator_next: | 
					
						
							|  |  |  |  * @enumerator: a #GSocketAddressEnumerator | 
					
						
							| 
									
										
										
										
											2016-10-28 18:29:02 -07:00
										 |  |  |  * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore. | 
					
						
							| 
									
										
										
										
											2008-12-29 13:38:28 -05:00
										 |  |  |  * @error: a #GError. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Retrieves the next #GSocketAddress from @enumerator. Note that this | 
					
						
							|  |  |  |  * may block for some amount of time. (Eg, a #GNetworkAddress may need | 
					
						
							|  |  |  |  * to do a DNS lookup before it can return an address.) Use | 
					
						
							|  |  |  |  * g_socket_address_enumerator_next_async() if you need to avoid | 
					
						
							|  |  |  |  * blocking. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * If @enumerator is expected to yield addresses, but for some reason | 
					
						
							|  |  |  |  * is unable to (eg, because of a DNS error), then the first call to | 
					
						
							|  |  |  |  * g_socket_address_enumerator_next() will return an appropriate error | 
					
						
							| 
									
										
										
										
											2024-12-15 18:51:26 -05:00
										 |  |  |  * in `*error`. However, if the first call to | 
					
						
							| 
									
										
										
										
											2008-12-29 13:38:28 -05:00
										 |  |  |  * g_socket_address_enumerator_next() succeeds, then any further | 
					
						
							|  |  |  |  * internal errors (other than @cancellable being triggered) will be | 
					
						
							|  |  |  |  * ignored. | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2022-05-31 11:19:07 +02:00
										 |  |  |  * Returns: (transfer full) (nullable): a #GSocketAddress (owned by the caller), or %NULL on | 
					
						
							| 
									
										
										
										
											2024-12-15 18:51:26 -05:00
										 |  |  |  *     error (in which case `*error` will be set) or if there are no | 
					
						
							| 
									
										
										
										
											2009-05-27 18:20:08 -04:00
										 |  |  |  *     more addresses. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2008-12-29 13:38:28 -05:00
										 |  |  | GSocketAddress * | 
					
						
							|  |  |  | g_socket_address_enumerator_next (GSocketAddressEnumerator  *enumerator, | 
					
						
							|  |  |  | 				  GCancellable              *cancellable, | 
					
						
							|  |  |  | 				  GError                   **error) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   GSocketAddressEnumeratorClass *klass; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   g_return_val_if_fail (G_IS_SOCKET_ADDRESS_ENUMERATOR (enumerator), NULL); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   klass = G_SOCKET_ADDRESS_ENUMERATOR_GET_CLASS (enumerator); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return (* klass->next) (enumerator, cancellable, error); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Default implementation just calls the synchronous method; this can
 | 
					
						
							|  |  |  |  * be used if the implementation already knows all of its addresses, | 
					
						
							|  |  |  |  * and so the synchronous method will never block. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | static void | 
					
						
							| 
									
										
										
										
											2009-05-27 18:20:08 -04:00
										 |  |  | g_socket_address_enumerator_real_next_async (GSocketAddressEnumerator *enumerator, | 
					
						
							|  |  |  | 					     GCancellable             *cancellable, | 
					
						
							|  |  |  | 					     GAsyncReadyCallback       callback, | 
					
						
							|  |  |  | 					     gpointer                  user_data) | 
					
						
							| 
									
										
										
										
											2008-12-29 13:38:28 -05:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2012-08-02 15:48:22 -04:00
										 |  |  |   GTask *task; | 
					
						
							| 
									
										
										
										
											2008-12-29 13:38:28 -05:00
										 |  |  |   GSocketAddress *address; | 
					
						
							|  |  |  |   GError *error = NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-08-02 15:48:22 -04:00
										 |  |  |   task = g_task_new (enumerator, NULL, callback, user_data); | 
					
						
							| 
									
										
										
										
											2016-06-16 19:39:38 -04:00
										 |  |  |   g_task_set_source_tag (task, g_socket_address_enumerator_real_next_async); | 
					
						
							| 
									
										
										
										
											2012-08-02 15:48:22 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-29 13:38:28 -05:00
										 |  |  |   address = g_socket_address_enumerator_next (enumerator, cancellable, &error); | 
					
						
							| 
									
										
										
										
											2012-08-02 15:48:22 -04:00
										 |  |  |   if (error) | 
					
						
							|  |  |  |     g_task_return_error (task, error); | 
					
						
							|  |  |  |   else | 
					
						
							|  |  |  |     g_task_return_pointer (task, address, g_object_unref); | 
					
						
							| 
									
										
										
										
											2010-09-23 13:02:50 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-08-02 15:48:22 -04:00
										 |  |  |   g_object_unref (task); | 
					
						
							| 
									
										
										
										
											2008-12-29 13:38:28 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  * g_socket_address_enumerator_next_async: | 
					
						
							|  |  |  |  * @enumerator: a #GSocketAddressEnumerator | 
					
						
							| 
									
										
										
										
											2016-10-28 18:29:02 -07:00
										 |  |  |  * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore. | 
					
						
							| 
									
										
										
										
											2023-02-20 14:43:02 -06:00
										 |  |  |  * @callback: (scope async) (closure user_data): a #GAsyncReadyCallback to call | 
					
						
							|  |  |  |  *   when the request is satisfied | 
					
						
							|  |  |  |  * @user_data: the data to pass to callback function | 
					
						
							| 
									
										
										
										
											2008-12-29 13:38:28 -05:00
										 |  |  |  * | 
					
						
							|  |  |  |  * Asynchronously retrieves the next #GSocketAddress from @enumerator | 
					
						
							|  |  |  |  * and then calls @callback, which must call | 
					
						
							|  |  |  |  * g_socket_address_enumerator_next_finish() to get the result. | 
					
						
							| 
									
										
										
										
											2018-12-11 10:10:10 -05:00
										 |  |  |  * | 
					
						
							|  |  |  |  * It is an error to call this multiple times before the previous callback has finished. | 
					
						
							| 
									
										
										
										
											2009-05-27 18:20:08 -04:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2008-12-29 13:38:28 -05:00
										 |  |  | void | 
					
						
							| 
									
										
										
										
											2009-05-27 18:20:08 -04:00
										 |  |  | g_socket_address_enumerator_next_async (GSocketAddressEnumerator *enumerator, | 
					
						
							|  |  |  | 					GCancellable             *cancellable, | 
					
						
							|  |  |  | 					GAsyncReadyCallback       callback, | 
					
						
							|  |  |  | 					gpointer                  user_data) | 
					
						
							| 
									
										
										
										
											2008-12-29 13:38:28 -05:00
										 |  |  | { | 
					
						
							|  |  |  |   GSocketAddressEnumeratorClass *klass; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   g_return_if_fail (G_IS_SOCKET_ADDRESS_ENUMERATOR (enumerator)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   klass = G_SOCKET_ADDRESS_ENUMERATOR_GET_CLASS (enumerator); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   (* klass->next_async) (enumerator, cancellable, callback, user_data); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static GSocketAddress * | 
					
						
							|  |  |  | g_socket_address_enumerator_real_next_finish (GSocketAddressEnumerator  *enumerator, | 
					
						
							|  |  |  | 					      GAsyncResult              *result, | 
					
						
							|  |  |  | 					      GError                   **error) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2012-08-02 15:48:22 -04:00
										 |  |  |   g_return_val_if_fail (g_task_is_valid (result, enumerator), NULL); | 
					
						
							| 
									
										
										
										
											2008-12-29 13:38:28 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-08-02 15:48:22 -04:00
										 |  |  |   return g_task_propagate_pointer (G_TASK (result), error); | 
					
						
							| 
									
										
										
										
											2008-12-29 13:38:28 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  * g_socket_address_enumerator_next_finish: | 
					
						
							|  |  |  |  * @enumerator: a #GSocketAddressEnumerator | 
					
						
							| 
									
										
										
										
											2009-05-27 18:20:08 -04:00
										 |  |  |  * @result: a #GAsyncResult | 
					
						
							|  |  |  |  * @error: a #GError | 
					
						
							| 
									
										
										
										
											2008-12-29 13:38:28 -05:00
										 |  |  |  * | 
					
						
							|  |  |  |  * Retrieves the result of a completed call to | 
					
						
							|  |  |  |  * g_socket_address_enumerator_next_async(). See | 
					
						
							|  |  |  |  * g_socket_address_enumerator_next() for more information about | 
					
						
							|  |  |  |  * error handling. | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2022-05-31 11:19:07 +02:00
										 |  |  |  * Returns: (transfer full) (nullable): a #GSocketAddress (owned by the caller), or %NULL on | 
					
						
							| 
									
										
										
										
											2024-12-15 18:51:26 -05:00
										 |  |  |  *     error (in which case `*error` will be set) or if there are no | 
					
						
							| 
									
										
										
										
											2009-05-27 18:20:08 -04:00
										 |  |  |  *     more addresses. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2008-12-29 13:38:28 -05:00
										 |  |  | GSocketAddress * | 
					
						
							|  |  |  | g_socket_address_enumerator_next_finish (GSocketAddressEnumerator  *enumerator, | 
					
						
							|  |  |  | 					 GAsyncResult              *result, | 
					
						
							|  |  |  | 					 GError                   **error) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   GSocketAddressEnumeratorClass *klass; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   g_return_val_if_fail (G_IS_SOCKET_ADDRESS_ENUMERATOR (enumerator), NULL); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   klass = G_SOCKET_ADDRESS_ENUMERATOR_GET_CLASS (enumerator); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return (* klass->next_finish) (enumerator, result, error); | 
					
						
							|  |  |  | } |