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:
222 case WISPR_ELEMENT_LOGOFF_URL:
228 static void error_handler(GMarkupParseContext *context,
229 GError *error, gpointer user_data)
231 struct wispr_msg *msg = user_data;
233 msg->has_error = TRUE;
236 static const GMarkupParser wispr_parser = {
237 start_element_handler,
244 static void parser_callback(const char *str, gpointer user_data)
246 struct wispr_session *wispr = user_data;
247 GMarkupParseContext *context;
250 //printf("%s\n", str);
252 context = g_markup_parse_context_new(&wispr_parser,
253 G_MARKUP_TREAT_CDATA_AS_TEXT, &wispr->msg, NULL);
255 result = g_markup_parse_context_parse(context, str, strlen(str), NULL);
257 result = g_markup_parse_context_end_parse(context, NULL);
259 g_markup_parse_context_free(context);
262 static gboolean wispr_input(const guint8 **data, gsize *length,
265 struct wispr_session *wispr = user_data;
267 g_free(wispr->formdata);
268 wispr->formdata = g_strdup_printf("button=Login&UserName=%s&"
269 "Password=%s&FNAME=0&OriginatingServer=%s",
270 wispr->username, wispr->password, wispr->originurl);
272 *data = (guint8 *) wispr->formdata;
273 *length = strlen(wispr->formdata);
278 static gboolean wispr_result(GWebResult *result, gpointer user_data)
280 struct wispr_session *wispr = user_data;
286 g_web_result_get_chunk(result, &chunk, &length);
289 //printf("%s\n", (char *) chunk);
290 g_web_parser_feed_data(wispr->parser, chunk, length);
294 g_web_parser_end_data(wispr->parser);
296 status = g_web_result_get_status(result);
298 g_print("status: %03u\n", status);
300 elapsed = g_timer_elapsed(timer, NULL);
302 g_print("elapse: %f seconds\n", elapsed);
304 if (wispr->msg.message_type < 0)
307 printf("Message type: %s (%d)\n",
308 message_type_to_string(wispr->msg.message_type),
309 wispr->msg.message_type);
310 printf("Response code: %s (%d)\n",
311 response_code_to_string(wispr->msg.response_code),
312 wispr->msg.response_code);
313 if (wispr->msg.login_url != NULL)
314 printf("Login URL: %s\n", wispr->msg.login_url);
315 if (wispr->msg.logoff_url != NULL)
316 printf("Logoff URL: %s\n", wispr->msg.logoff_url);
319 if (status == 302 && wispr->msg.message_type == 100) {
320 wispr->request = g_web_request_post(wispr->web,
321 wispr->msg.login_url,
322 "application/x-www-form-urlencoded",
323 wispr_input, wispr_result, wispr);
325 wispr_msg_init(&wispr->msg);
328 } else if (status == 200 && wispr->msg.message_type == 120) {
329 int code = wispr->msg.response_code;
330 printf("Login process: %s\n",
331 code == 50 ? "SUCCESS" : "FAILURE");
335 g_main_loop_quit(main_loop);
340 static gboolean option_debug = FALSE;
341 static gchar *option_nameserver = NULL;
342 static gchar *option_username = NULL;
343 static gchar *option_password = NULL;
344 static gchar *option_url = NULL;
346 static GOptionEntry options[] = {
347 { "debug", 'd', 0, G_OPTION_ARG_NONE, &option_debug,
348 "Enable debug output" },
349 { "nameserver", 'n', 0, G_OPTION_ARG_STRING, &option_nameserver,
350 "Specify nameserver", "ADDRESS" },
351 { "username", 'u', 0, G_OPTION_ARG_STRING, &option_username,
352 "Specify username", "USERNAME" },
353 { "password", 'p', 0, G_OPTION_ARG_STRING, &option_password,
354 "Specify password", "PASSWORD" },
355 { "url", 'U', 0, G_OPTION_ARG_STRING, &option_url,
356 "Specify arbitrary request", "URL" },
360 int main(int argc, char *argv[])
362 GOptionContext *context;
363 GError *error = NULL;
365 struct wispr_session wispr;
368 context = g_option_context_new(NULL);
369 g_option_context_add_main_entries(context, options, NULL);
371 if (g_option_context_parse(context, &argc, &argv, &error) == FALSE) {
373 g_printerr("%s\n", error->message);
376 g_printerr("An unknown error occurred\n");
380 g_option_context_free(context);
382 memset(&wispr, 0, sizeof(wispr));
383 wispr_msg_init(&wispr.msg);
385 wispr.web = g_web_new(index);
386 if (wispr.web == NULL) {
387 fprintf(stderr, "Failed to create web service\n");
391 if (option_debug == TRUE)
392 g_web_set_debug(wispr.web, web_debug, "WEB");
394 main_loop = g_main_loop_new(NULL, FALSE);
396 if (option_nameserver != NULL) {
397 g_web_add_nameserver(wispr.web, option_nameserver);
398 g_free(option_nameserver);
401 g_web_set_accept(wispr.web, NULL);
402 g_web_set_user_agent(wispr.web, "SmartClient/%s wispr", VERSION);
403 g_web_set_close_connection(wispr.web, TRUE);
405 if (option_url == NULL)
406 option_url = g_strdup(DEFAULT_URL);
408 wispr.username = option_username;
409 wispr.password = option_password;
410 wispr.originurl = option_url;
412 timer = g_timer_new();
414 wispr.parser = g_web_parser_new("<WISPAccessGatewayParam",
415 "WISPAccessGatewayParam>",
416 parser_callback, &wispr);
418 wispr.request = g_web_request_get(wispr.web, option_url,
419 wispr_result, &wispr);
421 if (wispr.request == 0) {
422 fprintf(stderr, "Failed to start request\n");
426 memset(&sa, 0, sizeof(sa));
427 sa.sa_handler = sig_term;
428 sigaction(SIGINT, &sa, NULL);
429 sigaction(SIGTERM, &sa, NULL);
431 g_main_loop_run(main_loop);
433 g_timer_destroy(timer);
435 if (wispr.request > 0)
436 g_web_cancel_request(wispr.web, wispr.request);
438 g_web_parser_unref(wispr.parser);
439 g_web_unref(wispr.web);
441 g_main_loop_unref(main_loop);
443 g_free(option_username);
444 g_free(option_password);