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