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"
46 static void web_debug(const char *str, void *data)
48 connman_info("%s: %s\n", (const char *) data, str);
51 static gboolean web_result(GWebResult *result, gpointer user_data)
53 struct connman_location *location = user_data;
54 struct server_data *data = connman_location_get_data(location);
57 if (data->request_id == 0)
60 status = g_web_result_get_status(result);
62 DBG("status %u", status);
66 connman_location_report_result(location,
67 CONNMAN_LOCATION_RESULT_ONLINE);
70 connman_location_report_result(location,
71 CONNMAN_LOCATION_RESULT_PORTAL);
74 connman_location_report_result(location,
75 CONNMAN_LOCATION_RESULT_UNKNOWN);
84 static void proxy_callback(const char *proxy, void *user_data)
86 struct connman_location *location = user_data;
87 struct server_data *data = connman_location_get_data(location);
89 DBG("proxy %s", proxy);
92 proxy = getenv("http_proxy");
95 g_web_set_proxy(data->web, proxy);
97 data->request_id = g_web_request_get(data->web, STATUS_URL,
98 web_result, location);
101 connman_location_unref(location);
104 static int location_detect(struct connman_location *location)
106 struct server_data *data;
107 enum connman_service_type service_type;
108 const char *interface;
111 DBG("location %p", location);
113 service_type = connman_location_get_type(location);
115 switch (service_type) {
116 case CONNMAN_SERVICE_TYPE_ETHERNET:
117 case CONNMAN_SERVICE_TYPE_WIFI:
118 case CONNMAN_SERVICE_TYPE_WIMAX:
119 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
120 case CONNMAN_SERVICE_TYPE_CELLULAR:
122 case CONNMAN_SERVICE_TYPE_UNKNOWN:
123 case CONNMAN_SERVICE_TYPE_SYSTEM:
124 case CONNMAN_SERVICE_TYPE_GPS:
125 case CONNMAN_SERVICE_TYPE_VPN:
126 case CONNMAN_SERVICE_TYPE_GADGET:
130 interface = connman_location_get_interface(location);
131 if (interface == NULL)
134 DBG("interface %s", interface);
136 data = g_try_new0(struct server_data, 1);
140 connman_location_set_data(location, data);
142 data->web = g_web_new(0);
143 if (data->web == NULL) {
148 if (getenv("CONNMAN_WEB_DEBUG"))
149 g_web_set_debug(data->web, web_debug, "WEB");
151 g_web_set_accept(data->web, NULL);
152 g_web_set_user_agent(data->web, "ConnMan/%s", VERSION);
153 g_web_set_close_connection(data->web, TRUE);
155 err = connman_proxy_lookup(interface, STATUS_URL,
156 proxy_callback, location);
160 connman_location_ref(location);
165 static int location_finish(struct connman_location *location)
167 struct server_data *data = connman_location_get_data(location);
169 DBG("location %p", location);
171 connman_location_set_data(location, NULL);
173 if (data->request_id > 0)
174 g_web_cancel_request(data->web, data->request_id);
176 g_web_unref(data->web);
183 static struct connman_location_driver location = {
185 .type = CONNMAN_SERVICE_TYPE_WIFI,
186 .priority = CONNMAN_LOCATION_PRIORITY_HIGH,
187 .detect = location_detect,
188 .finish = location_finish,
191 static int portal_init(void)
193 return connman_location_driver_register(&location);
196 static void portal_exit(void)
198 connman_location_driver_unregister(&location);
201 CONNMAN_PLUGIN_DEFINE(portal, "Portal detection plugin", VERSION,
202 CONNMAN_PLUGIN_PRIORITY_DEFAULT, portal_init, portal_exit)