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 <gweb/gresolv.h>
34 struct connman_service *service;
40 static GHashTable *wpad_list = NULL;
42 static void resolv_debug(const char *str, void *data)
44 connman_info("%s: %s\n", (const char *) data, str);
47 static void free_wpad(gpointer data)
49 struct connman_wpad *wpad = data;
51 g_resolv_unref(wpad->resolv);
53 g_strfreev(wpad->addrlist);
54 g_free(wpad->hostname);
58 static void download_pac(struct connman_wpad *wpad, const char *target)
62 static void wpad_result(GResolvResultStatus status,
63 char **results, gpointer user_data)
65 struct connman_wpad *wpad = user_data;
69 DBG("status %d", status);
71 if (status == G_RESOLV_RESULT_STATUS_SUCCESS) {
74 if (results == NULL || g_strv_length(results) == 0)
77 url = g_strdup_printf("http://%s/wpad.dat", wpad->hostname);
79 __connman_service_set_proxy_autoconfig(wpad->service, url);
81 wpad->addrlist = g_strdupv(results);
82 if (wpad->addrlist != NULL)
83 download_pac(wpad, "wpad.dat");
90 hostname = wpad->hostname;
92 if (strlen(hostname) < 6)
95 ptr = strchr(hostname + 5, '.');
96 if (ptr == NULL || strlen(ptr) < 2)
99 if (strchr(ptr + 1, '.') == NULL)
102 wpad->hostname = g_strdup_printf("wpad.%s", ptr + 1);
105 DBG("hostname %s", wpad->hostname);
107 g_resolv_lookup_hostname(wpad->resolv, wpad->hostname,
113 connman_service_set_proxy_method(wpad->service,
114 CONNMAN_SERVICE_PROXY_METHOD_DIRECT);
117 int __connman_wpad_start(struct connman_service *service)
119 struct connman_wpad *wpad;
120 const char *domainname;
125 DBG("service %p", service);
127 if (wpad_list == NULL)
130 index = __connman_service_get_index(service);
134 domainname = connman_service_get_domainname(service);
135 if (domainname == NULL)
138 nameservers = connman_service_get_nameservers(service);
139 if (nameservers == NULL)
142 wpad = g_try_new0(struct connman_wpad, 1);
146 wpad->service = service;
147 wpad->resolv = g_resolv_new(index);
148 if (wpad->resolv == NULL) {
153 if (getenv("CONNMAN_RESOLV_DEBUG"))
154 g_resolv_set_debug(wpad->resolv, resolv_debug, "RESOLV");
156 for (i = 0; nameservers[i] != NULL; i++)
157 g_resolv_add_nameserver(wpad->resolv, nameservers[i], 53, 0);
159 wpad->hostname = g_strdup_printf("wpad.%s", domainname);
161 DBG("hostname %s", wpad->hostname);
163 g_resolv_lookup_hostname(wpad->resolv, wpad->hostname,
166 g_hash_table_replace(wpad_list, GINT_TO_POINTER(index), wpad);
171 void __connman_wpad_stop(struct connman_service *service)
175 DBG("service %p", service);
177 if (wpad_list == NULL)
180 index = __connman_service_get_index(service);
184 g_hash_table_remove(wpad_list, GINT_TO_POINTER(index));
187 int __connman_wpad_init(void)
191 wpad_list = g_hash_table_new_full(g_direct_hash, g_direct_equal,
197 void __connman_wpad_cleanup(void)
201 g_hash_table_destroy(wpad_list);