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