5 * Copyright (C) 2007-2012 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,
61 struct connman_wispr_portal_context {
62 struct connman_service *service;
63 enum connman_ipconfig_type type;
65 /* Portal/WISPr common */
70 const char *status_url;
75 GWebParser *wispr_parser;
76 struct connman_wispr_message wispr_msg;
82 enum connman_wispr_result wispr_result;
87 struct connman_wispr_portal {
88 struct connman_wispr_portal_context *ipv4_context;
89 struct connman_wispr_portal_context *ipv6_context;
92 static gboolean wispr_portal_web_result(GWebResult *result, gpointer user_data);
94 static GHashTable *wispr_portal_list = NULL;
96 static void connman_wispr_message_init(struct connman_wispr_message *msg)
100 msg->has_error = FALSE;
101 msg->current_element = NULL;
103 msg->message_type = -1;
104 msg->response_code = -1;
106 g_free(msg->login_url);
107 msg->login_url = NULL;
109 g_free(msg->abort_login_url);
110 msg->abort_login_url = NULL;
112 g_free(msg->logoff_url);
113 msg->logoff_url = NULL;
115 g_free(msg->access_procedure);
116 msg->access_procedure = NULL;
118 g_free(msg->access_location);
119 msg->access_location = NULL;
121 g_free(msg->location_name);
122 msg->location_name = NULL;
125 static void free_wispr_routes(struct connman_wispr_portal_context *wp_context)
127 while (wp_context->route_list != NULL) {
128 struct wispr_route *route = wp_context->route_list->data;
130 DBG("free route to %s if %d type %d", route->address,
131 route->if_index, wp_context->type);
133 switch(wp_context->type) {
134 case CONNMAN_IPCONFIG_TYPE_IPV4:
135 connman_inet_del_host_route(route->if_index,
138 case CONNMAN_IPCONFIG_TYPE_IPV6:
139 connman_inet_del_ipv6_host_route(route->if_index,
142 case CONNMAN_IPCONFIG_TYPE_UNKNOWN:
146 g_free(route->address);
149 wp_context->route_list =
150 g_slist_delete_link(wp_context->route_list,
151 wp_context->route_list);
155 static void free_connman_wispr_portal_context(struct connman_wispr_portal_context *wp_context)
157 DBG("context %p", wp_context);
159 if (wp_context == NULL)
162 connman_service_unref(wp_context->service);
164 if (wp_context->token > 0)
165 connman_proxy_lookup_cancel(wp_context->token);
167 if (wp_context->request_id > 0)
168 g_web_cancel_request(wp_context->web, wp_context->request_id);
170 g_web_unref(wp_context->web);
172 g_free(wp_context->redirect_url);
174 g_web_parser_unref(wp_context->wispr_parser);
175 connman_wispr_message_init(&wp_context->wispr_msg);
177 g_free(wp_context->wispr_username);
178 g_free(wp_context->wispr_password);
179 g_free(wp_context->wispr_formdata);
181 free_wispr_routes(wp_context);
186 static void free_connman_wispr_portal(gpointer data)
188 struct connman_wispr_portal *wispr_portal = data;
192 if (wispr_portal == NULL)
195 free_connman_wispr_portal_context(wispr_portal->ipv4_context);
196 free_connman_wispr_portal_context(wispr_portal->ipv6_context);
198 g_free(wispr_portal);
201 static const char *message_type_to_string(int message_type)
203 switch (message_type) {
205 return "Initial redirect message";
207 return "Proxy notification";
209 return "Authentication notification";
211 return "Logoff notification";
213 return "Response to Authentication Poll";
215 return "Response to Abort Login";
221 static const char *response_code_to_string(int response_code)
223 switch (response_code) {
227 return "Login succeeded";
229 return "Login failed";
231 return "RADIUS server error/timeout";
233 return "RADIUS server not enabled";
235 return "Logoff succeeded";
237 return "Login aborted";
239 return "Proxy detection/repeat operation";
241 return "Authentication pending";
243 return "Access gateway internal error";
252 WISPR_ELEMENT_NONE = 0,
253 WISPR_ELEMENT_ACCESS_PROCEDURE = 1,
254 WISPR_ELEMENT_ACCESS_LOCATION = 2,
255 WISPR_ELEMENT_LOCATION_NAME = 3,
256 WISPR_ELEMENT_LOGIN_URL = 4,
257 WISPR_ELEMENT_ABORT_LOGIN_URL = 5,
258 WISPR_ELEMENT_MESSAGE_TYPE = 6,
259 WISPR_ELEMENT_RESPONSE_CODE = 7,
260 WISPR_ELEMENT_NEXT_URL = 8,
261 WISPR_ELEMENT_DELAY = 9,
262 WISPR_ELEMENT_REPLY_MESSAGE = 10,
263 WISPR_ELEMENT_LOGIN_RESULTS_URL = 11,
264 WISPR_ELEMENT_LOGOFF_URL = 12,
266 } wispr_element_map[] = {
267 { "AccessProcedure", WISPR_ELEMENT_ACCESS_PROCEDURE },
268 { "AccessLocation", WISPR_ELEMENT_ACCESS_LOCATION },
269 { "LocationName", WISPR_ELEMENT_LOCATION_NAME },
270 { "LoginURL", WISPR_ELEMENT_LOGIN_URL },
271 { "AbortLoginURL", WISPR_ELEMENT_ABORT_LOGIN_URL },
272 { "MessageType", WISPR_ELEMENT_MESSAGE_TYPE },
273 { "ResponseCode", WISPR_ELEMENT_RESPONSE_CODE },
274 { "NextURL", WISPR_ELEMENT_NEXT_URL },
275 { "Delay", WISPR_ELEMENT_DELAY },
276 { "ReplyMessage", WISPR_ELEMENT_REPLY_MESSAGE },
277 { "LoginResultsURL", WISPR_ELEMENT_LOGIN_RESULTS_URL },
278 { "LogoffURL", WISPR_ELEMENT_LOGOFF_URL },
279 { NULL, WISPR_ELEMENT_NONE },
282 static void xml_wispr_start_element_handler(GMarkupParseContext *context,
283 const gchar *element_name,
284 const gchar **attribute_names,
285 const gchar **attribute_values,
286 gpointer user_data, GError **error)
288 struct connman_wispr_message *msg = user_data;
290 msg->current_element = element_name;
293 static void xml_wispr_end_element_handler(GMarkupParseContext *context,
294 const gchar *element_name,
295 gpointer user_data, GError **error)
297 struct connman_wispr_message *msg = user_data;
299 msg->current_element = NULL;
302 static void xml_wispr_text_handler(GMarkupParseContext *context,
303 const gchar *text, gsize text_len,
304 gpointer user_data, GError **error)
306 struct connman_wispr_message *msg = user_data;
309 if (msg->current_element == NULL)
312 for (i = 0; wispr_element_map[i].str; i++) {
313 if (g_str_equal(wispr_element_map[i].str,
314 msg->current_element) == FALSE)
317 switch (wispr_element_map[i].element) {
318 case WISPR_ELEMENT_NONE:
319 case WISPR_ELEMENT_ACCESS_PROCEDURE:
320 g_free(msg->access_procedure);
321 msg->access_procedure = g_strdup(text);
323 case WISPR_ELEMENT_ACCESS_LOCATION:
324 g_free(msg->access_location);
325 msg->access_location = g_strdup(text);
327 case WISPR_ELEMENT_LOCATION_NAME:
328 g_free(msg->location_name);
329 msg->location_name = g_strdup(text);
331 case WISPR_ELEMENT_LOGIN_URL:
332 g_free(msg->login_url);
333 msg->login_url = g_strdup(text);
335 case WISPR_ELEMENT_ABORT_LOGIN_URL:
336 g_free(msg->abort_login_url);
337 msg->abort_login_url = g_strdup(text);
339 case WISPR_ELEMENT_MESSAGE_TYPE:
340 msg->message_type = atoi(text);
342 case WISPR_ELEMENT_RESPONSE_CODE:
343 msg->response_code = atoi(text);
345 case WISPR_ELEMENT_NEXT_URL:
346 case WISPR_ELEMENT_DELAY:
347 case WISPR_ELEMENT_REPLY_MESSAGE:
348 case WISPR_ELEMENT_LOGIN_RESULTS_URL:
350 case WISPR_ELEMENT_LOGOFF_URL:
351 g_free(msg->logoff_url);
352 msg->logoff_url = g_strdup(text);
358 static void xml_wispr_error_handler(GMarkupParseContext *context,
359 GError *error, gpointer user_data)
361 struct connman_wispr_message *msg = user_data;
363 msg->has_error = TRUE;
366 static const GMarkupParser xml_wispr_parser_handlers = {
367 xml_wispr_start_element_handler,
368 xml_wispr_end_element_handler,
369 xml_wispr_text_handler,
371 xml_wispr_error_handler,
374 static void xml_wispr_parser_callback(const char *str, gpointer user_data)
376 struct connman_wispr_portal_context *wp_context = user_data;
377 GMarkupParseContext *parser_context = NULL;
382 parser_context = g_markup_parse_context_new(&xml_wispr_parser_handlers,
383 G_MARKUP_TREAT_CDATA_AS_TEXT,
384 &(wp_context->wispr_msg), NULL);
386 result = g_markup_parse_context_parse(parser_context,
387 str, strlen(str), NULL);
389 result = g_markup_parse_context_end_parse(parser_context, NULL);
391 g_markup_parse_context_free(parser_context);
394 static void web_debug(const char *str, void *data)
396 connman_info("%s: %s\n", (const char *) data, str);
399 static void wispr_portal_error(struct connman_wispr_portal_context *wp_context)
401 DBG("Failed to proceed wispr/portal web request");
403 wp_context->wispr_result = CONNMAN_WISPR_RESULT_FAILED;
406 static void portal_manage_status(GWebResult *result,
407 struct connman_wispr_portal_context *wp_context)
409 const char *str = NULL;
413 /* We currently don't do anything with this info */
414 if (g_web_result_get_header(result, "X-ConnMan-Client-IP",
416 connman_info("Client-IP: %s", str);
418 if (g_web_result_get_header(result, "X-ConnMan-Client-Country",
420 connman_info("Client-Country: %s", str);
422 if (g_web_result_get_header(result, "X-ConnMan-Client-Region",
424 connman_info("Client-Region: %s", str);
426 __connman_service_ipconfig_indicate_state(wp_context->service,
427 CONNMAN_SERVICE_STATE_ONLINE,
431 static gboolean wispr_route_request(const char *address, int ai_family,
432 int if_index, gpointer user_data)
435 struct connman_wispr_portal_context *wp_context = user_data;
437 struct wispr_route *route;
439 gateway = __connman_ipconfig_get_gateway_from_index(if_index,
442 DBG("address %s if %d gw %s", address, if_index, gateway);
447 route = g_try_new0(struct wispr_route, 1);
449 DBG("could not create struct");
453 switch(wp_context->type) {
454 case CONNMAN_IPCONFIG_TYPE_IPV4:
455 result = connman_inet_add_host_route(if_index, address,
458 case CONNMAN_IPCONFIG_TYPE_IPV6:
459 result = connman_inet_add_ipv6_host_route(if_index, address,
462 case CONNMAN_IPCONFIG_TYPE_UNKNOWN:
471 route->address = g_strdup(address);
472 route->if_index = if_index;
473 wp_context->route_list = g_slist_prepend(wp_context->route_list, route);
478 static void wispr_portal_request_portal(struct connman_wispr_portal_context *wp_context)
482 wp_context->request_id = g_web_request_get(wp_context->web,
483 wp_context->status_url,
484 wispr_portal_web_result,
488 if (wp_context->request_id == 0)
489 wispr_portal_error(wp_context);
492 static gboolean wispr_input(const guint8 **data, gsize *length,
495 struct connman_wispr_portal_context *wp_context = user_data;
501 buf = g_string_sized_new(100);
503 g_string_append(buf, "button=Login&UserName=");
504 g_string_append_uri_escaped(buf, wp_context->wispr_username,
506 g_string_append(buf, "&Password=");
507 g_string_append_uri_escaped(buf, wp_context->wispr_password,
509 g_string_append(buf, "&FNAME=0&OriginatingServer=");
510 g_string_append_uri_escaped(buf, wp_context->status_url, NULL, FALSE);
514 g_free(wp_context->wispr_formdata);
515 wp_context->wispr_formdata = g_string_free(buf, FALSE);
517 *data = (guint8 *) wp_context->wispr_formdata;
523 static void wispr_portal_request_wispr_login(struct connman_service *service,
524 connman_bool_t success,
525 const char *ssid, int ssid_len,
526 const char *username, const char *password,
527 gboolean wps, const char *wpspin,
528 const char *error, void *user_data)
530 struct connman_wispr_portal_context *wp_context = user_data;
534 g_free(wp_context->wispr_username);
535 wp_context->wispr_username = g_strdup(username);
537 g_free(wp_context->wispr_password);
538 wp_context->wispr_password = g_strdup(password);
540 wp_context->request_id = g_web_request_post(wp_context->web,
541 wp_context->wispr_msg.login_url,
542 "application/x-www-form-urlencoded",
543 wispr_input, wispr_portal_web_result,
546 connman_wispr_message_init(&wp_context->wispr_msg);
549 static gboolean wispr_manage_message(GWebResult *result,
550 struct connman_wispr_portal_context *wp_context)
552 DBG("Message type: %s (%d)",
553 message_type_to_string(wp_context->wispr_msg.message_type),
554 wp_context->wispr_msg.message_type);
555 DBG("Response code: %s (%d)",
556 response_code_to_string(wp_context->wispr_msg.response_code),
557 wp_context->wispr_msg.response_code);
559 if (wp_context->wispr_msg.access_procedure != NULL)
560 DBG("Access procedure: %s",
561 wp_context->wispr_msg.access_procedure);
562 if (wp_context->wispr_msg.access_location != NULL)
563 DBG("Access location: %s",
564 wp_context->wispr_msg.access_location);
565 if (wp_context->wispr_msg.location_name != NULL)
566 DBG("Location name: %s",
567 wp_context->wispr_msg.location_name);
568 if (wp_context->wispr_msg.login_url != NULL)
569 DBG("Login URL: %s", wp_context->wispr_msg.login_url);
570 if (wp_context->wispr_msg.abort_login_url != NULL)
571 DBG("Abort login URL: %s",
572 wp_context->wispr_msg.abort_login_url);
573 if (wp_context->wispr_msg.logoff_url != NULL)
574 DBG("Logoff URL: %s", wp_context->wispr_msg.logoff_url);
576 switch (wp_context->wispr_msg.message_type) {
578 DBG("Login required");
580 wp_context->wispr_result = CONNMAN_WISPR_RESULT_LOGIN;
582 if (__connman_agent_request_login_input(wp_context->service,
583 wispr_portal_request_wispr_login,
585 wispr_portal_error(wp_context);
588 case 120: /* Falling down */
590 if (wp_context->wispr_msg.response_code == 50) {
591 wp_context->wispr_result = CONNMAN_WISPR_RESULT_ONLINE;
593 g_free(wp_context->wispr_username);
594 wp_context->wispr_username = NULL;
596 g_free(wp_context->wispr_password);
597 wp_context->wispr_password = NULL;
599 g_free(wp_context->wispr_formdata);
600 wp_context->wispr_formdata = NULL;
602 wispr_portal_request_portal(wp_context);
606 wispr_portal_error(wp_context);
616 static void wispr_portal_browser_reply_cb(struct connman_service *service,
617 connman_bool_t authentication_done,
618 const char *error, void *user_data)
620 struct connman_wispr_portal_context *wp_context = user_data;
624 if (service == NULL || wp_context == NULL)
627 if (authentication_done == FALSE) {
628 wispr_portal_error(wp_context);
629 free_wispr_routes(wp_context);
633 /* Restarting the test */
634 __connman_wispr_start(service, wp_context->type);
637 static gboolean wispr_portal_web_result(GWebResult *result, gpointer user_data)
639 struct connman_wispr_portal_context *wp_context = user_data;
640 const char *redirect = NULL;
641 const guint8 *chunk = NULL;
642 const char *str = NULL;
648 if (wp_context->request_id == 0)
651 if (wp_context->wispr_result != CONNMAN_WISPR_RESULT_ONLINE) {
652 g_web_result_get_chunk(result, &chunk, &length);
655 g_web_parser_feed_data(wp_context->wispr_parser,
660 g_web_parser_end_data(wp_context->wispr_parser);
662 if (wp_context->wispr_msg.message_type >= 0) {
663 if (wispr_manage_message(result, wp_context) == TRUE)
668 status = g_web_result_get_status(result);
670 DBG("status: %03u", status);
674 if (wp_context->wispr_msg.message_type >= 0)
677 if (g_web_result_get_header(result, "X-ConnMan-Status",
679 portal_manage_status(result, wp_context);
681 __connman_agent_request_browser(wp_context->service,
682 wispr_portal_browser_reply_cb,
683 wp_context->redirect_url, wp_context);
687 if (g_web_result_get_header(result, "Location",
688 &redirect) == FALSE) {
689 __connman_agent_request_browser(wp_context->service,
690 wispr_portal_browser_reply_cb,
691 wp_context->status_url, wp_context);
695 DBG("Redirect URL: %s", redirect);
697 wp_context->redirect_url = g_strdup(redirect);
699 wp_context->request_id = g_web_request_get(wp_context->web,
700 redirect, wispr_portal_web_result,
701 wispr_route_request, wp_context);
705 if (__connman_service_online_check_failed(wp_context->service,
706 wp_context->type) == 0)
707 wispr_portal_error(wp_context);
714 free_wispr_routes(wp_context);
715 wp_context->request_id = 0;
717 wp_context->wispr_msg.message_type = -1;
721 static void proxy_callback(const char *proxy, void *user_data)
723 struct connman_wispr_portal_context *wp_context = user_data;
725 DBG("proxy %s", proxy);
727 wp_context->token = 0;
729 if (proxy != NULL && g_strcmp0(proxy, "DIRECT") != 0)
730 g_web_set_proxy(wp_context->web, proxy);
732 g_web_set_accept(wp_context->web, NULL);
733 g_web_set_user_agent(wp_context->web, "ConnMan/%s wispr", VERSION);
734 g_web_set_close_connection(wp_context->web, TRUE);
736 connman_wispr_message_init(&wp_context->wispr_msg);
738 wp_context->wispr_parser = g_web_parser_new(
739 "<WISPAccessGatewayParam",
740 "WISPAccessGatewayParam>",
741 xml_wispr_parser_callback, wp_context);
743 wispr_portal_request_portal(wp_context);
746 static gboolean no_proxy_callback(gpointer user_data)
748 struct connman_wispr_portal_context *wp_context = user_data;
750 proxy_callback("DIRECT", wp_context);
755 static int wispr_portal_detect(struct connman_wispr_portal_context *wp_context)
757 enum connman_service_proxy_method proxy_method;
758 enum connman_service_type service_type;
759 char *interface = NULL;
760 char **nameservers = NULL;
765 DBG("wispr/portal context %p", wp_context);
766 DBG("service %p", wp_context->service);
768 service_type = connman_service_get_type(wp_context->service);
770 switch (service_type) {
771 case CONNMAN_SERVICE_TYPE_ETHERNET:
772 case CONNMAN_SERVICE_TYPE_WIFI:
773 case CONNMAN_SERVICE_TYPE_WIMAX:
774 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
775 case CONNMAN_SERVICE_TYPE_CELLULAR:
777 case CONNMAN_SERVICE_TYPE_UNKNOWN:
778 case CONNMAN_SERVICE_TYPE_SYSTEM:
779 case CONNMAN_SERVICE_TYPE_GPS:
780 case CONNMAN_SERVICE_TYPE_VPN:
781 case CONNMAN_SERVICE_TYPE_GADGET:
785 interface = connman_service_get_interface(wp_context->service);
786 if (interface == NULL)
789 DBG("interface %s", interface);
791 if_index = connman_inet_ifindex(interface);
793 DBG("Could not get ifindex");
798 nameservers = connman_service_get_nameservers(wp_context->service);
799 if (nameservers == NULL) {
800 DBG("Could not get nameservers");
805 wp_context->web = g_web_new(if_index);
806 if (wp_context->web == NULL) {
807 DBG("Could not set up GWeb");
812 if (getenv("CONNMAN_WEB_DEBUG"))
813 g_web_set_debug(wp_context->web, web_debug, "WEB");
815 if (wp_context->type == CONNMAN_IPCONFIG_TYPE_IPV4) {
816 g_web_set_address_family(wp_context->web, AF_INET);
817 wp_context->status_url = STATUS_URL_IPV4;
819 g_web_set_address_family(wp_context->web, AF_INET6);
820 wp_context->status_url = STATUS_URL_IPV6;
823 for (i = 0; nameservers[i] != NULL; i++)
824 g_web_add_nameserver(wp_context->web, nameservers[i]);
826 proxy_method = connman_service_get_proxy_method(wp_context->service);
828 if (proxy_method != CONNMAN_SERVICE_PROXY_METHOD_DIRECT) {
829 wp_context->token = connman_proxy_lookup(interface,
830 wp_context->status_url,
832 proxy_callback, wp_context);
834 if (wp_context->token == 0)
837 g_timeout_add_seconds(0, no_proxy_callback, wp_context);
841 g_strfreev(nameservers);
847 int __connman_wispr_start(struct connman_service *service,
848 enum connman_ipconfig_type type)
850 struct connman_wispr_portal_context *wp_context = NULL;
851 struct connman_wispr_portal *wispr_portal = NULL;
854 DBG("service %p", service);
856 if (wispr_portal_list == NULL)
859 index = __connman_service_get_index(service);
863 wispr_portal = g_hash_table_lookup(wispr_portal_list,
864 GINT_TO_POINTER(index));
865 if (wispr_portal == NULL) {
866 wispr_portal = g_try_new0(struct connman_wispr_portal, 1);
867 if (wispr_portal == NULL)
870 g_hash_table_replace(wispr_portal_list,
871 GINT_TO_POINTER(index), wispr_portal);
874 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
875 wp_context = wispr_portal->ipv4_context;
876 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
877 wp_context = wispr_portal->ipv6_context;
881 /* If there is already an existing context, we wipe it */
882 if (wp_context != NULL)
883 free_connman_wispr_portal_context(wp_context);
885 wp_context = g_try_new0(struct connman_wispr_portal_context, 1);
886 if (wp_context == NULL)
889 connman_service_ref(service);
891 wp_context->service = service;
892 wp_context->type = type;
894 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
895 wispr_portal->ipv4_context = wp_context;
897 wispr_portal->ipv6_context = wp_context;
899 return wispr_portal_detect(wp_context);
902 void __connman_wispr_stop(struct connman_service *service)
906 DBG("service %p", service);
908 if (wispr_portal_list == NULL)
911 index = __connman_service_get_index(service);
915 g_hash_table_remove(wispr_portal_list, GINT_TO_POINTER(index));
918 int __connman_wispr_init(void)
922 wispr_portal_list = g_hash_table_new_full(g_direct_hash,
923 g_direct_equal, NULL,
924 free_connman_wispr_portal);
929 void __connman_wispr_cleanup(void)
933 g_hash_table_destroy(wispr_portal_list);
934 wispr_portal_list = NULL;