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
29 #include <gresolv/gresolv.h>
34 struct connman_service *service;
39 static GHashTable *wpad_list = NULL;
41 static void resolv_debug(const char *str, void *data)
43 connman_info("%s: %s\n", (const char *) data, str);
46 static void free_wpad(gpointer data)
48 struct connman_wpad *wpad = data;
50 g_resolv_unref(wpad->resolv);
52 g_free(wpad->hostname);
56 static void wpad_result(GResolvResultStatus status,
57 char **results, gpointer user_data)
59 struct connman_wpad *wpad = user_data;
63 DBG("status %d", status);
65 if (status == G_RESOLV_RESULT_STATUS_SUCCESS) {
68 url = g_strdup_printf("http://%s/wpad.dat", wpad->hostname);
69 __connman_service_set_proxy_autoconfig(wpad->service, url);
75 hostname = wpad->hostname;
77 if (strlen(hostname) < 6)
80 ptr = strchr(hostname + 5, '.');
81 if (ptr == NULL || strlen(ptr) < 2)
84 if (strchr(ptr + 1, '.') == NULL)
87 wpad->hostname = g_strdup_printf("wpad.%s", ptr + 1);
90 DBG("hostname %s", wpad->hostname);
92 g_resolv_lookup_hostname(wpad->resolv, wpad->hostname,
96 void __connman_wpad_start(struct connman_service *service)
98 struct connman_wpad *wpad;
99 const char *domainname, *nameserver;
102 DBG("service %p", service);
104 if (wpad_list == NULL)
107 index = __connman_service_get_index(service);
111 domainname = __connman_service_get_domainname(service);
112 if (domainname == NULL)
115 nameserver = __connman_service_get_nameserver(service);
116 if (nameserver == NULL)
119 wpad = g_try_new0(struct connman_wpad, 1);
123 wpad->service = service;
124 wpad->resolv = g_resolv_new(index);
125 if (wpad->resolv == NULL) {
130 if (getenv("CONNMAN_RESOLV_DEBUG"))
131 g_resolv_set_debug(wpad->resolv, resolv_debug, "RESOLV");
133 g_resolv_add_nameserver(wpad->resolv, nameserver, 53, 0);
135 wpad->hostname = g_strdup_printf("wpad.%s", domainname);
137 DBG("hostname %s", wpad->hostname);
139 g_resolv_lookup_hostname(wpad->resolv, wpad->hostname,
142 g_hash_table_replace(wpad_list, GINT_TO_POINTER(index), wpad);
145 void __connman_wpad_stop(struct connman_service *service)
149 DBG("service %p", service);
151 if (wpad_list == NULL)
154 index = __connman_service_get_index(service);
158 g_hash_table_remove(wpad_list, GINT_TO_POINTER(index));
161 int __connman_wpad_init(void)
165 wpad_list = g_hash_table_new_full(g_direct_hash, g_direct_equal,
171 void __connman_wpad_cleanup(void)
175 g_hash_table_destroy(wpad_list);