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
32
#include <netinet/in.h>
33
#include <arpa/inet.h>
36
#include <glib/gprintf.h>
39
#define PROXY_PORT 911
41
#define HOST "connman.net"
42
#define USER_APP "connman"
44
#define CONNECT_TIMEOUT 120
45
#define MAX_COUNTER 80
47
enum get_page_status {
51
GET_PAGE_REDIRECTED = 3,
55
char host[MAX_COUNTER];
56
char page[MAX_COUNTER];
57
char proxy[MAX_COUNTER];
64
int (*get_page) (struct server_data *data, char *page, int len,
65
enum get_page_status status);
68
static GMainLoop *main_loop = NULL;
70
static int create_socket()
74
sk = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
76
perror("Error: Can not create TCP socket");
83
static char *get_ip_from_host(char *host)
87
struct hostent *host_ent;
89
ip = g_try_malloc0(ip_len + 1);
90
if ((host_ent = gethostbyname(host)) == NULL) {
91
perror("Error: Can not get IP");
95
if (inet_ntop(AF_INET, (void *) host_ent->h_addr_list[0],
96
ip, ip_len) == NULL) {
97
perror("Error: Can not resolve host");
104
static char *build_get_query(char *host, char *page)
107
char *host_page = page;
108
char *tpl = "GET /%s HTTP/1.0\r\nHost: %s\r\nUser-Agent: %s\r\n\r\n";
110
if(host_page[0] == '/')
111
host_page = host_page + 1;
113
query = g_try_malloc0(strlen(host) + strlen(host_page) +
114
strlen(USER_APP) + strlen(tpl) - 5);
115
sprintf(query, tpl, host_page, host, USER_APP);
120
static gboolean connect_timeout(gpointer user_data)
122
struct server_data *data = user_data;
127
data->get_page(data, NULL, 0, GET_PAGE_TIMEOUT);
132
static void remove_timeout(struct server_data *data)
134
if (data->timeout > 0) {
135
g_source_remove(data->timeout);
140
static gboolean tcp_event(GIOChannel *channel, GIOCondition condition,
146
struct server_data *data = user_data;
148
if (condition & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
149
remove_timeout(data);
152
data->get_page(data, NULL, 0, GET_PAGE_FAILED);
157
sk = g_io_channel_unix_get_fd(channel);
158
len = recv(sk, buf, BUFSIZ, 0);
161
remove_timeout(data);
163
data->get_page(data, buf, len, GET_PAGE_SUCCESS);
169
static gboolean socket_event(GIOChannel *channel, GIOCondition condition,
172
struct server_data *data = user_data;
175
unsigned int send_counter = 0;
178
if (condition & G_IO_OUT && data->connection_ready == 0) {
179
data->connection_ready = 1;
180
sk = g_io_channel_unix_get_fd(channel);
182
query = build_get_query(data->host, data->page);
183
fprintf(stderr, "query is:\n%s\n", query);
185
while (send_counter < strlen(query)) {
186
ret = send(sk, query+send_counter,
187
strlen(query) - send_counter, 0);
189
perror("Error sending query");
190
remove_timeout(data);
192
data->get_page(data, NULL, 0,
200
} else if (condition & G_IO_IN)
201
tcp_event(channel, condition, user_data);
206
static void remove_connection(struct server_data *data)
208
remove_timeout(data);
209
g_source_remove(data->watch);
210
g_io_channel_shutdown(data->channel, TRUE, NULL);
218
static int get_html(struct server_data *data, int ms_time)
220
struct sockaddr_in *remote_host;
224
data->connection_ready = 0;
225
data->sock = create_socket();
226
if (strlen(data->proxy) > 0)
227
ip = get_ip_from_host(data->proxy);
229
ip = get_ip_from_host(data->host);
231
fprintf(stderr, "IP from host %s is %s\n", data->host, ip);
233
remote_host = g_try_new0(struct sockaddr_in, 1);
234
remote_host->sin_family = AF_INET;
235
ret = inet_pton(AF_INET, ip, (void *) (&(remote_host->sin_addr.s_addr)));
237
perror("Error Calling inet_pton");
239
} else if (ret == 0) {
240
fprintf(stderr, "Error: wrong IP address:%s\n", ip);
243
if (strlen(data->proxy) > 0)
244
remote_host->sin_port = htons(data->proxy_port);
246
remote_host->sin_port = htons(PORT);
248
data->channel = g_io_channel_unix_new(data->sock);
249
g_io_channel_set_flags(data->channel, G_IO_FLAG_NONBLOCK, NULL);
250
g_io_channel_set_close_on_unref(data->channel, TRUE);
251
data->watch = g_io_add_watch(data->channel, G_IO_OUT | G_IO_IN,
253
data->timeout = g_timeout_add_seconds(ms_time, connect_timeout, data);
255
ret = connect(data->sock, (struct sockaddr *)remote_host,
256
sizeof(struct sockaddr));
257
if (ret < 0 && errno != EINPROGRESS) {
258
perror("Could not connect");
259
remove_timeout(data);
272
data->get_page(data, NULL, 0, GET_PAGE_FAILED);
277
static int get_status(struct server_data *data, char *page, int len)
282
int ret = GET_PAGE_REDIRECTED;
284
lines = g_strsplit(page, "\n", 13);
286
str = g_strrstr(lines[0], "200 OK");
288
for (i = 0; lines[i] != NULL && i < 12; i++) {
289
str = g_strstr_len(lines[i], 12, "Set-Cookie");
291
ret = GET_PAGE_SUCCESS;
299
static int get_page_cb(struct server_data *data, char *page, int len,
300
enum get_page_status status)
305
ret = get_status(data, page, len);
308
case GET_PAGE_SUCCESS:
309
fprintf(stderr, "%s\n", "Page was fetched");
311
case GET_PAGE_REDIRECTED:
312
fprintf(stderr, "%s\n", "Page was redirected");
314
case GET_PAGE_FAILED:
315
fprintf(stderr, "%s\n", "error can not get the page");
317
case GET_PAGE_TIMEOUT:
318
fprintf(stderr, "%s\n", "Page was timeout");
321
g_main_loop_quit(main_loop);
326
int main(int argc, char **argv)
331
struct server_data *data;
339
data = g_try_new0(struct server_data, 1);
343
memset(data, 0, sizeof(struct server_data));
344
strcpy(data->host, host);
345
strcpy(data->page, page);
346
data->get_page = get_page_cb;
349
main_loop = g_main_loop_new(NULL, FALSE);
351
proxy = getenv("http_proxy");
355
if (strncmp(proxy, "http://", 7) == 0)
356
strcpy(data->proxy, proxy + 7);
358
strcpy(data->proxy, proxy);
360
delim = strchr(data->proxy, ':');
364
len = delim - data->proxy;
365
data->proxy[len] = '\0';
367
data->proxy_port = atoi(delim + 1);
369
data->proxy_port = PROXY_PORT;
371
get_html(data, CONNECT_TIMEOUT);
373
g_main_loop_run(main_loop);
375
remove_connection(data);