948009483714486c4d09ec35a1e3781c23e3791e
[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 struct connman_wispr_message {
37         gboolean has_error;
38         const char *current_element;
39         int message_type;
40         int response_code;
41         char *login_url;
42         char *abort_login_url;
43         char *logoff_url;
44         char *access_procedure;
45         char *access_location;
46         char *location_name;
47 };
48
49 enum connman_wispr_result {
50         CONNMAN_WISPR_RESULT_UNKNOWN = 0,
51         CONNMAN_WISPR_RESULT_LOGIN   = 1,
52         CONNMAN_WISPR_RESULT_ONLINE  = 2,
53         CONNMAN_WISPR_RESULT_FAILED  = 3,
54 };
55
56 struct 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 gboolean 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 != NULL) {
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(struct connman_wispr_portal_context *wp_context)
159 {
160         DBG("context %p", wp_context);
161
162         if (wp_context == NULL)
163                 return;
164
165         if (wp_context->wispr_portal != NULL) {
166                 if (wp_context->wispr_portal->ipv4_context == wp_context)
167                         wp_context->wispr_portal->ipv4_context = NULL;
168
169                 if (wp_context->wispr_portal->ipv6_context == wp_context)
170                         wp_context->wispr_portal->ipv6_context = NULL;
171         }
172
173         if (wp_context->token > 0)
174                 connman_proxy_lookup_cancel(wp_context->token);
175
176         if (wp_context->request_id > 0)
177                 g_web_cancel_request(wp_context->web, wp_context->request_id);
178
179         if (wp_context->timeout > 0)
180                 g_source_remove(wp_context->timeout);
181
182         if (wp_context->web != NULL)
183                 g_web_unref(wp_context->web);
184
185         g_free(wp_context->redirect_url);
186
187         if (wp_context->wispr_parser != NULL)
188                 g_web_parser_unref(wp_context->wispr_parser);
189
190         connman_wispr_message_init(&wp_context->wispr_msg);
191
192         g_free(wp_context->wispr_username);
193         g_free(wp_context->wispr_password);
194         g_free(wp_context->wispr_formdata);
195
196         free_wispr_routes(wp_context);
197
198         g_free(wp_context);
199 }
200
201 static struct connman_wispr_portal_context *create_wispr_portal_context(void)
202 {
203         return g_try_new0(struct connman_wispr_portal_context, 1);
204 }
205
206 static void free_connman_wispr_portal(gpointer data)
207 {
208         struct connman_wispr_portal *wispr_portal = data;
209
210         DBG("");
211
212         if (wispr_portal == NULL)
213                 return;
214
215         free_connman_wispr_portal_context(wispr_portal->ipv4_context);
216         free_connman_wispr_portal_context(wispr_portal->ipv6_context);
217
218         g_free(wispr_portal);
219 }
220
221 static const char *message_type_to_string(int message_type)
222 {
223         switch (message_type) {
224         case 100:
225                 return "Initial redirect message";
226         case 110:
227                 return "Proxy notification";
228         case 120:
229                 return "Authentication notification";
230         case 130:
231                 return "Logoff notification";
232         case 140:
233                 return "Response to Authentication Poll";
234         case 150:
235                 return "Response to Abort Login";
236         }
237
238         return NULL;
239 }
240
241 static const char *response_code_to_string(int response_code)
242 {
243         switch (response_code) {
244         case 0:
245                 return "No error";
246         case 50:
247                 return "Login succeeded";
248         case 100:
249                 return "Login failed";
250         case 102:
251                 return "RADIUS server error/timeout";
252         case 105:
253                 return "RADIUS server not enabled";
254         case 150:
255                 return "Logoff succeeded";
256         case 151:
257                 return "Login aborted";
258         case 200:
259                 return "Proxy detection/repeat operation";
260         case 201:
261                 return "Authentication pending";
262         case 255:
263                 return "Access gateway internal error";
264         }
265
266         return NULL;
267 }
268
269 static struct {
270         const char *str;
271         enum {
272                 WISPR_ELEMENT_NONE              = 0,
273                 WISPR_ELEMENT_ACCESS_PROCEDURE  = 1,
274                 WISPR_ELEMENT_ACCESS_LOCATION   = 2,
275                 WISPR_ELEMENT_LOCATION_NAME     = 3,
276                 WISPR_ELEMENT_LOGIN_URL         = 4,
277                 WISPR_ELEMENT_ABORT_LOGIN_URL   = 5,
278                 WISPR_ELEMENT_MESSAGE_TYPE      = 6,
279                 WISPR_ELEMENT_RESPONSE_CODE     = 7,
280                 WISPR_ELEMENT_NEXT_URL          = 8,
281                 WISPR_ELEMENT_DELAY             = 9,
282                 WISPR_ELEMENT_REPLY_MESSAGE     = 10,
283                 WISPR_ELEMENT_LOGIN_RESULTS_URL = 11,
284                 WISPR_ELEMENT_LOGOFF_URL        = 12,
285         } element;
286 } wispr_element_map[] = {
287         { "AccessProcedure",    WISPR_ELEMENT_ACCESS_PROCEDURE  },
288         { "AccessLocation",     WISPR_ELEMENT_ACCESS_LOCATION   },
289         { "LocationName",       WISPR_ELEMENT_LOCATION_NAME     },
290         { "LoginURL",           WISPR_ELEMENT_LOGIN_URL         },
291         { "AbortLoginURL",      WISPR_ELEMENT_ABORT_LOGIN_URL   },
292         { "MessageType",        WISPR_ELEMENT_MESSAGE_TYPE      },
293         { "ResponseCode",       WISPR_ELEMENT_RESPONSE_CODE     },
294         { "NextURL",            WISPR_ELEMENT_NEXT_URL          },
295         { "Delay",              WISPR_ELEMENT_DELAY             },
296         { "ReplyMessage",       WISPR_ELEMENT_REPLY_MESSAGE     },
297         { "LoginResultsURL",    WISPR_ELEMENT_LOGIN_RESULTS_URL },
298         { "LogoffURL",          WISPR_ELEMENT_LOGOFF_URL        },
299         { NULL,                 WISPR_ELEMENT_NONE              },
300 };
301
302 static void xml_wispr_start_element_handler(GMarkupParseContext *context,
303                                         const gchar *element_name,
304                                         const gchar **attribute_names,
305                                         const gchar **attribute_values,
306                                         gpointer user_data, GError **error)
307 {
308         struct connman_wispr_message *msg = user_data;
309
310         msg->current_element = element_name;
311 }
312
313 static void xml_wispr_end_element_handler(GMarkupParseContext *context,
314                                         const gchar *element_name,
315                                         gpointer user_data, GError **error)
316 {
317         struct connman_wispr_message *msg = user_data;
318
319         msg->current_element = NULL;
320 }
321
322 static void xml_wispr_text_handler(GMarkupParseContext *context,
323                                         const gchar *text, gsize text_len,
324                                         gpointer user_data, GError **error)
325 {
326         struct connman_wispr_message *msg = user_data;
327         int i;
328
329         if (msg->current_element == NULL)
330                 return;
331
332         for (i = 0; wispr_element_map[i].str; i++) {
333                 if (g_str_equal(wispr_element_map[i].str,
334                                         msg->current_element) == FALSE)
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         gboolean 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 == TRUE)
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) == TRUE)
438                 connman_info("Client-IP: %s", str);
439
440         if (g_web_result_get_header(result, "X-ConnMan-Client-Country",
441                                 &str) == TRUE)
442                 connman_info("Client-Country: %s", str);
443
444         if (g_web_result_get_header(result, "X-ConnMan-Client-Region",
445                                 &str) == TRUE)
446                 connman_info("Client-Region: %s", str);
447
448         if (g_web_result_get_header(result, "X-ConnMan-Client-Timezone",
449                                 &str) == TRUE)
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 gboolean 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 == NULL)
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(struct connman_wispr_portal_context *wp_context)
506 {
507         DBG("");
508
509         wp_context->request_id = g_web_request_get(wp_context->web,
510                                         wp_context->status_url,
511                                         wispr_portal_web_result,
512                                         wispr_route_request,
513                                         wp_context);
514
515         if (wp_context->request_id == 0)
516                 wispr_portal_error(wp_context);
517 }
518
519 static gboolean wispr_input(const guint8 **data, gsize *length,
520                                                 gpointer user_data)
521 {
522         struct connman_wispr_portal_context *wp_context = user_data;
523         GString *buf;
524         gsize count;
525
526         DBG("");
527
528         buf = g_string_sized_new(100);
529
530         g_string_append(buf, "button=Login&UserName=");
531         g_string_append_uri_escaped(buf, wp_context->wispr_username,
532                                                                 NULL, FALSE);
533         g_string_append(buf, "&Password=");
534         g_string_append_uri_escaped(buf, wp_context->wispr_password,
535                                                                 NULL, FALSE);
536         g_string_append(buf, "&FNAME=0&OriginatingServer=");
537         g_string_append_uri_escaped(buf, wp_context->status_url, NULL, FALSE);
538
539         count = buf->len;
540
541         g_free(wp_context->wispr_formdata);
542         wp_context->wispr_formdata = g_string_free(buf, FALSE);
543
544         *data = (guint8 *) wp_context->wispr_formdata;
545         *length = count;
546
547         return FALSE;
548 }
549
550 static void wispr_portal_browser_reply_cb(struct connman_service *service,
551                                         connman_bool_t authentication_done,
552                                         const char *error, void *user_data)
553 {
554         struct connman_wispr_portal_context *wp_context = user_data;
555
556         DBG("");
557
558         if (service == NULL || wp_context == NULL)
559                 return;
560
561         if (authentication_done == FALSE) {
562                 wispr_portal_error(wp_context);
563                 free_wispr_routes(wp_context);
564                 return;
565         }
566
567         /* Restarting the test */
568         __connman_wispr_start(service, wp_context->type);
569 }
570
571 static void wispr_portal_request_wispr_login(struct connman_service *service,
572                                 connman_bool_t success,
573                                 const char *ssid, int ssid_len,
574                                 const char *username, const char *password,
575                                 gboolean wps, const char *wpspin,
576                                 const char *error, void *user_data)
577 {
578         struct connman_wispr_portal_context *wp_context = user_data;
579
580         DBG("");
581
582         if (error != NULL) {
583                 if (g_strcmp0(error,
584                         "net.connman.Agent.Error.LaunchBrowser") == 0) {
585                         if (__connman_agent_request_browser(service,
586                                         wispr_portal_browser_reply_cb,
587                                         wp_context->redirect_url,
588                                         wp_context) == -EINPROGRESS)
589                                 return;
590                 }
591
592                 free_connman_wispr_portal_context(wp_context);
593                 return;
594         }
595
596         g_free(wp_context->wispr_username);
597         wp_context->wispr_username = g_strdup(username);
598
599         g_free(wp_context->wispr_password);
600         wp_context->wispr_password = g_strdup(password);
601
602         wp_context->request_id = g_web_request_post(wp_context->web,
603                                         wp_context->wispr_msg.login_url,
604                                         "application/x-www-form-urlencoded",
605                                         wispr_input, wispr_portal_web_result,
606                                         wp_context);
607
608         connman_wispr_message_init(&wp_context->wispr_msg);
609 }
610
611 static gboolean wispr_manage_message(GWebResult *result,
612                         struct connman_wispr_portal_context *wp_context)
613 {
614         DBG("Message type: %s (%d)",
615                 message_type_to_string(wp_context->wispr_msg.message_type),
616                                         wp_context->wispr_msg.message_type);
617         DBG("Response code: %s (%d)",
618                 response_code_to_string(wp_context->wispr_msg.response_code),
619                                         wp_context->wispr_msg.response_code);
620
621         if (wp_context->wispr_msg.access_procedure != NULL)
622                 DBG("Access procedure: %s",
623                         wp_context->wispr_msg.access_procedure);
624         if (wp_context->wispr_msg.access_location != NULL)
625                 DBG("Access location: %s",
626                         wp_context->wispr_msg.access_location);
627         if (wp_context->wispr_msg.location_name != NULL)
628                 DBG("Location name: %s",
629                         wp_context->wispr_msg.location_name);
630         if (wp_context->wispr_msg.login_url != NULL)
631                 DBG("Login URL: %s", wp_context->wispr_msg.login_url);
632         if (wp_context->wispr_msg.abort_login_url != NULL)
633                 DBG("Abort login URL: %s",
634                         wp_context->wispr_msg.abort_login_url);
635         if (wp_context->wispr_msg.logoff_url != NULL)
636                 DBG("Logoff URL: %s", wp_context->wispr_msg.logoff_url);
637
638         switch (wp_context->wispr_msg.message_type) {
639         case 100:
640                 DBG("Login required");
641
642                 wp_context->wispr_result = CONNMAN_WISPR_RESULT_LOGIN;
643
644                 if (__connman_agent_request_login_input(wp_context->service,
645                                         wispr_portal_request_wispr_login,
646                                         wp_context) != -EINPROGRESS)
647                         wispr_portal_error(wp_context);
648
649                 break;
650         case 120: /* Falling down */
651         case 140:
652                 if (wp_context->wispr_msg.response_code == 50) {
653                         wp_context->wispr_result = CONNMAN_WISPR_RESULT_ONLINE;
654
655                         g_free(wp_context->wispr_username);
656                         wp_context->wispr_username = NULL;
657
658                         g_free(wp_context->wispr_password);
659                         wp_context->wispr_password = NULL;
660
661                         g_free(wp_context->wispr_formdata);
662                         wp_context->wispr_formdata = NULL;
663
664                         wispr_portal_request_portal(wp_context);
665
666                         return TRUE;
667                 } else
668                         wispr_portal_error(wp_context);
669
670                 break;
671         default:
672                 break;
673         }
674
675         return FALSE;
676 }
677
678 static gboolean wispr_portal_web_result(GWebResult *result, gpointer user_data)
679 {
680         struct connman_wispr_portal_context *wp_context = user_data;
681         const char *redirect = NULL;
682         const guint8 *chunk = NULL;
683         const char *str = NULL;
684         guint16 status;
685         gsize length;
686
687         DBG("");
688
689         if (wp_context->wispr_result != CONNMAN_WISPR_RESULT_ONLINE) {
690                 g_web_result_get_chunk(result, &chunk, &length);
691
692                 if (length > 0) {
693                         g_web_parser_feed_data(wp_context->wispr_parser,
694                                                                 chunk, length);
695                         return TRUE;
696                 }
697
698                 g_web_parser_end_data(wp_context->wispr_parser);
699
700                 if (wp_context->wispr_msg.message_type >= 0) {
701                         if (wispr_manage_message(result, wp_context) == TRUE)
702                                 goto done;
703                 }
704         }
705
706         status = g_web_result_get_status(result);
707
708         DBG("status: %03u", status);
709
710         switch (status) {
711         case 200:
712                 if (wp_context->wispr_msg.message_type >= 0)
713                         break;
714
715                 if (g_web_result_get_header(result, "X-ConnMan-Status",
716                                                 &str) == TRUE) {
717                         portal_manage_status(result, wp_context);
718                         return FALSE;
719                 }
720                 else
721                         __connman_agent_request_browser(wp_context->service,
722                                         wispr_portal_browser_reply_cb,
723                                         wp_context->redirect_url, wp_context);
724
725                 break;
726         case 302:
727                 if (g_web_supports_tls() == FALSE ||
728                                 g_web_result_get_header(result, "Location",
729                                                         &redirect) == FALSE) {
730
731                         __connman_agent_request_browser(wp_context->service,
732                                         wispr_portal_browser_reply_cb,
733                                         wp_context->status_url, wp_context);
734                         break;
735                 }
736
737                 DBG("Redirect URL: %s", redirect);
738
739                 wp_context->redirect_url = g_strdup(redirect);
740
741                 wp_context->request_id = g_web_request_get(wp_context->web,
742                                 redirect, wispr_portal_web_result,
743                                 wispr_route_request, wp_context);
744
745                 goto done;
746         case 400:
747         case 404:
748                 if (__connman_service_online_check_failed(wp_context->service,
749                                                 wp_context->type) == 0) {
750                         wispr_portal_error(wp_context);
751                         free_connman_wispr_portal_context(wp_context);
752                         return FALSE;
753                 }
754
755                 break;
756         default:
757                 break;
758         }
759
760         free_wispr_routes(wp_context);
761         wp_context->request_id = 0;
762 done:
763         wp_context->wispr_msg.message_type = -1;
764         return FALSE;
765 }
766
767 static void proxy_callback(const char *proxy, void *user_data)
768 {
769         struct connman_wispr_portal_context *wp_context = user_data;
770
771         DBG("proxy %s", proxy);
772
773         if (wp_context == NULL)
774                 return;
775
776         wp_context->token = 0;
777
778         if (proxy != NULL && g_strcmp0(proxy, "DIRECT") != 0)
779                 g_web_set_proxy(wp_context->web, proxy);
780
781         g_web_set_accept(wp_context->web, NULL);
782         g_web_set_user_agent(wp_context->web, "ConnMan/%s wispr", VERSION);
783         g_web_set_close_connection(wp_context->web, TRUE);
784
785         connman_wispr_message_init(&wp_context->wispr_msg);
786
787         wp_context->wispr_parser = g_web_parser_new(
788                                         "<WISPAccessGatewayParam",
789                                         "WISPAccessGatewayParam>",
790                                         xml_wispr_parser_callback, wp_context);
791
792         wispr_portal_request_portal(wp_context);
793 }
794
795 static gboolean no_proxy_callback(gpointer user_data)
796 {
797         struct connman_wispr_portal_context *wp_context = user_data;
798
799         wp_context->timeout = 0;
800
801         proxy_callback("DIRECT", wp_context);
802
803         return FALSE;
804 }
805
806 static int wispr_portal_detect(struct connman_wispr_portal_context *wp_context)
807 {
808         enum connman_service_proxy_method proxy_method;
809         enum connman_service_type service_type;
810         char *interface = NULL;
811         char **nameservers = NULL;
812         int if_index;
813         int err = 0;
814         int i;
815
816         DBG("wispr/portal context %p", wp_context);
817         DBG("service %p", wp_context->service);
818
819         service_type = connman_service_get_type(wp_context->service);
820
821         switch (service_type) {
822         case CONNMAN_SERVICE_TYPE_ETHERNET:
823         case CONNMAN_SERVICE_TYPE_WIFI:
824         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
825         case CONNMAN_SERVICE_TYPE_CELLULAR:
826                 break;
827         case CONNMAN_SERVICE_TYPE_UNKNOWN:
828         case CONNMAN_SERVICE_TYPE_SYSTEM:
829         case CONNMAN_SERVICE_TYPE_GPS:
830         case CONNMAN_SERVICE_TYPE_VPN:
831         case CONNMAN_SERVICE_TYPE_GADGET:
832                 return -EOPNOTSUPP;
833         }
834
835         interface = connman_service_get_interface(wp_context->service);
836         if (interface == NULL)
837                 return -EINVAL;
838
839         DBG("interface %s", interface);
840
841         if_index = connman_inet_ifindex(interface);
842         if (if_index < 0) {
843                 DBG("Could not get ifindex");
844                 err = -EINVAL;
845                 goto done;
846         }
847
848         nameservers = connman_service_get_nameservers(wp_context->service);
849         if (nameservers == NULL) {
850                 DBG("Could not get nameservers");
851                 err = -EINVAL;
852                 goto done;
853         }
854
855         wp_context->web = g_web_new(if_index);
856         if (wp_context->web == NULL) {
857                 DBG("Could not set up GWeb");
858                 err = -ENOMEM;
859                 goto done;
860         }
861
862         if (getenv("CONNMAN_WEB_DEBUG"))
863                 g_web_set_debug(wp_context->web, web_debug, "WEB");
864
865         if (wp_context->type == CONNMAN_IPCONFIG_TYPE_IPV4) {
866                 g_web_set_address_family(wp_context->web, AF_INET);
867                 wp_context->status_url = STATUS_URL_IPV4;
868         } else {
869                 g_web_set_address_family(wp_context->web, AF_INET6);
870                 wp_context->status_url = STATUS_URL_IPV6;
871         }
872
873         for (i = 0; nameservers[i] != NULL; i++)
874                 g_web_add_nameserver(wp_context->web, nameservers[i]);
875
876         proxy_method = connman_service_get_proxy_method(wp_context->service);
877
878         if (proxy_method != CONNMAN_SERVICE_PROXY_METHOD_DIRECT) {
879                 wp_context->token = connman_proxy_lookup(interface,
880                                                 wp_context->status_url,
881                                                 wp_context->service,
882                                                 proxy_callback, wp_context);
883
884                 if (wp_context->token == 0) {
885                         err = -EINVAL;
886                         free_connman_wispr_portal_context(wp_context);
887                 }
888         } else if (wp_context->timeout == 0) {
889                 wp_context->timeout =
890                         g_timeout_add_seconds(0, no_proxy_callback, wp_context);
891         }
892
893 done:
894         g_strfreev(nameservers);
895
896         g_free(interface);
897         return err;
898 }
899
900 int __connman_wispr_start(struct connman_service *service,
901                                         enum connman_ipconfig_type type)
902 {
903         struct connman_wispr_portal_context *wp_context = NULL;
904         struct connman_wispr_portal *wispr_portal = NULL;
905         int index;
906
907         DBG("service %p", service);
908
909         if (wispr_portal_list == NULL)
910                 return -EINVAL;
911
912         index = __connman_service_get_index(service);
913         if (index < 0)
914                 return -EINVAL;
915
916         wispr_portal = g_hash_table_lookup(wispr_portal_list,
917                                         GINT_TO_POINTER(index));
918         if (wispr_portal == NULL) {
919                 wispr_portal = g_try_new0(struct connman_wispr_portal, 1);
920                 if (wispr_portal == NULL)
921                         return -ENOMEM;
922
923                 g_hash_table_replace(wispr_portal_list,
924                                         GINT_TO_POINTER(index), wispr_portal);
925         }
926
927         if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
928                 wp_context = wispr_portal->ipv4_context;
929         else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
930                 wp_context = wispr_portal->ipv6_context;
931         else
932                 return -EINVAL;
933
934         /* If there is already an existing context, we wipe it */
935         if (wp_context != NULL)
936                 free_connman_wispr_portal_context(wp_context);
937
938         wp_context = create_wispr_portal_context();
939         if (wp_context == NULL)
940                 return -ENOMEM;
941
942         wp_context->service = service;
943         wp_context->type = type;
944         wp_context->wispr_portal = wispr_portal;
945
946         if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
947                 wispr_portal->ipv4_context = wp_context;
948         else
949                 wispr_portal->ipv6_context = wp_context;
950
951         return wispr_portal_detect(wp_context);
952 }
953
954 void __connman_wispr_stop(struct connman_service *service)
955 {
956         int index;
957
958         DBG("service %p", service);
959
960         if (wispr_portal_list == NULL)
961                 return;
962
963         index = __connman_service_get_index(service);
964         if (index < 0)
965                 return;
966
967         g_hash_table_remove(wispr_portal_list, GINT_TO_POINTER(index));
968 }
969
970 int __connman_wispr_init(void)
971 {
972         DBG("");
973
974         wispr_portal_list = g_hash_table_new_full(g_direct_hash,
975                                                 g_direct_equal, NULL,
976                                                 free_connman_wispr_portal);
977
978         return 0;
979 }
980
981 void __connman_wispr_cleanup(void)
982 {
983         DBG("");
984
985         g_hash_table_destroy(wispr_portal_list);
986         wispr_portal_list = NULL;
987 }