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