Add debug option to resolver test tool
[framework/connectivity/connman.git] / tools / resolv-test.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2010  Intel Corporation. All rights reserved.
6  *
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.
10  *
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.
15  *
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
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <signal.h>
30
31 #include <gresolv/gresolv.h>
32
33 static GMainLoop *main_loop = NULL;
34
35 static void resolv_debug(const char *str, void *data)
36 {
37         g_print("%s: %s\n", (const char *) data, str);
38 }
39
40 static void sig_term(int sig)
41 {
42         g_main_loop_quit(main_loop);
43 }
44
45 static void resolv_result(GResolvResultStatus status,
46                                         char **results, gpointer user_data)
47 {
48         int i;
49
50         g_print("status: %d\n", status);
51
52         if (results != NULL) {
53                 for (i = 0; results[i]; i++)
54                         g_print("result: %s\n", results[i]);
55         }
56
57         g_main_loop_quit(main_loop);
58 }
59
60 static gboolean option_debug = FALSE;
61
62 static GOptionEntry options[] = {
63         { "debug", 'd', 0, G_OPTION_ARG_NONE, &option_debug,
64                                         "Enable debug output" },
65         { NULL },
66 };
67
68 int main(int argc, char *argv[])
69 {
70         GOptionContext *context;
71         GError *error = NULL;
72         struct sigaction sa;
73         GResolv *resolv;
74         int index = 0;
75
76         context = g_option_context_new(NULL);
77         g_option_context_add_main_entries(context, options, NULL);
78
79         if (g_option_context_parse(context, &argc, &argv, &error) == FALSE) {
80                 if (error != NULL) {
81                         g_printerr("%s\n", error->message);
82                         g_error_free(error);
83                 } else
84                         g_printerr("An unknown error occurred\n");
85                 exit(1);
86         }
87
88         g_option_context_free(context);
89
90         if (argc < 2) {
91                 printf("missing argument\n");
92                 return 1;
93         }
94
95         resolv = g_resolv_new(index);
96         if (resolv == NULL) {
97                 printf("failed to create resolver\n");
98                 return 1;
99         }
100
101         if (option_debug == TRUE)
102                 g_resolv_set_debug(resolv, resolv_debug, "RESOLV");
103
104         main_loop = g_main_loop_new(NULL, FALSE);
105
106         if (argc > 2) {
107                 int i;
108
109                 for (i = 2; i < argc; i++)
110                         g_resolv_add_nameserver(resolv, argv[i], 53, 0);
111         } else
112                 g_resolv_add_nameserver(resolv, "127.0.0.1", 53, 0);
113
114         g_resolv_lookup_hostname(resolv, argv[1], resolv_result, NULL);
115
116         memset(&sa, 0, sizeof(sa));
117         sa.sa_handler = sig_term;
118         sigaction(SIGINT, &sa, NULL);
119         sigaction(SIGTERM, &sa, NULL);
120
121         g_main_loop_run(main_loop);
122
123         g_resolv_unref(resolv);
124
125         g_main_loop_unref(main_loop);
126
127         return 0;
128 }