5 * Copyright (C) 2007-2012 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
31 #include <gweb/gresolv.h>
35 static GMainLoop *main_loop;
37 static void resolv_debug(const char *str, void *data)
39 g_print("%s: %s\n", (const char *) data, str);
42 static void sig_term(int sig)
44 g_main_loop_quit(main_loop);
47 static const char *status2str(GResolvResultStatus status)
50 case G_RESOLV_RESULT_STATUS_SUCCESS:
52 case G_RESOLV_RESULT_STATUS_ERROR:
54 case G_RESOLV_RESULT_STATUS_NO_RESPONSE:
56 case G_RESOLV_RESULT_STATUS_FORMAT_ERROR:
57 return "format error";
58 case G_RESOLV_RESULT_STATUS_SERVER_FAILURE:
59 return "server failure";
60 case G_RESOLV_RESULT_STATUS_NAME_ERROR:
62 case G_RESOLV_RESULT_STATUS_NOT_IMPLEMENTED:
63 return "not implemented";
64 case G_RESOLV_RESULT_STATUS_REFUSED:
66 case G_RESOLV_RESULT_STATUS_NO_ANSWER:
73 static void resolv_result(GResolvResultStatus status,
74 char **results, gpointer user_data)
79 elapsed = g_timer_elapsed(timer, NULL);
81 g_print("elapse: %f seconds\n", elapsed);
83 g_print("status: %s\n", status2str(status));
86 for (i = 0; results[i]; i++)
87 g_print("result: %s\n", results[i]);
90 g_main_loop_quit(main_loop);
93 static bool option_debug = false;
95 static GOptionEntry options[] = {
96 { "debug", 'd', 0, G_OPTION_ARG_NONE, &option_debug,
97 "Enable debug output" },
101 int main(int argc, char *argv[])
103 GOptionContext *context;
104 GError *error = NULL;
109 context = g_option_context_new(NULL);
110 g_option_context_add_main_entries(context, options, NULL);
112 if (!g_option_context_parse(context, &argc, &argv, &error)) {
114 g_printerr("%s\n", error->message);
117 g_printerr("An unknown error occurred\n");
121 g_option_context_free(context);
124 printf("missing argument\n");
128 resolv = g_resolv_new(index);
130 printf("failed to create resolver\n");
135 g_resolv_set_debug(resolv, resolv_debug, "RESOLV");
137 main_loop = g_main_loop_new(NULL, FALSE);
142 for (i = 2; i < argc; i++)
143 g_resolv_add_nameserver(resolv, argv[i], 53, 0);
146 timer = g_timer_new();
148 if (g_resolv_lookup_hostname(resolv, argv[1],
149 resolv_result, NULL) == 0) {
150 printf("failed to start lookup\n");
154 memset(&sa, 0, sizeof(sa));
155 sa.sa_handler = sig_term;
156 sigaction(SIGINT, &sa, NULL);
157 sigaction(SIGTERM, &sa, NULL);
159 g_main_loop_run(main_loop);
161 g_timer_destroy(timer);
163 g_resolv_unref(resolv);
165 g_main_loop_unref(main_loop);