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