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);
51 WISPR_ACCESS_PROCEDURE,
52 WISPR_ACCESS_LOCATION,
55 WISPR_ABORT_LOGIN_URL,
61 WISPR_LOGIN_RESULTS_URL,
65 static enum wispr_elements current_element = WISPR_NONE;
67 static void start_element_handler(GMarkupParseContext *context,
68 const gchar *element_name,
69 const gchar **attribute_names,
70 const gchar **attribute_values,
71 gpointer user_data, GError **error)
73 if (g_str_equal(element_name, "AccessProcedure") == TRUE)
74 current_element = WISPR_ACCESS_PROCEDURE;
75 else if (g_str_equal(element_name, "AccessLocation") == TRUE)
76 current_element = WISPR_ACCESS_LOCATION;
77 else if (g_str_equal(element_name, "LocationName") == TRUE)
78 current_element = WISPR_LOCATION_NAME;
79 else if (g_str_equal(element_name, "LoginURL") == TRUE)
80 current_element = WISPR_LOGIN_URL;
81 else if (g_str_equal(element_name, "AbortLoginURL") == TRUE)
82 current_element = WISPR_ABORT_LOGIN_URL;
83 else if (g_str_equal(element_name, "MessageType") == TRUE)
84 current_element = WISPR_MESSAGE_TYPE;
85 else if (g_str_equal(element_name, "ResponseCode") == TRUE)
86 current_element = WISPR_RESPONSE_CODE;
87 else if (g_str_equal(element_name, "NextURL") == TRUE)
88 current_element = WISPR_NEXT_URL;
89 else if (g_str_equal(element_name, "Delay") == TRUE)
90 current_element = WISPR_DELAY;
91 else if (g_str_equal(element_name, "ReplyMessage") == TRUE)
92 current_element = WISPR_REPLY_MESSAGE;
93 else if (g_str_equal(element_name, "LoginResultsURL") == TRUE)
94 current_element = WISPR_LOGIN_RESULTS_URL;
95 else if (g_str_equal(element_name, "LogoffURL") == TRUE)
96 current_element = WISPR_LOGOFF_URL;
99 static void end_element_handler(GMarkupParseContext *context,
100 const gchar *element_name,
101 gpointer user_data, GError **error)
103 current_element = WISPR_NONE;
106 static void text_handler(GMarkupParseContext *context,
107 const gchar *text, gsize text_len,
108 gpointer user_data, GError **error)
112 switch (current_element) {
115 case WISPR_ACCESS_PROCEDURE:
116 printf("Access procedure: %s\n", text);
118 case WISPR_ACCESS_LOCATION:
119 printf("Access location: %s\n", text);
121 case WISPR_LOCATION_NAME:
122 printf("Location name: %s\n", text);
124 case WISPR_LOGIN_URL:
125 printf("Login URL: %s\n", text);
127 case WISPR_ABORT_LOGIN_URL:
128 printf("Abort login URL: %s\n", text);
130 case WISPR_MESSAGE_TYPE:
132 printf("Message type: %d\n", value);
135 printf(" Initial redirect message\n");
138 printf(" Proxy notification\n");
141 printf(" Authentication notification\n");
144 printf(" Logoff notification\n");
147 printf(" Response to Authentication Poll\n");
150 printf(" Response to Abort Login\n");
154 case WISPR_RESPONSE_CODE:
156 printf("Response code: %d\n", value);
159 printf(" No error\n");
162 printf(" Login succeeded (Access ACCEPT)\n");
165 printf(" Login failed (Access REJECT)\n");
168 printf(" RADIUS server error/timeout\n");
171 printf(" RADIUS server not enabled\n");
174 printf(" Logoff succeeded\n");
177 printf(" Login aborted\n");
180 printf(" Proxy detection/repeat operation\n");
183 printf(" Authentication pending\n");
186 printf(" Access gateway internal error\n");
191 printf("Next URL: %s\n", text);
195 printf("Delay: %d seconds\n", value);
197 case WISPR_REPLY_MESSAGE:
198 printf("Reply message: %s\n", text);
200 case WISPR_LOGIN_RESULTS_URL:
201 printf("Login results URL: %s\n", text);
203 case WISPR_LOGOFF_URL:
204 printf("Logoff URL: %s\n", text);
209 static void error_handler(GMarkupParseContext *context,
210 GError *error, gpointer user_data)
212 printf("%s\n", error->message);
215 static const GMarkupParser wispr_parser = {
216 start_element_handler,
223 static void parser_callback(const char *str, gpointer user_data)
225 GMarkupParseContext *context;
228 //printf("%s\n", str);
230 context = g_markup_parse_context_new(&wispr_parser,
231 G_MARKUP_TREAT_CDATA_AS_TEXT, NULL, NULL);
233 result = g_markup_parse_context_parse(context, str, strlen(str), NULL);
235 result = g_markup_parse_context_end_parse(context, NULL);
237 g_markup_parse_context_free(context);
240 static guint request_id;
241 static GWebParser *request_parser;
243 static gboolean web_result(GWebResult *result, gpointer user_data)
250 status = g_web_result_get_status(result);
254 g_web_result_get_chunk(result, &chunk, &length);
257 //printf("%s\n", (char *) chunk);
258 g_web_parser_feed_data(request_parser, chunk, length);
262 g_web_parser_end_data(request_parser);
265 g_print("status: %03u\n", status);
267 elapsed = g_timer_elapsed(timer, NULL);
269 g_print("elapse: %f seconds\n", elapsed);
271 g_main_loop_quit(main_loop);
276 static gboolean option_debug = FALSE;
277 static gchar *option_nameserver = NULL;
278 static gchar *option_url = NULL;
280 static GOptionEntry options[] = {
281 { "debug", 'd', 0, G_OPTION_ARG_NONE, &option_debug,
282 "Enable debug output" },
283 { "nameserver", 'n', 0, G_OPTION_ARG_STRING, &option_nameserver,
284 "Specify nameserver", "ADDRESS" },
285 { "url", 'u', 0, G_OPTION_ARG_STRING, &option_url,
286 "Specify arbitrary request", "URL" },
290 int main(int argc, char *argv[])
292 GOptionContext *context;
293 GError *error = NULL;
298 context = g_option_context_new(NULL);
299 g_option_context_add_main_entries(context, options, NULL);
301 if (g_option_context_parse(context, &argc, &argv, &error) == FALSE) {
303 g_printerr("%s\n", error->message);
306 g_printerr("An unknown error occurred\n");
310 g_option_context_free(context);
312 web = g_web_new(index);
314 fprintf(stderr, "Failed to create web service\n");
318 if (option_debug == TRUE)
319 g_web_set_debug(web, web_debug, "WEB");
321 main_loop = g_main_loop_new(NULL, FALSE);
323 if (option_nameserver != NULL) {
324 g_web_add_nameserver(web, option_nameserver);
325 g_free(option_nameserver);
328 g_web_set_accept(web, NULL);
329 g_web_set_user_agent(web, "SmartClient/%s wispr", VERSION);
330 g_web_set_close_connection(web, TRUE);
332 if (option_url == NULL)
333 option_url = g_strdup(DEFAULT_URL);
335 timer = g_timer_new();
337 request_parser = g_web_parser_new("<WISPAccessGatewayParam",
338 "WISPAccessGatewayParam>",
339 parser_callback, NULL);
341 g_web_parser_ref(request_parser);
342 g_web_parser_unref(request_parser);
344 request_id = g_web_request(web, G_WEB_METHOD_GET, option_url,
349 if (request_id == 0) {
350 fprintf(stderr, "Failed to start request\n");
354 memset(&sa, 0, sizeof(sa));
355 sa.sa_handler = sig_term;
356 sigaction(SIGINT, &sa, NULL);
357 sigaction(SIGTERM, &sa, NULL);
359 g_main_loop_run(main_loop);
361 g_timer_destroy(timer);
365 g_main_loop_unref(main_loop);