Remove wrong rules from connman dbus conf
[platform/upstream/connman.git] / tools / resolv-test.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2012  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 <gweb/gresolv.h>
32
33 static GTimer *timer;
34
35 static GMainLoop *main_loop;
36
37 static void resolv_debug(const char *str, void *data)
38 {
39         g_print("%s: %s\n", (const char *) data, str);
40 }
41
42 static void sig_term(int sig)
43 {
44         g_main_loop_quit(main_loop);
45 }
46
47 static const char *status2str(GResolvResultStatus status)
48 {
49         switch (status) {
50         case G_RESOLV_RESULT_STATUS_SUCCESS:
51                 return "success";
52         case G_RESOLV_RESULT_STATUS_ERROR:
53                 return "error";
54         case G_RESOLV_RESULT_STATUS_NO_RESPONSE:
55                 return "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:
61                 return "name error";
62         case G_RESOLV_RESULT_STATUS_NOT_IMPLEMENTED:
63                 return "not implemented";
64         case G_RESOLV_RESULT_STATUS_REFUSED:
65                 return "refused";
66         case G_RESOLV_RESULT_STATUS_NO_ANSWER:
67                 return "no answer";
68         }
69
70         return NULL;
71 }
72
73 static void resolv_result(GResolvResultStatus status,
74                                         char **results, gpointer user_data)
75 {
76         gdouble elapsed;
77         int i;
78
79         elapsed = g_timer_elapsed(timer, NULL);
80
81         g_print("elapse: %f seconds\n", elapsed);
82
83         g_print("status: %s\n", status2str(status));
84
85         if (results) {
86                 for (i = 0; results[i]; i++)
87                         g_print("result: %s\n", results[i]);
88         }
89
90         g_main_loop_quit(main_loop);
91 }
92
93 static bool option_debug = false;
94
95 static GOptionEntry options[] = {
96         { "debug", 'd', 0, G_OPTION_ARG_NONE, &option_debug,
97                                         "Enable debug output" },
98         { NULL },
99 };
100
101 int main(int argc, char *argv[])
102 {
103         GOptionContext *context;
104         GError *error = NULL;
105         struct sigaction sa;
106         GResolv *resolv;
107         int index = 0;
108
109         context = g_option_context_new(NULL);
110         g_option_context_add_main_entries(context, options, NULL);
111
112         if (!g_option_context_parse(context, &argc, &argv, &error)) {
113                 if (error) {
114                         g_printerr("%s\n", error->message);
115                         g_error_free(error);
116                 } else
117                         g_printerr("An unknown error occurred\n");
118                 exit(1);
119         }
120
121         g_option_context_free(context);
122
123         if (argc < 2) {
124                 printf("missing argument\n");
125                 return 1;
126         }
127
128         resolv = g_resolv_new(index);
129         if (!resolv) {
130                 printf("failed to create resolver\n");
131                 return 1;
132         }
133
134         if (option_debug)
135                 g_resolv_set_debug(resolv, resolv_debug, "RESOLV");
136
137         main_loop = g_main_loop_new(NULL, FALSE);
138
139         if (argc > 2) {
140                 int i;
141
142                 for (i = 2; i < argc; i++)
143                         g_resolv_add_nameserver(resolv, argv[i], 53, 0);
144         }
145
146         timer = g_timer_new();
147
148         if (g_resolv_lookup_hostname(resolv, argv[1],
149                                         resolv_result, NULL) == 0) {
150                 printf("failed to start lookup\n");
151                 return 1;
152         }
153
154         memset(&sa, 0, sizeof(sa));
155         sa.sa_handler = sig_term;
156         sigaction(SIGINT, &sa, NULL);
157         sigaction(SIGTERM, &sa, NULL);
158
159         g_main_loop_run(main_loop);
160
161         g_timer_destroy(timer);
162
163         g_resolv_unref(resolv);
164
165         g_main_loop_unref(main_loop);
166
167         return 0;
168 }