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 if (wp_context->token > 0)
124 connman_proxy_lookup_cancel(wp_context->token);
126 if (wp_context->request_id > 0)
127 g_web_cancel_request(wp_context->web, wp_context->request_id);
129 g_web_unref(wp_context->web);
131 g_web_parser_unref(wp_context->wispr_parser);
132 connman_wispr_message_init(&wp_context->wispr_msg);
134 g_free(wp_context->wispr_username);
135 g_free(wp_context->wispr_password);
136 g_free(wp_context->wispr_formdata);
141 static void free_connman_wispr_portal(gpointer data)
143 struct connman_wispr_portal *wispr_portal = data;
147 if (wispr_portal == NULL)
150 free_connman_wispr_portal_context(wispr_portal->ipv4_context);
151 free_connman_wispr_portal_context(wispr_portal->ipv6_context);
153 g_free(wispr_portal);
156 static const char *message_type_to_string(int message_type)
158 switch (message_type) {
160 return "Initial redirect message";
162 return "Proxy notification";
164 return "Authentication notification";
166 return "Logoff notification";
168 return "Response to Authentication Poll";
170 return "Response to Abort Login";
176 static const char *response_code_to_string(int response_code)
178 switch (response_code) {
182 return "Login succeeded";
184 return "Login failed";
186 return "RADIUS server error/timeout";
188 return "RADIUS server not enabled";
190 return "Logoff succeeded";
192 return "Login aborted";
194 return "Proxy detection/repeat operation";
196 return "Authentication pending";
198 return "Access gateway internal error";
207 WISPR_ELEMENT_NONE = 0,
208 WISPR_ELEMENT_ACCESS_PROCEDURE = 1,
209 WISPR_ELEMENT_ACCESS_LOCATION = 2,
210 WISPR_ELEMENT_LOCATION_NAME = 3,
211 WISPR_ELEMENT_LOGIN_URL = 4,
212 WISPR_ELEMENT_ABORT_LOGIN_URL = 5,
213 WISPR_ELEMENT_MESSAGE_TYPE = 6,
214 WISPR_ELEMENT_RESPONSE_CODE = 7,
215 WISPR_ELEMENT_NEXT_URL = 8,
216 WISPR_ELEMENT_DELAY = 9,
217 WISPR_ELEMENT_REPLY_MESSAGE = 10,
218 WISPR_ELEMENT_LOGIN_RESULTS_URL = 11,
219 WISPR_ELEMENT_LOGOFF_URL = 12,
221 } wispr_element_map[] = {
222 { "AccessProcedure", WISPR_ELEMENT_ACCESS_PROCEDURE },
223 { "AccessLocation", WISPR_ELEMENT_ACCESS_LOCATION },
224 { "LocationName", WISPR_ELEMENT_LOCATION_NAME },
225 { "LoginURL", WISPR_ELEMENT_LOGIN_URL },
226 { "AbortLoginURL", WISPR_ELEMENT_ABORT_LOGIN_URL },
227 { "MessageType", WISPR_ELEMENT_MESSAGE_TYPE },
228 { "ResponseCode", WISPR_ELEMENT_RESPONSE_CODE },
229 { "NextURL", WISPR_ELEMENT_NEXT_URL },
230 { "Delay", WISPR_ELEMENT_DELAY },
231 { "ReplyMessage", WISPR_ELEMENT_REPLY_MESSAGE },
232 { "LoginResultsURL", WISPR_ELEMENT_LOGIN_RESULTS_URL },
233 { "LogoffURL", WISPR_ELEMENT_LOGOFF_URL },
234 { NULL, WISPR_ELEMENT_NONE },
237 static void xml_wispr_start_element_handler(GMarkupParseContext *context,
238 const gchar *element_name,
239 const gchar **attribute_names,
240 const gchar **attribute_values,
241 gpointer user_data, GError **error)
243 struct connman_wispr_message *msg = user_data;
245 msg->current_element = element_name;
248 static void xml_wispr_end_element_handler(GMarkupParseContext *context,
249 const gchar *element_name,
250 gpointer user_data, GError **error)
252 struct connman_wispr_message *msg = user_data;
254 msg->current_element = NULL;
257 static void xml_wispr_text_handler(GMarkupParseContext *context,
258 const gchar *text, gsize text_len,
259 gpointer user_data, GError **error)
261 struct connman_wispr_message *msg = user_data;
264 if (msg->current_element == NULL)
267 for (i = 0; wispr_element_map[i].str; i++) {
268 if (g_str_equal(wispr_element_map[i].str,
269 msg->current_element) == FALSE)
272 switch (wispr_element_map[i].element) {
273 case WISPR_ELEMENT_NONE:
274 case WISPR_ELEMENT_ACCESS_PROCEDURE:
275 g_free(msg->access_procedure);
276 msg->access_procedure = g_strdup(text);
278 case WISPR_ELEMENT_ACCESS_LOCATION:
279 g_free(msg->access_location);
280 msg->access_location = g_strdup(text);
282 case WISPR_ELEMENT_LOCATION_NAME:
283 g_free(msg->location_name);
284 msg->location_name = g_strdup(text);
286 case WISPR_ELEMENT_LOGIN_URL:
287 g_free(msg->login_url);
288 msg->login_url = g_strdup(text);
290 case WISPR_ELEMENT_ABORT_LOGIN_URL:
291 g_free(msg->abort_login_url);
292 msg->abort_login_url = g_strdup(text);
294 case WISPR_ELEMENT_MESSAGE_TYPE:
295 msg->message_type = atoi(text);
297 case WISPR_ELEMENT_RESPONSE_CODE:
298 msg->response_code = atoi(text);
300 case WISPR_ELEMENT_NEXT_URL:
301 case WISPR_ELEMENT_DELAY:
302 case WISPR_ELEMENT_REPLY_MESSAGE:
303 case WISPR_ELEMENT_LOGIN_RESULTS_URL:
305 case WISPR_ELEMENT_LOGOFF_URL:
306 g_free(msg->logoff_url);
307 msg->logoff_url = g_strdup(text);
313 static void xml_wispr_error_handler(GMarkupParseContext *context,
314 GError *error, gpointer user_data)
316 struct connman_wispr_message *msg = user_data;
318 msg->has_error = TRUE;
321 static const GMarkupParser xml_wispr_parser_handlers = {
322 xml_wispr_start_element_handler,
323 xml_wispr_end_element_handler,
324 xml_wispr_text_handler,
326 xml_wispr_error_handler,
329 static void xml_wispr_parser_callback(const char *str, gpointer user_data)
331 struct connman_wispr_portal_context *wp_context = user_data;
332 GMarkupParseContext *parser_context = NULL;
337 parser_context = g_markup_parse_context_new(&xml_wispr_parser_handlers,
338 G_MARKUP_TREAT_CDATA_AS_TEXT,
339 &(wp_context->wispr_msg), NULL);
341 result = g_markup_parse_context_parse(parser_context,
342 str, strlen(str), NULL);
344 result = g_markup_parse_context_end_parse(parser_context, NULL);
346 g_markup_parse_context_free(parser_context);
349 static void web_debug(const char *str, void *data)
351 connman_info("%s: %s\n", (const char *) data, str);
354 static void wispr_portal_error(struct connman_wispr_portal_context *wp_context)
356 DBG("Failed to proceed wispr/portal web request");
358 wp_context->wispr_result = CONNMAN_WISPR_RESULT_FAILED;
361 static void portal_manage_status(GWebResult *result,
362 struct connman_wispr_portal_context *wp_context)
364 const char *str = NULL;
368 /* We currently don't do anything with this info */
369 if (g_web_result_get_header(result, "X-ConnMan-Client-IP",
371 connman_info("Client-IP: %s", str);
373 if (g_web_result_get_header(result, "X-ConnMan-Client-Country",
375 connman_info("Client-Country: %s", str);
377 if (g_web_result_get_header(result, "X-ConnMan-Client-Region",
379 connman_info("Client-Region: %s", str);
381 __connman_service_ipconfig_indicate_state(wp_context->service,
382 CONNMAN_SERVICE_STATE_ONLINE,
386 static void wispr_portal_request_portal(struct connman_wispr_portal_context *wp_context)
390 wp_context->request_id = g_web_request_get(wp_context->web,
391 wp_context->status_url,
392 wispr_portal_web_result, wp_context);
394 if (wp_context->request_id == 0)
395 wispr_portal_error(wp_context);
398 static gboolean wispr_input(const guint8 **data, gsize *length,
401 struct connman_wispr_portal_context *wp_context = user_data;
407 buf = g_string_sized_new(100);
409 g_string_append(buf, "button=Login&UserName=");
410 g_string_append_uri_escaped(buf, wp_context->wispr_username,
412 g_string_append(buf, "&Password=");
413 g_string_append_uri_escaped(buf, wp_context->wispr_password,
415 g_string_append(buf, "&FNAME=0&OriginatingServer=");
416 g_string_append_uri_escaped(buf, wp_context->status_url, NULL, FALSE);
420 g_free(wp_context->wispr_formdata);
421 wp_context->wispr_formdata = g_string_free(buf, FALSE);
423 *data = (guint8 *) wp_context->wispr_formdata;
429 static void wispr_portal_request_wispr_login(struct connman_service *service,
430 const char *username, const char *password,
433 struct connman_wispr_portal_context *wp_context = user_data;
437 g_free(wp_context->wispr_username);
438 wp_context->wispr_username = g_strdup(username);
440 g_free(wp_context->wispr_password);
441 wp_context->wispr_password = g_strdup(password);
443 wp_context->request_id = g_web_request_post(wp_context->web,
444 wp_context->wispr_msg.login_url,
445 "application/x-www-form-urlencoded",
446 wispr_input, wispr_portal_web_result,
449 connman_wispr_message_init(&wp_context->wispr_msg);
452 static gboolean wispr_manage_message(GWebResult *result,
453 struct connman_wispr_portal_context *wp_context)
455 DBG("Message type: %s (%d)",
456 message_type_to_string(wp_context->wispr_msg.message_type),
457 wp_context->wispr_msg.message_type);
458 DBG("Response code: %s (%d)",
459 response_code_to_string(wp_context->wispr_msg.response_code),
460 wp_context->wispr_msg.response_code);
462 if (wp_context->wispr_msg.access_procedure != NULL)
463 DBG("Access procedure: %s",
464 wp_context->wispr_msg.access_procedure);
465 if (wp_context->wispr_msg.access_location != NULL)
466 DBG("Access location: %s",
467 wp_context->wispr_msg.access_location);
468 if (wp_context->wispr_msg.location_name != NULL)
469 DBG("Location name: %s",
470 wp_context->wispr_msg.location_name);
471 if (wp_context->wispr_msg.login_url != NULL)
472 DBG("Login URL: %s", wp_context->wispr_msg.login_url);
473 if (wp_context->wispr_msg.abort_login_url != NULL)
474 DBG("Abort login URL: %s",
475 wp_context->wispr_msg.abort_login_url);
476 if (wp_context->wispr_msg.logoff_url != NULL)
477 DBG("Logoff URL: %s", wp_context->wispr_msg.logoff_url);
479 switch (wp_context->wispr_msg.message_type) {
481 DBG("Login required");
483 wp_context->wispr_result = CONNMAN_WISPR_RESULT_LOGIN;
485 __connman_service_request_login(wp_context->service);
487 if (__connman_agent_request_login_input(wp_context->service,
488 wispr_portal_request_wispr_login,
490 wispr_portal_error(wp_context);
493 case 120: /* Falling down */
495 if (wp_context->wispr_msg.response_code == 50) {
496 wp_context->wispr_result = CONNMAN_WISPR_RESULT_ONLINE;
498 g_free(wp_context->wispr_username);
499 wp_context->wispr_username = NULL;
501 g_free(wp_context->wispr_password);
502 wp_context->wispr_password = NULL;
504 g_free(wp_context->wispr_formdata);
505 wp_context->wispr_formdata = NULL;
507 wispr_portal_request_portal(wp_context);
511 wispr_portal_error(wp_context);
521 static gboolean wispr_portal_web_result(GWebResult *result, gpointer user_data)
523 struct connman_wispr_portal_context *wp_context = user_data;
524 const char *redirect = NULL;
525 const guint8 *chunk = NULL;
526 const char *str = NULL;
532 if (wp_context->request_id == 0)
535 if (wp_context->wispr_result != CONNMAN_WISPR_RESULT_ONLINE) {
536 g_web_result_get_chunk(result, &chunk, &length);
539 g_web_parser_feed_data(wp_context->wispr_parser,
544 g_web_parser_end_data(wp_context->wispr_parser);
546 if (wp_context->wispr_msg.message_type >= 0) {
547 if (wispr_manage_message(result, wp_context) == TRUE)
552 status = g_web_result_get_status(result);
554 DBG("status: %03u", status);
558 if (wp_context->wispr_msg.message_type >= 0)
561 if (g_web_result_get_header(result, "X-ConnMan-Status",
563 portal_manage_status(result, wp_context);
565 __connman_service_request_login(wp_context->service);
569 if (g_web_result_get_header(result, "Location",
573 DBG("Redirect URL: %s", redirect);
575 wp_context->request_id = g_web_request_get(wp_context->web,
576 redirect, wispr_portal_web_result, wp_context);
580 wispr_portal_error(wp_context);
587 wp_context->request_id = 0;
589 wp_context->wispr_msg.message_type = -1;
593 static void proxy_callback(const char *proxy, void *user_data)
595 struct connman_wispr_portal_context *wp_context = user_data;
597 DBG("proxy %s", proxy);
599 wp_context->token = 0;
602 proxy = getenv("http_proxy");
604 if (getenv("CONNMAN_WEB_DEBUG"))
605 g_web_set_debug(wp_context->web, web_debug, "WEB");
607 if (proxy != NULL && g_strcmp0(proxy, "DIRECT") != 0)
608 g_web_set_proxy(wp_context->web, proxy);
610 g_web_set_accept(wp_context->web, NULL);
611 g_web_set_user_agent(wp_context->web, "ConnMan/%s wispr", VERSION);
612 g_web_set_close_connection(wp_context->web, TRUE);
614 connman_wispr_message_init(&wp_context->wispr_msg);
616 wp_context->wispr_parser = g_web_parser_new(
617 "<WISPAccessGatewayParam",
618 "WISPAccessGatewayParam>",
619 xml_wispr_parser_callback, wp_context);
621 wispr_portal_request_portal(wp_context);
624 static int wispr_portal_detect(struct connman_wispr_portal_context *wp_context)
626 enum connman_service_type service_type;
627 char *interface = NULL;
630 DBG("wispr/portal context %p", wp_context);
631 DBG("service %p", wp_context->service);
633 service_type = connman_service_get_type(wp_context->service);
635 switch (service_type) {
636 case CONNMAN_SERVICE_TYPE_ETHERNET:
637 case CONNMAN_SERVICE_TYPE_WIFI:
638 case CONNMAN_SERVICE_TYPE_WIMAX:
639 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
640 case CONNMAN_SERVICE_TYPE_CELLULAR:
642 case CONNMAN_SERVICE_TYPE_UNKNOWN:
643 case CONNMAN_SERVICE_TYPE_SYSTEM:
644 case CONNMAN_SERVICE_TYPE_GPS:
645 case CONNMAN_SERVICE_TYPE_VPN:
646 case CONNMAN_SERVICE_TYPE_GADGET:
650 interface = connman_service_get_interface(wp_context->service);
651 if (interface == NULL)
654 DBG("interface %s", interface);
656 wp_context->web = g_web_new(0);
657 if (wp_context->web == NULL) {
662 if (wp_context->type == CONNMAN_IPCONFIG_TYPE_IPV4) {
663 g_web_set_address_family(wp_context->web, AF_INET);
664 wp_context->status_url = STATUS_URL_IPV4;
666 g_web_set_address_family(wp_context->web, AF_INET6);
667 wp_context->status_url = STATUS_URL_IPV6;
670 wp_context->token = connman_proxy_lookup(interface,
671 wp_context->status_url,
673 proxy_callback, wp_context);
674 if (wp_context->token == 0)
682 int __connman_wispr_start(struct connman_service *service,
683 enum connman_ipconfig_type type)
685 struct connman_wispr_portal_context *wp_context = NULL;
686 struct connman_wispr_portal *wispr_portal = NULL;
689 DBG("service %p", service);
691 if (wispr_portal_list == NULL)
694 index = __connman_service_get_index(service);
698 wispr_portal = g_hash_table_lookup(wispr_portal_list,
699 GINT_TO_POINTER(index));
700 if (wispr_portal == NULL) {
701 wispr_portal = g_try_new0(struct connman_wispr_portal, 1);
702 if (wispr_portal == NULL)
705 g_hash_table_replace(wispr_portal_list,
706 GINT_TO_POINTER(index), wispr_portal);
709 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
710 wp_context = wispr_portal->ipv4_context;
711 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
712 wp_context = wispr_portal->ipv6_context;
716 /* If there is already an existing context, we wipe it */
717 if (wp_context != NULL)
718 free_connman_wispr_portal_context(wp_context);
720 wp_context = g_try_new0(struct connman_wispr_portal_context, 1);
721 if (wp_context == NULL)
724 wp_context->service = service;
725 wp_context->type = type;
727 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
728 wispr_portal->ipv4_context = wp_context;
730 wispr_portal->ipv6_context = wp_context;
732 return wispr_portal_detect(wp_context);
735 void __connman_wispr_stop(struct connman_service *service)
739 DBG("service %p", service);
741 if (wispr_portal_list == NULL)
744 index = __connman_service_get_index(service);
748 g_hash_table_remove(wispr_portal_list, GINT_TO_POINTER(index));
751 int __connman_wispr_init(void)
755 wispr_portal_list = g_hash_table_new_full(g_direct_hash,
756 g_direct_equal, NULL,
757 free_connman_wispr_portal);
762 void __connman_wispr_cleanup(void)
766 g_hash_table_destroy(wispr_portal_list);
767 wispr_portal_list = NULL;