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