-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathevt_test.c
198 lines (165 loc) · 4.76 KB
/
evt_test.c
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
//%LICENSE////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2015 Devchandra M. Leishangthem (dlmeetei at gmail dot com)
//
// Distributed under the MIT License (See accompanying file LICENSE)
//
//////////////////////////////////////////////////////////////////////////
//
//%///////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <assert.h>
#include "evt_tls.h"
typedef struct test_tls_s test_tls_t;
struct test_tls_s {
evt_tls_t *endpt;
//indicates whether the endpoint is closing or closed
int is_closing;
};
//Needed for pretty printing
char *role[2] = {
"Client"
,"Server"
};
struct my_data {
char data[16*1024];
int sz;
int stalled;
}test_data;
int test_tls_init(evt_ctx_t *ctx, test_tls_t *tst_tls)
{
memset( tst_tls, 0, sizeof *tst_tls);
evt_tls_t *t = evt_ctx_get_tls(ctx);
assert(t != NULL);
//Now user data in evt_tls_t is test_tls_t
t->data = tst_tls;
tst_tls->endpt = t;
return 0;
}
void cls(evt_tls_t *evt, int status)
{
printf("[%s]: on_close_cb called ++++++++\n", role[evt_tls_get_role(evt)]);
evt_tls_free(evt);
}
int test_tls_close(test_tls_t *t, evt_close_cb cls)
{
int rv = 0;
rv = evt_tls_close(t->endpt, cls);
t->is_closing = 1;
return rv;
}
void on_client_rd( evt_tls_t *tls, char *buf, int sz)
{
printf("[%s]: on_client_rd called ++++++++\n", role[evt_tls_get_role(tls)]);
printf("%s", (char*)buf);
test_tls_close((test_tls_t *)tls->data, cls);
}
void on_write(evt_tls_t *tls, int status)
{
assert(status > 0);
printf("[%s]: On_write called ++++++++\n", role[evt_tls_get_role(tls)]);
evt_tls_read( tls, on_client_rd);
}
void on_server_wr(evt_tls_t *tls, int status)
{
assert(status > 0);
printf("[%s]: on_server_wr called ++++++++\n", role[evt_tls_get_role(tls)]);
test_tls_close((test_tls_t *)tls->data, cls);
}
void on_server_rd( evt_tls_t *tls, char *buf, int sz)
{
test_tls_t *test_tls = (test_tls_t*)tls->data;
if ( sz <= 0 ) {
test_tls_close(test_tls, cls);
return;
}
printf("[%s]: on_server_rd called ++++++++\n", role[evt_tls_get_role(tls)]);
printf("%s", (char*)buf);
evt_tls_write(tls, buf, sz, on_server_wr);
}
void on_connect(evt_tls_t *tls, int status)
{
int r = 0;
printf("[%s]: On_connect called ++++++++\n", role[evt_tls_get_role(tls)]);
if ( status ) {
char msg[] = "Hello from event based tls engine\n";
int str_len = sizeof(msg);
r = evt_tls_write(tls, msg, str_len, on_write);
assert( r = str_len);
}
else { //handle ssl_shutdown
test_tls_close((test_tls_t*)tls->data, cls);
}
}
//test net writer for the test code
int test_net_wrtr(evt_tls_t *c, void *buf, int sz)
{
static int first_time = 1;
if ((test_data.stalled) || (first_time == 1)) {
first_time = 0;
memset(&test_data, 0, sizeof(test_data));
memcpy(test_data.data, buf, sz);
test_data.sz = sz;
test_data.stalled = 0;
}
return 0;
}
int start_nio(test_tls_t *source, test_tls_t *destination)
{
for(;;) {
if ( test_data.stalled ) break;
//don't write to closed handle
if ( destination->is_closing ) break;
test_data.stalled = 1;
evt_tls_feed_data(destination->endpt, test_data.data, test_data.sz);
test_tls_t *tmp = destination;
destination = source;
source = tmp;
}
//make compiler happy
return 0;
}
int test_tls_connect(test_tls_t *t, evt_handshake_cb on_connect)
{
return evt_tls_connect(t->endpt, on_connect);
}
void on_accept(evt_tls_t *svc, int status)
{
printf("[%s]: On_accept called ++++++++\n", role[evt_tls_get_role(svc)]);
//read data now
if ( status > 0 ) {
evt_tls_read(svc, on_server_rd );
}
else {
test_tls_close((test_tls_t*)svc->data, cls);
}
}
int test_tls_accept(test_tls_t *tls, evt_handshake_cb on_accept)
{
return evt_tls_accept(tls->endpt, on_accept);
}
int main()
{
evt_ctx_t tls;
memset(&tls, 0, sizeof(tls));
assert(0 == evt_ctx_init(&tls));
assert(0 == evt_ctx_is_crtf_set(&tls));
assert(0 == evt_ctx_is_key_set(&tls));
if (!evt_ctx_is_crtf_set(&tls)) {
evt_ctx_set_crt_key(&tls, "server-cert.pem", "server-key.pem");
}
assert( 1 == evt_ctx_is_crtf_set(&tls));
assert( 1 == evt_ctx_is_key_set(&tls));
assert(tls.writer == NULL);
evt_ctx_set_writer(&tls, test_net_wrtr);
assert(tls.writer != NULL);
test_tls_t clnt_hdl;
test_tls_init( &tls, &clnt_hdl);
test_tls_t svc_hdl;
test_tls_init( &tls, &svc_hdl);
test_tls_connect(&clnt_hdl, on_connect);
test_tls_accept(&svc_hdl, on_accept);
start_nio(&clnt_hdl, &svc_hdl);
evt_ctx_free(&tls);
return 0;
}