element: Remove element.c
[framework/connectivity/connman.git] / src / wpad.c
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 <errno.h>
27 #include <stdlib.h>
28 #include <string.h>
29
30 #include <gweb/gresolv.h>
31
32 #include "connman.h"
33
34 struct connman_wpad {
35         struct connman_service *service;
36         GResolv *resolv;
37         char *hostname;
38         char **addrlist;
39 };
40
41 static GHashTable *wpad_list = NULL;
42
43 static void resolv_debug(const char *str, void *data)
44 {
45         connman_info("%s: %s\n", (const char *) data, str);
46 }
47
48 static void free_wpad(gpointer data)
49 {
50         struct connman_wpad *wpad = data;
51
52         g_resolv_unref(wpad->resolv);
53
54         g_strfreev(wpad->addrlist);
55         g_free(wpad->hostname);
56         g_free(wpad);
57 }
58
59 static void download_pac(struct connman_wpad *wpad, const char *target)
60 {
61 }
62
63 static void wpad_result(GResolvResultStatus status,
64                                         char **results, gpointer user_data)
65 {
66         struct connman_wpad *wpad = user_data;
67         const char *ptr;
68         char *hostname;
69
70         DBG("status %d", status);
71
72         if (status == G_RESOLV_RESULT_STATUS_SUCCESS) {
73                 char *url;
74
75                 if (results == NULL || g_strv_length(results) == 0)
76                         goto failed;
77
78                 url = g_strdup_printf("http://%s/wpad.dat", wpad->hostname);
79
80                 __connman_service_set_proxy_autoconfig(wpad->service, url);
81
82                 wpad->addrlist = g_strdupv(results);
83                 if (wpad->addrlist != NULL)
84                         download_pac(wpad, "wpad.dat");
85
86                 g_free(url);
87
88                 return;
89         }
90
91         hostname = wpad->hostname;
92
93         if (strlen(hostname) < 6)
94                 goto failed;
95
96         ptr = strchr(hostname + 5, '.');
97         if (ptr == NULL || strlen(ptr) < 2)
98                 goto failed;
99
100         if (strchr(ptr + 1, '.') == NULL)
101                 goto failed;
102
103         wpad->hostname = g_strdup_printf("wpad.%s", ptr + 1);
104         g_free(hostname);
105
106         DBG("hostname %s", wpad->hostname);
107
108         g_resolv_lookup_hostname(wpad->resolv, wpad->hostname,
109                                                         wpad_result, wpad);
110
111         return;
112
113 failed:
114         connman_service_set_proxy_method(wpad->service,
115                                 CONNMAN_SERVICE_PROXY_METHOD_DIRECT);
116 }
117
118 int __connman_wpad_start(struct connman_service *service)
119 {
120         struct connman_wpad *wpad;
121         const char *domainname;
122         char **nameservers;
123         int index;
124         int i;
125
126         DBG("service %p", service);
127
128         if (wpad_list == NULL)
129                 return -EINVAL;
130
131         index = __connman_service_get_index(service);
132         if (index < 0)
133                 return -EINVAL;
134
135         domainname = connman_service_get_domainname(service);
136         if (domainname == NULL)
137                 return -EINVAL;
138
139         nameservers = connman_service_get_nameservers(service);
140         if (nameservers == NULL)
141                 return -EINVAL;
142
143         wpad = g_try_new0(struct connman_wpad, 1);
144         if (wpad == NULL)
145                 return -ENOMEM;
146
147         wpad->service = service;
148         wpad->resolv = g_resolv_new(index);
149         if (wpad->resolv == NULL) {
150                 g_free(wpad);
151                 return -ENOMEM;
152         }
153
154         if (getenv("CONNMAN_RESOLV_DEBUG"))
155                 g_resolv_set_debug(wpad->resolv, resolv_debug, "RESOLV");
156
157         for (i = 0; nameservers[i] != NULL; i++)
158                 g_resolv_add_nameserver(wpad->resolv, nameservers[i], 53, 0);
159
160         wpad->hostname = g_strdup_printf("wpad.%s", domainname);
161
162         DBG("hostname %s", wpad->hostname);
163
164         g_resolv_lookup_hostname(wpad->resolv, wpad->hostname,
165                                                         wpad_result, wpad);
166
167         g_hash_table_replace(wpad_list, GINT_TO_POINTER(index), wpad);
168
169         return 0;
170 }
171
172 void __connman_wpad_stop(struct connman_service *service)
173 {
174         int index;
175
176         DBG("service %p", service);
177
178         if (wpad_list == NULL)
179                 return;
180
181         index = __connman_service_get_index(service);
182         if (index < 0)
183                 return;
184
185         g_hash_table_remove(wpad_list, GINT_TO_POINTER(index));
186 }
187
188 int __connman_wpad_init(void)
189 {
190         DBG("");
191
192         wpad_list = g_hash_table_new_full(g_direct_hash, g_direct_equal,
193                                                         NULL, free_wpad);
194
195         return 0;
196 }
197
198 void __connman_wpad_cleanup(void)
199 {
200         DBG("");
201
202         g_hash_table_destroy(wpad_list);
203         wpad_list = NULL;
204 }