5 * Copyright (C) 2007-2010 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
31 #define CONNMAN_API_SUBJECT_TO_CHANGE
32 #include <connman/plugin.h>
33 #include <connman/location.h>
34 #include <connman/proxy.h>
35 #include <connman/log.h>
37 #include "gweb/gweb.h"
39 #define STATUS_URL "http://www.connman.net/online/status.html"
47 static void web_debug(const char *str, void *data)
49 connman_info("%s: %s\n", (const char *) data, str);
52 static gboolean web_result(GWebResult *result, gpointer user_data)
54 struct connman_location *location = user_data;
55 struct server_data *data = connman_location_get_data(location);
59 if (data->request_id == 0)
62 status = g_web_result_get_status(result);
64 /* If status header is not available, it is a portal */
65 if (g_web_result_get_header(result, "X-ConnMan-Status", &str) == FALSE)
68 DBG("status %u", status);
72 if (g_web_result_get_header(result, "X-ConnMan-Client-IP",
74 connman_info("Client-IP: %s", str);
76 if (g_web_result_get_header(result, "X-ConnMan-Client-Country",
78 connman_info("Client-Country: %s", str);
80 connman_location_report_result(location,
81 CONNMAN_LOCATION_RESULT_ONLINE);
84 connman_location_report_result(location,
85 CONNMAN_LOCATION_RESULT_PORTAL);
88 connman_location_report_result(location,
89 CONNMAN_LOCATION_RESULT_UNKNOWN);
98 static void proxy_callback(const char *proxy, void *user_data)
100 struct connman_location *location = user_data;
101 struct server_data *data = connman_location_get_data(location);
103 DBG("proxy %s", proxy);
106 proxy = getenv("http_proxy");
109 if (proxy != NULL && g_strcmp0(proxy, "DIRECT") != 0)
110 g_web_set_proxy(data->web, proxy);
112 data->request_id = g_web_request_get(data->web, STATUS_URL,
113 web_result, location);
118 connman_location_unref(location);
121 static int location_detect(struct connman_location *location)
123 struct server_data *data;
124 struct connman_service *service;
125 enum connman_service_type service_type;
129 DBG("location %p", location);
131 service_type = connman_location_get_type(location);
133 switch (service_type) {
134 case CONNMAN_SERVICE_TYPE_ETHERNET:
135 case CONNMAN_SERVICE_TYPE_WIFI:
136 case CONNMAN_SERVICE_TYPE_WIMAX:
137 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
138 case CONNMAN_SERVICE_TYPE_CELLULAR:
140 case CONNMAN_SERVICE_TYPE_UNKNOWN:
141 case CONNMAN_SERVICE_TYPE_SYSTEM:
142 case CONNMAN_SERVICE_TYPE_GPS:
143 case CONNMAN_SERVICE_TYPE_VPN:
144 case CONNMAN_SERVICE_TYPE_GADGET:
148 interface = connman_location_get_interface(location);
149 if (interface == NULL)
152 DBG("interface %s", interface);
154 data = g_try_new0(struct server_data, 1);
160 connman_location_set_data(location, data);
162 data->web = g_web_new(0);
163 if (data->web == NULL) {
169 if (getenv("CONNMAN_WEB_DEBUG"))
170 g_web_set_debug(data->web, web_debug, "WEB");
172 g_web_set_accept(data->web, NULL);
173 g_web_set_user_agent(data->web, "ConnMan/%s", VERSION);
174 g_web_set_close_connection(data->web, TRUE);
176 connman_location_ref(location);
178 service = connman_location_get_service(location);
179 data->token = connman_proxy_lookup(interface, STATUS_URL,
180 service, proxy_callback, location);
182 if (data->token == 0) {
183 connman_location_unref(location);
193 static int location_finish(struct connman_location *location)
195 struct server_data *data = connman_location_get_data(location);
197 DBG("location %p", location);
199 connman_location_set_data(location, NULL);
201 if (data->request_id > 0)
202 g_web_cancel_request(data->web, data->request_id);
204 if (data->token > 0) {
205 connman_proxy_lookup_cancel(data->token);
206 connman_location_unref(location);
209 g_web_unref(data->web);
216 static struct connman_location_driver location = {
218 .type = CONNMAN_SERVICE_TYPE_WIFI,
219 .priority = CONNMAN_LOCATION_PRIORITY_HIGH,
220 .detect = location_detect,
221 .finish = location_finish,
224 static int portal_init(void)
226 return connman_location_driver_register(&location);
229 static void portal_exit(void)
231 connman_location_driver_unregister(&location);
234 CONNMAN_PLUGIN_DEFINE(portal, "Portal detection plugin", VERSION,
235 CONNMAN_PLUGIN_PRIORITY_DEFAULT, portal_init, portal_exit)