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);
49 static const char *message_type_to_string(int message_type)
51 switch (message_type) {
53 return "Initial redirect message";
55 return "Proxy notification";
57 return "Authentication notification";
59 return "Logoff notification";
61 return "Response to Authentication Poll";
63 return "Response to Abort Login";
69 static const char *response_code_to_string(int response_code)
71 switch (response_code) {
75 return "Login succeeded";
77 return "Login failed";
79 return "RADIUS server error/timeout";
81 return "RADIUS server not enabled";
83 return "Logoff succeeded";
85 return "Login aborted";
87 return "Proxy detection/repeat operation";
89 return "Authentication pending";
91 return "Access gateway internal error";
99 const char *current_element;
106 static inline void wispr_msg_init(struct wispr_msg *msg)
108 msg->has_error = FALSE;
109 msg->current_element = NULL;
111 msg->message_type = -1;
112 msg->response_code = -1;
114 g_free(msg->login_url);
115 msg->login_url = NULL;
117 g_free(msg->logoff_url);
118 msg->logoff_url = NULL;
121 struct wispr_session {
125 struct wispr_msg msg;
126 const char *username;
127 const char *password;
128 const char *originurl;
136 WISPR_ELEMENT_ACCESS_PROCEDURE,
137 WISPR_ELEMENT_ACCESS_LOCATION,
138 WISPR_ELEMENT_LOCATION_NAME,
139 WISPR_ELEMENT_LOGIN_URL,
140 WISPR_ELEMENT_ABORT_LOGIN_URL,
141 WISPR_ELEMENT_MESSAGE_TYPE,
142 WISPR_ELEMENT_RESPONSE_CODE,
143 WISPR_ELEMENT_NEXT_URL,
145 WISPR_ELEMENT_REPLY_MESSAGE,
146 WISPR_ELEMENT_LOGIN_RESULTS_URL,
147 WISPR_ELEMENT_LOGOFF_URL,
149 } wispr_element_map[] = {
150 { "AccessProcedure", WISPR_ELEMENT_ACCESS_PROCEDURE },
151 { "AccessLocation", WISPR_ELEMENT_ACCESS_LOCATION },
152 { "LocationName", WISPR_ELEMENT_LOCATION_NAME },
153 { "LoginURL", WISPR_ELEMENT_LOGIN_URL },
154 { "AbortLoginURL", WISPR_ELEMENT_ABORT_LOGIN_URL },
155 { "MessageType", WISPR_ELEMENT_MESSAGE_TYPE },
156 { "ResponseCode", WISPR_ELEMENT_RESPONSE_CODE },
157 { "NextURL", WISPR_ELEMENT_NEXT_URL },
158 { "Delay", WISPR_ELEMENT_DELAY },
159 { "ReplyMessage", WISPR_ELEMENT_REPLY_MESSAGE },
160 { "LoginResultsURL", WISPR_ELEMENT_LOGIN_RESULTS_URL },
161 { "LogoffURL", WISPR_ELEMENT_LOGOFF_URL },
162 { NULL, WISPR_ELEMENT_NONE },
165 static void start_element_handler(GMarkupParseContext *context,
166 const gchar *element_name,
167 const gchar **attribute_names,
168 const gchar **attribute_values,
169 gpointer user_data, GError **error)
171 struct wispr_msg *msg = user_data;
173 msg->current_element = element_name;
176 static void end_element_handler(GMarkupParseContext *context,
177 const gchar *element_name,
178 gpointer user_data, GError **error)
180 struct wispr_msg *msg = user_data;
182 msg->current_element = NULL;
185 static void text_handler(GMarkupParseContext *context,
186 const gchar *text, gsize text_len,
187 gpointer user_data, GError **error)
189 struct wispr_msg *msg = user_data;
192 if (msg->current_element == NULL)
195 for (i = 0; wispr_element_map[i].str; i++) {
196 if (g_str_equal(wispr_element_map[i].str,
197 msg->current_element) == FALSE)
200 switch (wispr_element_map[i].element) {
201 case WISPR_ELEMENT_NONE:
202 case WISPR_ELEMENT_ACCESS_PROCEDURE:
203 case WISPR_ELEMENT_ACCESS_LOCATION:
204 case WISPR_ELEMENT_LOCATION_NAME:
206 case WISPR_ELEMENT_LOGIN_URL:
207 g_free(msg->login_url);
208 msg->login_url = g_strdup(text);
210 case WISPR_ELEMENT_ABORT_LOGIN_URL:
212 case WISPR_ELEMENT_MESSAGE_TYPE:
213 msg->message_type = atoi(text);
215 case WISPR_ELEMENT_RESPONSE_CODE:
216 msg->response_code = atoi(text);
218 case WISPR_ELEMENT_NEXT_URL:
219 case WISPR_ELEMENT_DELAY:
220 case WISPR_ELEMENT_REPLY_MESSAGE:
221 case WISPR_ELEMENT_LOGIN_RESULTS_URL:
223 case WISPR_ELEMENT_LOGOFF_URL:
224 g_free(msg->logoff_url);
225 msg->logoff_url = g_strdup(text);
231 static void error_handler(GMarkupParseContext *context,
232 GError *error, gpointer user_data)
234 struct wispr_msg *msg = user_data;
236 msg->has_error = TRUE;
239 static const GMarkupParser wispr_parser = {
240 start_element_handler,
247 static void parser_callback(const char *str, gpointer user_data)
249 struct wispr_session *wispr = user_data;
250 GMarkupParseContext *context;
253 //printf("%s\n", str);
255 context = g_markup_parse_context_new(&wispr_parser,
256 G_MARKUP_TREAT_CDATA_AS_TEXT, &wispr->msg, NULL);
258 result = g_markup_parse_context_parse(context, str, strlen(str), NULL);
260 result = g_markup_parse_context_end_parse(context, NULL);
262 g_markup_parse_context_free(context);
265 static gboolean wispr_input(const guint8 **data, gsize *length,
268 struct wispr_session *wispr = user_data;
270 g_free(wispr->formdata);
271 wispr->formdata = g_strdup_printf("button=Login&UserName=%s&"
272 "Password=%s&FNAME=0&OriginatingServer=%s",
273 wispr->username, wispr->password, wispr->originurl);
275 *data = (guint8 *) wispr->formdata;
276 *length = strlen(wispr->formdata);
281 static gboolean wispr_result(GWebResult *result, gpointer user_data)
283 struct wispr_session *wispr = user_data;
289 g_web_result_get_chunk(result, &chunk, &length);
292 //printf("%s\n", (char *) chunk);
293 g_web_parser_feed_data(wispr->parser, chunk, length);
297 g_web_parser_end_data(wispr->parser);
299 status = g_web_result_get_status(result);
301 g_print("status: %03u\n", status);
303 elapsed = g_timer_elapsed(timer, NULL);
305 g_print("elapse: %f seconds\n", elapsed);
307 if (wispr->msg.message_type < 0)
310 printf("Message type: %s (%d)\n",
311 message_type_to_string(wispr->msg.message_type),
312 wispr->msg.message_type);
313 printf("Response code: %s (%d)\n",
314 response_code_to_string(wispr->msg.response_code),
315 wispr->msg.response_code);
316 if (wispr->msg.login_url != NULL)
317 printf("Login URL: %s\n", wispr->msg.login_url);
318 if (wispr->msg.logoff_url != NULL)
319 printf("Logoff URL: %s\n", wispr->msg.logoff_url);
322 if (status == 302 && wispr->msg.message_type == 100) {
323 wispr->request = g_web_request_post(wispr->web,
324 wispr->msg.login_url,
325 "application/x-www-form-urlencoded",
326 wispr_input, wispr_result, wispr);
328 wispr_msg_init(&wispr->msg);
331 } else if (status == 200 && wispr->msg.message_type == 120) {
332 int code = wispr->msg.response_code;
333 printf("Login process: %s\n",
334 code == 50 ? "SUCCESS" : "FAILURE");
338 g_main_loop_quit(main_loop);
343 static gboolean option_debug = FALSE;
344 static gchar *option_nameserver = NULL;
345 static gchar *option_username = NULL;
346 static gchar *option_password = NULL;
347 static gchar *option_url = NULL;
349 static GOptionEntry options[] = {
350 { "debug", 'd', 0, G_OPTION_ARG_NONE, &option_debug,
351 "Enable debug output" },
352 { "nameserver", 'n', 0, G_OPTION_ARG_STRING, &option_nameserver,
353 "Specify nameserver", "ADDRESS" },
354 { "username", 'u', 0, G_OPTION_ARG_STRING, &option_username,
355 "Specify username", "USERNAME" },
356 { "password", 'p', 0, G_OPTION_ARG_STRING, &option_password,
357 "Specify password", "PASSWORD" },
358 { "url", 'U', 0, G_OPTION_ARG_STRING, &option_url,
359 "Specify arbitrary request", "URL" },
363 int main(int argc, char *argv[])
365 GOptionContext *context;
366 GError *error = NULL;
368 struct wispr_session wispr;
371 context = g_option_context_new(NULL);
372 g_option_context_add_main_entries(context, options, NULL);
374 if (g_option_context_parse(context, &argc, &argv, &error) == FALSE) {
376 g_printerr("%s\n", error->message);
379 g_printerr("An unknown error occurred\n");
383 g_option_context_free(context);
385 memset(&wispr, 0, sizeof(wispr));
386 wispr_msg_init(&wispr.msg);
388 wispr.web = g_web_new(index);
389 if (wispr.web == NULL) {
390 fprintf(stderr, "Failed to create web service\n");
394 if (option_debug == TRUE)
395 g_web_set_debug(wispr.web, web_debug, "WEB");
397 main_loop = g_main_loop_new(NULL, FALSE);
399 if (option_nameserver != NULL) {
400 g_web_add_nameserver(wispr.web, option_nameserver);
401 g_free(option_nameserver);
404 g_web_set_accept(wispr.web, NULL);
405 g_web_set_user_agent(wispr.web, "SmartClient/%s wispr", VERSION);
406 g_web_set_close_connection(wispr.web, TRUE);
408 if (option_url == NULL)
409 option_url = g_strdup(DEFAULT_URL);
411 wispr.username = option_username;
412 wispr.password = option_password;
413 wispr.originurl = option_url;
415 timer = g_timer_new();
417 wispr.parser = g_web_parser_new("<WISPAccessGatewayParam",
418 "WISPAccessGatewayParam>",
419 parser_callback, &wispr);
421 wispr.request = g_web_request_get(wispr.web, option_url,
422 wispr_result, &wispr);
424 if (wispr.request == 0) {
425 fprintf(stderr, "Failed to start request\n");
429 memset(&sa, 0, sizeof(sa));
430 sa.sa_handler = sig_term;
431 sigaction(SIGINT, &sa, NULL);
432 sigaction(SIGTERM, &sa, NULL);
434 g_main_loop_run(main_loop);
436 g_timer_destroy(timer);
438 if (wispr.request > 0)
439 g_web_cancel_request(wispr.web, wispr.request);
441 g_web_parser_unref(wispr.parser);
442 g_web_unref(wispr.web);
444 g_main_loop_unref(main_loop);
446 g_free(option_username);
447 g_free(option_password);