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