-
Notifications
You must be signed in to change notification settings - Fork 5
/
client.c
399 lines (318 loc) · 11.3 KB
/
client.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
#include <assert.h>
#include <unistd.h>
#include <utlist.h>
#include "client.h"
#include "proxy.h"
#include "utils.h"
#define CLIENT_SESSION_START_TIMEOUT ((ev_tstamp)20.0) // 20 seconds
#define CLIENT_SESSION_INACTIVE_TIMEOUT ((ev_tstamp)300.0) // 5 minutes
client_context_t *new_client(struct proxy_context *ctx,
const session_t *addr)
{
assert(ctx && addr);
client_context_t *client = (client_context_t *)malloc(sizeof(client_context_t));
if (NULL==client) {
ERR("failed to allocate client_context");
return NULL;
}
memset(client, 0, sizeof(client_context_t));
client->proxy = ctx;
memcpy(&client->address, addr, sizeof(session_t));
backend_context_t *backend = next_backend(ctx);
if (NULL==backend) {
ERR("no available backend");
free(client);
return NULL;
}
client->client_fd = create_socket(addr);
if (client->client_fd <=0) {
ERR("unable to create socket to client");
free(client);
return NULL;
}
if (0!=bind(client->client_fd,
&ctx->listen.addr.addr.sa,
ctx->listen.addr.size)) {
ERR("bind client failed");
close(client->client_fd);
return NULL;
}
if (0!=connect(client->client_fd,
&addr->addr.sa,
addr->size)) {
ERR("connect to client failed");
close(client->client_fd);
return NULL;
}
client->backend_fd = create_socket(&backend->address);
if (client->backend_fd <=0) {
ERR("unable to create socket to backend");
close(client->client_fd);
free(client);
return NULL;
}
if (0!=connect(client->backend_fd,
&backend->address.addr.sa,
backend->address.size)) {
ERR("connect to backend failed");
close(client->client_fd);
close(client->backend_fd);
free(client);
return NULL;
}
client->index = ctx->clients.index++;
ctx->clients.count++;
LL_PREPEND(ctx->clients.client, client);
DBG("client %u linked to backend %u",
client->index, backend->address.ifindex);
return client;
}
void free_client(struct proxy_context *ctx,
client_context_t *client)
{
if (ctx && client) {
LL_DELETE(ctx->clients.client, client);
if (client->client_fd > 0) {
close(client->client_fd);
}
if (client->backend_fd > 0) {
close(client->backend_fd);
}
ctx->clients.count--;
free(client);
client = NULL;
}
}
client_context_t *find_client(struct proxy_context *ctx,
const session_t *addr)
{
assert(ctx);
if (NULL==addr) {
return NULL;
}
client_context_t *client = NULL;
LL_FOREACH(ctx->clients.client, client) {
if (dtls_session_equals(addr, &client->address)) {
return client;
}
}
return NULL;
}
/* copied form tinydtls' dtls_set_record_header() */
static inline uint8 *set_data_header(dtls_security_parameters_t *security, uint8 *buf)
{
if (NULL==security) {
return NULL;
}
dtls_int_to_uint8(buf, DTLS_CT_APPLICATION_DATA);
buf += sizeof(uint8);
dtls_int_to_uint16(buf, DTLS_VERSION);
buf += sizeof(uint16);
dtls_int_to_uint16(buf, security->epoch);
buf += sizeof(uint16);
dtls_int_to_uint48(buf, security->rseq);
buf += sizeof(uint48);
/* increment record sequence counter by 1 */
security->rseq++;
memset(buf, 0, sizeof(uint16));
return buf + sizeof(uint16);
}
/* copied form tinydtls' dtls_prepare_record() */
static int prepare_data_record(dtls_peer_t *peer, uint8 *data, size_t data_len,
uint8 *sendbuf, size_t *rlen)
{
dtls_security_parameters_t *security = dtls_security_params(peer);
#define DTLS_RH_LENGTH sizeof(dtls_record_header_t)
#define DTLS_RECORD_HEADER(M) ((dtls_record_header_t *)(M))
if (*rlen < DTLS_RH_LENGTH) {
ERR("The sendbuf (%zu bytes) is too small", *rlen);
return dtls_alert_fatal_create(DTLS_ALERT_INTERNAL_ERROR);
}
if (!security || security->cipher == TLS_NULL_WITH_NULL_NULL) {
return -1; // not supported
}
uint8 *p = set_data_header(security, sendbuf);
uint8 *start = p;
// TLS_PSK_WITH_AES_128_CCM_8 or TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8
#define A_DATA_LEN 13
unsigned char nonce[DTLS_CCM_BLOCKSIZE];
unsigned char A_DATA[A_DATA_LEN];
memcpy(p, &DTLS_RECORD_HEADER(sendbuf)->epoch, 8);
p += 8;
int res = 8;
// check the minimum that we need for packets that are not encrypted
if (*rlen < res + DTLS_RH_LENGTH + data_len) {
ERR("%s: send buffer too small", __func__);
return dtls_alert_fatal_create(DTLS_ALERT_INTERNAL_ERROR);
}
memcpy(p, data, data_len);
//p += data_len;
res += data_len;
memset(nonce, 0, DTLS_CCM_BLOCKSIZE);
memcpy(nonce, dtls_kb_local_iv(security, peer->role), dtls_kb_iv_size(security, peer->role));
memcpy(nonce + dtls_kb_iv_size(security, peer->role), start, 8); // epoch + seq_num
memcpy(A_DATA, &DTLS_RECORD_HEADER(sendbuf)->epoch, 8); /* epoch and seq_num */
memcpy(A_DATA + 8, &DTLS_RECORD_HEADER(sendbuf)->content_type, 3); // type and version
dtls_int_to_uint16(A_DATA + 11, res - 8); // length
res = dtls_encrypt(start + 8, res - 8, start + 8, nonce,
dtls_kb_local_write_key(security, peer->role),
dtls_kb_key_size(security, peer->role),
A_DATA, A_DATA_LEN);
if (res < 0) {
ERR("dtls_encrypt()=%d failed", res);
return res;
}
res += 8; // increment res by size of nonce_explicit
// fix length of fragment in sendbuf
dtls_int_to_uint16(sendbuf + 11, res);
*rlen = DTLS_RH_LENGTH + res;
return 0;
}
static int relay_to_client(client_context_t *client, uint8 *buf, size_t buf_len)
{
dtls_peer_t *peer = dtls_get_peer(client->proxy->dtls, &client->address);
if (!peer) {
return dtls_connect(client->proxy->dtls, &client->address);
}
if (peer->state != DTLS_STATE_CONNECTED) {
return 0;
}
unsigned char sendbuf[DTLS_MAX_BUF];
size_t len = sizeof(sendbuf);
int res = prepare_data_record(peer, buf, buf_len, sendbuf, &len);
if (res < 0) {
ERR("prepare_data_record()=%d failed", res);
return res;
}
//DBG("message (len=%zu):", buf_len);
//dumpbytes(buf, buf_len);
//DBG("encrypted (len=%zu):", len);
//dumpbytes(sendbuf, len);
return sendto(client->client_fd, sendbuf, len, MSG_DONTWAIT,
&client->address.addr.sa, client->address.size);
}
static void client_cb(EV_P_ ev_io *w, int revents)
{
//DBG("%s revents=%04X", __func__, revents);
client_context_t *client = (client_context_t *)w->data;
session_t session;
static uint8 buf[DTLS_MAX_BUF];
int len;
memset(&session, 0, sizeof(session_t));
session.size = sizeof(session.addr);
len = recvfrom(client->client_fd, buf, sizeof(buf), MSG_TRUNC,
&session.addr.sa, &session.size);
if (len < 0) {
perror("client recvfrom");
return;
} else {
//DBG("got %d bytes from port %u", len, ntohs(session.addr.sin6.sin6_port));
if (sizeof(buf) < (size_t)len) {
ERR("packet was truncated (%lu bytes lost)", len - sizeof(buf));
}
}
// mark client as active
ev_timer_again(EV_A_ &client->inactive_timer);
dtls_handle_message(client->proxy->dtls, &session, buf, len);
}
static void backend_cb(EV_P_ ev_io *w, int revents)
{
//DBG("%s revents=%04X", __func__, revents);
unsigned char packet[DTLS_MAX_BUF];
size_t packet_len = 0;
client_context_t *client = (client_context_t *)w->data;
if (w->fd == client->backend_fd) {
if (revents & EV_READ) {
//DBG("receive from backend");
int res = recv(w->fd, packet, sizeof(packet), 0);
if (res <= 0) {
ERR("recv() failed (res=%d)", res);
return;
}
packet_len = res;
relay_to_client(client, packet, packet_len);
}
}
// mark client as active
ev_timer_again(EV_A_ &client->inactive_timer);
}
static void inactive_timer_cb(EV_P_ ev_timer *w, int revents)
{
//DBG("%s revents=%04X", __func__, revents);
client_context_t *client = (client_context_t *)w->data;
if (NULL==client) {
ERR("no client context");
return;
}
proxy_context_t *ctx = (proxy_context_t *)client->proxy;
if (NULL==ctx) {
ERR("no proxy context");
return;
}
#if 0 // for debugging only
client_context_t *cc = ctx->clients.client;
while (cc) {
if (cc==client)
break;
cc = cc->next;
}
if (NULL==cc) {
// should not go here
ERR("inactive client already deleted?!");
ev_timer_stop(EV_A_ w);
return;
}
#endif
DBG("delete inactive client %u/%u", client->index, ctx->clients.count);
stop_client(ctx, client);
free_client(ctx, client);
}
static void start_client_watcher(EV_P_ ev_io *w, client_context_t *client)
{
DBG("client %u client fd=%d", client->index, client->client_fd);
loop = client->proxy->loop;
ev_io_init(w, client_cb, client->client_fd, EV_READ);
w->data = client;
ev_io_start(EV_A_ w);
}
static void start_backend_watcher(EV_P_ ev_io *w, client_context_t *client)
{
DBG("client %u backend fd=%d", client->index, client->backend_fd);
loop = client->proxy->loop;
ev_io_init(w, backend_cb, client->backend_fd, EV_READ);
w->data = client;
ev_io_start(EV_A_ w);
}
static void start_inactive_timer(EV_P_ ev_timer *w, client_context_t *client)
{
loop = client->proxy->loop;
ev_timer_init (w, inactive_timer_cb,
CLIENT_SESSION_START_TIMEOUT,
CLIENT_SESSION_INACTIVE_TIMEOUT);
w->data = client;
ev_timer_start (EV_A_ w);
}
int start_client(struct proxy_context *ctx,
client_context_t *client)
{
assert(ctx && client && (ctx==client->proxy));
//DBG("%s", __func__);
struct ev_loop *loop = ctx->loop;
start_client_watcher(EV_A_ &client->client_rd_watcher, client);
start_backend_watcher(EV_A_ &client->backend_rd_watcher, client);
start_inactive_timer(EV_A_ &client->inactive_timer, client);
return 0;
}
void stop_client(struct proxy_context *ctx,
client_context_t *client)
{
assert(ctx && client && (ctx==client->proxy));
//DBG("%s", __func__);
struct ev_loop *loop = ctx->loop;
ev_clear_pending(EV_A_ &client->inactive_timer);
ev_timer_stop(EV_A_ &client->inactive_timer);
ev_clear_pending(EV_A_ &client->client_rd_watcher);
ev_io_stop(EV_A_ &client->client_rd_watcher);
ev_clear_pending(EV_A_ &client->backend_rd_watcher);
ev_io_stop(EV_A_ &client->backend_rd_watcher);
}