Merge "Fix SIGSEV on freeing server domains list" 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         if (TIZEN_INS_ENABLED)
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 = online_check_ipv4_url;
941         } else {
942                 g_web_set_address_family(wp_context->web, AF_INET6);
943                 wp_context->status_url = online_check_ipv6_url;
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              service == wispr_portal->ipv4_context->service) ||
1052             (wispr_portal->ipv6_context &&
1053              service == wispr_portal->ipv6_context->service))
1054                 g_hash_table_remove(wispr_portal_list, GINT_TO_POINTER(index));
1055 }
1056
1057 int __connman_wispr_init(void)
1058 {
1059         DBG("");
1060
1061         wispr_portal_list = g_hash_table_new_full(g_direct_hash,
1062                                                 g_direct_equal, NULL,
1063                                                 free_connman_wispr_portal);
1064
1065         online_check_ipv4_url =
1066                 connman_setting_get_string("OnlineCheckIPv4URL");
1067         online_check_ipv6_url =
1068                 connman_setting_get_string("OnlineCheckIPv6URL");
1069
1070         enable_online_to_ready_transition =
1071                 connman_setting_get_bool("EnableOnlineToReadyTransition");
1072
1073         return 0;
1074 }
1075
1076 void __connman_wispr_cleanup(void)
1077 {
1078         DBG("");
1079
1080         g_hash_table_destroy(wispr_portal_list);
1081         wispr_portal_list = NULL;
1082 }