wispr: Free wispr portal context in the relevant place
[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         free_connman_wispr_portal_context(wp_context);
449
450         __connman_service_ipconfig_indicate_state(service,
451                                         CONNMAN_SERVICE_STATE_ONLINE, type);
452 }
453
454 static gboolean wispr_route_request(const char *address, int ai_family,
455                 int if_index, gpointer user_data)
456 {
457         int result = -1;
458         struct connman_wispr_portal_context *wp_context = user_data;
459         const char *gateway;
460         struct wispr_route *route;
461
462         gateway = __connman_ipconfig_get_gateway_from_index(if_index,
463                 wp_context->type);
464
465         DBG("address %s if %d gw %s", address, if_index, gateway);
466
467         if (gateway == NULL)
468                 return FALSE;
469
470         route = g_try_new0(struct wispr_route, 1);
471         if (route == 0) {
472                 DBG("could not create struct");
473                 return FALSE;
474         }
475
476         switch(wp_context->type) {
477         case CONNMAN_IPCONFIG_TYPE_IPV4:
478                 result = connman_inet_add_host_route(if_index, address,
479                                 gateway);
480                 break;
481         case CONNMAN_IPCONFIG_TYPE_IPV6:
482                 result = connman_inet_add_ipv6_host_route(if_index, address,
483                                 gateway);
484                 break;
485         case CONNMAN_IPCONFIG_TYPE_UNKNOWN:
486                 break;
487         }
488
489         if (result < 0) {
490                 g_free(route);
491                 return FALSE;
492         }
493
494         route->address = g_strdup(address);
495         route->if_index = if_index;
496         wp_context->route_list = g_slist_prepend(wp_context->route_list, route);
497
498         return TRUE;
499 }
500
501 static void wispr_portal_request_portal(struct connman_wispr_portal_context *wp_context)
502 {
503         DBG("");
504
505         wp_context->request_id = g_web_request_get(wp_context->web,
506                                         wp_context->status_url,
507                                         wispr_portal_web_result,
508                                         wispr_route_request,
509                                         wp_context);
510
511         if (wp_context->request_id == 0)
512                 wispr_portal_error(wp_context);
513 }
514
515 static gboolean wispr_input(const guint8 **data, gsize *length,
516                                                 gpointer user_data)
517 {
518         struct connman_wispr_portal_context *wp_context = user_data;
519         GString *buf;
520         gsize count;
521
522         DBG("");
523
524         buf = g_string_sized_new(100);
525
526         g_string_append(buf, "button=Login&UserName=");
527         g_string_append_uri_escaped(buf, wp_context->wispr_username,
528                                                                 NULL, FALSE);
529         g_string_append(buf, "&Password=");
530         g_string_append_uri_escaped(buf, wp_context->wispr_password,
531                                                                 NULL, FALSE);
532         g_string_append(buf, "&FNAME=0&OriginatingServer=");
533         g_string_append_uri_escaped(buf, wp_context->status_url, NULL, FALSE);
534
535         count = buf->len;
536
537         g_free(wp_context->wispr_formdata);
538         wp_context->wispr_formdata = g_string_free(buf, FALSE);
539
540         *data = (guint8 *) wp_context->wispr_formdata;
541         *length = count;
542
543         return FALSE;
544 }
545
546 static void wispr_portal_browser_reply_cb(struct connman_service *service,
547                                         connman_bool_t authentication_done,
548                                         const char *error, void *user_data)
549 {
550         struct connman_wispr_portal_context *wp_context = user_data;
551
552         DBG("");
553
554         if (service == NULL || wp_context == NULL)
555                 return;
556
557         if (authentication_done == FALSE) {
558                 wispr_portal_error(wp_context);
559                 free_wispr_routes(wp_context);
560                 return;
561         }
562
563         /* Restarting the test */
564         __connman_wispr_start(service, wp_context->type);
565 }
566
567 static void wispr_portal_request_wispr_login(struct connman_service *service,
568                                 connman_bool_t success,
569                                 const char *ssid, int ssid_len,
570                                 const char *username, const char *password,
571                                 gboolean wps, const char *wpspin,
572                                 const char *error, void *user_data)
573 {
574         struct connman_wispr_portal_context *wp_context = user_data;
575
576         DBG("");
577
578         if (error != NULL) {
579                 if (g_strcmp0(error,
580                         "net.connman.Agent.Error.LaunchBrowser") == 0) {
581                         if (__connman_agent_request_browser(service,
582                                         wispr_portal_browser_reply_cb,
583                                         wp_context->redirect_url,
584                                         wp_context) == -EINPROGRESS)
585                                 return;
586                 }
587
588                 free_connman_wispr_portal_context(wp_context);
589                 return;
590         }
591
592         g_free(wp_context->wispr_username);
593         wp_context->wispr_username = g_strdup(username);
594
595         g_free(wp_context->wispr_password);
596         wp_context->wispr_password = g_strdup(password);
597
598         wp_context->request_id = g_web_request_post(wp_context->web,
599                                         wp_context->wispr_msg.login_url,
600                                         "application/x-www-form-urlencoded",
601                                         wispr_input, wispr_portal_web_result,
602                                         wp_context);
603
604         connman_wispr_message_init(&wp_context->wispr_msg);
605 }
606
607 static gboolean wispr_manage_message(GWebResult *result,
608                         struct connman_wispr_portal_context *wp_context)
609 {
610         DBG("Message type: %s (%d)",
611                 message_type_to_string(wp_context->wispr_msg.message_type),
612                                         wp_context->wispr_msg.message_type);
613         DBG("Response code: %s (%d)",
614                 response_code_to_string(wp_context->wispr_msg.response_code),
615                                         wp_context->wispr_msg.response_code);
616
617         if (wp_context->wispr_msg.access_procedure != NULL)
618                 DBG("Access procedure: %s",
619                         wp_context->wispr_msg.access_procedure);
620         if (wp_context->wispr_msg.access_location != NULL)
621                 DBG("Access location: %s",
622                         wp_context->wispr_msg.access_location);
623         if (wp_context->wispr_msg.location_name != NULL)
624                 DBG("Location name: %s",
625                         wp_context->wispr_msg.location_name);
626         if (wp_context->wispr_msg.login_url != NULL)
627                 DBG("Login URL: %s", wp_context->wispr_msg.login_url);
628         if (wp_context->wispr_msg.abort_login_url != NULL)
629                 DBG("Abort login URL: %s",
630                         wp_context->wispr_msg.abort_login_url);
631         if (wp_context->wispr_msg.logoff_url != NULL)
632                 DBG("Logoff URL: %s", wp_context->wispr_msg.logoff_url);
633
634         switch (wp_context->wispr_msg.message_type) {
635         case 100:
636                 DBG("Login required");
637
638                 wp_context->wispr_result = CONNMAN_WISPR_RESULT_LOGIN;
639
640                 if (__connman_agent_request_login_input(wp_context->service,
641                                         wispr_portal_request_wispr_login,
642                                         wp_context) != -EINPROGRESS)
643                         wispr_portal_error(wp_context);
644
645                 break;
646         case 120: /* Falling down */
647         case 140:
648                 if (wp_context->wispr_msg.response_code == 50) {
649                         wp_context->wispr_result = CONNMAN_WISPR_RESULT_ONLINE;
650
651                         g_free(wp_context->wispr_username);
652                         wp_context->wispr_username = NULL;
653
654                         g_free(wp_context->wispr_password);
655                         wp_context->wispr_password = NULL;
656
657                         g_free(wp_context->wispr_formdata);
658                         wp_context->wispr_formdata = NULL;
659
660                         wispr_portal_request_portal(wp_context);
661
662                         return TRUE;
663                 } else
664                         wispr_portal_error(wp_context);
665
666                 break;
667         default:
668                 break;
669         }
670
671         return FALSE;
672 }
673
674 static gboolean wispr_portal_web_result(GWebResult *result, gpointer user_data)
675 {
676         struct connman_wispr_portal_context *wp_context = user_data;
677         const char *redirect = NULL;
678         const guint8 *chunk = NULL;
679         const char *str = NULL;
680         guint16 status;
681         gsize length;
682
683         DBG("");
684
685         if (wp_context->wispr_result != CONNMAN_WISPR_RESULT_ONLINE) {
686                 g_web_result_get_chunk(result, &chunk, &length);
687
688                 if (length > 0) {
689                         g_web_parser_feed_data(wp_context->wispr_parser,
690                                                                 chunk, length);
691                         return TRUE;
692                 }
693
694                 g_web_parser_end_data(wp_context->wispr_parser);
695
696                 if (wp_context->wispr_msg.message_type >= 0) {
697                         if (wispr_manage_message(result, wp_context) == TRUE)
698                                 goto done;
699                 }
700         }
701
702         status = g_web_result_get_status(result);
703
704         DBG("status: %03u", status);
705
706         switch (status) {
707         case 200:
708                 if (wp_context->wispr_msg.message_type >= 0)
709                         break;
710
711                 if (g_web_result_get_header(result, "X-ConnMan-Status",
712                                                 &str) == TRUE) {
713                         portal_manage_status(result, wp_context);
714                         return FALSE;
715                 }
716                 else
717                         __connman_agent_request_browser(wp_context->service,
718                                         wispr_portal_browser_reply_cb,
719                                         wp_context->redirect_url, wp_context);
720
721                 break;
722         case 302:
723                 if (g_web_supports_tls() == FALSE ||
724                                 g_web_result_get_header(result, "Location",
725                                                         &redirect) == FALSE) {
726
727                         __connman_agent_request_browser(wp_context->service,
728                                         wispr_portal_browser_reply_cb,
729                                         wp_context->status_url, wp_context);
730                         break;
731                 }
732
733                 DBG("Redirect URL: %s", redirect);
734
735                 wp_context->redirect_url = g_strdup(redirect);
736
737                 wp_context->request_id = g_web_request_get(wp_context->web,
738                                 redirect, wispr_portal_web_result,
739                                 wispr_route_request, wp_context);
740
741                 goto done;
742         case 400:
743         case 404:
744                 if (__connman_service_online_check_failed(wp_context->service,
745                                                 wp_context->type) == 0) {
746                         wispr_portal_error(wp_context);
747                         free_connman_wispr_portal_context(wp_context);
748                         return FALSE;
749                 }
750
751                 break;
752         default:
753                 break;
754         }
755
756         free_wispr_routes(wp_context);
757         wp_context->request_id = 0;
758 done:
759         wp_context->wispr_msg.message_type = -1;
760         return FALSE;
761 }
762
763 static void proxy_callback(const char *proxy, void *user_data)
764 {
765         struct connman_wispr_portal_context *wp_context = user_data;
766
767         DBG("proxy %s", proxy);
768
769         if (wp_context == NULL)
770                 return;
771
772         wp_context->token = 0;
773
774         if (proxy != NULL && g_strcmp0(proxy, "DIRECT") != 0)
775                 g_web_set_proxy(wp_context->web, proxy);
776
777         g_web_set_accept(wp_context->web, NULL);
778         g_web_set_user_agent(wp_context->web, "ConnMan/%s wispr", VERSION);
779         g_web_set_close_connection(wp_context->web, TRUE);
780
781         connman_wispr_message_init(&wp_context->wispr_msg);
782
783         wp_context->wispr_parser = g_web_parser_new(
784                                         "<WISPAccessGatewayParam",
785                                         "WISPAccessGatewayParam>",
786                                         xml_wispr_parser_callback, wp_context);
787
788         wispr_portal_request_portal(wp_context);
789 }
790
791 static gboolean no_proxy_callback(gpointer user_data)
792 {
793         struct connman_wispr_portal_context *wp_context = user_data;
794
795         wp_context->timeout = 0;
796
797         proxy_callback("DIRECT", wp_context);
798
799         return FALSE;
800 }
801
802 static int wispr_portal_detect(struct connman_wispr_portal_context *wp_context)
803 {
804         enum connman_service_proxy_method proxy_method;
805         enum connman_service_type service_type;
806         char *interface = NULL;
807         char **nameservers = NULL;
808         int if_index;
809         int err = 0;
810         int i;
811
812         DBG("wispr/portal context %p", wp_context);
813         DBG("service %p", wp_context->service);
814
815         service_type = connman_service_get_type(wp_context->service);
816
817         switch (service_type) {
818         case CONNMAN_SERVICE_TYPE_ETHERNET:
819         case CONNMAN_SERVICE_TYPE_WIFI:
820         case CONNMAN_SERVICE_TYPE_WIMAX:
821         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
822         case CONNMAN_SERVICE_TYPE_CELLULAR:
823                 break;
824         case CONNMAN_SERVICE_TYPE_UNKNOWN:
825         case CONNMAN_SERVICE_TYPE_SYSTEM:
826         case CONNMAN_SERVICE_TYPE_GPS:
827         case CONNMAN_SERVICE_TYPE_VPN:
828         case CONNMAN_SERVICE_TYPE_GADGET:
829                 return -EOPNOTSUPP;
830         }
831
832         interface = connman_service_get_interface(wp_context->service);
833         if (interface == NULL)
834                 return -EINVAL;
835
836         DBG("interface %s", interface);
837
838         if_index = connman_inet_ifindex(interface);
839         if (if_index < 0) {
840                 DBG("Could not get ifindex");
841                 err = -EINVAL;
842                 goto done;
843         }
844
845         nameservers = connman_service_get_nameservers(wp_context->service);
846         if (nameservers == NULL) {
847                 DBG("Could not get nameservers");
848                 err = -EINVAL;
849                 goto done;
850         }
851
852         wp_context->web = g_web_new(if_index);
853         if (wp_context->web == NULL) {
854                 DBG("Could not set up GWeb");
855                 err = -ENOMEM;
856                 goto done;
857         }
858
859         if (getenv("CONNMAN_WEB_DEBUG"))
860                 g_web_set_debug(wp_context->web, web_debug, "WEB");
861
862         if (wp_context->type == CONNMAN_IPCONFIG_TYPE_IPV4) {
863                 g_web_set_address_family(wp_context->web, AF_INET);
864                 wp_context->status_url = STATUS_URL_IPV4;
865         } else {
866                 g_web_set_address_family(wp_context->web, AF_INET6);
867                 wp_context->status_url = STATUS_URL_IPV6;
868         }
869
870         for (i = 0; nameservers[i] != NULL; i++)
871                 g_web_add_nameserver(wp_context->web, nameservers[i]);
872
873         proxy_method = connman_service_get_proxy_method(wp_context->service);
874
875         if (proxy_method != CONNMAN_SERVICE_PROXY_METHOD_DIRECT) {
876                 wp_context->token = connman_proxy_lookup(interface,
877                                                 wp_context->status_url,
878                                                 wp_context->service,
879                                                 proxy_callback, wp_context);
880
881                 if (wp_context->token == 0) {
882                         err = -EINVAL;
883                         free_connman_wispr_portal_context(wp_context);
884                 }
885         } else if (wp_context->timeout == 0) {
886                 wp_context->timeout =
887                         g_timeout_add_seconds(0, no_proxy_callback, wp_context);
888         }
889
890 done:
891         g_strfreev(nameservers);
892
893         g_free(interface);
894         return err;
895 }
896
897 int __connman_wispr_start(struct connman_service *service,
898                                         enum connman_ipconfig_type type)
899 {
900         struct connman_wispr_portal_context *wp_context = NULL;
901         struct connman_wispr_portal *wispr_portal = NULL;
902         int index;
903
904         DBG("service %p", service);
905
906         if (wispr_portal_list == NULL)
907                 return -EINVAL;
908
909         index = __connman_service_get_index(service);
910         if (index < 0)
911                 return -EINVAL;
912
913         wispr_portal = g_hash_table_lookup(wispr_portal_list,
914                                         GINT_TO_POINTER(index));
915         if (wispr_portal == NULL) {
916                 wispr_portal = g_try_new0(struct connman_wispr_portal, 1);
917                 if (wispr_portal == NULL)
918                         return -ENOMEM;
919
920                 g_hash_table_replace(wispr_portal_list,
921                                         GINT_TO_POINTER(index), wispr_portal);
922         }
923
924         if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
925                 wp_context = wispr_portal->ipv4_context;
926         else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
927                 wp_context = wispr_portal->ipv6_context;
928         else
929                 return -EINVAL;
930
931         /* If there is already an existing context, we wipe it */
932         if (wp_context != NULL)
933                 free_connman_wispr_portal_context(wp_context);
934
935         wp_context = create_wispr_portal_context();
936         if (wp_context == NULL)
937                 return -ENOMEM;
938
939         wp_context->service = service;
940         wp_context->type = type;
941         wp_context->wispr_portal = wispr_portal;
942
943         if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
944                 wispr_portal->ipv4_context = wp_context;
945         else
946                 wispr_portal->ipv6_context = wp_context;
947
948         return wispr_portal_detect(wp_context);
949 }
950
951 void __connman_wispr_stop(struct connman_service *service)
952 {
953         int index;
954
955         DBG("service %p", service);
956
957         if (wispr_portal_list == NULL)
958                 return;
959
960         index = __connman_service_get_index(service);
961         if (index < 0)
962                 return;
963
964         g_hash_table_remove(wispr_portal_list, GINT_TO_POINTER(index));
965 }
966
967 int __connman_wispr_init(void)
968 {
969         DBG("");
970
971         wispr_portal_list = g_hash_table_new_full(g_direct_hash,
972                                                 g_direct_equal, NULL,
973                                                 free_connman_wispr_portal);
974
975         return 0;
976 }
977
978 void __connman_wispr_cleanup(void)
979 {
980         DBG("");
981
982         g_hash_table_destroy(wispr_portal_list);
983         wispr_portal_list = NULL;
984 }