Add simple timer for resolver test program
[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 <stdlib.h>
28 #include <string.h>
29 #include <signal.h>
30
31 #include <gresolv/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 void resolv_result(GResolvResultStatus status,
48                                         char **results, gpointer user_data)
49 {
50         gdouble elapsed;
51         int i;
52
53         elapsed = g_timer_elapsed(timer, NULL);
54
55         g_print("elapse: %f seconds\n", elapsed);
56
57         g_print("status: %d\n", status);
58
59         if (results != NULL) {
60                 for (i = 0; results[i]; i++)
61                         g_print("result: %s\n", results[i]);
62         }
63
64         g_main_loop_quit(main_loop);
65 }
66
67 static gboolean option_debug = FALSE;
68
69 static GOptionEntry options[] = {
70         { "debug", 'd', 0, G_OPTION_ARG_NONE, &option_debug,
71                                         "Enable debug output" },
72         { NULL },
73 };
74
75 int main(int argc, char *argv[])
76 {
77         GOptionContext *context;
78         GError *error = NULL;
79         struct sigaction sa;
80         GResolv *resolv;
81         int index = 0;
82
83         context = g_option_context_new(NULL);
84         g_option_context_add_main_entries(context, options, NULL);
85
86         if (g_option_context_parse(context, &argc, &argv, &error) == FALSE) {
87                 if (error != NULL) {
88                         g_printerr("%s\n", error->message);
89                         g_error_free(error);
90                 } else
91                         g_printerr("An unknown error occurred\n");
92                 exit(1);
93         }
94
95         g_option_context_free(context);
96
97         if (argc < 2) {
98                 printf("missing argument\n");
99                 return 1;
100         }
101
102         resolv = g_resolv_new(index);
103         if (resolv == NULL) {
104                 printf("failed to create resolver\n");
105                 return 1;
106         }
107
108         if (option_debug == TRUE)
109                 g_resolv_set_debug(resolv, resolv_debug, "RESOLV");
110
111         main_loop = g_main_loop_new(NULL, FALSE);
112
113         if (argc > 2) {
114                 int i;
115
116                 for (i = 2; i < argc; i++)
117                         g_resolv_add_nameserver(resolv, argv[i], 53, 0);
118         } else
119                 g_resolv_add_nameserver(resolv, "127.0.0.1", 53, 0);
120
121         timer = g_timer_new();
122
123         g_resolv_lookup_hostname(resolv, argv[1], resolv_result, NULL);
124
125         memset(&sa, 0, sizeof(sa));
126         sa.sa_handler = sig_term;
127         sigaction(SIGINT, &sa, NULL);
128         sigaction(SIGTERM, &sa, NULL);
129
130         g_main_loop_run(main_loop);
131
132         g_timer_destroy(timer);
133
134         g_resolv_unref(resolv);
135
136         g_main_loop_unref(main_loop);
137
138         return 0;
139 }