tools: Update copyright information
[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         }
67
68         return NULL;
69 }
70
71 static void resolv_result(GResolvResultStatus status,
72                                         char **results, gpointer user_data)
73 {
74         gdouble elapsed;
75         int i;
76
77         elapsed = g_timer_elapsed(timer, NULL);
78
79         g_print("elapse: %f seconds\n", elapsed);
80
81         g_print("status: %s\n", status2str(status));
82
83         if (results != NULL) {
84                 for (i = 0; results[i]; i++)
85                         g_print("result: %s\n", results[i]);
86         }
87
88         g_main_loop_quit(main_loop);
89 }
90
91 static gboolean option_debug = FALSE;
92
93 static GOptionEntry options[] = {
94         { "debug", 'd', 0, G_OPTION_ARG_NONE, &option_debug,
95                                         "Enable debug output" },
96         { NULL },
97 };
98
99 int main(int argc, char *argv[])
100 {
101         GOptionContext *context;
102         GError *error = NULL;
103         struct sigaction sa;
104         GResolv *resolv;
105         int index = 0;
106
107         context = g_option_context_new(NULL);
108         g_option_context_add_main_entries(context, options, NULL);
109
110         if (g_option_context_parse(context, &argc, &argv, &error) == FALSE) {
111                 if (error != NULL) {
112                         g_printerr("%s\n", error->message);
113                         g_error_free(error);
114                 } else
115                         g_printerr("An unknown error occurred\n");
116                 exit(1);
117         }
118
119         g_option_context_free(context);
120
121         if (argc < 2) {
122                 printf("missing argument\n");
123                 return 1;
124         }
125
126         resolv = g_resolv_new(index);
127         if (resolv == NULL) {
128                 printf("failed to create resolver\n");
129                 return 1;
130         }
131
132         if (option_debug == TRUE)
133                 g_resolv_set_debug(resolv, resolv_debug, "RESOLV");
134
135         main_loop = g_main_loop_new(NULL, FALSE);
136
137         if (argc > 2) {
138                 int i;
139
140                 for (i = 2; i < argc; i++)
141                         g_resolv_add_nameserver(resolv, argv[i], 53, 0);
142         }
143
144         timer = g_timer_new();
145
146         if (g_resolv_lookup_hostname(resolv, argv[1],
147                                         resolv_result, NULL) == 0) {
148                 printf("failed to start lookup\n");
149                 return 1;
150         }
151
152         memset(&sa, 0, sizeof(sa));
153         sa.sa_handler = sig_term;
154         sigaction(SIGINT, &sa, NULL);
155         sigaction(SIGTERM, &sa, NULL);
156
157         g_main_loop_run(main_loop);
158
159         g_timer_destroy(timer);
160
161         g_resolv_unref(resolv);
162
163         g_main_loop_unref(main_loop);
164
165         return 0;
166 }