1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
3 /* GIO - GLib Input, Output and Streaming Library
5 * Copyright (C) 2008 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, write to the
19 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20 * Boston, MA 02111-1307, USA.
35 static GResolver *resolver;
36 static GCancellable *cancellable;
37 static GMainLoop *loop;
38 static int nlookups = 0;
40 static void G_GNUC_NORETURN
43 fprintf (stderr, "Usage: resolver [-s] [hostname | IP | service/protocol/domain ] ...\n");
44 fprintf (stderr, " resolver [-s] -c NUMBER [hostname | IP | service/protocol/domain ]\n");
45 fprintf (stderr, " Use -s to do synchronous lookups.\n");
46 fprintf (stderr, " Use -c NUMBER (and only a single resolvable argument) to test GSocketConnectable.\n");
47 fprintf (stderr, " The given NUMBER determines how many times the connectable will be enumerated.\n");
51 G_LOCK_DEFINE_STATIC (response);
59 /* In the sync case we need to make sure we don't call
60 * g_main_loop_quit before the loop is actually running...
62 g_idle_add ((GSourceFunc)g_main_loop_quit, loop);
67 print_resolved_name (const char *phys,
72 printf ("Address: %s\n", phys);
75 printf ("Error: %s\n", error->message);
80 printf ("Name: %s\n", name);
90 print_resolved_addresses (const char *name,
98 printf ("Name: %s\n", name);
101 printf ("Error: %s\n", error->message);
102 g_error_free (error);
106 for (a = addresses; a; a = a->next)
108 phys = g_inet_address_to_string (a->data);
109 printf ("Address: %s\n", phys);
111 g_object_unref (a->data);
113 g_list_free (addresses);
122 print_resolved_service (const char *service,
129 printf ("Service: %s\n", service);
132 printf ("Error: %s\n", error->message);
133 g_error_free (error);
137 for (t = targets; t; t = t->next)
139 printf ("%s:%u (pri %u, weight %u)\n",
140 g_srv_target_get_hostname (t->data),
141 g_srv_target_get_port (t->data),
142 g_srv_target_get_priority (t->data),
143 g_srv_target_get_weight (t->data));
144 g_srv_target_free (t->data);
146 g_list_free (targets);
155 lookup_one_sync (const char *arg)
157 GError *error = NULL;
159 if (strchr (arg, '/'))
162 /* service/protocol/domain */
163 char **parts = g_strsplit (arg, "/", 3);
165 if (!parts || !parts[2])
168 targets = g_resolver_lookup_service (resolver,
169 parts[0], parts[1], parts[2],
170 cancellable, &error);
171 print_resolved_service (arg, targets, error);
173 else if (g_hostname_is_ip_address (arg))
175 GInetAddress *addr = g_inet_address_new_from_string (arg);
178 name = g_resolver_lookup_by_address (resolver, addr, cancellable, &error);
179 print_resolved_name (arg, name, error);
180 g_object_unref (addr);
186 addresses = g_resolver_lookup_by_name (resolver, arg, cancellable, &error);
187 print_resolved_addresses (arg, addresses, error);
192 lookup_thread (gpointer arg)
194 lookup_one_sync (arg);
199 start_sync_lookups (char **argv, int argc)
203 for (i = 0; i < argc; i++)
206 thread = g_thread_new ("lookup", lookup_thread, argv[i]);
207 g_thread_unref (thread);
212 lookup_by_addr_callback (GObject *source, GAsyncResult *result,
215 const char *phys = user_data;
216 GError *error = NULL;
219 name = g_resolver_lookup_by_address_finish (resolver, result, &error);
220 print_resolved_name (phys, name, error);
224 lookup_by_name_callback (GObject *source, GAsyncResult *result,
227 const char *name = user_data;
228 GError *error = NULL;
231 addresses = g_resolver_lookup_by_name_finish (resolver, result, &error);
232 print_resolved_addresses (name, addresses, error);
236 lookup_service_callback (GObject *source, GAsyncResult *result,
239 const char *service = user_data;
240 GError *error = NULL;
243 targets = g_resolver_lookup_service_finish (resolver, result, &error);
244 print_resolved_service (service, targets, error);
248 start_async_lookups (char **argv, int argc)
252 for (i = 0; i < argc; i++)
254 if (strchr (argv[i], '/'))
256 /* service/protocol/domain */
257 char **parts = g_strsplit (argv[i], "/", 3);
259 if (!parts || !parts[2])
262 g_resolver_lookup_service_async (resolver,
263 parts[0], parts[1], parts[2],
265 lookup_service_callback, argv[i]);
267 else if (g_hostname_is_ip_address (argv[i]))
269 GInetAddress *addr = g_inet_address_new_from_string (argv[i]);
271 g_resolver_lookup_by_address_async (resolver, addr, cancellable,
272 lookup_by_addr_callback, argv[i]);
273 g_object_unref (addr);
277 g_resolver_lookup_by_name_async (resolver, argv[i], cancellable,
278 lookup_by_name_callback,
282 /* Stress-test the reloading code */
283 g_signal_emit_by_name (resolver, "reload");
288 print_connectable_sockaddr (GSocketAddress *sockaddr,
295 printf ("Error: %s\n", error->message);
296 g_error_free (error);
298 else if (!G_IS_INET_SOCKET_ADDRESS (sockaddr))
300 printf ("Error: Unexpected sockaddr type '%s'\n", g_type_name_from_instance ((GTypeInstance *)sockaddr));
301 g_object_unref (sockaddr);
305 GInetSocketAddress *isa = G_INET_SOCKET_ADDRESS (sockaddr);
306 phys = g_inet_address_to_string (g_inet_socket_address_get_address (isa));
307 printf ("Address: %s%s%s:%d\n",
308 strchr (phys, ':') ? "[" : "", phys, strchr (phys, ':') ? "]" : "",
309 g_inet_socket_address_get_port (isa));
311 g_object_unref (sockaddr);
316 do_sync_connectable (GSocketAddressEnumerator *enumerator)
318 GSocketAddress *sockaddr;
319 GError *error = NULL;
321 while ((sockaddr = g_socket_address_enumerator_next (enumerator, cancellable, &error)))
322 print_connectable_sockaddr (sockaddr, error);
324 g_object_unref (enumerator);
328 static void do_async_connectable (GSocketAddressEnumerator *enumerator);
331 got_next_async (GObject *source, GAsyncResult *result, gpointer user_data)
333 GSocketAddressEnumerator *enumerator = G_SOCKET_ADDRESS_ENUMERATOR (source);
334 GSocketAddress *sockaddr;
335 GError *error = NULL;
337 sockaddr = g_socket_address_enumerator_next_finish (enumerator, result, &error);
338 if (sockaddr || error)
339 print_connectable_sockaddr (sockaddr, error);
341 do_async_connectable (enumerator);
344 g_object_unref (enumerator);
350 do_async_connectable (GSocketAddressEnumerator *enumerator)
352 g_socket_address_enumerator_next_async (enumerator, cancellable,
353 got_next_async, NULL);
357 do_connectable (const char *arg, gboolean synchronous, guint count)
360 GSocketConnectable *connectable;
361 GSocketAddressEnumerator *enumerator;
363 if (strchr (arg, '/'))
365 /* service/protocol/domain */
366 parts = g_strsplit (arg, "/", 3);
367 if (!parts || !parts[2])
370 connectable = g_network_service_new (parts[0], parts[1], parts[2]);
376 parts = g_strsplit (arg, ":", 2);
377 if (parts && parts[1])
380 port = strtoul (parts[1], NULL, 10);
385 if (g_hostname_is_ip_address (arg))
387 GInetAddress *addr = g_inet_address_new_from_string (arg);
388 GSocketAddress *sockaddr = g_inet_socket_address_new (addr, port);
390 g_object_unref (addr);
391 connectable = G_SOCKET_CONNECTABLE (sockaddr);
394 connectable = g_network_address_new (arg, port);
399 enumerator = g_socket_connectable_enumerate (connectable);
402 do_sync_connectable (enumerator);
404 do_async_connectable (enumerator);
407 g_object_unref (connectable);
411 static int cancel_fds[2];
414 interrupted (int sig)
418 signal (SIGINT, SIG_DFL);
419 c = write (cancel_fds[1], "x", 1);
420 g_assert_cmpint(c, ==, 1);
424 async_cancel (GIOChannel *source, GIOCondition cond, gpointer cancel)
426 g_cancellable_cancel (cancel);
432 main (int argc, char **argv)
434 gboolean synchronous = FALSE;
435 guint connectable_count = 0;
443 /* FIXME: use GOptionContext */
444 while (argc >= 2 && argv[1][0] == '-')
446 if (!strcmp (argv[1], "-s"))
448 else if (!strcmp (argv[1], "-c"))
450 connectable_count = atoi (argv[2]);
461 if (argc < 2 || (argc > 2 && connectable_count))
464 resolver = g_resolver_get_default ();
466 cancellable = g_cancellable_new ();
469 /* Set up cancellation; we want to cancel if the user ^C's the
470 * program, but we can't cancel directly from an interrupt.
472 signal (SIGINT, interrupted);
474 if (pipe (cancel_fds) == -1)
479 chan = g_io_channel_unix_new (cancel_fds[0]);
480 watch = g_io_add_watch (chan, G_IO_IN, async_cancel, cancellable);
481 g_io_channel_unref (chan);
485 loop = g_main_loop_new (NULL, TRUE);
487 if (connectable_count)
489 nlookups = connectable_count;
490 do_connectable (argv[1], synchronous, connectable_count);
495 start_sync_lookups (argv + 1, argc - 1);
497 start_async_lookups (argv + 1, argc - 1);
500 g_main_loop_run (loop);
501 g_main_loop_unref (loop);
504 g_source_remove (watch);
506 g_object_unref (cancellable);