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 enum connman_wispr_result {
49 CONNMAN_WISPR_RESULT_UNKNOWN = 0,
50 CONNMAN_WISPR_RESULT_LOGIN = 1,
51 CONNMAN_WISPR_RESULT_ONLINE = 2,
52 CONNMAN_WISPR_RESULT_FAILED = 3,
55 struct connman_wispr_portal_context {
56 struct connman_service *service;
57 enum connman_ipconfig_type type;
59 /* Portal/WISPr common */
65 GWebParser *wispr_parser;
66 struct connman_wispr_message wispr_msg;
72 enum connman_wispr_result wispr_result;
75 struct connman_wispr_portal {
76 struct connman_wispr_portal_context *ipv4_context;
77 struct connman_wispr_portal_context *ipv6_context;
80 static gboolean wispr_portal_web_result(GWebResult *result, gpointer user_data);
82 static GHashTable *wispr_portal_list = NULL;
84 static void connman_wispr_message_init(struct connman_wispr_message *msg)
88 msg->has_error = FALSE;
89 msg->current_element = NULL;
91 msg->message_type = -1;
92 msg->response_code = -1;
94 g_free(msg->login_url);
95 msg->login_url = NULL;
97 g_free(msg->abort_login_url);
98 msg->abort_login_url = NULL;
100 g_free(msg->logoff_url);
101 msg->logoff_url = NULL;
103 g_free(msg->access_procedure);
104 msg->access_procedure = NULL;
106 g_free(msg->access_location);
107 msg->access_location = NULL;
109 g_free(msg->location_name);
110 msg->location_name = NULL;
113 static void free_connman_wispr_portal_context(struct connman_wispr_portal_context *wp_context)
117 if (wp_context == NULL)
120 if (wp_context->token > 0)
121 connman_proxy_lookup_cancel(wp_context->token);
123 if (wp_context->request_id > 0)
124 g_web_cancel_request(wp_context->web, wp_context->request_id);
126 g_web_unref(wp_context->web);
128 g_web_parser_unref(wp_context->wispr_parser);
129 connman_wispr_message_init(&wp_context->wispr_msg);
131 g_free(wp_context->wispr_username);
132 g_free(wp_context->wispr_password);
133 g_free(wp_context->wispr_formdata);
138 static void free_connman_wispr_portal(gpointer data)
140 struct connman_wispr_portal *wispr_portal = data;
144 if (wispr_portal == NULL)
147 free_connman_wispr_portal_context(wispr_portal->ipv4_context);
148 free_connman_wispr_portal_context(wispr_portal->ipv6_context);
150 g_free(wispr_portal);
153 static const char *message_type_to_string(int message_type)
155 switch (message_type) {
157 return "Initial redirect message";
159 return "Proxy notification";
161 return "Authentication notification";
163 return "Logoff notification";
165 return "Response to Authentication Poll";
167 return "Response to Abort Login";
173 static const char *response_code_to_string(int response_code)
175 switch (response_code) {
179 return "Login succeeded";
181 return "Login failed";
183 return "RADIUS server error/timeout";
185 return "RADIUS server not enabled";
187 return "Logoff succeeded";
189 return "Login aborted";
191 return "Proxy detection/repeat operation";
193 return "Authentication pending";
195 return "Access gateway internal error";
204 WISPR_ELEMENT_NONE = 0,
205 WISPR_ELEMENT_ACCESS_PROCEDURE = 1,
206 WISPR_ELEMENT_ACCESS_LOCATION = 2,
207 WISPR_ELEMENT_LOCATION_NAME = 3,
208 WISPR_ELEMENT_LOGIN_URL = 4,
209 WISPR_ELEMENT_ABORT_LOGIN_URL = 5,
210 WISPR_ELEMENT_MESSAGE_TYPE = 6,
211 WISPR_ELEMENT_RESPONSE_CODE = 7,
212 WISPR_ELEMENT_NEXT_URL = 8,
213 WISPR_ELEMENT_DELAY = 9,
214 WISPR_ELEMENT_REPLY_MESSAGE = 10,
215 WISPR_ELEMENT_LOGIN_RESULTS_URL = 11,
216 WISPR_ELEMENT_LOGOFF_URL = 12,
218 } wispr_element_map[] = {
219 { "AccessProcedure", WISPR_ELEMENT_ACCESS_PROCEDURE },
220 { "AccessLocation", WISPR_ELEMENT_ACCESS_LOCATION },
221 { "LocationName", WISPR_ELEMENT_LOCATION_NAME },
222 { "LoginURL", WISPR_ELEMENT_LOGIN_URL },
223 { "AbortLoginURL", WISPR_ELEMENT_ABORT_LOGIN_URL },
224 { "MessageType", WISPR_ELEMENT_MESSAGE_TYPE },
225 { "ResponseCode", WISPR_ELEMENT_RESPONSE_CODE },
226 { "NextURL", WISPR_ELEMENT_NEXT_URL },
227 { "Delay", WISPR_ELEMENT_DELAY },
228 { "ReplyMessage", WISPR_ELEMENT_REPLY_MESSAGE },
229 { "LoginResultsURL", WISPR_ELEMENT_LOGIN_RESULTS_URL },
230 { "LogoffURL", WISPR_ELEMENT_LOGOFF_URL },
231 { NULL, WISPR_ELEMENT_NONE },
234 static void xml_wispr_start_element_handler(GMarkupParseContext *context,
235 const gchar *element_name,
236 const gchar **attribute_names,
237 const gchar **attribute_values,
238 gpointer user_data, GError **error)
240 struct connman_wispr_message *msg = user_data;
242 msg->current_element = element_name;
245 static void xml_wispr_end_element_handler(GMarkupParseContext *context,
246 const gchar *element_name,
247 gpointer user_data, GError **error)
249 struct connman_wispr_message *msg = user_data;
251 msg->current_element = NULL;
254 static void xml_wispr_text_handler(GMarkupParseContext *context,
255 const gchar *text, gsize text_len,
256 gpointer user_data, GError **error)
258 struct connman_wispr_message *msg = user_data;
261 if (msg->current_element == NULL)
264 for (i = 0; wispr_element_map[i].str; i++) {
265 if (g_str_equal(wispr_element_map[i].str,
266 msg->current_element) == FALSE)
269 switch (wispr_element_map[i].element) {
270 case WISPR_ELEMENT_NONE:
271 case WISPR_ELEMENT_ACCESS_PROCEDURE:
272 g_free(msg->access_procedure);
273 msg->access_procedure = g_strdup(text);
275 case WISPR_ELEMENT_ACCESS_LOCATION:
276 g_free(msg->access_location);
277 msg->access_location = g_strdup(text);
279 case WISPR_ELEMENT_LOCATION_NAME:
280 g_free(msg->location_name);
281 msg->location_name = g_strdup(text);
283 case WISPR_ELEMENT_LOGIN_URL:
284 g_free(msg->login_url);
285 msg->login_url = g_strdup(text);
287 case WISPR_ELEMENT_ABORT_LOGIN_URL:
288 g_free(msg->abort_login_url);
289 msg->abort_login_url = g_strdup(text);
291 case WISPR_ELEMENT_MESSAGE_TYPE:
292 msg->message_type = atoi(text);
294 case WISPR_ELEMENT_RESPONSE_CODE:
295 msg->response_code = atoi(text);
297 case WISPR_ELEMENT_NEXT_URL:
298 case WISPR_ELEMENT_DELAY:
299 case WISPR_ELEMENT_REPLY_MESSAGE:
300 case WISPR_ELEMENT_LOGIN_RESULTS_URL:
302 case WISPR_ELEMENT_LOGOFF_URL:
303 g_free(msg->logoff_url);
304 msg->logoff_url = g_strdup(text);
310 static void xml_wispr_error_handler(GMarkupParseContext *context,
311 GError *error, gpointer user_data)
313 struct connman_wispr_message *msg = user_data;
315 msg->has_error = TRUE;
318 static const GMarkupParser xml_wispr_parser_handlers = {
319 xml_wispr_start_element_handler,
320 xml_wispr_end_element_handler,
321 xml_wispr_text_handler,
323 xml_wispr_error_handler,
326 static void xml_wispr_parser_callback(const char *str, gpointer user_data)
328 struct connman_wispr_portal_context *wp_context = user_data;
329 GMarkupParseContext *parser_context = NULL;
334 parser_context = g_markup_parse_context_new(&xml_wispr_parser_handlers,
335 G_MARKUP_TREAT_CDATA_AS_TEXT,
336 &(wp_context->wispr_msg), NULL);
338 result = g_markup_parse_context_parse(parser_context,
339 str, strlen(str), NULL);
341 result = g_markup_parse_context_end_parse(parser_context, NULL);
343 g_markup_parse_context_free(parser_context);
346 static void web_debug(const char *str, void *data)
348 connman_info("%s: %s\n", (const char *) data, str);
351 static void wispr_portal_error(struct connman_wispr_portal_context *wp_context)
353 DBG("Failed to proceed wispr/portal web request");
355 wp_context->wispr_result = CONNMAN_WISPR_RESULT_FAILED;
358 static void portal_manage_status(GWebResult *result,
359 struct connman_wispr_portal_context *wp_context)
361 const char *str = NULL;
365 /* We currently don't do anything with this info */
366 if (g_web_result_get_header(result, "X-ConnMan-Client-IP",
368 connman_info("Client-IP: %s", str);
370 if (g_web_result_get_header(result, "X-ConnMan-Client-Country",
372 connman_info("Client-Country: %s", str);
374 if (g_web_result_get_header(result, "X-ConnMan-Client-Region",
376 connman_info("Client-Region: %s", str);
378 __connman_service_ipconfig_indicate_state(wp_context->service,
379 CONNMAN_SERVICE_STATE_ONLINE,
383 static void wispr_portal_request_portal(struct connman_wispr_portal_context *wp_context)
387 wp_context->request_id = g_web_request_get(wp_context->web,
388 STATUS_URL, wispr_portal_web_result, wp_context);
390 if (wp_context->request_id == 0)
391 wispr_portal_error(wp_context);
394 static gboolean wispr_input(const guint8 **data, gsize *length,
397 struct connman_wispr_portal_context *wp_context = user_data;
403 buf = g_string_sized_new(100);
405 g_string_append(buf, "button=Login&UserName=");
406 g_string_append_uri_escaped(buf, wp_context->wispr_username,
408 g_string_append(buf, "&Password=");
409 g_string_append_uri_escaped(buf, wp_context->wispr_password,
411 g_string_append(buf, "&FNAME=0&OriginatingServer=");
412 g_string_append_uri_escaped(buf, STATUS_URL, NULL, FALSE);
416 g_free(wp_context->wispr_formdata);
417 wp_context->wispr_formdata = g_string_free(buf, FALSE);
419 *data = (guint8 *) wp_context->wispr_formdata;
425 static void wispr_portal_request_wispr_login(struct connman_service *service,
426 const char *username, const char *password,
429 struct connman_wispr_portal_context *wp_context = user_data;
433 g_free(wp_context->wispr_username);
434 wp_context->wispr_username = g_strdup(username);
436 g_free(wp_context->wispr_password);
437 wp_context->wispr_password = g_strdup(password);
439 wp_context->request_id = g_web_request_post(wp_context->web,
440 wp_context->wispr_msg.login_url,
441 "application/x-www-form-urlencoded",
442 wispr_input, wispr_portal_web_result,
445 connman_wispr_message_init(&wp_context->wispr_msg);
448 static gboolean wispr_manage_message(GWebResult *result,
449 struct connman_wispr_portal_context *wp_context)
451 DBG("Message type: %s (%d)",
452 message_type_to_string(wp_context->wispr_msg.message_type),
453 wp_context->wispr_msg.message_type);
454 DBG("Response code: %s (%d)",
455 response_code_to_string(wp_context->wispr_msg.response_code),
456 wp_context->wispr_msg.response_code);
458 if (wp_context->wispr_msg.access_procedure != NULL)
459 DBG("Access procedure: %s",
460 wp_context->wispr_msg.access_procedure);
461 if (wp_context->wispr_msg.access_location != NULL)
462 DBG("Access location: %s",
463 wp_context->wispr_msg.access_location);
464 if (wp_context->wispr_msg.location_name != NULL)
465 DBG("Location name: %s",
466 wp_context->wispr_msg.location_name);
467 if (wp_context->wispr_msg.login_url != NULL)
468 DBG("Login URL: %s", wp_context->wispr_msg.login_url);
469 if (wp_context->wispr_msg.abort_login_url != NULL)
470 DBG("Abort login URL: %s",
471 wp_context->wispr_msg.abort_login_url);
472 if (wp_context->wispr_msg.logoff_url != NULL)
473 DBG("Logoff URL: %s", wp_context->wispr_msg.logoff_url);
475 switch (wp_context->wispr_msg.message_type) {
477 DBG("Login required");
479 wp_context->wispr_result = CONNMAN_WISPR_RESULT_LOGIN;
481 __connman_agent_request_login_input(wp_context->service,
482 wispr_portal_request_wispr_login,
485 case 120: /* Falling down */
487 if (wp_context->wispr_msg.response_code == 50) {
488 wp_context->wispr_result = CONNMAN_WISPR_RESULT_ONLINE;
490 g_free(wp_context->wispr_username);
491 wp_context->wispr_username = NULL;
493 g_free(wp_context->wispr_password);
494 wp_context->wispr_password = NULL;
496 g_free(wp_context->wispr_formdata);
497 wp_context->wispr_formdata = NULL;
499 wispr_portal_request_portal(wp_context);
503 wispr_portal_error(wp_context);
513 static gboolean wispr_portal_web_result(GWebResult *result, gpointer user_data)
515 struct connman_wispr_portal_context *wp_context = user_data;
516 const char *redirect = NULL;
517 const guint8 *chunk = NULL;
518 const char *str = NULL;
524 if (wp_context->request_id == 0)
527 if (wp_context->wispr_result != CONNMAN_WISPR_RESULT_ONLINE) {
528 g_web_result_get_chunk(result, &chunk, &length);
531 g_web_parser_feed_data(wp_context->wispr_parser,
536 g_web_parser_end_data(wp_context->wispr_parser);
538 if (wp_context->wispr_msg.message_type >= 0) {
539 if (wispr_manage_message(result, wp_context) == TRUE)
544 status = g_web_result_get_status(result);
546 DBG("status: %03u", status);
550 if (wp_context->wispr_msg.message_type >= 0)
553 if (g_web_result_get_header(result, "X-ConnMan-Status",
555 portal_manage_status(result, wp_context);
557 __connman_service_request_login(wp_context->service);
561 if (g_web_result_get_header(result, "Location",
565 DBG("Redirect URL: %s", redirect);
567 wp_context->request_id = g_web_request_get(wp_context->web,
568 redirect, wispr_portal_web_result, wp_context);
572 wispr_portal_error(wp_context);
579 wp_context->request_id = 0;
581 wp_context->wispr_msg.message_type = -1;
585 static void proxy_callback(const char *proxy, void *user_data)
587 struct connman_wispr_portal_context *wp_context = user_data;
589 DBG("proxy %s", proxy);
591 wp_context->token = 0;
594 proxy = getenv("http_proxy");
596 if (getenv("CONNMAN_WEB_DEBUG"))
597 g_web_set_debug(wp_context->web, web_debug, "WEB");
599 if (proxy != NULL && g_strcmp0(proxy, "DIRECT") != 0)
600 g_web_set_proxy(wp_context->web, proxy);
602 g_web_set_accept(wp_context->web, NULL);
603 g_web_set_user_agent(wp_context->web, "ConnMan/%s", VERSION);
604 g_web_set_close_connection(wp_context->web, TRUE);
606 connman_wispr_message_init(&wp_context->wispr_msg);
608 wp_context->wispr_parser = g_web_parser_new(
609 "<WISPAccessGatewayParam",
610 "WISPAccessGatewayParam>",
611 xml_wispr_parser_callback, wp_context);
613 wispr_portal_request_portal(wp_context);
616 static int wispr_portal_detect(struct connman_wispr_portal_context *wp_context)
618 enum connman_service_type service_type;
619 char *interface = NULL;
622 DBG("wispr/portal context %p", wp_context);
623 DBG("service %p", wp_context->service);
625 service_type = connman_service_get_type(wp_context->service);
627 switch (service_type) {
628 case CONNMAN_SERVICE_TYPE_ETHERNET:
629 case CONNMAN_SERVICE_TYPE_WIFI:
630 case CONNMAN_SERVICE_TYPE_WIMAX:
631 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
632 case CONNMAN_SERVICE_TYPE_CELLULAR:
634 case CONNMAN_SERVICE_TYPE_UNKNOWN:
635 case CONNMAN_SERVICE_TYPE_SYSTEM:
636 case CONNMAN_SERVICE_TYPE_GPS:
637 case CONNMAN_SERVICE_TYPE_VPN:
638 case CONNMAN_SERVICE_TYPE_GADGET:
642 interface = connman_service_get_interface(wp_context->service);
643 if (interface == NULL)
646 DBG("interface %s", interface);
648 wp_context->web = g_web_new(0);
649 if (wp_context->web == NULL) {
654 if (wp_context->type == CONNMAN_IPCONFIG_TYPE_IPV4)
655 g_web_set_address_family(wp_context->web, AF_INET);
657 g_web_set_address_family(wp_context->web, AF_INET6);
659 wp_context->token = connman_proxy_lookup(interface,
660 STATUS_URL, wp_context->service,
661 proxy_callback, wp_context);
662 if (wp_context->token == 0)
670 int __connman_wispr_start(struct connman_service *service,
671 enum connman_ipconfig_type type)
673 struct connman_wispr_portal_context *wp_context = NULL;
674 struct connman_wispr_portal *wispr_portal = NULL;
677 DBG("service %p", service);
679 if (wispr_portal_list == NULL)
682 index = __connman_service_get_index(service);
686 wispr_portal = g_hash_table_lookup(wispr_portal_list,
687 GINT_TO_POINTER(index));
688 if (wispr_portal == NULL) {
689 wispr_portal = g_try_new0(struct connman_wispr_portal, 1);
690 if (wispr_portal == NULL)
693 g_hash_table_replace(wispr_portal_list,
694 GINT_TO_POINTER(index), wispr_portal);
697 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
698 wp_context = wispr_portal->ipv4_context;
699 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
700 wp_context = wispr_portal->ipv6_context;
704 if (wp_context == NULL) {
705 wp_context = g_try_new0(struct connman_wispr_portal_context, 1);
706 if (wp_context == NULL)
709 wp_context->service = service;
710 wp_context->type = type;
712 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
713 wispr_portal->ipv4_context = wp_context;
715 wispr_portal->ipv6_context = wp_context;
717 return wispr_portal_detect(wp_context);
723 void __connman_wispr_stop(struct connman_service *service)
727 DBG("service %p", service);
729 if (wispr_portal_list == NULL)
732 index = __connman_service_get_index(service);
736 g_hash_table_remove(wispr_portal_list, GINT_TO_POINTER(index));
739 int __connman_wispr_init(void)
743 wispr_portal_list = g_hash_table_new_full(g_direct_hash,
744 g_direct_equal, NULL,
745 free_connman_wispr_portal);
750 void __connman_wispr_cleanup(void)
754 g_hash_table_destroy(wispr_portal_list);
755 wispr_portal_list = NULL;