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