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
29 #include <gweb/gweb.h>
33 #define STATUS_URL "http://www.connman.net/online/status.html"
35 struct connman_wispr_message {
37 const char *current_element;
41 char *abort_login_url;
43 char *access_procedure;
44 char *access_location;
48 struct connman_wispr_portal_context {
49 struct connman_service *service;
50 enum connman_ipconfig_type type;
52 /* Portal/WISPr common */
58 GWebParser *wispr_parser;
59 struct connman_wispr_message wispr_msg;
62 struct connman_wispr_portal {
63 struct connman_wispr_portal_context *ipv4_context;
64 struct connman_wispr_portal_context *ipv6_context;
67 static GHashTable *wispr_portal_list = NULL;
69 static void connman_wispr_message_init(struct connman_wispr_message *msg)
73 msg->has_error = FALSE;
74 msg->current_element = NULL;
76 msg->message_type = -1;
77 msg->response_code = -1;
79 g_free(msg->login_url);
80 msg->login_url = NULL;
82 g_free(msg->abort_login_url);
83 msg->abort_login_url = NULL;
85 g_free(msg->logoff_url);
86 msg->logoff_url = NULL;
88 g_free(msg->access_procedure);
89 msg->access_procedure = NULL;
91 g_free(msg->access_location);
92 msg->access_location = NULL;
94 g_free(msg->location_name);
95 msg->location_name = NULL;
98 static void free_connman_wispr_portal_context(struct connman_wispr_portal_context *wp_context)
102 if (wp_context == NULL)
105 if (wp_context->token > 0)
106 connman_proxy_lookup_cancel(wp_context->token);
108 if (wp_context->request_id > 0)
109 g_web_cancel_request(wp_context->web, wp_context->request_id);
111 g_web_unref(wp_context->web);
113 g_web_parser_unref(wp_context->wispr_parser);
114 connman_wispr_message_init(&wp_context->wispr_msg);
119 static void free_connman_wispr_portal(gpointer data)
121 struct connman_wispr_portal *wispr_portal = data;
125 if (wispr_portal == NULL)
128 free_connman_wispr_portal_context(wispr_portal->ipv4_context);
129 free_connman_wispr_portal_context(wispr_portal->ipv6_context);
131 g_free(wispr_portal);
137 WISPR_ELEMENT_NONE = 0,
138 WISPR_ELEMENT_ACCESS_PROCEDURE = 1,
139 WISPR_ELEMENT_ACCESS_LOCATION = 2,
140 WISPR_ELEMENT_LOCATION_NAME = 3,
141 WISPR_ELEMENT_LOGIN_URL = 4,
142 WISPR_ELEMENT_ABORT_LOGIN_URL = 5,
143 WISPR_ELEMENT_MESSAGE_TYPE = 6,
144 WISPR_ELEMENT_RESPONSE_CODE = 7,
145 WISPR_ELEMENT_NEXT_URL = 8,
146 WISPR_ELEMENT_DELAY = 9,
147 WISPR_ELEMENT_REPLY_MESSAGE = 10,
148 WISPR_ELEMENT_LOGIN_RESULTS_URL = 11,
149 WISPR_ELEMENT_LOGOFF_URL = 12,
151 } wispr_element_map[] = {
152 { "AccessProcedure", WISPR_ELEMENT_ACCESS_PROCEDURE },
153 { "AccessLocation", WISPR_ELEMENT_ACCESS_LOCATION },
154 { "LocationName", WISPR_ELEMENT_LOCATION_NAME },
155 { "LoginURL", WISPR_ELEMENT_LOGIN_URL },
156 { "AbortLoginURL", WISPR_ELEMENT_ABORT_LOGIN_URL },
157 { "MessageType", WISPR_ELEMENT_MESSAGE_TYPE },
158 { "ResponseCode", WISPR_ELEMENT_RESPONSE_CODE },
159 { "NextURL", WISPR_ELEMENT_NEXT_URL },
160 { "Delay", WISPR_ELEMENT_DELAY },
161 { "ReplyMessage", WISPR_ELEMENT_REPLY_MESSAGE },
162 { "LoginResultsURL", WISPR_ELEMENT_LOGIN_RESULTS_URL },
163 { "LogoffURL", WISPR_ELEMENT_LOGOFF_URL },
164 { NULL, WISPR_ELEMENT_NONE },
167 static void xml_wispr_start_element_handler(GMarkupParseContext *context,
168 const gchar *element_name,
169 const gchar **attribute_names,
170 const gchar **attribute_values,
171 gpointer user_data, GError **error)
173 struct connman_wispr_message *msg = user_data;
175 msg->current_element = element_name;
178 static void xml_wispr_end_element_handler(GMarkupParseContext *context,
179 const gchar *element_name,
180 gpointer user_data, GError **error)
182 struct connman_wispr_message *msg = user_data;
184 msg->current_element = NULL;
187 static void xml_wispr_text_handler(GMarkupParseContext *context,
188 const gchar *text, gsize text_len,
189 gpointer user_data, GError **error)
191 struct connman_wispr_message *msg = user_data;
194 if (msg->current_element == NULL)
197 for (i = 0; wispr_element_map[i].str; i++) {
198 if (g_str_equal(wispr_element_map[i].str,
199 msg->current_element) == FALSE)
202 switch (wispr_element_map[i].element) {
203 case WISPR_ELEMENT_NONE:
204 case WISPR_ELEMENT_ACCESS_PROCEDURE:
205 g_free(msg->access_procedure);
206 msg->access_procedure = g_strdup(text);
208 case WISPR_ELEMENT_ACCESS_LOCATION:
209 g_free(msg->access_location);
210 msg->access_location = g_strdup(text);
212 case WISPR_ELEMENT_LOCATION_NAME:
213 g_free(msg->location_name);
214 msg->location_name = g_strdup(text);
216 case WISPR_ELEMENT_LOGIN_URL:
217 g_free(msg->login_url);
218 msg->login_url = g_strdup(text);
220 case WISPR_ELEMENT_ABORT_LOGIN_URL:
221 g_free(msg->abort_login_url);
222 msg->abort_login_url = g_strdup(text);
224 case WISPR_ELEMENT_MESSAGE_TYPE:
225 msg->message_type = atoi(text);
227 case WISPR_ELEMENT_RESPONSE_CODE:
228 msg->response_code = atoi(text);
230 case WISPR_ELEMENT_NEXT_URL:
231 case WISPR_ELEMENT_DELAY:
232 case WISPR_ELEMENT_REPLY_MESSAGE:
233 case WISPR_ELEMENT_LOGIN_RESULTS_URL:
235 case WISPR_ELEMENT_LOGOFF_URL:
236 g_free(msg->logoff_url);
237 msg->logoff_url = g_strdup(text);
243 static void xml_wispr_error_handler(GMarkupParseContext *context,
244 GError *error, gpointer user_data)
246 struct connman_wispr_message *msg = user_data;
248 msg->has_error = TRUE;
251 static const GMarkupParser xml_wispr_parser_handlers = {
252 xml_wispr_start_element_handler,
253 xml_wispr_end_element_handler,
254 xml_wispr_text_handler,
256 xml_wispr_error_handler,
259 static void xml_wispr_parser_callback(const char *str, gpointer user_data)
261 struct connman_wispr_portal_context *wp_context = user_data;
262 GMarkupParseContext *parser_context = NULL;
267 parser_context = g_markup_parse_context_new(&xml_wispr_parser_handlers,
268 G_MARKUP_TREAT_CDATA_AS_TEXT,
269 &(wp_context->wispr_msg), NULL);
271 result = g_markup_parse_context_parse(parser_context,
272 str, strlen(str), NULL);
274 result = g_markup_parse_context_end_parse(parser_context, NULL);
276 g_markup_parse_context_free(parser_context);
279 static void web_debug(const char *str, void *data)
281 connman_info("%s: %s\n", (const char *) data, str);
284 static void wispr_portal_error(struct connman_wispr_portal_context *wp_context)
286 DBG("Failed to proceed wispr/portal web request");
289 static void portal_manage_status(GWebResult *result,
290 struct connman_wispr_portal_context *wp_context)
292 const char *str = NULL;
296 /* We currently don't do anything with this info */
297 if (g_web_result_get_header(result, "X-ConnMan-Client-IP",
299 connman_info("Client-IP: %s", str);
301 if (g_web_result_get_header(result, "X-ConnMan-Client-Country",
303 connman_info("Client-Country: %s", str);
305 if (g_web_result_get_header(result, "X-ConnMan-Client-Region",
307 connman_info("Client-Region: %s", str);
309 __connman_service_ipconfig_indicate_state(wp_context->service,
310 CONNMAN_SERVICE_STATE_ONLINE,
314 static gboolean wispr_portal_web_result(GWebResult *result, gpointer user_data)
316 struct connman_wispr_portal_context *wp_context = user_data;
317 const char *redirect = NULL;
318 const guint8 *chunk = NULL;
319 const char *str = NULL;
325 if (wp_context->request_id == 0)
328 g_web_result_get_chunk(result, &chunk, &length);
331 g_web_parser_feed_data(wp_context->wispr_parser,
336 g_web_parser_end_data(wp_context->wispr_parser);
338 status = g_web_result_get_status(result);
340 DBG("status: %03u", status);
344 if (wp_context->wispr_msg.message_type >= 0)
347 if (g_web_result_get_header(result, "X-ConnMan-Status",
349 portal_manage_status(result, wp_context);
353 if (g_web_result_get_header(result, "Location",
357 DBG("Redirect URL: %s", redirect);
359 wp_context->request_id = g_web_request_get(wp_context->web,
360 redirect, wispr_portal_web_result, wp_context);
364 wispr_portal_error(wp_context);
371 wp_context->request_id = 0;
373 wp_context->wispr_msg.message_type = -1;
377 static void wispr_portal_request_portal(struct connman_wispr_portal_context *wp_context)
381 wp_context->request_id = g_web_request_get(wp_context->web,
382 STATUS_URL, wispr_portal_web_result, wp_context);
384 if (wp_context->request_id == 0)
385 wispr_portal_error(wp_context);
388 static void proxy_callback(const char *proxy, void *user_data)
390 struct connman_wispr_portal_context *wp_context = user_data;
392 DBG("proxy %s", proxy);
394 wp_context->token = 0;
397 proxy = getenv("http_proxy");
399 if (getenv("CONNMAN_WEB_DEBUG"))
400 g_web_set_debug(wp_context->web, web_debug, "WEB");
402 if (proxy != NULL && g_strcmp0(proxy, "DIRECT") != 0)
403 g_web_set_proxy(wp_context->web, proxy);
405 g_web_set_accept(wp_context->web, NULL);
406 g_web_set_user_agent(wp_context->web, "ConnMan/%s", VERSION);
407 g_web_set_close_connection(wp_context->web, TRUE);
409 connman_wispr_message_init(&wp_context->wispr_msg);
411 wp_context->wispr_parser = g_web_parser_new(
412 "<WISPAccessGatewayParam",
413 "WISPAccessGatewayParam>",
414 xml_wispr_parser_callback, wp_context);
416 wispr_portal_request_portal(wp_context);
419 static int wispr_portal_detect(struct connman_wispr_portal_context *wp_context)
421 enum connman_service_type service_type;
422 char *interface = NULL;
425 DBG("wispr/portal context %p", wp_context);
426 DBG("service %p", wp_context->service);
428 service_type = connman_service_get_type(wp_context->service);
430 switch (service_type) {
431 case CONNMAN_SERVICE_TYPE_ETHERNET:
432 case CONNMAN_SERVICE_TYPE_WIFI:
433 case CONNMAN_SERVICE_TYPE_WIMAX:
434 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
435 case CONNMAN_SERVICE_TYPE_CELLULAR:
437 case CONNMAN_SERVICE_TYPE_UNKNOWN:
438 case CONNMAN_SERVICE_TYPE_SYSTEM:
439 case CONNMAN_SERVICE_TYPE_GPS:
440 case CONNMAN_SERVICE_TYPE_VPN:
441 case CONNMAN_SERVICE_TYPE_GADGET:
445 interface = connman_service_get_interface(wp_context->service);
446 if (interface == NULL)
449 DBG("interface %s", interface);
451 wp_context->web = g_web_new(0);
452 if (wp_context->web == NULL) {
457 if (wp_context->type == CONNMAN_IPCONFIG_TYPE_IPV4)
458 g_web_set_address_family(wp_context->web, AF_INET);
460 g_web_set_address_family(wp_context->web, AF_INET6);
462 wp_context->token = connman_proxy_lookup(interface,
463 STATUS_URL, wp_context->service,
464 proxy_callback, wp_context);
465 if (wp_context->token == 0)
473 int __connman_wispr_start(struct connman_service *service,
474 enum connman_ipconfig_type type)
476 struct connman_wispr_portal_context *wp_context = NULL;
477 struct connman_wispr_portal *wispr_portal = NULL;
480 DBG("service %p", service);
482 if (wispr_portal_list == NULL)
485 index = __connman_service_get_index(service);
489 wispr_portal = g_hash_table_lookup(wispr_portal_list,
490 GINT_TO_POINTER(index));
491 if (wispr_portal == NULL) {
492 wispr_portal = g_try_new0(struct connman_wispr_portal, 1);
493 if (wispr_portal == NULL)
496 g_hash_table_replace(wispr_portal_list,
497 GINT_TO_POINTER(index), wispr_portal);
500 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
501 wp_context = wispr_portal->ipv4_context;
502 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
503 wp_context = wispr_portal->ipv6_context;
507 if (wp_context == NULL) {
508 wp_context = g_try_new0(struct connman_wispr_portal_context, 1);
509 if (wp_context == NULL)
512 wp_context->service = service;
513 wp_context->type = type;
515 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
516 wispr_portal->ipv4_context = wp_context;
518 wispr_portal->ipv6_context = wp_context;
520 return wispr_portal_detect(wp_context);
526 void __connman_wispr_stop(struct connman_service *service)
530 DBG("service %p", service);
532 if (wispr_portal_list == NULL)
535 index = __connman_service_get_index(service);
539 g_hash_table_remove(wispr_portal_list, GINT_TO_POINTER(index));
542 int __connman_wispr_init(void)
546 wispr_portal_list = g_hash_table_new_full(g_direct_hash,
547 g_direct_equal, NULL,
548 free_connman_wispr_portal);
553 void __connman_wispr_cleanup(void)
557 g_hash_table_destroy(wispr_portal_list);
558 wispr_portal_list = NULL;