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:
71 static void resolv_result(GResolvResultStatus status,
72 char **results, gpointer user_data)
77 elapsed = g_timer_elapsed(timer, NULL);
79 g_print("elapse: %f seconds\n", elapsed);
81 g_print("status: %s\n", status2str(status));
83 if (results != NULL) {
84 for (i = 0; results[i]; i++)
85 g_print("result: %s\n", results[i]);
88 g_main_loop_quit(main_loop);
91 static gboolean option_debug = FALSE;
93 static GOptionEntry options[] = {
94 { "debug", 'd', 0, G_OPTION_ARG_NONE, &option_debug,
95 "Enable debug output" },
99 int main(int argc, char *argv[])
101 GOptionContext *context;
102 GError *error = NULL;
107 context = g_option_context_new(NULL);
108 g_option_context_add_main_entries(context, options, NULL);
110 if (g_option_context_parse(context, &argc, &argv, &error) == FALSE) {
112 g_printerr("%s\n", error->message);
115 g_printerr("An unknown error occurred\n");
119 g_option_context_free(context);
122 printf("missing argument\n");
126 resolv = g_resolv_new(index);
127 if (resolv == NULL) {
128 printf("failed to create resolver\n");
132 if (option_debug == TRUE)
133 g_resolv_set_debug(resolv, resolv_debug, "RESOLV");
135 main_loop = g_main_loop_new(NULL, FALSE);
140 for (i = 2; i < argc; i++)
141 g_resolv_add_nameserver(resolv, argv[i], 53, 0);
144 timer = g_timer_new();
146 if (g_resolv_lookup_hostname(resolv, argv[1],
147 resolv_result, NULL) == 0) {
148 printf("failed to start lookup\n");
152 memset(&sa, 0, sizeof(sa));
153 sa.sa_handler = sig_term;
154 sigaction(SIGINT, &sa, NULL);
155 sigaction(SIGTERM, &sa, NULL);
157 g_main_loop_run(main_loop);
159 g_timer_destroy(timer);
161 g_resolv_unref(resolv);
163 g_main_loop_unref(main_loop);