~cyphermox/ubuntu/natty/connman/release-0.64

« back to all changes in this revision

Viewing changes to tools/wpad-test.c

  • Committer: Mathieu Trudel-Lapierre
  • Date: 2010-11-30 15:51:10 UTC
  • mfrom: (1.1.13 upstream)
  • Revision ID: mathieu.trudel-lapierre@canonical.com-20101130155110-32g0usyc4jbl131x
New upstream release 0.64.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 <unistd.h>
 
29
#include <string.h>
 
30
#include <signal.h>
 
31
 
 
32
#include <gweb/gresolv.h>
 
33
 
 
34
static GTimer *timer;
 
35
 
 
36
static GMainLoop *main_loop;
 
37
 
 
38
static GResolv *resolv;
 
39
 
 
40
static void resolv_debug(const char *str, void *data)
 
41
{
 
42
        g_print("%s: %s\n", (const char *) data, str);
 
43
}
 
44
 
 
45
static void sig_term(int sig)
 
46
{
 
47
        g_main_loop_quit(main_loop);
 
48
}
 
49
 
 
50
static void resolv_result(GResolvResultStatus status,
 
51
                                        char **results, gpointer user_data)
 
52
{
 
53
        char *hostname = user_data;
 
54
        char *str, *ptr;
 
55
        gdouble elapsed;
 
56
        int i;
 
57
 
 
58
        if (status == G_RESOLV_RESULT_STATUS_SUCCESS) {
 
59
                g_print("Found %s\n", hostname);
 
60
                goto done;
 
61
        }
 
62
 
 
63
        g_print("No result for %s\n", hostname);
 
64
 
 
65
        ptr = strchr(hostname + 5, '.');
 
66
        if (ptr == NULL || strlen(ptr) < 2) {
 
67
                g_print("No more names\n");
 
68
                goto done;
 
69
        }
 
70
 
 
71
        if (strchr(ptr + 1, '.') == NULL) {
 
72
                g_print("Not found\n");
 
73
                goto done;
 
74
        }
 
75
 
 
76
        str = g_strdup_printf("wpad.%s", ptr + 1);
 
77
 
 
78
        g_resolv_lookup_hostname(resolv, str, resolv_result, str);
 
79
 
 
80
        g_free(hostname);
 
81
 
 
82
        return;
 
83
 
 
84
done:
 
85
        elapsed = g_timer_elapsed(timer, NULL);
 
86
 
 
87
        g_print("elapse: %f seconds\n", elapsed);
 
88
 
 
89
        if (results != NULL) {
 
90
                for (i = 0; results[i]; i++)
 
91
                        g_print("result: %s\n", results[i]);
 
92
        }
 
93
 
 
94
        g_free(hostname);
 
95
 
 
96
        g_main_loop_quit(main_loop);
 
97
}
 
98
 
 
99
static void start_wpad(const char *search)
 
100
{
 
101
        char domainname[256];
 
102
        char *hostname;
 
103
 
 
104
        if (search == NULL) {
 
105
                if (getdomainname(domainname, sizeof(domainname)) < 0) {
 
106
                        g_printerr("Failed to get domain name\n");
 
107
                        goto quit;
 
108
                }
 
109
        } else
 
110
                snprintf(domainname, sizeof(domainname), "%s", search);
 
111
 
 
112
        if (strlen(domainname) == 0) {
 
113
                g_printerr("Domain name is not set\n");
 
114
                goto quit;
 
115
        }
 
116
                
 
117
        g_print("domainname: %s\n", domainname);
 
118
 
 
119
        hostname = g_strdup_printf("wpad.%s", domainname);
 
120
 
 
121
        g_resolv_lookup_hostname(resolv, hostname, resolv_result, hostname);
 
122
 
 
123
        return;
 
124
 
 
125
quit:
 
126
        g_main_loop_quit(main_loop);
 
127
}
 
128
 
 
129
static gboolean option_debug = FALSE;
 
130
static gchar *option_search = NULL;
 
131
 
 
132
static GOptionEntry options[] = {
 
133
        { "debug", 'd', 0, G_OPTION_ARG_NONE, &option_debug,
 
134
                                        "Enable debug output" },
 
135
        { "search", 's', 0, G_OPTION_ARG_STRING, &option_search,
 
136
                                        "Specify search domain name" },
 
137
        { NULL },
 
138
};
 
139
 
 
140
int main(int argc, char *argv[])
 
141
{
 
142
        GOptionContext *context;
 
143
        GError *error = NULL;
 
144
        struct sigaction sa;
 
145
        int index = 0;
 
146
 
 
147
        context = g_option_context_new(NULL);
 
148
        g_option_context_add_main_entries(context, options, NULL);
 
149
 
 
150
        if (g_option_context_parse(context, &argc, &argv, &error) == FALSE) {
 
151
                if (error != NULL) {
 
152
                        g_printerr("%s\n", error->message);
 
153
                        g_error_free(error);
 
154
                } else
 
155
                        g_printerr("An unknown error occurred\n");
 
156
                exit(1);
 
157
        }
 
158
 
 
159
        g_option_context_free(context);
 
160
 
 
161
        resolv = g_resolv_new(index);
 
162
        if (resolv == NULL) {
 
163
                g_printerr("Failed to create resolver\n");
 
164
                return 1;
 
165
        }
 
166
 
 
167
        if (option_debug == TRUE)
 
168
                g_resolv_set_debug(resolv, resolv_debug, "RESOLV");
 
169
 
 
170
        main_loop = g_main_loop_new(NULL, FALSE);
 
171
 
 
172
        if (argc > 1) {
 
173
                int i;
 
174
 
 
175
                for (i = 1; i < argc; i++)
 
176
                        g_resolv_add_nameserver(resolv, argv[i], 53, 0);
 
177
        } else
 
178
                g_resolv_add_nameserver(resolv, "127.0.0.1", 53, 0);
 
179
 
 
180
        timer = g_timer_new();
 
181
 
 
182
        start_wpad(option_search);
 
183
 
 
184
        g_free(option_search);
 
185
 
 
186
        memset(&sa, 0, sizeof(sa));
 
187
        sa.sa_handler = sig_term;
 
188
        sigaction(SIGINT, &sa, NULL);
 
189
        sigaction(SIGTERM, &sa, NULL);
 
190
 
 
191
        g_main_loop_run(main_loop);
 
192
 
 
193
        g_timer_destroy(timer);
 
194
 
 
195
        g_resolv_unref(resolv);
 
196
 
 
197
        g_main_loop_unref(main_loop);
 
198
 
 
199
        return 0;
 
200
}