wispr: Fix refcounting issue
[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                 if (__connman_agent_request_browser(service,
642                                 wispr_portal_browser_reply_cb,
643                                 wp_context->redirect_url,
644                                 wp_context) != -EINPROGRESS)
645                         wispr_portal_context_unref(wp_context);
646
647                 return;
648         }
649
650         g_free(wp_context->wispr_username);
651         wp_context->wispr_username = g_strdup(username);
652
653         g_free(wp_context->wispr_password);
654         wp_context->wispr_password = g_strdup(password);
655
656         wispr_portal_context_ref(wp_context);
657
658         wp_context->request_id = g_web_request_post(wp_context->web,
659                                         wp_context->wispr_msg.login_url,
660                                         "application/x-www-form-urlencoded",
661                                         wispr_input, wispr_portal_web_result,
662                                         wp_context);
663         if (wp_context->request_id == 0)
664                 wispr_portal_context_unref(wp_context);
665
666         connman_wispr_message_init(&wp_context->wispr_msg);
667 }
668
669 static gboolean wispr_manage_message(GWebResult *result,
670                         struct connman_wispr_portal_context *wp_context)
671 {
672         DBG("Message type: %s (%d)",
673                 message_type_to_string(wp_context->wispr_msg.message_type),
674                                         wp_context->wispr_msg.message_type);
675         DBG("Response code: %s (%d)",
676                 response_code_to_string(wp_context->wispr_msg.response_code),
677                                         wp_context->wispr_msg.response_code);
678
679         if (wp_context->wispr_msg.access_procedure != NULL)
680                 DBG("Access procedure: %s",
681                         wp_context->wispr_msg.access_procedure);
682         if (wp_context->wispr_msg.access_location != NULL)
683                 DBG("Access location: %s",
684                         wp_context->wispr_msg.access_location);
685         if (wp_context->wispr_msg.location_name != NULL)
686                 DBG("Location name: %s",
687                         wp_context->wispr_msg.location_name);
688         if (wp_context->wispr_msg.login_url != NULL)
689                 DBG("Login URL: %s", wp_context->wispr_msg.login_url);
690         if (wp_context->wispr_msg.abort_login_url != NULL)
691                 DBG("Abort login URL: %s",
692                         wp_context->wispr_msg.abort_login_url);
693         if (wp_context->wispr_msg.logoff_url != NULL)
694                 DBG("Logoff URL: %s", wp_context->wispr_msg.logoff_url);
695
696         switch (wp_context->wispr_msg.message_type) {
697         case 100:
698                 DBG("Login required");
699
700                 wp_context->wispr_result = CONNMAN_WISPR_RESULT_LOGIN;
701
702                 wispr_portal_context_ref(wp_context);
703
704                 if (__connman_agent_request_login_input(wp_context->service,
705                                         wispr_portal_request_wispr_login,
706                                         wp_context) != -EINPROGRESS) {
707                         wispr_portal_context_unref(wp_context);
708                         wispr_portal_error(wp_context);
709                 }
710
711                 break;
712         case 120: /* Falling down */
713         case 140:
714                 if (wp_context->wispr_msg.response_code == 50) {
715                         wp_context->wispr_result = CONNMAN_WISPR_RESULT_ONLINE;
716
717                         g_free(wp_context->wispr_username);
718                         wp_context->wispr_username = NULL;
719
720                         g_free(wp_context->wispr_password);
721                         wp_context->wispr_password = NULL;
722
723                         g_free(wp_context->wispr_formdata);
724                         wp_context->wispr_formdata = NULL;
725
726                         wispr_portal_request_portal(wp_context);
727
728                         return TRUE;
729                 } else
730                         wispr_portal_error(wp_context);
731
732                 break;
733         default:
734                 break;
735         }
736
737         return FALSE;
738 }
739
740 static gboolean wispr_portal_web_result(GWebResult *result, gpointer user_data)
741 {
742         struct connman_wispr_portal_context *wp_context = user_data;
743         const char *redirect = NULL;
744         const guint8 *chunk = NULL;
745         const char *str = NULL;
746         guint16 status;
747         gsize length;
748
749         DBG("");
750
751         wp_context = wispr_portal_context_unref(wp_context);
752         if (wp_context == NULL)
753                 return FALSE;
754
755         if (wp_context->request_id == 0)
756                 return FALSE;
757
758         if (wp_context->wispr_result != CONNMAN_WISPR_RESULT_ONLINE) {
759                 g_web_result_get_chunk(result, &chunk, &length);
760
761                 if (length > 0) {
762                         wispr_portal_context_ref(wp_context);
763
764                         g_web_parser_feed_data(wp_context->wispr_parser,
765                                                                 chunk, length);
766                         return TRUE;
767                 }
768
769                 g_web_parser_end_data(wp_context->wispr_parser);
770
771                 if (wp_context->wispr_msg.message_type >= 0) {
772                         if (wispr_manage_message(result, wp_context) == TRUE)
773                                 goto done;
774                 }
775         }
776
777         status = g_web_result_get_status(result);
778
779         DBG("status: %03u", status);
780
781         switch (status) {
782         case 200:
783                 if (wp_context->wispr_msg.message_type >= 0)
784                         break;
785
786                 if (g_web_result_get_header(result, "X-ConnMan-Status",
787                                                                 &str) == TRUE)
788                         portal_manage_status(result, wp_context);
789                 else {
790                         wispr_portal_context_ref(wp_context);
791
792                         __connman_agent_request_browser(wp_context->service,
793                                         wispr_portal_browser_reply_cb,
794                                         wp_context->redirect_url, wp_context);
795                 }
796
797                 break;
798         case 302:
799                 if (g_web_supports_tls() == FALSE ||
800                                 g_web_result_get_header(result, "Location",
801                                                         &redirect) == FALSE) {
802                         wispr_portal_context_ref(wp_context);
803
804                         __connman_agent_request_browser(wp_context->service,
805                                         wispr_portal_browser_reply_cb,
806                                         wp_context->status_url, wp_context);
807                         break;
808                 }
809
810                 DBG("Redirect URL: %s", redirect);
811
812                 wp_context->redirect_url = g_strdup(redirect);
813
814                 wp_context->request_id = g_web_request_get(wp_context->web,
815                                 redirect, wispr_portal_web_result,
816                                 wispr_route_request, wp_context);
817                 if (wp_context->request_id != 0)
818                         wispr_portal_context_ref(wp_context);
819
820                 goto done;
821         case 400:
822         case 404:
823                 if (__connman_service_online_check_failed(wp_context->service,
824                                                         wp_context->type) == 0)
825                         wispr_portal_error(wp_context);
826
827                 break;
828         default:
829                 break;
830         }
831
832         free_wispr_routes(wp_context);
833         wp_context->request_id = 0;
834 done:
835         wp_context->wispr_msg.message_type = -1;
836         return FALSE;
837 }
838
839 static void proxy_callback(const char *proxy, void *user_data)
840 {
841         struct connman_wispr_portal_context *wp_context = user_data;
842
843         DBG("proxy %s", proxy);
844
845         wp_context = wispr_portal_context_unref(wp_context);
846         if (wp_context == NULL)
847                 return;
848
849         wp_context->token = 0;
850
851         if (proxy != NULL && g_strcmp0(proxy, "DIRECT") != 0)
852                 g_web_set_proxy(wp_context->web, proxy);
853
854         g_web_set_accept(wp_context->web, NULL);
855         g_web_set_user_agent(wp_context->web, "ConnMan/%s wispr", VERSION);
856         g_web_set_close_connection(wp_context->web, TRUE);
857
858         connman_wispr_message_init(&wp_context->wispr_msg);
859
860         wp_context->wispr_parser = g_web_parser_new(
861                                         "<WISPAccessGatewayParam",
862                                         "WISPAccessGatewayParam>",
863                                         xml_wispr_parser_callback, wp_context);
864
865         wispr_portal_request_portal(wp_context);
866 }
867
868 static gboolean no_proxy_callback(gpointer user_data)
869 {
870         struct connman_wispr_portal_context *wp_context = user_data;
871
872         proxy_callback("DIRECT", wp_context);
873
874         return FALSE;
875 }
876
877 static int wispr_portal_detect(struct connman_wispr_portal_context *wp_context)
878 {
879         enum connman_service_proxy_method proxy_method;
880         enum connman_service_type service_type;
881         char *interface = NULL;
882         char **nameservers = NULL;
883         int if_index;
884         int err = 0;
885         int i;
886
887         DBG("wispr/portal context %p", wp_context);
888         DBG("service %p", wp_context->service);
889
890         service_type = connman_service_get_type(wp_context->service);
891
892         switch (service_type) {
893         case CONNMAN_SERVICE_TYPE_ETHERNET:
894         case CONNMAN_SERVICE_TYPE_WIFI:
895         case CONNMAN_SERVICE_TYPE_WIMAX:
896         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
897         case CONNMAN_SERVICE_TYPE_CELLULAR:
898                 break;
899         case CONNMAN_SERVICE_TYPE_UNKNOWN:
900         case CONNMAN_SERVICE_TYPE_SYSTEM:
901         case CONNMAN_SERVICE_TYPE_GPS:
902         case CONNMAN_SERVICE_TYPE_VPN:
903         case CONNMAN_SERVICE_TYPE_GADGET:
904                 return -EOPNOTSUPP;
905         }
906
907         interface = connman_service_get_interface(wp_context->service);
908         if (interface == NULL)
909                 return -EINVAL;
910
911         DBG("interface %s", interface);
912
913         if_index = connman_inet_ifindex(interface);
914         if (if_index < 0) {
915                 DBG("Could not get ifindex");
916                 err = -EINVAL;
917                 goto done;
918         }
919
920         nameservers = connman_service_get_nameservers(wp_context->service);
921         if (nameservers == NULL) {
922                 DBG("Could not get nameservers");
923                 err = -EINVAL;
924                 goto done;
925         }
926
927         wp_context->web = g_web_new(if_index);
928         if (wp_context->web == NULL) {
929                 DBG("Could not set up GWeb");
930                 err = -ENOMEM;
931                 goto done;
932         }
933
934         if (getenv("CONNMAN_WEB_DEBUG"))
935                 g_web_set_debug(wp_context->web, web_debug, "WEB");
936
937         if (wp_context->type == CONNMAN_IPCONFIG_TYPE_IPV4) {
938                 g_web_set_address_family(wp_context->web, AF_INET);
939                 wp_context->status_url = STATUS_URL_IPV4;
940         } else {
941                 g_web_set_address_family(wp_context->web, AF_INET6);
942                 wp_context->status_url = STATUS_URL_IPV6;
943         }
944
945         for (i = 0; nameservers[i] != NULL; i++)
946                 g_web_add_nameserver(wp_context->web, nameservers[i]);
947
948         proxy_method = connman_service_get_proxy_method(wp_context->service);
949
950         if (proxy_method != CONNMAN_SERVICE_PROXY_METHOD_DIRECT) {
951                 wp_context->token = connman_proxy_lookup(interface,
952                                                 wp_context->status_url,
953                                                 wp_context->service,
954                                                 proxy_callback, wp_context);
955
956                 if (wp_context->token == 0) {
957                         err = -EINVAL;
958                         goto done;
959                 }
960         } else {
961                 g_timeout_add_seconds(0, no_proxy_callback, wp_context);
962         }
963
964         wispr_portal_context_ref(wp_context);
965 done:
966         g_strfreev(nameservers);
967
968         g_free(interface);
969         return err;
970 }
971
972 int __connman_wispr_start(struct connman_service *service,
973                                         enum connman_ipconfig_type type)
974 {
975         struct connman_wispr_portal_context *wp_context = NULL;
976         struct connman_wispr_portal *wispr_portal = NULL;
977         int index;
978
979         DBG("service %p", service);
980
981         if (wispr_portal_list == NULL)
982                 return -EINVAL;
983
984         index = __connman_service_get_index(service);
985         if (index < 0)
986                 return -EINVAL;
987
988         wispr_portal = g_hash_table_lookup(wispr_portal_list,
989                                         GINT_TO_POINTER(index));
990         if (wispr_portal == NULL) {
991                 wispr_portal = g_try_new0(struct connman_wispr_portal, 1);
992                 if (wispr_portal == NULL)
993                         return -ENOMEM;
994
995                 g_hash_table_replace(wispr_portal_list,
996                                         GINT_TO_POINTER(index), wispr_portal);
997         }
998
999         if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
1000                 wp_context = wispr_portal->ipv4_context;
1001         else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
1002                 wp_context = wispr_portal->ipv6_context;
1003         else
1004                 return -EINVAL;
1005
1006         /* If there is already an existing context, we wipe it */
1007         if (wp_context != NULL)
1008                 wispr_portal_context_unref(wp_context);
1009
1010         wp_context = create_wispr_portal_context();
1011         if (wp_context == NULL)
1012                 return -ENOMEM;
1013
1014         connman_service_ref(service);
1015
1016         wp_context->service = service;
1017         wp_context->type = type;
1018
1019         if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
1020                 wispr_portal->ipv4_context = wp_context;
1021         else
1022                 wispr_portal->ipv6_context = wp_context;
1023
1024         return wispr_portal_detect(wp_context);
1025 }
1026
1027 void __connman_wispr_stop(struct connman_service *service)
1028 {
1029         int index;
1030
1031         DBG("service %p", service);
1032
1033         if (wispr_portal_list == NULL)
1034                 return;
1035
1036         index = __connman_service_get_index(service);
1037         if (index < 0)
1038                 return;
1039
1040         g_hash_table_remove(wispr_portal_list, GINT_TO_POINTER(index));
1041 }
1042
1043 int __connman_wispr_init(void)
1044 {
1045         DBG("");
1046
1047         wispr_portal_list = g_hash_table_new_full(g_direct_hash,
1048                                                 g_direct_equal, NULL,
1049                                                 free_connman_wispr_portal);
1050
1051         return 0;
1052 }
1053
1054 void __connman_wispr_cleanup(void)
1055 {
1056         DBG("");
1057
1058         g_hash_table_destroy(wispr_portal_list);
1059         wispr_portal_list = NULL;
1060 }