1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2010 Collabora, Ltd.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Author: Nicolas Dufresne <nicolas.dufresne@collabora.co.uk>
24 #include "gproxyaddressenumerator.h"
28 #include "gasyncresult.h"
29 #include "ginetaddress.h"
31 #include "gnetworkaddress.h"
32 #include "gnetworkingprivate.h"
34 #include "gproxyaddress.h"
35 #include "gproxyresolver.h"
36 #include "gsimpleasyncresult.h"
37 #include "gresolver.h"
38 #include "gsocketaddress.h"
39 #include "gsocketaddressenumerator.h"
40 #include "gsocketconnectable.h"
42 G_DEFINE_TYPE (GProxyAddressEnumerator, g_proxy_address_enumerator, G_TYPE_SOCKET_ADDRESS_ENUMERATOR);
44 #define GET_PRIVATE(o) (G_PROXY_ADDRESS_ENUMERATOR (o)->priv)
53 struct _GProxyAddressEnumeratorPrivate
55 /* Destination address */
56 GSocketConnectable *connectable;
62 /* Proxy enumeration */
65 GSocketAddressEnumerator *addr_enum;
66 GSocketAddress *proxy_address;
68 gchar *proxy_username;
69 gchar *proxy_password;
70 gboolean supports_hostname;
73 /* Async attributes */
74 GSimpleAsyncResult *simple;
75 GCancellable *cancellable;
79 save_userinfo (GProxyAddressEnumeratorPrivate *priv,
84 if (priv->proxy_username)
86 g_free (priv->proxy_username);
87 priv->proxy_username = NULL;
90 if (priv->proxy_password)
92 g_free (priv->proxy_password);
93 priv->proxy_password = NULL;
96 if (_g_uri_parse_authority (proxy, NULL, NULL, &userinfo))
100 gchar **split = g_strsplit (userinfo, ":", 2);
102 if (split[0] != NULL)
104 priv->proxy_username = g_uri_unescape_string (split[0], NULL);
105 if (split[1] != NULL)
106 priv->proxy_password = g_uri_unescape_string (split[1], NULL);
116 next_enumerator (GProxyAddressEnumeratorPrivate *priv)
118 if (priv->proxy_address)
121 while (priv->addr_enum == NULL && *priv->next_proxy)
123 GSocketConnectable *connectable = NULL;
124 const gchar *proxy_uri;
127 proxy_uri = *priv->next_proxy++;
128 g_free (priv->proxy_type);
129 priv->proxy_type = g_uri_parse_scheme (proxy_uri);
131 if (priv->proxy_type == NULL)
134 /* Assumes hostnames are supported for unkown protocols */
135 priv->supports_hostname = TRUE;
136 proxy = g_proxy_get_default_for_protocol (priv->proxy_type);
139 priv->supports_hostname = g_proxy_supports_hostname (proxy);
140 g_object_unref (proxy);
143 if (strcmp ("direct", priv->proxy_type) == 0)
145 if (priv->connectable)
146 connectable = g_object_ref (priv->connectable);
148 connectable = g_network_address_new (priv->dest_hostname,
153 GError *error = NULL;
155 connectable = g_network_address_parse_uri (proxy_uri, 0, &error);
159 g_warning ("Invalid proxy URI '%s': %s",
160 proxy_uri, error->message);
161 g_error_free (error);
164 save_userinfo (priv, proxy_uri);
169 priv->addr_enum = g_socket_connectable_enumerate (connectable);
170 g_object_unref (connectable);
175 static GSocketAddress *
176 g_proxy_address_enumerator_next (GSocketAddressEnumerator *enumerator,
177 GCancellable *cancellable,
180 GProxyAddressEnumeratorPrivate *priv = GET_PRIVATE (enumerator);
181 GSocketAddress *result = NULL;
182 GError *first_error = NULL;
184 if (priv->proxies == NULL)
186 GProxyResolver *resolver = g_proxy_resolver_get_default ();
187 priv->proxies = g_proxy_resolver_lookup (resolver,
191 priv->next_proxy = priv->proxies;
193 if (priv->proxies == NULL)
197 while (result == NULL && (*priv->next_proxy || priv->addr_enum))
199 gchar *dest_hostname;
200 GInetSocketAddress *inetsaddr;
201 GInetAddress *inetaddr;
204 next_enumerator (priv);
206 if (!priv->addr_enum)
209 if (priv->proxy_address == NULL)
211 priv->proxy_address = g_socket_address_enumerator_next (
214 first_error ? NULL : &first_error);
217 if (priv->proxy_address == NULL)
219 g_object_unref (priv->addr_enum);
220 priv->addr_enum = NULL;
224 g_resolver_free_addresses (priv->dest_ips);
225 priv->dest_ips = NULL;
231 if (strcmp ("direct", priv->proxy_type) == 0)
233 result = priv->proxy_address;
234 priv->proxy_address = NULL;
238 if (!priv->supports_hostname)
240 GInetAddress *dest_ip;
246 resolver = g_resolver_get_default();
247 priv->dest_ips = g_resolver_lookup_by_name (resolver,
250 first_error ? NULL : &first_error);
251 g_object_unref (resolver);
255 g_object_unref (priv->proxy_address);
256 priv->proxy_address = NULL;
261 if (!priv->next_dest_ip)
262 priv->next_dest_ip = priv->dest_ips;
264 dest_ip = G_INET_ADDRESS (priv->next_dest_ip->data);
265 dest_hostname = g_inet_address_to_string (dest_ip);
267 priv->next_dest_ip = g_list_next (priv->next_dest_ip);
271 dest_hostname = g_strdup (priv->dest_hostname);
275 g_return_val_if_fail (G_IS_INET_SOCKET_ADDRESS (priv->proxy_address),
278 inetsaddr = G_INET_SOCKET_ADDRESS (priv->proxy_address);
279 inetaddr = g_inet_socket_address_get_address (inetsaddr);
280 port = g_inet_socket_address_get_port (inetsaddr);
282 result = g_proxy_address_new (inetaddr, port,
284 dest_hostname, priv->dest_port,
285 priv->proxy_username,
286 priv->proxy_password);
288 g_free (dest_hostname);
290 if (priv->supports_hostname || priv->next_dest_ip == NULL)
292 g_object_unref (priv->proxy_address);
293 priv->proxy_address = NULL;
297 if (result == NULL && first_error)
298 g_propagate_error (error, first_error);
299 else if (first_error)
300 g_error_free (first_error);
308 complete_async (GProxyAddressEnumeratorPrivate *priv)
310 GSimpleAsyncResult *simple = priv->simple;
312 if (priv->cancellable)
314 g_object_unref (priv->cancellable);
315 priv->cancellable = NULL;
319 g_simple_async_result_complete (simple);
320 g_object_unref (simple);
324 save_result (GProxyAddressEnumeratorPrivate *priv)
326 GSocketAddress *result;
328 if (strcmp ("direct", priv->proxy_type) == 0)
330 result = priv->proxy_address;
331 priv->proxy_address = NULL;
335 gchar *dest_hostname;
336 GInetSocketAddress *inetsaddr;
337 GInetAddress *inetaddr;
340 if (!priv->supports_hostname)
342 GInetAddress *dest_ip;
344 if (!priv->next_dest_ip)
345 priv->next_dest_ip = priv->dest_ips;
347 dest_ip = G_INET_ADDRESS (priv->next_dest_ip->data);
348 dest_hostname = g_inet_address_to_string (dest_ip);
350 priv->next_dest_ip = g_list_next (priv->next_dest_ip);
354 dest_hostname = g_strdup (priv->dest_hostname);
357 g_return_if_fail (G_IS_INET_SOCKET_ADDRESS (priv->proxy_address));
359 inetsaddr = G_INET_SOCKET_ADDRESS (priv->proxy_address);
360 inetaddr = g_inet_socket_address_get_address (inetsaddr);
361 port = g_inet_socket_address_get_port (inetsaddr);
363 result = g_proxy_address_new (inetaddr, port,
365 dest_hostname, priv->dest_port,
366 priv->proxy_username,
367 priv->proxy_password);
369 g_free (dest_hostname);
371 if (priv->supports_hostname || priv->next_dest_ip == NULL)
373 g_object_unref (priv->proxy_address);
374 priv->proxy_address = NULL;
378 g_simple_async_result_set_op_res_gpointer (priv->simple,
384 dest_hostname_lookup_cb (GObject *object,
385 GAsyncResult *result,
388 GError *error = NULL;
389 GProxyAddressEnumeratorPrivate *priv = user_data;
390 GSimpleAsyncResult *simple = priv->simple;
392 priv->dest_ips = g_resolver_lookup_by_name_finish (G_RESOLVER (object),
401 g_simple_async_result_set_from_error (simple, error);
402 g_error_free (error);
405 complete_async (priv);
409 address_enumerate_cb (GObject *object,
410 GAsyncResult *result,
413 GError *error = NULL;
414 GProxyAddressEnumeratorPrivate *priv = user_data;
415 GSimpleAsyncResult *simple = priv->simple;
417 priv->proxy_address =
418 g_socket_address_enumerator_next_finish (priv->addr_enum,
421 if (priv->proxy_address)
423 if (!priv->supports_hostname && !priv->dest_ips)
426 resolver = g_resolver_get_default();
427 g_resolver_lookup_by_name_async (resolver,
430 dest_hostname_lookup_cb,
432 g_object_unref (resolver);
438 else if (*priv->next_proxy)
440 g_object_unref (priv->addr_enum);
441 priv->addr_enum = NULL;
445 g_resolver_free_addresses (priv->dest_ips);
446 priv->dest_ips = NULL;
449 next_enumerator (priv);
453 g_socket_address_enumerator_next_async (priv->addr_enum,
455 address_enumerate_cb,
463 g_simple_async_result_set_from_error (simple, error);
464 g_error_free (error);
467 complete_async (priv);
471 proxy_lookup_cb (GObject *object,
472 GAsyncResult *result,
475 GError *error = NULL;
476 GProxyAddressEnumeratorPrivate *priv = user_data;
477 GSimpleAsyncResult *simple = priv->simple;
479 priv->proxies = g_proxy_resolver_lookup_finish (G_PROXY_RESOLVER (object),
482 priv->next_proxy = priv->proxies;
486 g_simple_async_result_set_from_error (simple, error);
487 g_error_free (error);
491 next_enumerator (priv);
494 g_socket_address_enumerator_next_async (priv->addr_enum,
496 address_enumerate_cb,
502 complete_async (priv);
506 g_proxy_address_enumerator_next_async (GSocketAddressEnumerator *enumerator,
507 GCancellable *cancellable,
508 GAsyncReadyCallback callback,
511 GProxyAddressEnumeratorPrivate *priv = GET_PRIVATE (enumerator);
513 g_return_if_fail (priv->simple == NULL);
514 g_return_if_fail (priv->cancellable == NULL);
516 priv->simple = g_simple_async_result_new (G_OBJECT (enumerator),
518 g_proxy_address_enumerator_next_async);
520 priv->cancellable = cancellable ? g_object_ref (cancellable) : NULL;
522 if (priv->proxies == NULL)
524 GProxyResolver *resolver = g_proxy_resolver_get_default ();
525 g_proxy_resolver_lookup_async (resolver,
535 if (priv->proxy_address)
541 g_socket_address_enumerator_next_async (priv->addr_enum,
543 address_enumerate_cb,
549 g_simple_async_result_complete_in_idle (priv->simple);
551 g_object_unref (priv->simple);
554 if (priv->cancellable)
556 g_object_unref (priv->cancellable);
557 priv->cancellable = NULL;
561 static GSocketAddress *
562 g_proxy_address_enumerator_next_finish (GSocketAddressEnumerator *enumerator,
563 GAsyncResult *result,
566 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
567 GSocketAddress *address;
569 if (g_simple_async_result_propagate_error (simple, error))
572 address = g_simple_async_result_get_op_res_gpointer (simple);
574 g_object_ref (address);
580 g_proxy_address_enumerator_get_property (GObject *object,
585 GProxyAddressEnumeratorPrivate *priv = GET_PRIVATE (object);
589 g_value_set_string (value, priv->dest_uri);
592 case PROP_CONNECTABLE:
593 g_value_set_object (value, priv->connectable);
597 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
602 g_proxy_address_enumerator_set_property (GObject *object,
607 GProxyAddressEnumeratorPrivate *priv = GET_PRIVATE (object);
614 g_free (priv->dest_hostname);
615 priv->dest_hostname = NULL;
618 g_free (priv->dest_uri);
619 priv->dest_uri = NULL;
621 uri = g_value_get_string (value);
625 GSocketConnectable *conn;
627 conn = g_network_address_parse_uri (uri, 0, NULL);
632 priv->dest_uri = g_strdup (uri);
635 "hostname", &priv->dest_hostname,
639 priv->dest_port = port;
640 g_object_unref (conn);
643 g_warning ("Invalid URI '%s'", uri);
649 case PROP_CONNECTABLE:
650 priv->connectable = g_value_dup_object (value);
654 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
659 g_proxy_address_enumerator_finalize (GObject *object)
661 GProxyAddressEnumeratorPrivate *priv = GET_PRIVATE (object);
663 if (priv->connectable)
664 g_object_unref (priv->connectable);
666 g_free (priv->dest_uri);
667 g_free (priv->dest_hostname);
670 g_resolver_free_addresses (priv->dest_ips);
672 g_strfreev (priv->proxies);
675 g_object_unref (priv->addr_enum);
677 g_free (priv->proxy_type);
678 g_free (priv->proxy_username);
679 g_free (priv->proxy_password);
681 if (priv->cancellable)
682 g_object_unref (priv->cancellable);
684 G_OBJECT_CLASS (g_proxy_address_enumerator_parent_class)->finalize (object);
688 g_proxy_address_enumerator_init (GProxyAddressEnumerator *self)
690 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
691 G_TYPE_PROXY_ADDRESS_ENUMERATOR,
692 GProxyAddressEnumeratorPrivate);
696 g_proxy_address_enumerator_class_init (GProxyAddressEnumeratorClass *proxy_enumerator_class)
698 GObjectClass *object_class = G_OBJECT_CLASS (proxy_enumerator_class);
699 GSocketAddressEnumeratorClass *enumerator_class = G_SOCKET_ADDRESS_ENUMERATOR_CLASS (proxy_enumerator_class);
701 g_type_class_add_private (enumerator_class,
702 sizeof (GProxyAddressEnumeratorPrivate));
704 object_class->set_property = g_proxy_address_enumerator_set_property;
705 object_class->get_property = g_proxy_address_enumerator_get_property;
706 object_class->finalize = g_proxy_address_enumerator_finalize;
708 enumerator_class->next = g_proxy_address_enumerator_next;
709 enumerator_class->next_async = g_proxy_address_enumerator_next_async;
710 enumerator_class->next_finish = g_proxy_address_enumerator_next_finish;
712 g_object_class_install_property (object_class,
714 g_param_spec_string ("uri",
716 P_("The destination URI, use none:// for generic socket"),
719 G_PARAM_CONSTRUCT_ONLY |
720 G_PARAM_STATIC_STRINGS));
722 g_object_class_install_property (object_class,
724 g_param_spec_object ("connectable",
726 P_("The connectable being enumerated."),
727 G_TYPE_SOCKET_CONNECTABLE,
729 G_PARAM_CONSTRUCT_ONLY |
730 G_PARAM_STATIC_STRINGS));