Merge tag 'upstream/1.41' into tizen
[platform/upstream/connman.git] / src / wispr.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2013  Intel Corporation. All rights reserved.
6  *
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.
10  *
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.
15  *
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
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <errno.h>
27 #include <stdlib.h>
28
29 #include <gweb/gweb.h>
30
31 #include "connman.h"
32
33 struct connman_wispr_message {
34         bool has_error;
35         const char *current_element;
36         int message_type;
37         int response_code;
38         char *login_url;
39         char *abort_login_url;
40         char *logoff_url;
41         char *access_procedure;
42         char *access_location;
43         char *location_name;
44 };
45
46 enum connman_wispr_result {
47         CONNMAN_WISPR_RESULT_UNKNOWN = 0,
48         CONNMAN_WISPR_RESULT_LOGIN   = 1,
49         CONNMAN_WISPR_RESULT_ONLINE  = 2,
50         CONNMAN_WISPR_RESULT_FAILED  = 3,
51 };
52
53 struct wispr_route {
54         char *address;
55         int if_index;
56 };
57
58 struct connman_wispr_portal_context {
59         struct connman_service *service;
60         enum connman_ipconfig_type type;
61         struct connman_wispr_portal *wispr_portal;
62
63         /* Portal/WISPr common */
64         GWeb *web;
65         unsigned int token;
66         guint request_id;
67
68         const char *status_url;
69
70         char *redirect_url;
71
72         /* WISPr specific */
73         GWebParser *wispr_parser;
74         struct connman_wispr_message wispr_msg;
75
76         char *wispr_username;
77         char *wispr_password;
78         char *wispr_formdata;
79
80         enum connman_wispr_result wispr_result;
81
82         GSList *route_list;
83
84         guint timeout;
85 };
86
87 struct connman_wispr_portal {
88         struct connman_wispr_portal_context *ipv4_context;
89         struct connman_wispr_portal_context *ipv6_context;
90 };
91
92 static bool wispr_portal_web_result(GWebResult *result, gpointer user_data);
93
94 static GHashTable *wispr_portal_list = NULL;
95
96 static char *online_check_ipv4_url = NULL;
97 static char *online_check_ipv6_url = NULL;
98 static bool enable_online_to_ready_transition = false;
99
100 static void connman_wispr_message_init(struct connman_wispr_message *msg)
101 {
102         DBG("");
103
104         msg->has_error = false;
105         msg->current_element = NULL;
106
107         msg->message_type = -1;
108         msg->response_code = -1;
109
110         g_free(msg->login_url);
111         msg->login_url = NULL;
112
113         g_free(msg->abort_login_url);
114         msg->abort_login_url = NULL;
115
116         g_free(msg->logoff_url);
117         msg->logoff_url = NULL;
118
119         g_free(msg->access_procedure);
120         msg->access_procedure = NULL;
121
122         g_free(msg->access_location);
123         msg->access_location = NULL;
124
125         g_free(msg->location_name);
126         msg->location_name = NULL;
127 }
128
129 static void free_wispr_routes(struct connman_wispr_portal_context *wp_context)
130 {
131         while (wp_context->route_list) {
132                 struct wispr_route *route = wp_context->route_list->data;
133
134                 DBG("free route to %s if %d type %d", route->address,
135                                 route->if_index, wp_context->type);
136
137                 switch (wp_context->type) {
138                 case CONNMAN_IPCONFIG_TYPE_IPV4:
139                         connman_inet_del_host_route(route->if_index,
140                                         route->address);
141                         break;
142                 case CONNMAN_IPCONFIG_TYPE_IPV6:
143                         connman_inet_del_ipv6_host_route(route->if_index,
144                                         route->address);
145                         break;
146                 case CONNMAN_IPCONFIG_TYPE_UNKNOWN:
147                 case CONNMAN_IPCONFIG_TYPE_ALL:
148                         break;
149                 }
150
151                 g_free(route->address);
152                 g_free(route);
153
154                 wp_context->route_list =
155                         g_slist_delete_link(wp_context->route_list,
156                                         wp_context->route_list);
157         }
158 }
159
160 static void free_connman_wispr_portal_context(
161                 struct connman_wispr_portal_context *wp_context)
162 {
163         DBG("context %p", wp_context);
164
165         if (!wp_context)
166                 return;
167
168         if (wp_context->wispr_portal) {
169                 if (wp_context->wispr_portal->ipv4_context == wp_context)
170                         wp_context->wispr_portal->ipv4_context = NULL;
171
172                 if (wp_context->wispr_portal->ipv6_context == wp_context)
173                         wp_context->wispr_portal->ipv6_context = NULL;
174         }
175
176         if (wp_context->token > 0)
177                 connman_proxy_lookup_cancel(wp_context->token);
178
179         if (wp_context->request_id > 0)
180                 g_web_cancel_request(wp_context->web, wp_context->request_id);
181
182         if (wp_context->timeout > 0)
183                 g_source_remove(wp_context->timeout);
184
185         if (wp_context->web)
186                 g_web_unref(wp_context->web);
187
188         g_free(wp_context->redirect_url);
189
190         if (wp_context->wispr_parser)
191                 g_web_parser_unref(wp_context->wispr_parser);
192
193         connman_wispr_message_init(&wp_context->wispr_msg);
194
195         g_free(wp_context->wispr_username);
196         g_free(wp_context->wispr_password);
197         g_free(wp_context->wispr_formdata);
198
199         free_wispr_routes(wp_context);
200
201         g_free(wp_context);
202 }
203
204 static struct connman_wispr_portal_context *create_wispr_portal_context(void)
205 {
206         return g_try_new0(struct connman_wispr_portal_context, 1);
207 }
208
209 static void free_connman_wispr_portal(gpointer data)
210 {
211         struct connman_wispr_portal *wispr_portal = data;
212
213         DBG("");
214
215         if (!wispr_portal)
216                 return;
217
218         free_connman_wispr_portal_context(wispr_portal->ipv4_context);
219         free_connman_wispr_portal_context(wispr_portal->ipv6_context);
220
221         g_free(wispr_portal);
222 }
223
224 static const char *message_type_to_string(int message_type)
225 {
226         switch (message_type) {
227         case 100:
228                 return "Initial redirect message";
229         case 110:
230                 return "Proxy notification";
231         case 120:
232                 return "Authentication notification";
233         case 130:
234                 return "Logoff notification";
235         case 140:
236                 return "Response to Authentication Poll";
237         case 150:
238                 return "Response to Abort Login";
239         }
240
241         return NULL;
242 }
243
244 static const char *response_code_to_string(int response_code)
245 {
246         switch (response_code) {
247         case 0:
248                 return "No error";
249         case 50:
250                 return "Login succeeded";
251         case 100:
252                 return "Login failed";
253         case 102:
254                 return "RADIUS server error/timeout";
255         case 105:
256                 return "RADIUS server not enabled";
257         case 150:
258                 return "Logoff succeeded";
259         case 151:
260                 return "Login aborted";
261         case 200:
262                 return "Proxy detection/repeat operation";
263         case 201:
264                 return "Authentication pending";
265         case 255:
266                 return "Access gateway internal error";
267         }
268
269         return NULL;
270 }
271
272 static struct {
273         const char *str;
274         enum {
275                 WISPR_ELEMENT_NONE              = 0,
276                 WISPR_ELEMENT_ACCESS_PROCEDURE  = 1,
277                 WISPR_ELEMENT_ACCESS_LOCATION   = 2,
278                 WISPR_ELEMENT_LOCATION_NAME     = 3,
279                 WISPR_ELEMENT_LOGIN_URL         = 4,
280                 WISPR_ELEMENT_ABORT_LOGIN_URL   = 5,
281                 WISPR_ELEMENT_MESSAGE_TYPE      = 6,
282                 WISPR_ELEMENT_RESPONSE_CODE     = 7,
283                 WISPR_ELEMENT_NEXT_URL          = 8,
284                 WISPR_ELEMENT_DELAY             = 9,
285                 WISPR_ELEMENT_REPLY_MESSAGE     = 10,
286                 WISPR_ELEMENT_LOGIN_RESULTS_URL = 11,
287                 WISPR_ELEMENT_LOGOFF_URL        = 12,
288         } element;
289 } wispr_element_map[] = {
290         { "AccessProcedure",    WISPR_ELEMENT_ACCESS_PROCEDURE  },
291         { "AccessLocation",     WISPR_ELEMENT_ACCESS_LOCATION   },
292         { "LocationName",       WISPR_ELEMENT_LOCATION_NAME     },
293         { "LoginURL",           WISPR_ELEMENT_LOGIN_URL         },
294         { "AbortLoginURL",      WISPR_ELEMENT_ABORT_LOGIN_URL   },
295         { "MessageType",        WISPR_ELEMENT_MESSAGE_TYPE      },
296         { "ResponseCode",       WISPR_ELEMENT_RESPONSE_CODE     },
297         { "NextURL",            WISPR_ELEMENT_NEXT_URL          },
298         { "Delay",              WISPR_ELEMENT_DELAY             },
299         { "ReplyMessage",       WISPR_ELEMENT_REPLY_MESSAGE     },
300         { "LoginResultsURL",    WISPR_ELEMENT_LOGIN_RESULTS_URL },
301         { "LogoffURL",          WISPR_ELEMENT_LOGOFF_URL        },
302         { NULL,                 WISPR_ELEMENT_NONE              },
303 };
304
305 static void xml_wispr_start_element_handler(GMarkupParseContext *context,
306                                         const gchar *element_name,
307                                         const gchar **attribute_names,
308                                         const gchar **attribute_values,
309                                         gpointer user_data, GError **error)
310 {
311         struct connman_wispr_message *msg = user_data;
312
313         msg->current_element = element_name;
314 }
315
316 static void xml_wispr_end_element_handler(GMarkupParseContext *context,
317                                         const gchar *element_name,
318                                         gpointer user_data, GError **error)
319 {
320         struct connman_wispr_message *msg = user_data;
321
322         msg->current_element = NULL;
323 }
324
325 static void xml_wispr_text_handler(GMarkupParseContext *context,
326                                         const gchar *text, gsize text_len,
327                                         gpointer user_data, GError **error)
328 {
329         struct connman_wispr_message *msg = user_data;
330         int i;
331
332         if (!msg->current_element)
333                 return;
334
335         for (i = 0; wispr_element_map[i].str; i++) {
336                 if (!g_str_equal(wispr_element_map[i].str, msg->current_element))
337                         continue;
338
339                 switch (wispr_element_map[i].element) {
340                 case WISPR_ELEMENT_NONE:
341                 case WISPR_ELEMENT_ACCESS_PROCEDURE:
342                         g_free(msg->access_procedure);
343                         msg->access_procedure = g_strdup(text);
344                         break;
345                 case WISPR_ELEMENT_ACCESS_LOCATION:
346                         g_free(msg->access_location);
347                         msg->access_location = g_strdup(text);
348                         break;
349                 case WISPR_ELEMENT_LOCATION_NAME:
350                         g_free(msg->location_name);
351                         msg->location_name = g_strdup(text);
352                         break;
353                 case WISPR_ELEMENT_LOGIN_URL:
354                         g_free(msg->login_url);
355                         msg->login_url = g_strdup(text);
356                         break;
357                 case WISPR_ELEMENT_ABORT_LOGIN_URL:
358                         g_free(msg->abort_login_url);
359                         msg->abort_login_url = g_strdup(text);
360                         break;
361                 case WISPR_ELEMENT_MESSAGE_TYPE:
362                         msg->message_type = atoi(text);
363                         break;
364                 case WISPR_ELEMENT_RESPONSE_CODE:
365                         msg->response_code = atoi(text);
366                         break;
367                 case WISPR_ELEMENT_NEXT_URL:
368                 case WISPR_ELEMENT_DELAY:
369                 case WISPR_ELEMENT_REPLY_MESSAGE:
370                 case WISPR_ELEMENT_LOGIN_RESULTS_URL:
371                         break;
372                 case WISPR_ELEMENT_LOGOFF_URL:
373                         g_free(msg->logoff_url);
374                         msg->logoff_url = g_strdup(text);
375                         break;
376                 }
377         }
378 }
379
380 static void xml_wispr_error_handler(GMarkupParseContext *context,
381                                         GError *error, gpointer user_data)
382 {
383         struct connman_wispr_message *msg = user_data;
384
385         msg->has_error = true;
386 }
387
388 static const GMarkupParser xml_wispr_parser_handlers = {
389         xml_wispr_start_element_handler,
390         xml_wispr_end_element_handler,
391         xml_wispr_text_handler,
392         NULL,
393         xml_wispr_error_handler,
394 };
395
396 static void xml_wispr_parser_callback(const char *str, gpointer user_data)
397 {
398         struct connman_wispr_portal_context *wp_context = user_data;
399         GMarkupParseContext *parser_context = NULL;
400         bool result;
401
402         DBG("");
403
404         parser_context = g_markup_parse_context_new(&xml_wispr_parser_handlers,
405                                         G_MARKUP_TREAT_CDATA_AS_TEXT,
406                                         &(wp_context->wispr_msg), NULL);
407
408         result = g_markup_parse_context_parse(parser_context,
409                                         str, strlen(str), NULL);
410         if (result)
411                 g_markup_parse_context_end_parse(parser_context, NULL);
412
413         g_markup_parse_context_free(parser_context);
414 }
415
416 static void web_debug(const char *str, void *data)
417 {
418         connman_info("%s: %s\n", (const char *) data, str);
419 }
420
421 static void wispr_portal_error(struct connman_wispr_portal_context *wp_context)
422 {
423         DBG("Failed to proceed wispr/portal web request");
424
425         wp_context->wispr_result = CONNMAN_WISPR_RESULT_FAILED;
426
427 #if defined TIZEN_EXT
428         connman_service_set_internet_connection(wp_context->service, false);
429 #endif
430 }
431
432 static void portal_manage_status(GWebResult *result,
433                         struct connman_wispr_portal_context *wp_context)
434 {
435         struct connman_service *service = wp_context->service;
436         enum connman_ipconfig_type type = wp_context->type;
437         const char *str = NULL;
438
439         DBG("");
440
441         /* We currently don't do anything with this info */
442         if (g_web_result_get_header(result, "X-ConnMan-Client-IP",
443                                 &str))
444                 connman_info("Client-IP: %s", str);
445
446         if (g_web_result_get_header(result, "X-ConnMan-Client-Country",
447                                 &str))
448                 connman_info("Client-Country: %s", str);
449
450         if (g_web_result_get_header(result, "X-ConnMan-Client-Region",
451                                 &str))
452                 connman_info("Client-Region: %s", str);
453
454         if (g_web_result_get_header(result, "X-ConnMan-Client-Timezone",
455                                 &str))
456                 connman_info("Client-Timezone: %s", str);
457
458         if (!enable_online_to_ready_transition)
459                 free_connman_wispr_portal_context(wp_context);
460
461         __connman_service_ipconfig_indicate_state(service,
462                                         CONNMAN_SERVICE_STATE_ONLINE, type);
463
464         if (enable_online_to_ready_transition)
465                 __connman_service_online_check(service, type, true);
466 }
467
468 static bool wispr_route_request(const char *address, int ai_family,
469                 int if_index, gpointer user_data)
470 {
471         int result = -1;
472         struct connman_wispr_portal_context *wp_context = user_data;
473         const char *gateway;
474         struct wispr_route *route;
475
476         gateway = __connman_ipconfig_get_gateway_from_index(if_index,
477                 wp_context->type);
478
479         DBG("address %s if %d gw %s", address, if_index, gateway);
480
481         if (!gateway)
482                 return false;
483
484         route = g_try_new0(struct wispr_route, 1);
485         if (route == 0) {
486                 DBG("could not create struct");
487                 return false;
488         }
489
490         switch (wp_context->type) {
491         case CONNMAN_IPCONFIG_TYPE_IPV4:
492                 result = connman_inet_add_host_route(if_index, address,
493                                 gateway);
494                 break;
495         case CONNMAN_IPCONFIG_TYPE_IPV6:
496                 result = connman_inet_add_ipv6_host_route(if_index, address,
497                                 gateway);
498                 break;
499         case CONNMAN_IPCONFIG_TYPE_UNKNOWN:
500         case CONNMAN_IPCONFIG_TYPE_ALL:
501                 break;
502         }
503
504         if (result < 0) {
505                 g_free(route);
506                 return false;
507         }
508
509         route->address = g_strdup(address);
510         route->if_index = if_index;
511         wp_context->route_list = g_slist_prepend(wp_context->route_list, route);
512
513         return true;
514 }
515
516 static void wispr_portal_request_portal(
517                 struct connman_wispr_portal_context *wp_context)
518 {
519         DBG("");
520
521         wp_context->request_id = g_web_request_get(wp_context->web,
522                                         wp_context->status_url,
523                                         wispr_portal_web_result,
524                                         wispr_route_request,
525                                         wp_context);
526
527         if (wp_context->request_id == 0)
528                 wispr_portal_error(wp_context);
529 }
530
531 static bool wispr_input(const guint8 **data, gsize *length,
532                                                 gpointer user_data)
533 {
534         struct connman_wispr_portal_context *wp_context = user_data;
535         GString *buf;
536         gsize count;
537
538         DBG("");
539
540         buf = g_string_sized_new(100);
541
542         g_string_append(buf, "button=Login&UserName=");
543         g_string_append_uri_escaped(buf, wp_context->wispr_username,
544                                                                 NULL, FALSE);
545         g_string_append(buf, "&Password=");
546         g_string_append_uri_escaped(buf, wp_context->wispr_password,
547                                                                 NULL, FALSE);
548         g_string_append(buf, "&FNAME=0&OriginatingServer=");
549         g_string_append_uri_escaped(buf, wp_context->status_url, NULL, FALSE);
550
551         count = buf->len;
552
553         g_free(wp_context->wispr_formdata);
554         wp_context->wispr_formdata = g_string_free(buf, FALSE);
555
556         *data = (guint8 *) wp_context->wispr_formdata;
557         *length = count;
558
559         return false;
560 }
561
562 static void wispr_portal_browser_reply_cb(struct connman_service *service,
563                                         bool authentication_done,
564                                         const char *error, void *user_data)
565 {
566         struct connman_wispr_portal_context *wp_context = user_data;
567         struct connman_wispr_portal *wispr_portal;
568         int index;
569
570         DBG("");
571
572         if (!service || !wp_context)
573                 return;
574
575         /*
576          * No way to cancel this if wp_context has been freed, so we lookup
577          * from the service and check that this is still the right context.
578          */
579         index = __connman_service_get_index(service);
580         if (index < 0)
581                 return;
582
583         wispr_portal = g_hash_table_lookup(wispr_portal_list,
584                                         GINT_TO_POINTER(index));
585         if (!wispr_portal)
586                 return;
587
588         if (wp_context != wispr_portal->ipv4_context &&
589                 wp_context != wispr_portal->ipv6_context)
590                 return;
591
592         if (!authentication_done) {
593                 wispr_portal_error(wp_context);
594                 free_wispr_routes(wp_context);
595                 return;
596         }
597
598         /* Restarting the test */
599         __connman_service_wispr_start(service, wp_context->type);
600 }
601
602 static void wispr_portal_request_wispr_login(struct connman_service *service,
603                                 bool success,
604                                 const char *ssid, int ssid_len,
605                                 const char *username, const char *password,
606                                 bool wps, const char *wpspin,
607                                 const char *error, void *user_data)
608 {
609         struct connman_wispr_portal_context *wp_context = user_data;
610
611         DBG("");
612
613         if (error) {
614                 if (g_strcmp0(error,
615                         "net.connman.Agent.Error.LaunchBrowser") == 0) {
616                         if (__connman_agent_request_browser(service,
617                                         wispr_portal_browser_reply_cb,
618                                         wp_context->redirect_url,
619                                         wp_context) == -EINPROGRESS)
620                                 return;
621                 }
622
623                 free_connman_wispr_portal_context(wp_context);
624                 return;
625         }
626
627         g_free(wp_context->wispr_username);
628         wp_context->wispr_username = g_strdup(username);
629
630         g_free(wp_context->wispr_password);
631         wp_context->wispr_password = g_strdup(password);
632
633         wp_context->request_id = g_web_request_post(wp_context->web,
634                                         wp_context->wispr_msg.login_url,
635                                         "application/x-www-form-urlencoded",
636                                         wispr_input, wispr_portal_web_result,
637                                         wp_context);
638
639         connman_wispr_message_init(&wp_context->wispr_msg);
640 }
641
642 static bool wispr_manage_message(GWebResult *result,
643                         struct connman_wispr_portal_context *wp_context)
644 {
645         DBG("Message type: %s (%d)",
646                 message_type_to_string(wp_context->wispr_msg.message_type),
647                                         wp_context->wispr_msg.message_type);
648         DBG("Response code: %s (%d)",
649                 response_code_to_string(wp_context->wispr_msg.response_code),
650                                         wp_context->wispr_msg.response_code);
651
652         if (wp_context->wispr_msg.access_procedure)
653                 DBG("Access procedure: %s",
654                         wp_context->wispr_msg.access_procedure);
655         if (wp_context->wispr_msg.access_location)
656                 DBG("Access location: %s",
657                         wp_context->wispr_msg.access_location);
658         if (wp_context->wispr_msg.location_name)
659                 DBG("Location name: %s",
660                         wp_context->wispr_msg.location_name);
661         if (wp_context->wispr_msg.login_url)
662                 DBG("Login URL: %s", wp_context->wispr_msg.login_url);
663         if (wp_context->wispr_msg.abort_login_url)
664                 DBG("Abort login URL: %s",
665                         wp_context->wispr_msg.abort_login_url);
666         if (wp_context->wispr_msg.logoff_url)
667                 DBG("Logoff URL: %s", wp_context->wispr_msg.logoff_url);
668
669         switch (wp_context->wispr_msg.message_type) {
670         case 100:
671                 DBG("Login required");
672
673                 wp_context->wispr_result = CONNMAN_WISPR_RESULT_LOGIN;
674
675                 if (__connman_agent_request_login_input(wp_context->service,
676                                         wispr_portal_request_wispr_login,
677                                         wp_context) != -EINPROGRESS)
678                         wispr_portal_error(wp_context);
679                 else
680                         return true;
681
682                 break;
683         case 120: /* Falling down */
684         case 140:
685                 if (wp_context->wispr_msg.response_code == 50) {
686                         wp_context->wispr_result = CONNMAN_WISPR_RESULT_ONLINE;
687
688                         g_free(wp_context->wispr_username);
689                         wp_context->wispr_username = NULL;
690
691                         g_free(wp_context->wispr_password);
692                         wp_context->wispr_password = NULL;
693
694                         g_free(wp_context->wispr_formdata);
695                         wp_context->wispr_formdata = NULL;
696
697                         wispr_portal_request_portal(wp_context);
698
699                         return true;
700                 } else
701                         wispr_portal_error(wp_context);
702
703                 break;
704         default:
705                 break;
706         }
707
708         return false;
709 }
710
711 static bool wispr_portal_web_result(GWebResult *result, gpointer user_data)
712 {
713         struct connman_wispr_portal_context *wp_context = user_data;
714         const char *redirect = NULL;
715         const guint8 *chunk = NULL;
716         const char *str = NULL;
717         guint16 status;
718         gsize length;
719 #if defined TIZEN_MAINTAIN_ONLINE
720         static int retried = 0;
721 #endif
722
723         DBG("");
724
725         if (wp_context->wispr_result != CONNMAN_WISPR_RESULT_ONLINE) {
726                 g_web_result_get_chunk(result, &chunk, &length);
727
728                 if (length > 0) {
729                         g_web_parser_feed_data(wp_context->wispr_parser,
730                                                                 chunk, length);
731                         return true;
732                 }
733
734                 g_web_parser_end_data(wp_context->wispr_parser);
735
736                 if (wp_context->wispr_msg.message_type >= 0) {
737                         if (wispr_manage_message(result, wp_context))
738                                 goto done;
739                 }
740         }
741
742         status = g_web_result_get_status(result);
743
744         DBG("status: %03u", status);
745
746         switch (status) {
747         case 000:
748                 __connman_agent_request_browser(wp_context->service,
749                                 wispr_portal_browser_reply_cb,
750                                 wp_context->status_url, wp_context);
751                 break;
752         case 200:
753 #if defined TIZEN_MAINTAIN_ONLINE
754                 retried = 0;
755 #endif
756                 if (wp_context->wispr_msg.message_type >= 0)
757                         break;
758
759                 if (g_web_result_get_header(result, "X-ConnMan-Status",
760                                                 &str)) {
761                         portal_manage_status(result, wp_context);
762                         return false;
763                 } else
764                         __connman_agent_request_browser(wp_context->service,
765                                         wispr_portal_browser_reply_cb,
766                                         wp_context->redirect_url, wp_context);
767
768                 break;
769         case 300:
770         case 301:
771         case 302:
772         case 303:
773         case 307:
774         case 308:
775                 if (!g_web_supports_tls() ||
776                         !g_web_result_get_header(result, "Location",
777                                                         &redirect)) {
778
779                         __connman_agent_request_browser(wp_context->service,
780                                         wispr_portal_browser_reply_cb,
781                                         wp_context->status_url, wp_context);
782                         break;
783                 }
784
785                 DBG("Redirect URL: %s", redirect);
786
787                 wp_context->redirect_url = g_strdup(redirect);
788
789                 wp_context->request_id = g_web_request_get(wp_context->web,
790                                 redirect, wispr_portal_web_result,
791                                 wispr_route_request, wp_context);
792
793                 goto done;
794         case 400:
795         case 404:
796                 __connman_service_online_check(wp_context->service,
797                                                 wp_context->type, false);
798 #if defined TIZEN_MAINTAIN_ONLINE
799                         if (wp_context->type == CONNMAN_IPCONFIG_TYPE_IPV4) {
800                                 if (retried == 0) {
801                                         connman_agent_report_error(wp_context->service,
802                                                 __connman_service_get_path(wp_context->service),
803                                                 "internet-unreachable",
804                                                 NULL, NULL, NULL);
805
806                                         retried = 1;
807                                 }
808                                 break;
809                         }
810 #endif
811                 break;
812         case 505:
813                 __connman_agent_request_browser(wp_context->service,
814                                 wispr_portal_browser_reply_cb,
815                                 wp_context->status_url, wp_context);
816                 break;
817         default:
818                 break;
819         }
820
821         free_wispr_routes(wp_context);
822         wp_context->request_id = 0;
823 done:
824         wp_context->wispr_msg.message_type = -1;
825         return false;
826 }
827
828 static void proxy_callback(const char *proxy, void *user_data)
829 {
830         struct connman_wispr_portal_context *wp_context = user_data;
831
832         DBG("proxy %s", proxy);
833
834         if (!wp_context)
835                 return;
836
837         wp_context->token = 0;
838
839         if (proxy && g_strcmp0(proxy, "DIRECT") != 0) {
840                 if (g_str_has_prefix(proxy, "PROXY")) {
841                         proxy += 5;
842                         for (; *proxy == ' ' && *proxy != '\0'; proxy++);
843                 }
844                 g_web_set_proxy(wp_context->web, proxy);
845         }
846
847         g_web_set_accept(wp_context->web, NULL);
848         g_web_set_user_agent(wp_context->web, "ConnMan/%s wispr", VERSION);
849         g_web_set_close_connection(wp_context->web, TRUE);
850
851         connman_wispr_message_init(&wp_context->wispr_msg);
852
853         wp_context->wispr_parser = g_web_parser_new(
854                                         "<WISPAccessGatewayParam",
855                                         "WISPAccessGatewayParam>",
856                                         xml_wispr_parser_callback, wp_context);
857
858         wispr_portal_request_portal(wp_context);
859 }
860
861 static gboolean no_proxy_callback(gpointer user_data)
862 {
863         struct connman_wispr_portal_context *wp_context = user_data;
864
865         wp_context->timeout = 0;
866
867         proxy_callback("DIRECT", wp_context);
868
869         return FALSE;
870 }
871
872 static int wispr_portal_detect(struct connman_wispr_portal_context *wp_context)
873 {
874         enum connman_service_proxy_method proxy_method;
875         enum connman_service_type service_type;
876         char *interface = NULL;
877         char **nameservers = NULL;
878         int if_index;
879         int err = 0;
880         int i;
881
882         DBG("wispr/portal context %p service %p", wp_context,
883                 wp_context->service);
884
885         service_type = connman_service_get_type(wp_context->service);
886
887         switch (service_type) {
888         case CONNMAN_SERVICE_TYPE_ETHERNET:
889         case CONNMAN_SERVICE_TYPE_WIFI:
890         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
891         case CONNMAN_SERVICE_TYPE_CELLULAR:
892         case CONNMAN_SERVICE_TYPE_GADGET:
893                 break;
894         case CONNMAN_SERVICE_TYPE_UNKNOWN:
895         case CONNMAN_SERVICE_TYPE_SYSTEM:
896         case CONNMAN_SERVICE_TYPE_GPS:
897         case CONNMAN_SERVICE_TYPE_VPN:
898         case CONNMAN_SERVICE_TYPE_P2P:
899 #if defined TIZEN_EXT_WIFI_MESH
900         case CONNMAN_SERVICE_TYPE_MESH:
901 #endif
902                 return -EOPNOTSUPP;
903         }
904
905         interface = connman_service_get_interface(wp_context->service);
906         if (!interface)
907                 return -EINVAL;
908
909         DBG("interface %s", interface);
910
911         if_index = connman_inet_ifindex(interface);
912         if (if_index < 0) {
913                 DBG("Could not get ifindex");
914                 err = -EINVAL;
915                 goto done;
916         }
917
918         nameservers = connman_service_get_nameservers(wp_context->service);
919         if (!nameservers) {
920                 DBG("Could not get nameservers");
921                 err = -EINVAL;
922                 goto done;
923         }
924
925         wp_context->web = g_web_new(if_index);
926         if (!wp_context->web) {
927                 DBG("Could not set up GWeb");
928                 err = -ENOMEM;
929                 goto done;
930         }
931
932 #if !defined TIZEN_EXT
933         if (getenv("CONNMAN_WEB_DEBUG"))
934 #endif
935                 g_web_set_debug(wp_context->web, web_debug, "WEB");
936
937         if (wp_context->type == CONNMAN_IPCONFIG_TYPE_IPV4) {
938                 g_web_set_address_family(wp_context->web, AF_INET);
939                 wp_context->status_url = online_check_ipv4_url;
940         } else {
941                 g_web_set_address_family(wp_context->web, AF_INET6);
942                 wp_context->status_url = online_check_ipv6_url;
943         }
944
945         for (i = 0; nameservers[i]; i++)
946                 g_web_add_nameserver(wp_context->web, nameservers[i]);
947
948         proxy_method = connman_service_get_proxy_method(wp_context->service);
949
950         if (proxy_method != CONNMAN_SERVICE_PROXY_METHOD_DIRECT) {
951                 wp_context->token = connman_proxy_lookup(interface,
952                                                 wp_context->status_url,
953                                                 wp_context->service,
954                                                 proxy_callback, wp_context);
955
956                 if (wp_context->token == 0) {
957                         err = -EINVAL;
958                         free_connman_wispr_portal_context(wp_context);
959                 }
960         } else if (wp_context->timeout == 0) {
961                 wp_context->timeout = g_idle_add(no_proxy_callback, wp_context);
962         }
963
964 done:
965         g_strfreev(nameservers);
966
967         g_free(interface);
968         return err;
969 }
970
971 int __connman_wispr_start(struct connman_service *service,
972                                         enum connman_ipconfig_type type)
973 {
974         struct connman_wispr_portal_context *wp_context = NULL;
975         struct connman_wispr_portal *wispr_portal = NULL;
976         int index;
977
978         DBG("service %p", service);
979
980 #if defined TIZEN_EXT
981         if (connman_service_get_type(service) == CONNMAN_SERVICE_TYPE_CELLULAR)
982                 return -EPERM;
983 #endif
984
985         if (!wispr_portal_list)
986                 return -EINVAL;
987
988         index = __connman_service_get_index(service);
989         if (index < 0)
990                 return -EINVAL;
991
992         wispr_portal = g_hash_table_lookup(wispr_portal_list,
993                                         GINT_TO_POINTER(index));
994         if (!wispr_portal) {
995                 wispr_portal = g_try_new0(struct connman_wispr_portal, 1);
996                 if (!wispr_portal)
997                         return -ENOMEM;
998
999                 g_hash_table_replace(wispr_portal_list,
1000                                         GINT_TO_POINTER(index), wispr_portal);
1001         }
1002
1003         if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
1004                 wp_context = wispr_portal->ipv4_context;
1005         else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
1006                 wp_context = wispr_portal->ipv6_context;
1007         else
1008                 return -EINVAL;
1009
1010         /* If there is already an existing context, we wipe it */
1011         if (wp_context)
1012                 free_connman_wispr_portal_context(wp_context);
1013
1014         wp_context = create_wispr_portal_context();
1015         if (!wp_context)
1016                 return -ENOMEM;
1017
1018         wp_context->service = service;
1019         wp_context->type = type;
1020         wp_context->wispr_portal = wispr_portal;
1021
1022         if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
1023                 wispr_portal->ipv4_context = wp_context;
1024         else
1025                 wispr_portal->ipv6_context = wp_context;
1026
1027         return wispr_portal_detect(wp_context);
1028 }
1029
1030 void __connman_wispr_stop(struct connman_service *service)
1031 {
1032         struct connman_wispr_portal *wispr_portal;
1033         int index;
1034
1035         DBG("service %p", service);
1036
1037         if (!wispr_portal_list)
1038                 return;
1039
1040         index = __connman_service_get_index(service);
1041         if (index < 0)
1042                 return;
1043
1044         wispr_portal = g_hash_table_lookup(wispr_portal_list,
1045                                         GINT_TO_POINTER(index));
1046         if (!wispr_portal)
1047                 return;
1048
1049         if ((wispr_portal->ipv4_context &&
1050              service == wispr_portal->ipv4_context->service) ||
1051             (wispr_portal->ipv6_context &&
1052              service == wispr_portal->ipv6_context->service))
1053                 g_hash_table_remove(wispr_portal_list, GINT_TO_POINTER(index));
1054 }
1055
1056 int __connman_wispr_init(void)
1057 {
1058         DBG("");
1059
1060         wispr_portal_list = g_hash_table_new_full(g_direct_hash,
1061                                                 g_direct_equal, NULL,
1062                                                 free_connman_wispr_portal);
1063
1064         online_check_ipv4_url =
1065                 connman_setting_get_string("OnlineCheckIPv4URL");
1066         online_check_ipv6_url =
1067                 connman_setting_get_string("OnlineCheckIPv6URL");
1068
1069         enable_online_to_ready_transition =
1070                 connman_setting_get_bool("EnableOnlineToReadyTransition");
1071
1072         return 0;
1073 }
1074
1075 void __connman_wispr_cleanup(void)
1076 {
1077         DBG("");
1078
1079         g_hash_table_destroy(wispr_portal_list);
1080         wispr_portal_list = NULL;
1081 }