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