Add support for retrieving proxy auto-configuration via DNS
[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
28 #include <gresolv/gresolv.h>
29
30 #include "connman.h"
31
32 struct connman_wpad {
33         struct connman_service *service;
34         GResolv *resolv;
35         char *hostname;
36 };
37
38 static GHashTable *wpad_list = NULL;
39
40 static void resolv_debug(const char *str, void *data)
41 {
42         connman_info("%s: %s\n", (const char *) data, str);
43 }
44
45 static void free_wpad(gpointer data)
46 {
47         struct connman_wpad *wpad = data;
48
49         g_resolv_unref(wpad->resolv);
50
51         g_free(wpad->hostname);
52         g_free(wpad);
53 }
54
55 static void wpad_result(GResolvResultStatus status,
56                                         char **results, gpointer user_data)
57 {
58         struct connman_wpad *wpad = user_data;
59
60         DBG("status %d", status);
61
62         if (status == G_RESOLV_RESULT_STATUS_SUCCESS)
63                 connman_info("PAC: http://%s/wpad.dat", wpad->hostname);
64 }
65
66 void __connman_wpad_start(struct connman_service *service)
67 {
68         struct connman_wpad *wpad;
69         const char *domainname, *nameserver;
70         int index;
71
72         DBG("service %p", service);
73
74         if (wpad_list == NULL)
75                 return;
76
77         index = __connman_service_get_index(service);
78         if (index < 0)
79                 return;
80
81         domainname = __connman_service_get_domainname(service);
82         if (domainname == NULL)
83                 return;
84
85         nameserver = __connman_service_get_nameserver(service);
86         if (nameserver == NULL)
87                 return;
88
89         wpad = g_try_new0(struct connman_wpad, 1);
90         if (wpad == NULL)
91                 return;
92
93         wpad->service = service;
94         wpad->resolv = g_resolv_new(index);
95         if (wpad->resolv == NULL) {
96                 g_free(wpad);
97                 return;
98         }
99
100         if (getenv("CONNMAN_RESOLV_DEBUG"))
101                 g_resolv_set_debug(wpad->resolv, resolv_debug, "RESOLV");
102
103         g_resolv_add_nameserver(wpad->resolv, nameserver, 53, 0);
104
105         wpad->hostname = g_strdup_printf("wpad.%s", domainname);
106
107         g_resolv_lookup_hostname(wpad->resolv, wpad->hostname,
108                                                         wpad_result, wpad);
109
110         g_hash_table_insert(wpad_list, GINT_TO_POINTER(index), wpad);
111 }
112
113 void __connman_wpad_stop(struct connman_service *service)
114 {
115         int index;
116
117         DBG("service %p", service);
118
119         if (wpad_list == NULL)
120                 return;
121
122         index = __connman_service_get_index(service);
123         if (index < 0)
124                 return;
125
126         g_hash_table_remove(wpad_list, GINT_TO_POINTER(index));
127 }
128
129 int __connman_wpad_init(void)
130 {
131         DBG("");
132
133         wpad_list = g_hash_table_new_full(g_direct_hash, g_direct_equal,
134                                                         NULL, free_wpad);
135
136         return 0;
137 }
138
139 void __connman_wpad_cleanup(void)
140 {
141         DBG("");
142
143         g_hash_table_destroy(wpad_list);
144         wpad_list = NULL;
145 }