Make resolver test tool use internal resolver library
[platform/upstream/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 int main(int argc, char *argv[])
45 {
46         struct sigaction sa;
47         GResolv *resolv;
48         int index = 0;
49
50         if (argc < 2) {
51                 printf("missing argument\n");
52                 return 1;
53         }
54
55         resolv = g_resolv_new(index);
56         if (resolv == NULL) {
57                 printf("failed to create resolver\n");
58                 return 1;
59         }
60
61         g_resolv_set_debug(resolv, resolv_debug, "RESOLV");
62
63         main_loop = g_main_loop_new(NULL, FALSE);
64
65         if (argc > 2) {
66                 int i;
67
68                 for (i = 2; i < argc; i++)
69                         g_resolv_add_nameserver(resolv, argv[i], 53, 0);
70         } else
71                 g_resolv_add_nameserver(resolv, "127.0.0.1", 53, 0);
72
73         g_resolv_lookup_hostname(resolv, argv[1]);
74
75         memset(&sa, 0, sizeof(sa));
76         sa.sa_handler = sig_term;
77         sigaction(SIGINT, &sa, NULL);
78         sigaction(SIGTERM, &sa, NULL);
79
80         g_main_loop_run(main_loop);
81
82         g_resolv_unref(resolv);
83
84         g_main_loop_unref(main_loop);
85
86         return 0;
87 }