5 * Copyright (C) 2007-2010 Intel Corporation. All rights reserved.
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.
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.
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
31 #include <gweb/gweb.h>
33 #define DEFAULT_URL "http://www.connman.net/online/status.html"
37 static GMainLoop *main_loop;
39 static void web_debug(const char *str, void *data)
41 g_print("%s: %s\n", (const char *) data, str);
44 static void sig_term(int sig)
46 g_main_loop_quit(main_loop);
53 WISPR_PAGE_AUTHENTICATION_REPLY,
54 WISPR_PAGE_AUTHENTICATION_POLL_REPLY,
55 WISPR_PAGE_LOGOFF_REPLY,
56 WISPR_PAGE_ABORT_LOGIN_REPLY,
61 WISPR_ACCESS_PROCEDURE,
62 WISPR_ACCESS_LOCATION,
65 WISPR_ABORT_LOGIN_URL,
71 WISPR_LOGIN_RESULTS_URL,
75 static enum wispr_pages current_page = WISPR_NONE;
76 static enum wispr_elements current_element = WISPR_NONE;
78 static void start_element_handler(GMarkupParseContext *context,
79 const gchar *element_name,
80 const gchar **attribute_names,
81 const gchar **attribute_values,
82 gpointer user_data, GError **error)
84 if (g_str_equal(element_name, "Redirect") == TRUE)
85 current_page = WISPR_PAGE_REDIRECT;
86 else if (g_str_equal(element_name, "Proxy") == TRUE)
87 current_page = WISPR_PAGE_PROXY;
88 else if (g_str_equal(element_name, "AuthenticationReply") == TRUE)
89 current_page = WISPR_PAGE_AUTHENTICATION_REPLY;
90 else if (g_str_equal(element_name, "AuthenticationPollReply") == TRUE)
91 current_page = WISPR_PAGE_AUTHENTICATION_POLL_REPLY;
92 else if (g_str_equal(element_name, "LogoffReply") == TRUE)
93 current_page = WISPR_PAGE_LOGOFF_REPLY;
94 else if (g_str_equal(element_name, "AbortLoginReply") == TRUE)
95 current_page = WISPR_PAGE_ABORT_LOGIN_REPLY;
97 current_page = WISPR_PAGE_NONE;
99 if (g_str_equal(element_name, "AccessProcedure") == TRUE)
100 current_element = WISPR_ACCESS_PROCEDURE;
101 else if (g_str_equal(element_name, "AccessLocation") == TRUE)
102 current_element = WISPR_ACCESS_LOCATION;
103 else if (g_str_equal(element_name, "LocationName") == TRUE)
104 current_element = WISPR_LOCATION_NAME;
105 else if (g_str_equal(element_name, "LoginURL") == TRUE)
106 current_element = WISPR_LOGIN_URL;
107 else if (g_str_equal(element_name, "AbortLoginURL") == TRUE)
108 current_element = WISPR_ABORT_LOGIN_URL;
109 else if (g_str_equal(element_name, "MessageType") == TRUE)
110 current_element = WISPR_MESSAGE_TYPE;
111 else if (g_str_equal(element_name, "ResponseCode") == TRUE)
112 current_element = WISPR_RESPONSE_CODE;
113 else if (g_str_equal(element_name, "NextURL") == TRUE)
114 current_element = WISPR_NEXT_URL;
115 else if (g_str_equal(element_name, "Delay") == TRUE)
116 current_element = WISPR_DELAY;
117 else if (g_str_equal(element_name, "ReplyMessage") == TRUE)
118 current_element = WISPR_REPLY_MESSAGE;
119 else if (g_str_equal(element_name, "LoginResultsURL") == TRUE)
120 current_element = WISPR_LOGIN_RESULTS_URL;
121 else if (g_str_equal(element_name, "LogoffURL") == TRUE)
122 current_element = WISPR_LOGOFF_URL;
124 current_element = WISPR_NONE;
127 static void end_element_handler(GMarkupParseContext *context,
128 const gchar *element_name,
129 gpointer user_data, GError **error)
131 current_page = WISPR_PAGE_NONE;
133 current_element = WISPR_NONE;
136 static void text_handler(GMarkupParseContext *context,
137 const gchar *text, gsize text_len,
138 gpointer user_data, GError **error)
142 switch (current_page) {
143 case WISPR_PAGE_NONE:
145 case WISPR_PAGE_REDIRECT:
146 printf("[ Redirect ]\n");
148 case WISPR_PAGE_PROXY:
149 printf("[ Proxy ]\n");
151 case WISPR_PAGE_AUTHENTICATION_REPLY:
152 printf("[ Authentication reply ]\n");
154 case WISPR_PAGE_AUTHENTICATION_POLL_REPLY:
155 printf("[ Authentication poll reply ]\n");
157 case WISPR_PAGE_LOGOFF_REPLY:
158 printf("[ Logoff reply ]\n");
160 case WISPR_PAGE_ABORT_LOGIN_REPLY:
161 printf("[ Abort login reply ]\n");
165 switch (current_element) {
168 case WISPR_ACCESS_PROCEDURE:
169 printf("Access procedure: %s\n", text);
171 case WISPR_ACCESS_LOCATION:
172 printf("Access location: %s\n", text);
174 case WISPR_LOCATION_NAME:
175 printf("Location name: %s\n", text);
177 case WISPR_LOGIN_URL:
178 printf("Login URL: %s\n", text);
180 case WISPR_ABORT_LOGIN_URL:
181 printf("Abort login URL: %s\n", text);
183 case WISPR_MESSAGE_TYPE:
185 printf("Message type: %d\n", value);
188 printf(" Initial redirect message\n");
191 printf(" Proxy notification\n");
194 printf(" Authentication notification\n");
197 printf(" Logoff notification\n");
200 printf(" Response to Authentication Poll\n");
203 printf(" Response to Abort Login\n");
207 case WISPR_RESPONSE_CODE:
209 printf("Response code: %d\n", value);
212 printf(" No error\n");
215 printf(" Login succeeded (Access ACCEPT)\n");
218 printf(" Login failed (Access REJECT)\n");
221 printf(" RADIUS server error/timeout\n");
224 printf(" RADIUS server not enabled\n");
227 printf(" Logoff succeeded\n");
230 printf(" Login aborted\n");
233 printf(" Proxy detection/repeat operation\n");
236 printf(" Authentication pending\n");
239 printf(" Access gateway internal error\n");
244 printf("Next URL: %s\n", text);
248 printf("Delay: %d seconds\n", value);
250 case WISPR_REPLY_MESSAGE:
251 printf("Reply message: %s\n", text);
253 case WISPR_LOGIN_RESULTS_URL:
254 printf("Login results URL: %s\n", text);
256 case WISPR_LOGOFF_URL:
257 printf("Logoff URL: %s\n", text);
262 static void error_handler(GMarkupParseContext *context,
263 GError *error, gpointer user_data)
265 printf("%s\n", error->message);
268 static const GMarkupParser wispr_parser = {
269 start_element_handler,
276 static void parser_callback(const char *str, gpointer user_data)
278 GMarkupParseContext *context;
281 //printf("%s\n", str);
283 context = g_markup_parse_context_new(&wispr_parser,
284 G_MARKUP_TREAT_CDATA_AS_TEXT, NULL, NULL);
286 result = g_markup_parse_context_parse(context, str, strlen(str), NULL);
288 result = g_markup_parse_context_end_parse(context, NULL);
290 g_markup_parse_context_free(context);
293 static guint request_id;
294 static GWebParser *request_parser;
296 static gboolean web_result(GWebResult *result, gpointer user_data)
303 status = g_web_result_get_status(result);
307 g_web_result_get_chunk(result, &chunk, &length);
310 //printf("%s\n", (char *) chunk);
311 g_web_parser_feed_data(request_parser, chunk, length);
315 g_web_parser_end_data(request_parser);
318 g_print("status: %03u\n", status);
320 elapsed = g_timer_elapsed(timer, NULL);
322 g_print("elapse: %f seconds\n", elapsed);
324 g_main_loop_quit(main_loop);
329 static gboolean option_debug = FALSE;
330 static gchar *option_nameserver = NULL;
331 static gchar *option_url = NULL;
333 static GOptionEntry options[] = {
334 { "debug", 'd', 0, G_OPTION_ARG_NONE, &option_debug,
335 "Enable debug output" },
336 { "nameserver", 'n', 0, G_OPTION_ARG_STRING, &option_nameserver,
337 "Specify nameserver", "ADDRESS" },
338 { "url", 'u', 0, G_OPTION_ARG_STRING, &option_url,
339 "Specify arbitrary request", "URL" },
343 int main(int argc, char *argv[])
345 GOptionContext *context;
346 GError *error = NULL;
351 context = g_option_context_new(NULL);
352 g_option_context_add_main_entries(context, options, NULL);
354 if (g_option_context_parse(context, &argc, &argv, &error) == FALSE) {
356 g_printerr("%s\n", error->message);
359 g_printerr("An unknown error occurred\n");
363 g_option_context_free(context);
365 web = g_web_new(index);
367 fprintf(stderr, "Failed to create web service\n");
371 if (option_debug == TRUE)
372 g_web_set_debug(web, web_debug, "WEB");
374 main_loop = g_main_loop_new(NULL, FALSE);
376 if (option_nameserver != NULL) {
377 g_web_add_nameserver(web, option_nameserver);
378 g_free(option_nameserver);
381 g_web_set_accept(web, NULL);
382 g_web_set_user_agent(web, "SmartClient/%s wispr", VERSION);
383 g_web_set_close_connection(web, TRUE);
385 if (option_url == NULL)
386 option_url = g_strdup(DEFAULT_URL);
388 timer = g_timer_new();
390 request_parser = g_web_parser_new("<WISPAccessGatewayParam",
391 "WISPAccessGatewayParam>",
392 parser_callback, NULL);
394 g_web_parser_ref(request_parser);
395 g_web_parser_unref(request_parser);
397 request_id = g_web_request(web, G_WEB_METHOD_GET, option_url,
402 if (request_id == 0) {
403 fprintf(stderr, "Failed to start request\n");
407 memset(&sa, 0, sizeof(sa));
408 sa.sa_handler = sig_term;
409 sigaction(SIGINT, &sa, NULL);
410 sigaction(SIGTERM, &sa, NULL);
412 g_main_loop_run(main_loop);
414 g_timer_destroy(timer);
418 g_main_loop_unref(main_loop);