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_IPV4 "http://ipv4.connman.net/online/status.html"
34 #define STATUS_URL_IPV6 "http://ipv6.connman.net/online/status.html"
36 struct connman_wispr_message {
38 const char *current_element;
42 char *abort_login_url;
44 char *access_procedure;
45 char *access_location;
49 enum connman_wispr_result {
50 CONNMAN_WISPR_RESULT_UNKNOWN = 0,
51 CONNMAN_WISPR_RESULT_LOGIN = 1,
52 CONNMAN_WISPR_RESULT_ONLINE = 2,
53 CONNMAN_WISPR_RESULT_FAILED = 3,
56 struct connman_wispr_portal_context {
57 struct connman_service *service;
58 enum connman_ipconfig_type type;
60 /* Portal/WISPr common */
65 const char *status_url;
68 GWebParser *wispr_parser;
69 struct connman_wispr_message wispr_msg;
75 enum connman_wispr_result wispr_result;
78 struct connman_wispr_portal {
79 struct connman_wispr_portal_context *ipv4_context;
80 struct connman_wispr_portal_context *ipv6_context;
83 static gboolean wispr_portal_web_result(GWebResult *result, gpointer user_data);
85 static GHashTable *wispr_portal_list = NULL;
87 static void connman_wispr_message_init(struct connman_wispr_message *msg)
91 msg->has_error = FALSE;
92 msg->current_element = NULL;
94 msg->message_type = -1;
95 msg->response_code = -1;
97 g_free(msg->login_url);
98 msg->login_url = NULL;
100 g_free(msg->abort_login_url);
101 msg->abort_login_url = NULL;
103 g_free(msg->logoff_url);
104 msg->logoff_url = NULL;
106 g_free(msg->access_procedure);
107 msg->access_procedure = NULL;
109 g_free(msg->access_location);
110 msg->access_location = NULL;
112 g_free(msg->location_name);
113 msg->location_name = NULL;
116 static void free_connman_wispr_portal_context(struct connman_wispr_portal_context *wp_context)
120 if (wp_context == NULL)
123 connman_service_unref(wp_context->service);
125 if (wp_context->token > 0)
126 connman_proxy_lookup_cancel(wp_context->token);
128 if (wp_context->request_id > 0)
129 g_web_cancel_request(wp_context->web, wp_context->request_id);
131 g_web_unref(wp_context->web);
133 g_web_parser_unref(wp_context->wispr_parser);
134 connman_wispr_message_init(&wp_context->wispr_msg);
136 g_free(wp_context->wispr_username);
137 g_free(wp_context->wispr_password);
138 g_free(wp_context->wispr_formdata);
143 static void free_connman_wispr_portal(gpointer data)
145 struct connman_wispr_portal *wispr_portal = data;
149 if (wispr_portal == NULL)
152 free_connman_wispr_portal_context(wispr_portal->ipv4_context);
153 free_connman_wispr_portal_context(wispr_portal->ipv6_context);
155 g_free(wispr_portal);
158 static const char *message_type_to_string(int message_type)
160 switch (message_type) {
162 return "Initial redirect message";
164 return "Proxy notification";
166 return "Authentication notification";
168 return "Logoff notification";
170 return "Response to Authentication Poll";
172 return "Response to Abort Login";
178 static const char *response_code_to_string(int response_code)
180 switch (response_code) {
184 return "Login succeeded";
186 return "Login failed";
188 return "RADIUS server error/timeout";
190 return "RADIUS server not enabled";
192 return "Logoff succeeded";
194 return "Login aborted";
196 return "Proxy detection/repeat operation";
198 return "Authentication pending";
200 return "Access gateway internal error";
209 WISPR_ELEMENT_NONE = 0,
210 WISPR_ELEMENT_ACCESS_PROCEDURE = 1,
211 WISPR_ELEMENT_ACCESS_LOCATION = 2,
212 WISPR_ELEMENT_LOCATION_NAME = 3,
213 WISPR_ELEMENT_LOGIN_URL = 4,
214 WISPR_ELEMENT_ABORT_LOGIN_URL = 5,
215 WISPR_ELEMENT_MESSAGE_TYPE = 6,
216 WISPR_ELEMENT_RESPONSE_CODE = 7,
217 WISPR_ELEMENT_NEXT_URL = 8,
218 WISPR_ELEMENT_DELAY = 9,
219 WISPR_ELEMENT_REPLY_MESSAGE = 10,
220 WISPR_ELEMENT_LOGIN_RESULTS_URL = 11,
221 WISPR_ELEMENT_LOGOFF_URL = 12,
223 } wispr_element_map[] = {
224 { "AccessProcedure", WISPR_ELEMENT_ACCESS_PROCEDURE },
225 { "AccessLocation", WISPR_ELEMENT_ACCESS_LOCATION },
226 { "LocationName", WISPR_ELEMENT_LOCATION_NAME },
227 { "LoginURL", WISPR_ELEMENT_LOGIN_URL },
228 { "AbortLoginURL", WISPR_ELEMENT_ABORT_LOGIN_URL },
229 { "MessageType", WISPR_ELEMENT_MESSAGE_TYPE },
230 { "ResponseCode", WISPR_ELEMENT_RESPONSE_CODE },
231 { "NextURL", WISPR_ELEMENT_NEXT_URL },
232 { "Delay", WISPR_ELEMENT_DELAY },
233 { "ReplyMessage", WISPR_ELEMENT_REPLY_MESSAGE },
234 { "LoginResultsURL", WISPR_ELEMENT_LOGIN_RESULTS_URL },
235 { "LogoffURL", WISPR_ELEMENT_LOGOFF_URL },
236 { NULL, WISPR_ELEMENT_NONE },
239 static void xml_wispr_start_element_handler(GMarkupParseContext *context,
240 const gchar *element_name,
241 const gchar **attribute_names,
242 const gchar **attribute_values,
243 gpointer user_data, GError **error)
245 struct connman_wispr_message *msg = user_data;
247 msg->current_element = element_name;
250 static void xml_wispr_end_element_handler(GMarkupParseContext *context,
251 const gchar *element_name,
252 gpointer user_data, GError **error)
254 struct connman_wispr_message *msg = user_data;
256 msg->current_element = NULL;
259 static void xml_wispr_text_handler(GMarkupParseContext *context,
260 const gchar *text, gsize text_len,
261 gpointer user_data, GError **error)
263 struct connman_wispr_message *msg = user_data;
266 if (msg->current_element == NULL)
269 for (i = 0; wispr_element_map[i].str; i++) {
270 if (g_str_equal(wispr_element_map[i].str,
271 msg->current_element) == FALSE)
274 switch (wispr_element_map[i].element) {
275 case WISPR_ELEMENT_NONE:
276 case WISPR_ELEMENT_ACCESS_PROCEDURE:
277 g_free(msg->access_procedure);
278 msg->access_procedure = g_strdup(text);
280 case WISPR_ELEMENT_ACCESS_LOCATION:
281 g_free(msg->access_location);
282 msg->access_location = g_strdup(text);
284 case WISPR_ELEMENT_LOCATION_NAME:
285 g_free(msg->location_name);
286 msg->location_name = g_strdup(text);
288 case WISPR_ELEMENT_LOGIN_URL:
289 g_free(msg->login_url);
290 msg->login_url = g_strdup(text);
292 case WISPR_ELEMENT_ABORT_LOGIN_URL:
293 g_free(msg->abort_login_url);
294 msg->abort_login_url = g_strdup(text);
296 case WISPR_ELEMENT_MESSAGE_TYPE:
297 msg->message_type = atoi(text);
299 case WISPR_ELEMENT_RESPONSE_CODE:
300 msg->response_code = atoi(text);
302 case WISPR_ELEMENT_NEXT_URL:
303 case WISPR_ELEMENT_DELAY:
304 case WISPR_ELEMENT_REPLY_MESSAGE:
305 case WISPR_ELEMENT_LOGIN_RESULTS_URL:
307 case WISPR_ELEMENT_LOGOFF_URL:
308 g_free(msg->logoff_url);
309 msg->logoff_url = g_strdup(text);
315 static void xml_wispr_error_handler(GMarkupParseContext *context,
316 GError *error, gpointer user_data)
318 struct connman_wispr_message *msg = user_data;
320 msg->has_error = TRUE;
323 static const GMarkupParser xml_wispr_parser_handlers = {
324 xml_wispr_start_element_handler,
325 xml_wispr_end_element_handler,
326 xml_wispr_text_handler,
328 xml_wispr_error_handler,
331 static void xml_wispr_parser_callback(const char *str, gpointer user_data)
333 struct connman_wispr_portal_context *wp_context = user_data;
334 GMarkupParseContext *parser_context = NULL;
339 parser_context = g_markup_parse_context_new(&xml_wispr_parser_handlers,
340 G_MARKUP_TREAT_CDATA_AS_TEXT,
341 &(wp_context->wispr_msg), NULL);
343 result = g_markup_parse_context_parse(parser_context,
344 str, strlen(str), NULL);
346 result = g_markup_parse_context_end_parse(parser_context, NULL);
348 g_markup_parse_context_free(parser_context);
351 static void web_debug(const char *str, void *data)
353 connman_info("%s: %s\n", (const char *) data, str);
356 static void wispr_portal_error(struct connman_wispr_portal_context *wp_context)
358 DBG("Failed to proceed wispr/portal web request");
360 wp_context->wispr_result = CONNMAN_WISPR_RESULT_FAILED;
363 static void portal_manage_status(GWebResult *result,
364 struct connman_wispr_portal_context *wp_context)
366 const char *str = NULL;
370 /* We currently don't do anything with this info */
371 if (g_web_result_get_header(result, "X-ConnMan-Client-IP",
373 connman_info("Client-IP: %s", str);
375 if (g_web_result_get_header(result, "X-ConnMan-Client-Country",
377 connman_info("Client-Country: %s", str);
379 if (g_web_result_get_header(result, "X-ConnMan-Client-Region",
381 connman_info("Client-Region: %s", str);
383 __connman_service_ipconfig_indicate_state(wp_context->service,
384 CONNMAN_SERVICE_STATE_ONLINE,
388 static void wispr_portal_request_portal(struct connman_wispr_portal_context *wp_context)
392 wp_context->request_id = g_web_request_get(wp_context->web,
393 wp_context->status_url,
394 wispr_portal_web_result, wp_context);
396 if (wp_context->request_id == 0)
397 wispr_portal_error(wp_context);
400 static gboolean wispr_input(const guint8 **data, gsize *length,
403 struct connman_wispr_portal_context *wp_context = user_data;
409 buf = g_string_sized_new(100);
411 g_string_append(buf, "button=Login&UserName=");
412 g_string_append_uri_escaped(buf, wp_context->wispr_username,
414 g_string_append(buf, "&Password=");
415 g_string_append_uri_escaped(buf, wp_context->wispr_password,
417 g_string_append(buf, "&FNAME=0&OriginatingServer=");
418 g_string_append_uri_escaped(buf, wp_context->status_url, NULL, FALSE);
422 g_free(wp_context->wispr_formdata);
423 wp_context->wispr_formdata = g_string_free(buf, FALSE);
425 *data = (guint8 *) wp_context->wispr_formdata;
431 static void wispr_portal_request_wispr_login(struct connman_service *service,
432 const char *username, const char *password,
435 struct connman_wispr_portal_context *wp_context = user_data;
439 g_free(wp_context->wispr_username);
440 wp_context->wispr_username = g_strdup(username);
442 g_free(wp_context->wispr_password);
443 wp_context->wispr_password = g_strdup(password);
445 wp_context->request_id = g_web_request_post(wp_context->web,
446 wp_context->wispr_msg.login_url,
447 "application/x-www-form-urlencoded",
448 wispr_input, wispr_portal_web_result,
451 connman_wispr_message_init(&wp_context->wispr_msg);
454 static gboolean wispr_manage_message(GWebResult *result,
455 struct connman_wispr_portal_context *wp_context)
457 DBG("Message type: %s (%d)",
458 message_type_to_string(wp_context->wispr_msg.message_type),
459 wp_context->wispr_msg.message_type);
460 DBG("Response code: %s (%d)",
461 response_code_to_string(wp_context->wispr_msg.response_code),
462 wp_context->wispr_msg.response_code);
464 if (wp_context->wispr_msg.access_procedure != NULL)
465 DBG("Access procedure: %s",
466 wp_context->wispr_msg.access_procedure);
467 if (wp_context->wispr_msg.access_location != NULL)
468 DBG("Access location: %s",
469 wp_context->wispr_msg.access_location);
470 if (wp_context->wispr_msg.location_name != NULL)
471 DBG("Location name: %s",
472 wp_context->wispr_msg.location_name);
473 if (wp_context->wispr_msg.login_url != NULL)
474 DBG("Login URL: %s", wp_context->wispr_msg.login_url);
475 if (wp_context->wispr_msg.abort_login_url != NULL)
476 DBG("Abort login URL: %s",
477 wp_context->wispr_msg.abort_login_url);
478 if (wp_context->wispr_msg.logoff_url != NULL)
479 DBG("Logoff URL: %s", wp_context->wispr_msg.logoff_url);
481 switch (wp_context->wispr_msg.message_type) {
483 DBG("Login required");
485 wp_context->wispr_result = CONNMAN_WISPR_RESULT_LOGIN;
487 __connman_service_request_login(wp_context->service);
489 if (__connman_agent_request_login_input(wp_context->service,
490 wispr_portal_request_wispr_login,
492 wispr_portal_error(wp_context);
495 case 120: /* Falling down */
497 if (wp_context->wispr_msg.response_code == 50) {
498 wp_context->wispr_result = CONNMAN_WISPR_RESULT_ONLINE;
500 g_free(wp_context->wispr_username);
501 wp_context->wispr_username = NULL;
503 g_free(wp_context->wispr_password);
504 wp_context->wispr_password = NULL;
506 g_free(wp_context->wispr_formdata);
507 wp_context->wispr_formdata = NULL;
509 wispr_portal_request_portal(wp_context);
513 wispr_portal_error(wp_context);
523 static gboolean wispr_portal_web_result(GWebResult *result, gpointer user_data)
525 struct connman_wispr_portal_context *wp_context = user_data;
526 const char *redirect = NULL;
527 const guint8 *chunk = NULL;
528 const char *str = NULL;
534 if (wp_context->request_id == 0)
537 if (wp_context->wispr_result != CONNMAN_WISPR_RESULT_ONLINE) {
538 g_web_result_get_chunk(result, &chunk, &length);
541 g_web_parser_feed_data(wp_context->wispr_parser,
546 g_web_parser_end_data(wp_context->wispr_parser);
548 if (wp_context->wispr_msg.message_type >= 0) {
549 if (wispr_manage_message(result, wp_context) == TRUE)
554 status = g_web_result_get_status(result);
556 DBG("status: %03u", status);
560 if (wp_context->wispr_msg.message_type >= 0)
563 if (g_web_result_get_header(result, "X-ConnMan-Status",
565 portal_manage_status(result, wp_context);
567 __connman_service_request_login(wp_context->service);
571 if (g_web_result_get_header(result, "Location",
575 DBG("Redirect URL: %s", redirect);
577 wp_context->request_id = g_web_request_get(wp_context->web,
578 redirect, wispr_portal_web_result, wp_context);
582 wispr_portal_error(wp_context);
589 wp_context->request_id = 0;
591 wp_context->wispr_msg.message_type = -1;
595 static void proxy_callback(const char *proxy, void *user_data)
597 struct connman_wispr_portal_context *wp_context = user_data;
599 DBG("proxy %s", proxy);
601 wp_context->token = 0;
604 proxy = getenv("http_proxy");
606 if (getenv("CONNMAN_WEB_DEBUG"))
607 g_web_set_debug(wp_context->web, web_debug, "WEB");
609 if (proxy != NULL && g_strcmp0(proxy, "DIRECT") != 0)
610 g_web_set_proxy(wp_context->web, proxy);
612 g_web_set_accept(wp_context->web, NULL);
613 g_web_set_user_agent(wp_context->web, "ConnMan/%s wispr", VERSION);
614 g_web_set_close_connection(wp_context->web, TRUE);
616 connman_wispr_message_init(&wp_context->wispr_msg);
618 wp_context->wispr_parser = g_web_parser_new(
619 "<WISPAccessGatewayParam",
620 "WISPAccessGatewayParam>",
621 xml_wispr_parser_callback, wp_context);
623 wispr_portal_request_portal(wp_context);
626 static int wispr_portal_detect(struct connman_wispr_portal_context *wp_context)
628 enum connman_service_type service_type;
629 char *interface = NULL;
633 DBG("wispr/portal context %p", wp_context);
634 DBG("service %p", wp_context->service);
636 service_type = connman_service_get_type(wp_context->service);
638 switch (service_type) {
639 case CONNMAN_SERVICE_TYPE_ETHERNET:
640 case CONNMAN_SERVICE_TYPE_WIFI:
641 case CONNMAN_SERVICE_TYPE_WIMAX:
642 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
643 case CONNMAN_SERVICE_TYPE_CELLULAR:
645 case CONNMAN_SERVICE_TYPE_UNKNOWN:
646 case CONNMAN_SERVICE_TYPE_SYSTEM:
647 case CONNMAN_SERVICE_TYPE_GPS:
648 case CONNMAN_SERVICE_TYPE_VPN:
649 case CONNMAN_SERVICE_TYPE_GADGET:
653 interface = connman_service_get_interface(wp_context->service);
654 if (interface == NULL)
657 DBG("interface %s", interface);
659 if_index = connman_inet_ifindex(interface);
663 wp_context->web = g_web_new(if_index);
664 if (wp_context->web == NULL) {
669 if (wp_context->type == CONNMAN_IPCONFIG_TYPE_IPV4) {
670 g_web_set_address_family(wp_context->web, AF_INET);
671 wp_context->status_url = STATUS_URL_IPV4;
673 g_web_set_address_family(wp_context->web, AF_INET6);
674 wp_context->status_url = STATUS_URL_IPV6;
677 wp_context->token = connman_proxy_lookup(interface,
678 wp_context->status_url,
680 proxy_callback, wp_context);
681 if (wp_context->token == 0)
689 int __connman_wispr_start(struct connman_service *service,
690 enum connman_ipconfig_type type)
692 struct connman_wispr_portal_context *wp_context = NULL;
693 struct connman_wispr_portal *wispr_portal = NULL;
696 DBG("service %p", service);
698 if (wispr_portal_list == NULL)
701 index = __connman_service_get_index(service);
705 wispr_portal = g_hash_table_lookup(wispr_portal_list,
706 GINT_TO_POINTER(index));
707 if (wispr_portal == NULL) {
708 wispr_portal = g_try_new0(struct connman_wispr_portal, 1);
709 if (wispr_portal == NULL)
712 g_hash_table_replace(wispr_portal_list,
713 GINT_TO_POINTER(index), wispr_portal);
716 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
717 wp_context = wispr_portal->ipv4_context;
718 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
719 wp_context = wispr_portal->ipv6_context;
723 /* If there is already an existing context, we wipe it */
724 if (wp_context != NULL)
725 free_connman_wispr_portal_context(wp_context);
727 wp_context = g_try_new0(struct connman_wispr_portal_context, 1);
728 if (wp_context == NULL)
731 connman_service_ref(service);
733 wp_context->service = service;
734 wp_context->type = type;
736 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
737 wispr_portal->ipv4_context = wp_context;
739 wispr_portal->ipv6_context = wp_context;
741 return wispr_portal_detect(wp_context);
744 void __connman_wispr_stop(struct connman_service *service)
748 DBG("service %p", service);
750 if (wispr_portal_list == NULL)
753 index = __connman_service_get_index(service);
757 g_hash_table_remove(wispr_portal_list, GINT_TO_POINTER(index));
760 int __connman_wispr_init(void)
764 wispr_portal_list = g_hash_table_new_full(g_direct_hash,
765 g_direct_equal, NULL,
766 free_connman_wispr_portal);
771 void __connman_wispr_cleanup(void)
775 g_hash_table_destroy(wispr_portal_list);
776 wispr_portal_list = NULL;