Check also top-level DNS records for WPAD information
[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 <gresolv/gresolv.h>
30
31 #include "connman.h"
32
33 struct connman_wpad {
34         struct connman_service *service;
35         GResolv *resolv;
36         char *hostname;
37 };
38
39 static GHashTable *wpad_list = NULL;
40
41 static void resolv_debug(const char *str, void *data)
42 {
43         connman_info("%s: %s\n", (const char *) data, str);
44 }
45
46 static void free_wpad(gpointer data)
47 {
48         struct connman_wpad *wpad = data;
49
50         g_resolv_unref(wpad->resolv);
51
52         g_free(wpad->hostname);
53         g_free(wpad);
54 }
55
56 static void wpad_result(GResolvResultStatus status,
57                                         char **results, gpointer user_data)
58 {
59         struct connman_wpad *wpad = user_data;
60         const char *ptr;
61         char *hostname;
62
63         DBG("status %d", status);
64
65         if (status == G_RESOLV_RESULT_STATUS_SUCCESS) {
66                 char *url;
67
68                 url = g_strdup_printf("http://%s/wpad.dat", wpad->hostname);
69                 __connman_service_set_proxy_autoconfig(wpad->service, url);
70                 g_free(url);
71
72                 return;
73         }
74
75         hostname = wpad->hostname;
76
77         if (strlen(hostname) < 6)
78                 return;
79
80         ptr = strchr(hostname + 5, '.');
81         if (ptr == NULL || strlen(ptr) < 2)
82                 return;
83
84         if (strchr(ptr + 1, '.') == NULL)
85                 return;
86
87         wpad->hostname = g_strdup_printf("wpad.%s", ptr + 1);
88         g_free(hostname);
89
90         DBG("hostname %s", wpad->hostname);
91
92         g_resolv_lookup_hostname(wpad->resolv, wpad->hostname,
93                                                         wpad_result, wpad);
94 }
95
96 void __connman_wpad_start(struct connman_service *service)
97 {
98         struct connman_wpad *wpad;
99         const char *domainname, *nameserver;
100         int index;
101
102         DBG("service %p", service);
103
104         if (wpad_list == NULL)
105                 return;
106
107         index = __connman_service_get_index(service);
108         if (index < 0)
109                 return;
110
111         domainname = __connman_service_get_domainname(service);
112         if (domainname == NULL)
113                 return;
114
115         nameserver = __connman_service_get_nameserver(service);
116         if (nameserver == NULL)
117                 return;
118
119         wpad = g_try_new0(struct connman_wpad, 1);
120         if (wpad == NULL)
121                 return;
122
123         wpad->service = service;
124         wpad->resolv = g_resolv_new(index);
125         if (wpad->resolv == NULL) {
126                 g_free(wpad);
127                 return;
128         }
129
130         if (getenv("CONNMAN_RESOLV_DEBUG"))
131                 g_resolv_set_debug(wpad->resolv, resolv_debug, "RESOLV");
132
133         g_resolv_add_nameserver(wpad->resolv, nameserver, 53, 0);
134
135         wpad->hostname = g_strdup_printf("wpad.%s", domainname);
136
137         DBG("hostname %s", wpad->hostname);
138
139         g_resolv_lookup_hostname(wpad->resolv, wpad->hostname,
140                                                         wpad_result, wpad);
141
142         g_hash_table_replace(wpad_list, GINT_TO_POINTER(index), wpad);
143 }
144
145 void __connman_wpad_stop(struct connman_service *service)
146 {
147         int index;
148
149         DBG("service %p", service);
150
151         if (wpad_list == NULL)
152                 return;
153
154         index = __connman_service_get_index(service);
155         if (index < 0)
156                 return;
157
158         g_hash_table_remove(wpad_list, GINT_TO_POINTER(index));
159 }
160
161 int __connman_wpad_init(void)
162 {
163         DBG("");
164
165         wpad_list = g_hash_table_new_full(g_direct_hash, g_direct_equal,
166                                                         NULL, free_wpad);
167
168         return 0;
169 }
170
171 void __connman_wpad_cleanup(void)
172 {
173         DBG("");
174
175         g_hash_table_destroy(wpad_list);
176         wpad_list = NULL;
177 }