5
* Copyright (C) 2007-2010 Intel Corporation. All rights reserved.
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.
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.
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
32
#include <gweb/gresolv.h>
36
static GMainLoop *main_loop;
38
static GResolv *resolv;
40
static void resolv_debug(const char *str, void *data)
42
g_print("%s: %s\n", (const char *) data, str);
45
static void sig_term(int sig)
47
g_main_loop_quit(main_loop);
50
static void resolv_result(GResolvResultStatus status,
51
char **results, gpointer user_data)
53
char *hostname = user_data;
58
if (status == G_RESOLV_RESULT_STATUS_SUCCESS) {
59
g_print("Found %s\n", hostname);
63
g_print("No result for %s\n", hostname);
65
ptr = strchr(hostname + 5, '.');
66
if (ptr == NULL || strlen(ptr) < 2) {
67
g_print("No more names\n");
71
if (strchr(ptr + 1, '.') == NULL) {
72
g_print("Not found\n");
76
str = g_strdup_printf("wpad.%s", ptr + 1);
78
g_resolv_lookup_hostname(resolv, str, resolv_result, str);
85
elapsed = g_timer_elapsed(timer, NULL);
87
g_print("elapse: %f seconds\n", elapsed);
89
if (results != NULL) {
90
for (i = 0; results[i]; i++)
91
g_print("result: %s\n", results[i]);
96
g_main_loop_quit(main_loop);
99
static void start_wpad(const char *search)
101
char domainname[256];
104
if (search == NULL) {
105
if (getdomainname(domainname, sizeof(domainname)) < 0) {
106
g_printerr("Failed to get domain name\n");
110
snprintf(domainname, sizeof(domainname), "%s", search);
112
if (strlen(domainname) == 0) {
113
g_printerr("Domain name is not set\n");
117
g_print("domainname: %s\n", domainname);
119
hostname = g_strdup_printf("wpad.%s", domainname);
121
g_resolv_lookup_hostname(resolv, hostname, resolv_result, hostname);
126
g_main_loop_quit(main_loop);
129
static gboolean option_debug = FALSE;
130
static gchar *option_search = NULL;
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" },
140
int main(int argc, char *argv[])
142
GOptionContext *context;
143
GError *error = NULL;
147
context = g_option_context_new(NULL);
148
g_option_context_add_main_entries(context, options, NULL);
150
if (g_option_context_parse(context, &argc, &argv, &error) == FALSE) {
152
g_printerr("%s\n", error->message);
155
g_printerr("An unknown error occurred\n");
159
g_option_context_free(context);
161
resolv = g_resolv_new(index);
162
if (resolv == NULL) {
163
g_printerr("Failed to create resolver\n");
167
if (option_debug == TRUE)
168
g_resolv_set_debug(resolv, resolv_debug, "RESOLV");
170
main_loop = g_main_loop_new(NULL, FALSE);
175
for (i = 1; i < argc; i++)
176
g_resolv_add_nameserver(resolv, argv[i], 53, 0);
178
g_resolv_add_nameserver(resolv, "127.0.0.1", 53, 0);
180
timer = g_timer_new();
182
start_wpad(option_search);
184
g_free(option_search);
186
memset(&sa, 0, sizeof(sa));
187
sa.sa_handler = sig_term;
188
sigaction(SIGINT, &sa, NULL);
189
sigaction(SIGTERM, &sa, NULL);
191
g_main_loop_run(main_loop);
193
g_timer_destroy(timer);
195
g_resolv_unref(resolv);
197
g_main_loop_unref(main_loop);