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;
70 GWebParser *wispr_parser;
71 struct connman_wispr_message wispr_msg;
77 enum connman_wispr_result wispr_result;
80 struct connman_wispr_portal {
81 struct connman_wispr_portal_context *ipv4_context;
82 struct connman_wispr_portal_context *ipv6_context;
85 static gboolean wispr_portal_web_result(GWebResult *result, gpointer user_data);
87 static GHashTable *wispr_portal_list = NULL;
89 static void connman_wispr_message_init(struct connman_wispr_message *msg)
93 msg->has_error = FALSE;
94 msg->current_element = NULL;
96 msg->message_type = -1;
97 msg->response_code = -1;
99 g_free(msg->login_url);
100 msg->login_url = NULL;
102 g_free(msg->abort_login_url);
103 msg->abort_login_url = NULL;
105 g_free(msg->logoff_url);
106 msg->logoff_url = NULL;
108 g_free(msg->access_procedure);
109 msg->access_procedure = NULL;
111 g_free(msg->access_location);
112 msg->access_location = NULL;
114 g_free(msg->location_name);
115 msg->location_name = NULL;
118 static void free_connman_wispr_portal_context(struct connman_wispr_portal_context *wp_context)
122 if (wp_context == NULL)
125 connman_service_unref(wp_context->service);
127 if (wp_context->token > 0)
128 connman_proxy_lookup_cancel(wp_context->token);
130 if (wp_context->request_id > 0)
131 g_web_cancel_request(wp_context->web, wp_context->request_id);
133 g_web_unref(wp_context->web);
135 g_free(wp_context->redirect_url);
137 g_web_parser_unref(wp_context->wispr_parser);
138 connman_wispr_message_init(&wp_context->wispr_msg);
140 g_free(wp_context->wispr_username);
141 g_free(wp_context->wispr_password);
142 g_free(wp_context->wispr_formdata);
147 static void free_connman_wispr_portal(gpointer data)
149 struct connman_wispr_portal *wispr_portal = data;
153 if (wispr_portal == NULL)
156 free_connman_wispr_portal_context(wispr_portal->ipv4_context);
157 free_connman_wispr_portal_context(wispr_portal->ipv6_context);
159 g_free(wispr_portal);
162 static const char *message_type_to_string(int message_type)
164 switch (message_type) {
166 return "Initial redirect message";
168 return "Proxy notification";
170 return "Authentication notification";
172 return "Logoff notification";
174 return "Response to Authentication Poll";
176 return "Response to Abort Login";
182 static const char *response_code_to_string(int response_code)
184 switch (response_code) {
188 return "Login succeeded";
190 return "Login failed";
192 return "RADIUS server error/timeout";
194 return "RADIUS server not enabled";
196 return "Logoff succeeded";
198 return "Login aborted";
200 return "Proxy detection/repeat operation";
202 return "Authentication pending";
204 return "Access gateway internal error";
213 WISPR_ELEMENT_NONE = 0,
214 WISPR_ELEMENT_ACCESS_PROCEDURE = 1,
215 WISPR_ELEMENT_ACCESS_LOCATION = 2,
216 WISPR_ELEMENT_LOCATION_NAME = 3,
217 WISPR_ELEMENT_LOGIN_URL = 4,
218 WISPR_ELEMENT_ABORT_LOGIN_URL = 5,
219 WISPR_ELEMENT_MESSAGE_TYPE = 6,
220 WISPR_ELEMENT_RESPONSE_CODE = 7,
221 WISPR_ELEMENT_NEXT_URL = 8,
222 WISPR_ELEMENT_DELAY = 9,
223 WISPR_ELEMENT_REPLY_MESSAGE = 10,
224 WISPR_ELEMENT_LOGIN_RESULTS_URL = 11,
225 WISPR_ELEMENT_LOGOFF_URL = 12,
227 } wispr_element_map[] = {
228 { "AccessProcedure", WISPR_ELEMENT_ACCESS_PROCEDURE },
229 { "AccessLocation", WISPR_ELEMENT_ACCESS_LOCATION },
230 { "LocationName", WISPR_ELEMENT_LOCATION_NAME },
231 { "LoginURL", WISPR_ELEMENT_LOGIN_URL },
232 { "AbortLoginURL", WISPR_ELEMENT_ABORT_LOGIN_URL },
233 { "MessageType", WISPR_ELEMENT_MESSAGE_TYPE },
234 { "ResponseCode", WISPR_ELEMENT_RESPONSE_CODE },
235 { "NextURL", WISPR_ELEMENT_NEXT_URL },
236 { "Delay", WISPR_ELEMENT_DELAY },
237 { "ReplyMessage", WISPR_ELEMENT_REPLY_MESSAGE },
238 { "LoginResultsURL", WISPR_ELEMENT_LOGIN_RESULTS_URL },
239 { "LogoffURL", WISPR_ELEMENT_LOGOFF_URL },
240 { NULL, WISPR_ELEMENT_NONE },
243 static void xml_wispr_start_element_handler(GMarkupParseContext *context,
244 const gchar *element_name,
245 const gchar **attribute_names,
246 const gchar **attribute_values,
247 gpointer user_data, GError **error)
249 struct connman_wispr_message *msg = user_data;
251 msg->current_element = element_name;
254 static void xml_wispr_end_element_handler(GMarkupParseContext *context,
255 const gchar *element_name,
256 gpointer user_data, GError **error)
258 struct connman_wispr_message *msg = user_data;
260 msg->current_element = NULL;
263 static void xml_wispr_text_handler(GMarkupParseContext *context,
264 const gchar *text, gsize text_len,
265 gpointer user_data, GError **error)
267 struct connman_wispr_message *msg = user_data;
270 if (msg->current_element == NULL)
273 for (i = 0; wispr_element_map[i].str; i++) {
274 if (g_str_equal(wispr_element_map[i].str,
275 msg->current_element) == FALSE)
278 switch (wispr_element_map[i].element) {
279 case WISPR_ELEMENT_NONE:
280 case WISPR_ELEMENT_ACCESS_PROCEDURE:
281 g_free(msg->access_procedure);
282 msg->access_procedure = g_strdup(text);
284 case WISPR_ELEMENT_ACCESS_LOCATION:
285 g_free(msg->access_location);
286 msg->access_location = g_strdup(text);
288 case WISPR_ELEMENT_LOCATION_NAME:
289 g_free(msg->location_name);
290 msg->location_name = g_strdup(text);
292 case WISPR_ELEMENT_LOGIN_URL:
293 g_free(msg->login_url);
294 msg->login_url = g_strdup(text);
296 case WISPR_ELEMENT_ABORT_LOGIN_URL:
297 g_free(msg->abort_login_url);
298 msg->abort_login_url = g_strdup(text);
300 case WISPR_ELEMENT_MESSAGE_TYPE:
301 msg->message_type = atoi(text);
303 case WISPR_ELEMENT_RESPONSE_CODE:
304 msg->response_code = atoi(text);
306 case WISPR_ELEMENT_NEXT_URL:
307 case WISPR_ELEMENT_DELAY:
308 case WISPR_ELEMENT_REPLY_MESSAGE:
309 case WISPR_ELEMENT_LOGIN_RESULTS_URL:
311 case WISPR_ELEMENT_LOGOFF_URL:
312 g_free(msg->logoff_url);
313 msg->logoff_url = g_strdup(text);
319 static void xml_wispr_error_handler(GMarkupParseContext *context,
320 GError *error, gpointer user_data)
322 struct connman_wispr_message *msg = user_data;
324 msg->has_error = TRUE;
327 static const GMarkupParser xml_wispr_parser_handlers = {
328 xml_wispr_start_element_handler,
329 xml_wispr_end_element_handler,
330 xml_wispr_text_handler,
332 xml_wispr_error_handler,
335 static void xml_wispr_parser_callback(const char *str, gpointer user_data)
337 struct connman_wispr_portal_context *wp_context = user_data;
338 GMarkupParseContext *parser_context = NULL;
343 parser_context = g_markup_parse_context_new(&xml_wispr_parser_handlers,
344 G_MARKUP_TREAT_CDATA_AS_TEXT,
345 &(wp_context->wispr_msg), NULL);
347 result = g_markup_parse_context_parse(parser_context,
348 str, strlen(str), NULL);
350 result = g_markup_parse_context_end_parse(parser_context, NULL);
352 g_markup_parse_context_free(parser_context);
355 static void web_debug(const char *str, void *data)
357 connman_info("%s: %s\n", (const char *) data, str);
360 static void wispr_portal_error(struct connman_wispr_portal_context *wp_context)
362 DBG("Failed to proceed wispr/portal web request");
364 wp_context->wispr_result = CONNMAN_WISPR_RESULT_FAILED;
367 static void portal_manage_status(GWebResult *result,
368 struct connman_wispr_portal_context *wp_context)
370 const char *str = NULL;
374 /* We currently don't do anything with this info */
375 if (g_web_result_get_header(result, "X-ConnMan-Client-IP",
377 connman_info("Client-IP: %s", str);
379 if (g_web_result_get_header(result, "X-ConnMan-Client-Country",
381 connman_info("Client-Country: %s", str);
383 if (g_web_result_get_header(result, "X-ConnMan-Client-Region",
385 connman_info("Client-Region: %s", str);
387 __connman_service_ipconfig_indicate_state(wp_context->service,
388 CONNMAN_SERVICE_STATE_ONLINE,
392 static void wispr_portal_request_portal(struct connman_wispr_portal_context *wp_context)
396 wp_context->request_id = g_web_request_get(wp_context->web,
397 wp_context->status_url,
398 wispr_portal_web_result, wp_context);
400 if (wp_context->request_id == 0)
401 wispr_portal_error(wp_context);
404 static gboolean wispr_input(const guint8 **data, gsize *length,
407 struct connman_wispr_portal_context *wp_context = user_data;
413 buf = g_string_sized_new(100);
415 g_string_append(buf, "button=Login&UserName=");
416 g_string_append_uri_escaped(buf, wp_context->wispr_username,
418 g_string_append(buf, "&Password=");
419 g_string_append_uri_escaped(buf, wp_context->wispr_password,
421 g_string_append(buf, "&FNAME=0&OriginatingServer=");
422 g_string_append_uri_escaped(buf, wp_context->status_url, NULL, FALSE);
426 g_free(wp_context->wispr_formdata);
427 wp_context->wispr_formdata = g_string_free(buf, FALSE);
429 *data = (guint8 *) wp_context->wispr_formdata;
435 static void wispr_portal_request_wispr_login(struct connman_service *service,
436 connman_bool_t success,
437 const char *ssid, int ssid_len,
438 const char *username, const char *password,
439 gboolean wps, const char *wpspin,
442 struct connman_wispr_portal_context *wp_context = user_data;
446 g_free(wp_context->wispr_username);
447 wp_context->wispr_username = g_strdup(username);
449 g_free(wp_context->wispr_password);
450 wp_context->wispr_password = g_strdup(password);
452 wp_context->request_id = g_web_request_post(wp_context->web,
453 wp_context->wispr_msg.login_url,
454 "application/x-www-form-urlencoded",
455 wispr_input, wispr_portal_web_result,
458 connman_wispr_message_init(&wp_context->wispr_msg);
461 static gboolean wispr_manage_message(GWebResult *result,
462 struct connman_wispr_portal_context *wp_context)
464 DBG("Message type: %s (%d)",
465 message_type_to_string(wp_context->wispr_msg.message_type),
466 wp_context->wispr_msg.message_type);
467 DBG("Response code: %s (%d)",
468 response_code_to_string(wp_context->wispr_msg.response_code),
469 wp_context->wispr_msg.response_code);
471 if (wp_context->wispr_msg.access_procedure != NULL)
472 DBG("Access procedure: %s",
473 wp_context->wispr_msg.access_procedure);
474 if (wp_context->wispr_msg.access_location != NULL)
475 DBG("Access location: %s",
476 wp_context->wispr_msg.access_location);
477 if (wp_context->wispr_msg.location_name != NULL)
478 DBG("Location name: %s",
479 wp_context->wispr_msg.location_name);
480 if (wp_context->wispr_msg.login_url != NULL)
481 DBG("Login URL: %s", wp_context->wispr_msg.login_url);
482 if (wp_context->wispr_msg.abort_login_url != NULL)
483 DBG("Abort login URL: %s",
484 wp_context->wispr_msg.abort_login_url);
485 if (wp_context->wispr_msg.logoff_url != NULL)
486 DBG("Logoff URL: %s", wp_context->wispr_msg.logoff_url);
488 switch (wp_context->wispr_msg.message_type) {
490 DBG("Login required");
492 wp_context->wispr_result = CONNMAN_WISPR_RESULT_LOGIN;
494 if (__connman_agent_request_login_input(wp_context->service,
495 wispr_portal_request_wispr_login,
497 wispr_portal_error(wp_context);
500 case 120: /* Falling down */
502 if (wp_context->wispr_msg.response_code == 50) {
503 wp_context->wispr_result = CONNMAN_WISPR_RESULT_ONLINE;
505 g_free(wp_context->wispr_username);
506 wp_context->wispr_username = NULL;
508 g_free(wp_context->wispr_password);
509 wp_context->wispr_password = NULL;
511 g_free(wp_context->wispr_formdata);
512 wp_context->wispr_formdata = NULL;
514 wispr_portal_request_portal(wp_context);
518 wispr_portal_error(wp_context);
528 static void wispr_portal_browser_reply_cb(struct connman_service *service,
529 connman_bool_t authentication_done,
532 struct connman_wispr_portal_context *wp_context = user_data;
534 if (service == NULL || wp_context == NULL)
537 if (authentication_done == FALSE) {
538 wispr_portal_error(wp_context);
542 /* Restarting the test */
543 __connman_wispr_start(service, wp_context->type);
546 static gboolean wispr_portal_web_result(GWebResult *result, gpointer user_data)
548 struct connman_wispr_portal_context *wp_context = user_data;
549 const char *redirect = NULL;
550 const guint8 *chunk = NULL;
551 const char *str = NULL;
557 if (wp_context->request_id == 0)
560 if (wp_context->wispr_result != CONNMAN_WISPR_RESULT_ONLINE) {
561 g_web_result_get_chunk(result, &chunk, &length);
564 g_web_parser_feed_data(wp_context->wispr_parser,
569 g_web_parser_end_data(wp_context->wispr_parser);
571 if (wp_context->wispr_msg.message_type >= 0) {
572 if (wispr_manage_message(result, wp_context) == TRUE)
577 status = g_web_result_get_status(result);
579 DBG("status: %03u", status);
583 if (wp_context->wispr_msg.message_type >= 0)
586 if (g_web_result_get_header(result, "X-ConnMan-Status",
588 portal_manage_status(result, wp_context);
590 __connman_agent_request_browser(wp_context->service,
591 wispr_portal_browser_reply_cb,
592 wp_context->redirect_url, wp_context);
596 if (g_web_result_get_header(result, "Location",
597 &redirect) == FALSE) {
598 __connman_agent_request_browser(wp_context->service,
599 wispr_portal_browser_reply_cb,
600 wp_context->status_url, wp_context);
604 DBG("Redirect URL: %s", redirect);
606 wp_context->redirect_url = g_strdup(redirect);
608 wp_context->request_id = g_web_request_get(wp_context->web,
609 redirect, wispr_portal_web_result, wp_context);
613 if (__connman_service_online_check_failed(wp_context->service,
614 wp_context->type) == 0)
615 wispr_portal_error(wp_context);
622 wp_context->request_id = 0;
624 wp_context->wispr_msg.message_type = -1;
628 static void proxy_callback(const char *proxy, void *user_data)
630 struct connman_wispr_portal_context *wp_context = user_data;
632 DBG("proxy %s", proxy);
634 wp_context->token = 0;
636 if (getenv("CONNMAN_WEB_DEBUG"))
637 g_web_set_debug(wp_context->web, web_debug, "WEB");
639 if (proxy != NULL && g_strcmp0(proxy, "DIRECT") != 0)
640 g_web_set_proxy(wp_context->web, proxy);
642 g_web_set_accept(wp_context->web, NULL);
643 g_web_set_user_agent(wp_context->web, "ConnMan/%s wispr", VERSION);
644 g_web_set_close_connection(wp_context->web, TRUE);
646 connman_wispr_message_init(&wp_context->wispr_msg);
648 wp_context->wispr_parser = g_web_parser_new(
649 "<WISPAccessGatewayParam",
650 "WISPAccessGatewayParam>",
651 xml_wispr_parser_callback, wp_context);
653 wispr_portal_request_portal(wp_context);
656 static gboolean no_proxy_callback(gpointer user_data)
658 struct connman_wispr_portal_context *wp_context = user_data;
660 proxy_callback("DIRECT", wp_context);
665 static int wispr_portal_detect(struct connman_wispr_portal_context *wp_context)
667 enum connman_service_proxy_method proxy_method;
668 enum connman_service_type service_type;
669 char *interface = NULL;
670 char **nameservers = NULL;
675 DBG("wispr/portal context %p", wp_context);
676 DBG("service %p", wp_context->service);
678 service_type = connman_service_get_type(wp_context->service);
680 switch (service_type) {
681 case CONNMAN_SERVICE_TYPE_ETHERNET:
682 case CONNMAN_SERVICE_TYPE_WIFI:
683 case CONNMAN_SERVICE_TYPE_WIMAX:
684 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
685 case CONNMAN_SERVICE_TYPE_CELLULAR:
687 case CONNMAN_SERVICE_TYPE_UNKNOWN:
688 case CONNMAN_SERVICE_TYPE_SYSTEM:
689 case CONNMAN_SERVICE_TYPE_GPS:
690 case CONNMAN_SERVICE_TYPE_VPN:
691 case CONNMAN_SERVICE_TYPE_GADGET:
695 interface = connman_service_get_interface(wp_context->service);
696 if (interface == NULL)
699 DBG("interface %s", interface);
701 if_index = connman_inet_ifindex(interface);
707 nameservers = connman_service_get_nameservers(wp_context->service);
708 if (nameservers == NULL) {
713 wp_context->web = g_web_new(if_index);
714 if (wp_context->web == NULL) {
719 if (wp_context->type == CONNMAN_IPCONFIG_TYPE_IPV4) {
720 g_web_set_address_family(wp_context->web, AF_INET);
721 wp_context->status_url = STATUS_URL_IPV4;
723 g_web_set_address_family(wp_context->web, AF_INET6);
724 wp_context->status_url = STATUS_URL_IPV6;
727 for (i = 0; nameservers[i] != NULL; i++)
728 g_web_add_nameserver(wp_context->web, nameservers[i]);
730 proxy_method = connman_service_get_proxy_method(wp_context->service);
732 if (proxy_method != CONNMAN_SERVICE_PROXY_METHOD_DIRECT) {
733 wp_context->token = connman_proxy_lookup(interface,
734 wp_context->status_url,
736 proxy_callback, wp_context);
738 if (wp_context->token == 0)
741 g_timeout_add_seconds(0, no_proxy_callback, wp_context);
745 g_strfreev(nameservers);
751 int __connman_wispr_start(struct connman_service *service,
752 enum connman_ipconfig_type type)
754 struct connman_wispr_portal_context *wp_context = NULL;
755 struct connman_wispr_portal *wispr_portal = NULL;
758 DBG("service %p", service);
760 if (wispr_portal_list == NULL)
763 index = __connman_service_get_index(service);
767 wispr_portal = g_hash_table_lookup(wispr_portal_list,
768 GINT_TO_POINTER(index));
769 if (wispr_portal == NULL) {
770 wispr_portal = g_try_new0(struct connman_wispr_portal, 1);
771 if (wispr_portal == NULL)
774 g_hash_table_replace(wispr_portal_list,
775 GINT_TO_POINTER(index), wispr_portal);
778 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
779 wp_context = wispr_portal->ipv4_context;
780 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
781 wp_context = wispr_portal->ipv6_context;
785 /* If there is already an existing context, we wipe it */
786 if (wp_context != NULL)
787 free_connman_wispr_portal_context(wp_context);
789 wp_context = g_try_new0(struct connman_wispr_portal_context, 1);
790 if (wp_context == NULL)
793 connman_service_ref(service);
795 wp_context->service = service;
796 wp_context->type = type;
798 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
799 wispr_portal->ipv4_context = wp_context;
801 wispr_portal->ipv6_context = wp_context;
803 return wispr_portal_detect(wp_context);
806 void __connman_wispr_stop(struct connman_service *service)
810 DBG("service %p", service);
812 if (wispr_portal_list == NULL)
815 index = __connman_service_get_index(service);
819 g_hash_table_remove(wispr_portal_list, GINT_TO_POINTER(index));
822 int __connman_wispr_init(void)
826 wispr_portal_list = g_hash_table_new_full(g_direct_hash,
827 g_direct_equal, NULL,
828 free_connman_wispr_portal);
833 void __connman_wispr_cleanup(void)
837 g_hash_table_destroy(wispr_portal_list);
838 wispr_portal_list = NULL;