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 [-t] [-s] [hostname | IP | service/protocol/domain ] ...\n");
44 fprintf (stderr, " resolver [-t] [-s] -c [hostname | IP | service/protocol/domain ]\n");
45 fprintf (stderr, " Use -t to enable threading.\n");
46 fprintf (stderr, " Use -s to do synchronous lookups.\n");
47 fprintf (stderr, " Both together will result in simultaneous lookups in multiple threads\n");
48 fprintf (stderr, " Use -c (and only a single resolvable argument) to test GSocketConnectable.\n");
52 G_LOCK_DEFINE_STATIC (response);
60 /* In the sync case we need to make sure we don't call
61 * g_main_loop_quit before the loop is actually running...
63 g_idle_add ((GSourceFunc)g_main_loop_quit, loop);
68 print_resolved_name (const char *phys,
73 printf ("Address: %s\n", phys);
76 printf ("Error: %s\n", error->message);
81 printf ("Name: %s\n", name);
91 print_resolved_addresses (const char *name,
99 printf ("Name: %s\n", name);
102 printf ("Error: %s\n", error->message);
103 g_error_free (error);
107 for (a = addresses; a; a = a->next)
109 phys = g_inet_address_to_string (a->data);
110 printf ("Address: %s\n", phys);
112 g_object_unref (a->data);
114 g_list_free (addresses);
123 print_resolved_service (const char *service,
130 printf ("Service: %s\n", service);
133 printf ("Error: %s\n", error->message);
134 g_error_free (error);
138 for (t = targets; t; t = t->next)
140 printf ("%s:%u (pri %u, weight %u)\n",
141 g_srv_target_get_hostname (t->data),
142 g_srv_target_get_port (t->data),
143 g_srv_target_get_priority (t->data),
144 g_srv_target_get_weight (t->data));
145 g_srv_target_free (t->data);
147 g_list_free (targets);
156 lookup_one_sync (const char *arg)
158 GError *error = NULL;
160 if (strchr (arg, '/'))
163 /* service/protocol/domain */
164 char **parts = g_strsplit (arg, "/", 3);
166 if (!parts || !parts[2])
169 targets = g_resolver_lookup_service (resolver,
170 parts[0], parts[1], parts[2],
171 cancellable, &error);
172 print_resolved_service (arg, targets, error);
174 else if (g_hostname_is_ip_address (arg))
176 GInetAddress *addr = g_inet_address_new_from_string (arg);
179 name = g_resolver_lookup_by_address (resolver, addr, cancellable, &error);
180 print_resolved_name (arg, name, error);
181 g_object_unref (addr);
187 addresses = g_resolver_lookup_by_name (resolver, arg, cancellable, &error);
188 print_resolved_addresses (arg, addresses, error);
193 lookup_thread (gpointer arg)
195 lookup_one_sync (arg);
200 start_threaded_lookups (char **argv, int argc)
204 for (i = 0; i < argc; i++)
205 g_thread_create (lookup_thread, argv[i], FALSE, NULL);
209 start_sync_lookups (char **argv, int argc)
213 for (i = 0; i < argc; i++)
214 lookup_one_sync (argv[i]);
218 lookup_by_addr_callback (GObject *source, GAsyncResult *result,
221 const char *phys = user_data;
222 GError *error = NULL;
225 name = g_resolver_lookup_by_address_finish (resolver, result, &error);
226 print_resolved_name (phys, name, error);
230 lookup_by_name_callback (GObject *source, GAsyncResult *result,
233 const char *name = user_data;
234 GError *error = NULL;
237 addresses = g_resolver_lookup_by_name_finish (resolver, result, &error);
238 print_resolved_addresses (name, addresses, error);
242 lookup_service_callback (GObject *source, GAsyncResult *result,
245 const char *service = user_data;
246 GError *error = NULL;
249 targets = g_resolver_lookup_service_finish (resolver, result, &error);
250 print_resolved_service (service, targets, error);
254 start_async_lookups (char **argv, int argc)
258 for (i = 0; i < argc; i++)
260 if (strchr (argv[i], '/'))
262 /* service/protocol/domain */
263 char **parts = g_strsplit (argv[i], "/", 3);
265 if (!parts || !parts[2])
268 g_resolver_lookup_service_async (resolver,
269 parts[0], parts[1], parts[2],
271 lookup_service_callback, argv[i]);
273 else if (g_hostname_is_ip_address (argv[i]))
275 GInetAddress *addr = g_inet_address_new_from_string (argv[i]);
277 g_resolver_lookup_by_address_async (resolver, addr, cancellable,
278 lookup_by_addr_callback, argv[i]);
279 g_object_unref (addr);
283 g_resolver_lookup_by_name_async (resolver, argv[i], cancellable,
284 lookup_by_name_callback,
288 /* Stress-test the reloading code */
289 g_signal_emit_by_name (resolver, "reload");
294 print_connectable_sockaddr (GSocketAddress *sockaddr,
301 printf ("Error: %s\n", error->message);
302 g_error_free (error);
304 else if (!G_IS_INET_SOCKET_ADDRESS (sockaddr))
306 printf ("Error: Unexpected sockaddr type '%s'\n", g_type_name_from_instance ((GTypeInstance *)sockaddr));
307 g_object_unref (sockaddr);
311 GInetSocketAddress *isa = G_INET_SOCKET_ADDRESS (sockaddr);
312 phys = g_inet_address_to_string (g_inet_socket_address_get_address (isa));
313 printf ("Address: %s%s%s:%d\n",
314 strchr (phys, ':') ? "[" : "", phys, strchr (phys, ':') ? "]" : "",
315 g_inet_socket_address_get_port (isa));
317 g_object_unref (sockaddr);
322 do_sync_connectable (GSocketAddressEnumerator *enumerator)
324 GSocketAddress *sockaddr;
325 GError *error = NULL;
327 while ((sockaddr = g_socket_address_enumerator_next (enumerator, cancellable, &error)))
328 print_connectable_sockaddr (sockaddr, error);
330 g_object_unref (enumerator);
334 static void do_async_connectable (GSocketAddressEnumerator *enumerator);
337 got_next_async (GObject *source, GAsyncResult *result, gpointer user_data)
339 GSocketAddressEnumerator *enumerator = G_SOCKET_ADDRESS_ENUMERATOR (source);
340 GSocketAddress *sockaddr;
341 GError *error = NULL;
343 sockaddr = g_socket_address_enumerator_next_finish (enumerator, result, &error);
344 if (sockaddr || error)
345 print_connectable_sockaddr (sockaddr, error);
347 do_async_connectable (enumerator);
350 g_object_unref (enumerator);
356 do_async_connectable (GSocketAddressEnumerator *enumerator)
358 g_socket_address_enumerator_next_async (enumerator, cancellable,
359 got_next_async, NULL);
363 do_connectable (const char *arg, gboolean synchronous)
366 GSocketConnectable *connectable;
367 GSocketAddressEnumerator *enumerator;
369 if (strchr (arg, '/'))
371 /* service/protocol/domain */
372 parts = g_strsplit (arg, "/", 3);
373 if (!parts || !parts[2])
376 connectable = g_network_service_new (parts[0], parts[1], parts[2]);
382 parts = g_strsplit (arg, ":", 2);
383 if (parts && parts[1])
386 port = strtoul (parts[1], NULL, 10);
391 if (g_hostname_is_ip_address (arg))
393 GInetAddress *addr = g_inet_address_new_from_string (arg);
394 GSocketAddress *sockaddr = g_inet_socket_address_new (addr, port);
396 g_object_unref (addr);
397 connectable = G_SOCKET_CONNECTABLE (sockaddr);
400 connectable = g_network_address_new (arg, port);
403 enumerator = g_socket_connectable_enumerate (connectable);
404 g_object_unref (connectable);
407 do_sync_connectable (enumerator);
409 do_async_connectable (enumerator);
413 static int cancel_fds[2];
416 interrupted (int sig)
420 signal (SIGINT, SIG_DFL);
421 c = write (cancel_fds[1], "x", 1);
422 g_assert_cmpint(c, ==, 1);
426 async_cancel (GIOChannel *source, GIOCondition cond, gpointer cancel)
428 g_cancellable_cancel (cancel);
434 main (int argc, char **argv)
436 gboolean threaded = FALSE, synchronous = FALSE;
437 gboolean use_connectable = FALSE;
443 /* We can't use GOptionContext because we use the arguments to
444 * decide whether or not to call g_thread_init().
446 while (argc >= 2 && argv[1][0] == '-')
448 if (!strcmp (argv[1], "-t"))
450 g_thread_init (NULL);
453 else if (!strcmp (argv[1], "-s"))
455 else if (!strcmp (argv[1], "-c"))
456 use_connectable = TRUE;
465 if (argc < 2 || (argc > 2 && use_connectable))
468 resolver = g_resolver_get_default ();
470 cancellable = g_cancellable_new ();
473 /* Set up cancellation; we want to cancel if the user ^C's the
474 * program, but we can't cancel directly from an interrupt.
476 signal (SIGINT, interrupted);
478 if (pipe (cancel_fds) == -1)
483 chan = g_io_channel_unix_new (cancel_fds[0]);
484 watch = g_io_add_watch (chan, G_IO_IN, async_cancel, cancellable);
485 g_io_channel_unref (chan);
489 loop = g_main_loop_new (NULL, TRUE);
492 do_connectable (argv[1], synchronous);
495 if (threaded && synchronous)
496 start_threaded_lookups (argv + 1, argc - 1);
497 else if (synchronous)
498 start_sync_lookups (argv + 1, argc - 1);
500 start_async_lookups (argv + 1, argc - 1);
503 g_main_loop_run (loop);
504 g_main_loop_unref (loop);
507 g_source_remove (watch);
509 g_object_unref (cancellable);