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