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