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