1 /* GLib testing framework examples and tests
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 * Authors: Nicolas Dufresne <nicolas.dufresne@collabora.co.uk>
35 #include "gio/gunixsocketaddress.h"
38 static const gchar *info = NULL;
39 static GCancellable *cancellable = NULL;
40 static gint return_value = 0;
42 static void G_GNUC_NORETURN
45 fprintf (stderr, "Usage: proxy [-s] (uri|host:port|ip:port|path|srv/protocol/domain)\n");
46 fprintf (stderr, " Use -t to enable threading.\n");
47 fprintf (stderr, " Use -s to do synchronous lookups.\n");
48 fprintf (stderr, " Use -c to cancel operation.\n");
49 fprintf (stderr, " Use -e to use enumerator.\n");
50 fprintf (stderr, " Use -inet to use GInetSocketAddress enumerator (ip:port).\n");
52 fprintf (stderr, " Use -unix to use GUnixSocketAddress enumerator (path).\n");
54 fprintf (stderr, " Use -proxyaddr tp use GProxyAddress enumerator "
55 "(ip:port:protocol:dest_host:dest_port[:username[:password]]).\n");
56 fprintf (stderr, " Use -netaddr to use GNetworkAddress enumerator (host:port).\n");
57 fprintf (stderr, " Use -neturi to use GNetworkAddress enumerator (uri).\n");
58 fprintf (stderr, " Use -netsrv to use GNetworkService enumerator (srv/protocol/domain).\n");
59 fprintf (stderr, " Use -connect to create a connection using GSocketClient object (uri).\n");
64 print_and_free_error (GError *error)
66 fprintf (stderr, "Failed to obtain proxies: %s\n", error->message);
72 print_proxies (const gchar *info, gchar **proxies)
74 printf ("Proxies for URI '%s' are:\n", info);
76 if (proxies == NULL || proxies[0] == NULL)
79 for (; proxies[0]; proxies++)
80 printf ("\t%s\n", proxies[0]);
84 _proxy_lookup_cb (GObject *source_object,
90 GMainLoop *loop = user_data;
92 proxies = g_proxy_resolver_lookup_finish (G_PROXY_RESOLVER (source_object),
97 print_and_free_error (error);
101 print_proxies (info, proxies);
102 g_strfreev (proxies);
105 g_main_loop_quit (loop);
109 use_resolver (gboolean synchronous)
111 GProxyResolver *resolver;
113 resolver = g_proxy_resolver_get_default ();
117 GError *error = NULL;
120 proxies = g_proxy_resolver_lookup (resolver, info, cancellable, &error);
123 print_and_free_error (error);
125 print_proxies (info, proxies);
127 g_strfreev (proxies);
131 GMainLoop *loop = g_main_loop_new (NULL, FALSE);
133 g_proxy_resolver_lookup_async (resolver,
139 g_main_loop_run (loop);
140 g_main_loop_unref (loop);
145 print_proxy_address (GSocketAddress *sockaddr)
147 GProxyAddress *proxy = NULL;
149 if (sockaddr == NULL)
151 printf ("\tdirect://\n");
155 if (G_IS_PROXY_ADDRESS (sockaddr))
157 proxy = G_PROXY_ADDRESS (sockaddr);
158 printf ("\t%s://", g_proxy_address_get_protocol(proxy));
162 printf ("\tdirect://");
165 if (G_IS_INET_SOCKET_ADDRESS (sockaddr))
167 GInetAddress *inetaddr;
171 g_object_get (sockaddr,
172 "address", &inetaddr,
176 addr = g_inet_address_to_string (inetaddr);
178 printf ("%s:%u", addr, port);
185 if (g_proxy_address_get_username(proxy))
186 printf (" (Username: %s Password: %s)",
187 g_proxy_address_get_username(proxy),
188 g_proxy_address_get_password(proxy));
189 printf (" (Hostname: %s, Port: %i)",
190 g_proxy_address_get_destination_hostname (proxy),
191 g_proxy_address_get_destination_port (proxy));
198 _proxy_enumerate_cb (GObject *object,
199 GAsyncResult *result,
202 GError *error = NULL;
203 GMainLoop *loop = user_data;
204 GSocketAddressEnumerator *enumerator = G_SOCKET_ADDRESS_ENUMERATOR (object);
205 GSocketAddress *sockaddr;
207 sockaddr = g_socket_address_enumerator_next_finish (enumerator,
212 print_proxy_address (sockaddr);
213 g_socket_address_enumerator_next_async (enumerator,
217 g_object_unref (sockaddr);
222 print_and_free_error (error);
224 g_main_loop_quit (loop);
229 run_with_enumerator (gboolean synchronous, GSocketAddressEnumerator *enumerator)
231 GError *error = NULL;
235 GSocketAddress *sockaddr;
237 while ((sockaddr = g_socket_address_enumerator_next (enumerator,
241 print_proxy_address (sockaddr);
242 g_object_unref (sockaddr);
246 print_and_free_error (error);
250 GMainLoop *loop = g_main_loop_new (NULL, FALSE);
252 g_socket_address_enumerator_next_async (enumerator,
256 g_main_loop_run (loop);
257 g_main_loop_unref (loop);
262 use_enumerator (gboolean synchronous)
264 GSocketAddressEnumerator *enumerator;
266 enumerator = g_object_new (G_TYPE_PROXY_ADDRESS_ENUMERATOR,
270 printf ("Proxies for URI '%s' are:\n", info);
271 run_with_enumerator (synchronous, enumerator);
273 g_object_unref (enumerator);
277 use_inet_address (gboolean synchronous)
279 GSocketAddressEnumerator *enumerator;
280 GSocketAddress *sockaddr;
285 ip_and_port = g_strsplit (info, ":", 2);
289 addr = g_inet_address_new_from_string (ip_and_port[0]);
291 port = strtoul (ip_and_port [1], NULL, 10);
294 g_strfreev (ip_and_port);
296 if (addr == NULL || port <= 0 || port >= 65535)
298 fprintf (stderr, "Bad 'ip:port' parameter '%s'\n", info);
300 g_object_unref (addr);
305 sockaddr = g_inet_socket_address_new (addr, port);
306 g_object_unref (addr);
309 g_socket_connectable_proxy_enumerate (G_SOCKET_CONNECTABLE (sockaddr));
310 g_object_unref (sockaddr);
312 printf ("Proxies for ip and port '%s' are:\n", info);
313 run_with_enumerator (synchronous, enumerator);
315 g_object_unref (enumerator);
320 use_unix_address (gboolean synchronous)
322 GSocketAddressEnumerator *enumerator;
323 GSocketAddress *sockaddr;
325 sockaddr = g_unix_socket_address_new_abstract (info, -1);
327 if (sockaddr == NULL)
329 fprintf (stderr, "Failed to create unix socket with name '%s'\n", info);
335 g_socket_connectable_proxy_enumerate (G_SOCKET_CONNECTABLE (sockaddr));
336 g_object_unref (sockaddr);
338 printf ("Proxies for path '%s' are:\n", info);
339 run_with_enumerator (synchronous, enumerator);
341 g_object_unref (enumerator);
346 use_proxy_address (gboolean synchronous)
348 GSocketAddressEnumerator *enumerator;
349 GSocketAddress *sockaddr;
355 gchar *username = NULL;
356 gchar *password = NULL;
359 split_info = g_strsplit (info, ":", 7);
367 fprintf (stderr, "Bad 'ip:port:protocol:dest_host:dest_port' parameter '%s'\n", info);
372 addr = g_inet_address_new_from_string (split_info[0]);
373 port = strtoul (split_info [1], NULL, 10);
374 protocol = g_strdup (split_info[2]);
375 dest_host = g_strdup (split_info[3]);
376 dest_port = strtoul (split_info[4], NULL, 10);
380 username = g_strdup (split_info[5]);
382 password = g_strdup (split_info[6]);
385 g_strfreev (split_info);
387 sockaddr = g_proxy_address_new (addr, port,
388 protocol, dest_host, dest_port,
391 g_object_unref (addr);
398 g_socket_connectable_proxy_enumerate (G_SOCKET_CONNECTABLE (sockaddr));
399 g_object_unref (sockaddr);
401 printf ("Proxies for ip and port '%s' are:\n", info);
402 run_with_enumerator (synchronous, enumerator);
404 g_object_unref (enumerator);
408 use_network_address (gboolean synchronous)
410 GError *error = NULL;
411 GSocketAddressEnumerator *enumerator;
412 GSocketConnectable *connectable;
414 connectable = g_network_address_parse (info, -1, &error);
418 print_and_free_error (error);
422 enumerator = g_socket_connectable_proxy_enumerate (connectable);
423 g_object_unref (connectable);
425 printf ("Proxies for hostname and port '%s' are:\n", info);
426 run_with_enumerator (synchronous, enumerator);
428 g_object_unref (enumerator);
432 use_network_uri (gboolean synchronous)
434 GError *error = NULL;
435 GSocketAddressEnumerator *enumerator;
436 GSocketConnectable *connectable;
438 connectable = g_network_address_parse_uri (info, 0, &error);
442 print_and_free_error (error);
446 enumerator = g_socket_connectable_proxy_enumerate (connectable);
447 g_object_unref (connectable);
449 printf ("Proxies for URI '%s' are:\n", info);
450 run_with_enumerator (synchronous, enumerator);
452 g_object_unref (enumerator);
456 use_network_service (gboolean synchronous)
458 GSocketAddressEnumerator *enumerator;
459 GSocketConnectable *connectable = NULL;
462 split = g_strsplit (info, "/", 3);
464 if (split[0] && split[1] && split[2])
465 connectable = g_network_service_new (split[0], split[1], split[2]);
469 if (connectable == NULL)
471 fprintf (stderr, "Bad 'srv/protocol/domain' parameter '%s'\n", info);
476 enumerator = g_socket_connectable_proxy_enumerate (connectable);
477 g_object_unref (connectable);
479 printf ("Proxies for hostname and port '%s' are:\n", info);
480 run_with_enumerator (synchronous, enumerator);
482 g_object_unref (enumerator);
486 _socket_connect_cb (GObject *object,
487 GAsyncResult *result,
490 GError *error = NULL;
491 GMainLoop *loop = user_data;
492 GSocketClient *client = G_SOCKET_CLIENT (object);
493 GSocketConnection *connection;
495 connection = g_socket_client_connect_to_uri_finish (client,
500 GSocketAddress *proxy_addr;
501 proxy_addr = g_socket_connection_get_remote_address (connection, NULL);
502 print_proxy_address (proxy_addr);
506 print_and_free_error (error);
509 g_main_loop_quit (loop);
513 use_socket_client (gboolean synchronous)
515 GError *error = NULL;
516 GSocketClient *client;
518 client = g_socket_client_new ();
520 printf ("Proxies for URI '%s' are:\n", info);
524 GSocketConnection *connection;
525 GSocketAddress *proxy_addr;
527 connection = g_socket_client_connect_to_uri (client,
535 proxy_addr = g_socket_connection_get_remote_address (connection, NULL);
536 print_proxy_address (proxy_addr);
540 print_and_free_error (error);
545 GMainLoop *loop = g_main_loop_new (NULL, FALSE);
547 g_socket_client_connect_to_uri_async (client,
554 g_main_loop_run (loop);
555 g_main_loop_unref (loop);
558 g_object_unref (client);
566 USE_UNIX_SOCKET_ADDRESS,
568 USE_INET_SOCKET_ADDRESS,
577 main (gint argc, gchar **argv)
579 gboolean synchronous = FALSE;
580 gboolean cancel = FALSE;
581 ProxyTestType type = USE_RESOLVER;
585 while (argc >= 2 && argv[1][0] == '-')
587 if (!strcmp (argv[1], "-s"))
589 else if (!strcmp (argv[1], "-c"))
591 else if (!strcmp (argv[1], "-e"))
592 type = USE_ENUMERATOR;
593 else if (!strcmp (argv[1], "-inet"))
594 type = USE_INET_SOCKET_ADDRESS;
596 else if (!strcmp (argv[1], "-unix"))
597 type = USE_UNIX_SOCKET_ADDRESS;
599 else if (!strcmp (argv[1], "-proxyaddr"))
600 type = USE_PROXY_ADDRESS;
601 else if (!strcmp (argv[1], "-netaddr"))
602 type = USE_NETWORK_ADDRESS;
603 else if (!strcmp (argv[1], "-neturi"))
604 type = USE_NETWORK_URI;
605 else if (!strcmp (argv[1], "-netsrv"))
606 type = USE_NETWORK_SERVICE;
607 else if (!strcmp (argv[1], "-connect"))
608 type = USE_SOCKET_CLIENT;
619 /* Save URI for asynchronous callback */
624 cancellable = g_cancellable_new ();
625 g_cancellable_cancel (cancellable);
631 use_resolver (synchronous);
634 use_enumerator (synchronous);
636 case USE_INET_SOCKET_ADDRESS:
637 use_inet_address (synchronous);
640 case USE_UNIX_SOCKET_ADDRESS:
641 use_unix_address (synchronous);
644 case USE_PROXY_ADDRESS:
645 use_proxy_address (synchronous);
647 case USE_NETWORK_ADDRESS:
648 use_network_address (synchronous);
650 case USE_NETWORK_URI:
651 use_network_uri (synchronous);
653 case USE_NETWORK_SERVICE:
654 use_network_service (synchronous);
656 case USE_SOCKET_CLIENT:
657 use_socket_client (synchronous);