summaryrefslogtreecommitdiff
path: root/tests/tls-test.c
blob: 8c83edb8eec24b4dda011f11bbebb37bbc3504f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- */

/*  Lac - Library for asynchronous communication
 *  Copyright (C) 2002  Søren Sandmann (sandmann@daimi.au.dk)
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Library General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Library General Public License for more details.
 *
 *  You should have received a copy of the GNU Library General Public
 *  License along with this library; if not, write to the 
 *  Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 *  Boston, MA  02111-1307, USA.
 */
#include <lac.h>
#include <string.h>

GMainLoop *main_loop;
int n_connections = 0;
const int port = 443;

static void
connection_write (LacConnection *connection, gchar *str)
{
#if 0
    g_print ("%s", str);
#endif
    lac_connection_write (connection, str, strlen (str));
}

static void
conn_callback (LacConnection *connection, const LacConnectionEvent *event)
{
    gchar *s;

    g_print ("connection to %s:%d: ", (gchar *)lac_connection_get_data (connection), port);
    switch (event->type)
    {
    case LAC_CONNECTION_EVENT_CONNECT:
        g_print ("CONNECT\n");
        connection_write (connection, "GET / HTTP/1.1\r\n");
        connection_write (connection, "Host: ");
        connection_write (connection, lac_connection_get_data (connection));
        connection_write (connection, "\r\n\r\n");
        break;

    case LAC_CONNECTION_EVENT_READ:
        g_print ("READ (%d bytes)\n", event->read.len);
        s = g_new (guchar, event->read.len + 1);
        strncpy (s, event->read.data, event->read.len);
        s[event->read.len] = '\0';
        g_print ("%s\n", s);
        lac_connection_close (connection);
        g_free (s);
        break;
   
    case LAC_CONNECTION_EVENT_CLOSE:
        g_print ("CLOSE (%s)\n", event->close.remote_closed?
                 "remote" : "local");
        lac_connection_unref (connection);
        g_main_quit (main_loop);
        break;

    case LAC_CONNECTION_EVENT_ERROR:
        g_print ("ERROR (%s)\n", event->error.err->message);
        lac_connection_unref (connection);
        break;

    default:
        g_print ("event->type == %d?\n", event->type);
        g_assert_not_reached ();
        break;
    }
}

static void
callback (const LacAddress *addr, gpointer data, const GError *err)
{
    if (err)
    {
        g_print ("  (%p)  ", err->message);
        g_print ("%s\n", err->message);
    }
    else
    {
        LacConnection *connection;

        connection = lac_connection_new_tls (addr, port, conn_callback, data);
    }
}

int
main (int argc, char *argv[])
{
    int i;

#if 0
    lac_set_verbose (TRUE);
#endif
    
    if (argc > 1)
    {
        for (i = 1; i < argc; ++i)
        {
            ++n_connections;
            lac_address_new_lookup_from_name (argv[i], callback, argv[i]);
        }
    }
    else
    {
        g_print ("usage %s <names>\n", argv[0]);
        return 1;
    }

    main_loop = g_main_loop_new (NULL, TRUE);
    g_assert (main_loop);
    g_main_loop_run (main_loop);

    return 0;
}