Fix resolver test script to show lookup results
[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 <string.h>
28 #include <signal.h>
29
30 #include <gresolv/gresolv.h>
31
32 static GMainLoop *main_loop = NULL;
33
34 static void resolv_debug(const char *str, void *data)
35 {
36         g_print("%s: %s\n", (const char *) data, str);
37 }
38
39 static void sig_term(int sig)
40 {
41         g_main_loop_quit(main_loop);
42 }
43
44 static void resolv_result(GResolvResultStatus status,
45                                         char **results, gpointer user_data)
46 {
47         int i;
48
49         g_print("status: %d\n", status);
50
51         if (results != NULL) {
52                 for (i = 0; results[i]; i++)
53                         g_print("result: %s\n", results[i]);
54         }
55
56         g_main_loop_quit(main_loop);
57 }
58
59 int main(int argc, char *argv[])
60 {
61         struct sigaction sa;
62         GResolv *resolv;
63         int index = 0;
64
65         if (argc < 2) {
66                 printf("missing argument\n");
67                 return 1;
68         }
69
70         resolv = g_resolv_new(index);
71         if (resolv == NULL) {
72                 printf("failed to create resolver\n");
73                 return 1;
74         }
75
76         g_resolv_set_debug(resolv, resolv_debug, "RESOLV");
77
78         main_loop = g_main_loop_new(NULL, FALSE);
79
80         if (argc > 2) {
81                 int i;
82
83                 for (i = 2; i < argc; i++)
84                         g_resolv_add_nameserver(resolv, argv[i], 53, 0);
85         } else
86                 g_resolv_add_nameserver(resolv, "127.0.0.1", 53, 0);
87
88         g_resolv_lookup_hostname(resolv, argv[1], resolv_result, NULL);
89
90         memset(&sa, 0, sizeof(sa));
91         sa.sa_handler = sig_term;
92         sigaction(SIGINT, &sa, NULL);
93         sigaction(SIGTERM, &sa, NULL);
94
95         g_main_loop_run(main_loop);
96
97         g_resolv_unref(resolv);
98
99         g_main_loop_unref(main_loop);
100
101         return 0;
102 }