7e7db21f7d366174aeb2872fe0901cb5341e1c1b
[framework/connectivity/connman.git] / tools / wispr.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2010  Intel Corporation. All rights reserved.
6  *
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.
10  *
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.
15  *
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
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdio.h>
27 #include <errno.h>
28 #include <unistd.h>
29 #include <string.h>
30 #include <signal.h>
31 #include <sys/socket.h>
32 #include <arpa/inet.h>
33
34 #include "gweb/giognutls.h"
35
36 static GMainLoop *main_loop;
37
38 static void sig_term(int sig)
39 {
40         g_main_loop_quit(main_loop);
41 }
42
43 static int create_connection(const char *address, unsigned short port)
44 {
45         struct sockaddr_in sin;
46         int sk;
47
48         sk = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
49         if (sk < 0)
50                 return -EIO;
51
52         memset(&sin, 0, sizeof(sin));
53         sin.sin_family = AF_INET;
54         sin.sin_port = htons(port);
55         sin.sin_addr.s_addr = inet_addr(address);
56
57         if (connect(sk, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
58                 close(sk);
59                 return -EIO;
60         }
61
62         return sk;
63 }
64
65 static gboolean received_data(GIOChannel *channel, GIOCondition cond,
66                                                         gpointer user_data)
67 {
68         gchar buf[2048];
69         gsize bytes_read;
70         GIOStatus status;
71
72         if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
73                 g_main_loop_quit(main_loop);
74                 return FALSE;
75         }
76
77         memset(buf, 0, sizeof(buf));
78
79         status = g_io_channel_read_chars(channel, buf, sizeof(buf) - 1,
80                                                         &bytes_read, NULL);
81
82         printf("%s\n", buf);
83
84         if (bytes_read == 0) {
85                 g_main_loop_quit(main_loop);
86                 return FALSE;
87         }
88
89         return TRUE;
90 }
91
92 static gboolean option_debug = FALSE;
93
94 static GOptionEntry options[] = {
95         { "debug", 'd', 0, G_OPTION_ARG_NONE, &option_debug,
96                                         "Enable debug output" },
97         { NULL },
98 };
99
100 int main(int argc, char *argv[])
101 {
102         GOptionContext *context;
103         GError *error = NULL;
104         struct sigaction sa;
105         GIOChannel *channel;
106         gsize written;
107         int sk;
108
109         context = g_option_context_new(NULL);
110         g_option_context_add_main_entries(context, options, NULL);
111
112         if (g_option_context_parse(context, &argc, &argv, &error) == FALSE) {
113                 if (error != NULL) {
114                         g_printerr("%s\n", error->message);
115                         g_error_free(error);
116                 } else
117                         g_printerr("An unknown error occurred\n");
118                 return 1;
119         }
120
121         g_option_context_free(context);
122
123         sk = create_connection("140.211.169.100", 443);
124         if (sk < 0) {
125                 fprintf(stderr, "Failed to create connection\n");
126                 return 1;
127         }
128
129         main_loop = g_main_loop_new(NULL, FALSE);
130
131         channel = g_io_channel_gnutls_new(sk);
132         if (channel == NULL) {
133                 fprintf(stderr, "Failed to create GnuTLS IO channel\n");
134                 return 1;
135         }
136
137         g_io_channel_set_close_on_unref(channel, TRUE);
138
139         memset(&sa, 0, sizeof(sa));
140         sa.sa_handler = sig_term;
141         sigaction(SIGINT, &sa, NULL);
142         sigaction(SIGTERM, &sa, NULL);
143
144         g_io_add_watch(channel, G_IO_IN | G_IO_ERR | G_IO_HUP,
145                                                 received_data, NULL);
146
147 #define MSG "GET / HTTP/1.0\r\n\r\n"
148
149         g_io_channel_write_chars(channel, MSG, strlen(MSG), &written, NULL);
150
151         g_main_loop_run(main_loop);
152
153         g_main_loop_unref(main_loop);
154
155         g_io_channel_unref(channel);
156
157         return 0;
158 }