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