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