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 <sys/socket.h>
32 #include <arpa/inet.h>
34 #include "gweb/giognutls.h"
36 static GMainLoop *main_loop;
38 static void sig_term(int sig)
40 g_main_loop_quit(main_loop);
43 static int create_connection(const char *address, unsigned short port)
45 struct sockaddr_in sin;
48 sk = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
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);
57 if (connect(sk, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
65 static gboolean received_data(GIOChannel *channel, GIOCondition cond,
72 if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
73 g_main_loop_quit(main_loop);
77 memset(buf, 0, sizeof(buf));
79 status = g_io_channel_read_chars(channel, buf, sizeof(buf) - 1,
84 if (bytes_read == 0) {
85 g_main_loop_quit(main_loop);
92 static gboolean option_debug = FALSE;
94 static GOptionEntry options[] = {
95 { "debug", 'd', 0, G_OPTION_ARG_NONE, &option_debug,
96 "Enable debug output" },
100 int main(int argc, char *argv[])
102 GOptionContext *context;
103 GError *error = NULL;
109 context = g_option_context_new(NULL);
110 g_option_context_add_main_entries(context, options, NULL);
112 if (g_option_context_parse(context, &argc, &argv, &error) == FALSE) {
114 g_printerr("%s\n", error->message);
117 g_printerr("An unknown error occurred\n");
121 g_option_context_free(context);
123 sk = create_connection("140.211.169.100", 443);
125 fprintf(stderr, "Failed to create connection\n");
129 main_loop = g_main_loop_new(NULL, FALSE);
131 channel = g_io_channel_gnutls_new(sk);
132 if (channel == NULL) {
133 fprintf(stderr, "Failed to create GnuTLS IO channel\n");
137 g_io_channel_set_close_on_unref(channel, TRUE);
139 memset(&sa, 0, sizeof(sa));
140 sa.sa_handler = sig_term;
141 sigaction(SIGINT, &sa, NULL);
142 sigaction(SIGTERM, &sa, NULL);
144 g_io_add_watch(channel, G_IO_IN | G_IO_ERR | G_IO_HUP,
145 received_data, NULL);
147 #define MSG "GET / HTTP/1.0\r\n\r\n"
149 g_io_channel_write_chars(channel, MSG, strlen(MSG), &written, NULL);
151 g_main_loop_run(main_loop);
153 g_main_loop_unref(main_loop);
155 g_io_channel_unref(channel);