5 * Copyright (C) 2007-2012 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
30 #include <gweb/gresolv.h>
35 struct connman_service *service;
41 static GHashTable *wpad_list = NULL;
43 static void resolv_debug(const char *str, void *data)
45 connman_info("%s: %s\n", (const char *) data, str);
48 static void free_wpad(gpointer data)
50 struct connman_wpad *wpad = data;
52 connman_service_unref(wpad->service);
54 g_resolv_unref(wpad->resolv);
56 g_strfreev(wpad->addrlist);
57 g_free(wpad->hostname);
61 static void download_pac(struct connman_wpad *wpad, const char *target)
65 static void wpad_result(GResolvResultStatus status,
66 char **results, gpointer user_data)
68 struct connman_wpad *wpad = user_data;
72 DBG("status %d", status);
74 if (status == G_RESOLV_RESULT_STATUS_SUCCESS) {
77 if (!results || g_strv_length(results) == 0)
80 url = g_strdup_printf("http://%s/wpad.dat", wpad->hostname);
82 __connman_service_set_proxy_autoconfig(wpad->service, url);
84 wpad->addrlist = g_strdupv(results);
86 download_pac(wpad, "wpad.dat");
90 __connman_service_wispr_start(wpad->service,
91 CONNMAN_IPCONFIG_TYPE_IPV4);
96 hostname = wpad->hostname;
98 if (strlen(hostname) < 6)
101 ptr = strchr(hostname + 5, '.');
102 if (!ptr || strlen(ptr) < 2)
105 if (!strchr(ptr + 1, '.'))
108 wpad->hostname = g_strdup_printf("wpad.%s", ptr + 1);
111 DBG("hostname %s", wpad->hostname);
113 g_resolv_lookup_hostname(wpad->resolv, wpad->hostname,
119 connman_service_set_proxy_method(wpad->service,
120 CONNMAN_SERVICE_PROXY_METHOD_DIRECT);
122 __connman_service_wispr_start(wpad->service,
123 CONNMAN_IPCONFIG_TYPE_IPV4);
126 int __connman_wpad_start(struct connman_service *service)
128 struct connman_wpad *wpad;
129 const char *domainname;
134 DBG("service %p", service);
139 index = __connman_service_get_index(service);
143 domainname = connman_service_get_domainname(service);
147 nameservers = connman_service_get_nameservers(service);
151 wpad = g_try_new0(struct connman_wpad, 1);
153 g_strfreev(nameservers);
157 wpad->resolv = g_resolv_new(index);
159 g_strfreev(nameservers);
164 #if !defined TIZEN_EXT
165 if (getenv("CONNMAN_RESOLV_DEBUG"))
167 g_resolv_set_debug(wpad->resolv, resolv_debug, "RESOLV");
169 for (i = 0; nameservers[i]; i++)
170 g_resolv_add_nameserver(wpad->resolv, nameservers[i], 53, 0);
172 g_strfreev(nameservers);
174 wpad->hostname = g_strdup_printf("wpad.%s", domainname);
176 DBG("hostname %s", wpad->hostname);
178 wpad->service = connman_service_ref(service);
180 g_resolv_lookup_hostname(wpad->resolv, wpad->hostname,
183 g_hash_table_replace(wpad_list, GINT_TO_POINTER(index), wpad);
188 void __connman_wpad_stop(struct connman_service *service)
192 DBG("service %p", service);
197 index = __connman_service_get_index(service);
201 g_hash_table_remove(wpad_list, GINT_TO_POINTER(index));
204 int __connman_wpad_init(void)
208 wpad_list = g_hash_table_new_full(g_direct_hash, g_direct_equal,
214 void __connman_wpad_cleanup(void)
218 g_hash_table_destroy(wpad_list);