-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathproxy.c
303 lines (251 loc) · 8.29 KB
/
proxy.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
#include <assert.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include "proxy.h"
#include "utils.h"
#ifdef DTLS_PSK
static int get_psk_info(struct dtls_context_t *dtls_ctx,
const session_t *session, dtls_credentials_type_t type,
const unsigned char *id, size_t id_len,
unsigned char *result, size_t result_length)
{
if (type != DTLS_PSK_KEY) {
return 0;
}
proxy_context_t *ctx = (proxy_context_t *)dtls_get_app_data(dtls_ctx);
if (id && ctx) {
for (keystore_t *psk=ctx->psk; psk && psk->id; psk=psk->next) {
//DBG("psk=%s\n", psk->id);
if (id_len == psk->id_length && memcmp(id, psk->id, id_len) == 0) {
if (result_length < psk->key_length) {
ERR("buffer too small for PSK");
return dtls_alert_fatal_create(DTLS_ALERT_INTERNAL_ERROR);
}
memcpy(result, psk->key, psk->key_length);
return psk->key_length;
}
}
}
return dtls_alert_fatal_create(DTLS_ALERT_DECRYPT_ERROR);
}
#endif /* DTLS_PSK */
static int dtls_send_to_peer(struct dtls_context_t *dtls_ctx,
session_t *session, uint8 *data, size_t len)
{
proxy_context_t *ctx = (proxy_context_t *)dtls_get_app_data(dtls_ctx);
int fd = ctx->listen.fd;
return sendto(fd, data, len, MSG_DONTWAIT,
&session->addr.sa, session->size);
}
static int dtls_read_from_peer(struct dtls_context_t *dtls_ctx,
session_t *dtls_session, uint8 *data, size_t len)
{
proxy_context_t *ctx = (proxy_context_t *)dtls_get_app_data(dtls_ctx);
//dtls_peer_t *peer = dtls_get_peer(dtls_ctx, dtls_session);
//DBG("%s: peer=%lx", __func__, (unsigned long)peer);
//dumpbytes(data, len);
#if 0 // echo only
return dtls_write(dtls_ctx, session, data, len);
#else
client_context_t *client = find_client(ctx, dtls_session);
if (NULL!=client) {
//DBG("forward to backend=%d", client->backend_fd);
return send(client->backend_fd, data, len, 0);
}
return -1;
#endif
}
static int dtls_event(struct dtls_context_t *dtls_ctx, session_t *dtls_session,
dtls_alert_level_t level, unsigned short code)
{
proxy_context_t *ctx = (proxy_context_t *)dtls_get_app_data(dtls_ctx);
dtls_peer_t *peer = dtls_get_peer(dtls_ctx, dtls_session);
client_context_t *client = find_client(ctx, dtls_session);
if ((DTLS_ALERT_LEVEL_FATAL==level) || (DTLS_ALERT_CLOSE_NOTIFY==code)) {
if (NULL!=client) {
stop_client(ctx, client);
DBG("delete client %u/%u", client->index, ctx->clients.count);
free_client(ctx, client);
}
return 0;
}
switch (code)
{
case DTLS_EVENT_CONNECT:
DBG("%s: connect", __func__);
break;
case DTLS_EVENT_CONNECTED:
if (NULL!=client) {
DBG("client %u/%u already existing",
client->index, ctx->clients.count);
return 0;
}
if (NULL==peer) {
DBG("event connected but unknown peer");
return -1;
}
client = new_client(ctx, &peer->session);
if (NULL==client) {
// failed to allocate new client
return -1;
}
//DBG("%s: connected client %lx", __func__, (unsigned long)client);
if (0 != start_client(ctx, client)) {
free_client(ctx, client);
return -1;
}
return 0;
case DTLS_EVENT_RENEGOTIATE:
DBG("%s: renegotiate", __func__);
break;
default:
DBG("%s: unknown event=%u (alert=%d)", __func__, code, level);
break;
}
return 0;
}
static dtls_handler_t cb = {
.write = dtls_send_to_peer,
.read = dtls_read_from_peer,
.event = dtls_event,
#ifdef DTLS_PSK
.get_psk_info = get_psk_info,
#endif
#ifdef DTLS_ECC
.get_ecdsa_key = NULL,
.verify_ecdsa_key = NULL
#endif
};
static int init_addresses(proxy_context_t *ctx,
char *listen_addr_buf,
char *backends_addr_buf)
{
assert (ctx && listen_addr_buf && backends_addr_buf);
char *sep = NULL;
if (NULL == (sep = strrchr(listen_addr_buf, ':'))) {
return -1;
}
*sep = '\0';
if (resolve_address(listen_addr_buf, sep+1, &ctx->listen.addr) < 0) {
ERR("cannot resolve listen address");
return -1;
}
char addrbuf[64];
memset(addrbuf, 0, sizeof(addrbuf));
print_address(&ctx->listen.addr, addrbuf, sizeof(addrbuf)-1);
DBG("listen: %s", addrbuf);
char *ptr = backends_addr_buf;
char *addr_str = strtok_r(backends_addr_buf, ",", &ptr);
while (addr_str) {
if (NULL==new_backend(ctx, addr_str)) {
ERR("new_backend(%s) failed", addr_str);
return -1;
}
addr_str = strtok_r(NULL, ",", &ptr);
}
return 0;
}
// returns non-zero on error
int proxy_init(proxy_context_t *ctx,
char *listen_addr_buf,
char *backends_addr_buf,
char *psk_buf)
{
assert (ctx && psk_buf);
if (0!=init_addresses(ctx, listen_addr_buf, backends_addr_buf)) {
return -1;
}
ctx->psk = new_keystore(psk_buf);
if (NULL==ctx->psk) {
return -1;
}
/* init socket and set it to non-blocking */
ctx->listen.fd = create_socket(&ctx->listen.addr);
if (ctx->listen.fd <= 0) {
ERR("socket: %s", strerror(errno));
return -1;
}
if (bind(ctx->listen.fd, (struct sockaddr*)&ctx->listen.addr.addr,
ctx->listen.addr.size) < 0) {
ERR("bind: %s", strerror(errno));
return -1;
}
dtls_init();
ctx->dtls = dtls_new_context(ctx);
if (NULL==ctx->dtls) {
ERR("unable to allocate new dtl context");
return -1;
}
dtls_set_handler(ctx->dtls, &cb);
return 0;
}
static void proxy_cb(EV_P_ ev_io *w, int revents)
{
//DBG("%s revents=%04X", __func__, revents);
proxy_context_t *ctx = (proxy_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(ctx->listen.fd, buf, sizeof(buf), MSG_TRUNC,
&session.addr.sa, &session.size);
if (len < 0) {
perror("proxy recvfrom");
return;
} else {
//DBG("got %d bytes from port %u", len, ntohs(session.addr.sin6.sin6_port));
if (sizeof(buf) < len) {
ERR("packet was truncated (%lu bytes lost)", len - sizeof(buf));
}
}
dtls_handle_message(ctx->dtls, &session, buf, len);
}
static void start_watcher(EV_P_ ev_io *w, proxy_context_t *ctx)
{
DBG("proxy fd=%d", ctx->listen.fd);
loop = ctx->loop;
ev_io_init(w, proxy_cb, ctx->listen.fd, EV_READ);
w->data = ctx;
ev_io_start(EV_A_ w);
}
int proxy_run(proxy_context_t *ctx)
{
assert(NULL!=ctx);
struct ev_loop *loop = ev_default_loop(0);
ctx->loop = loop;
start_watcher(EV_A_ &ctx->watcher, ctx);
return ev_run(EV_A_ 0);
}
void proxy_exit(proxy_context_t *ctx)
{
assert(NULL!=ctx);
struct ev_loop *loop = ctx->loop;
client_context_t *client = ctx->clients.client;
while(client) {
stop_client(ctx, client);
client = client->next;
}
ev_io_stop(EV_A_ &ctx->watcher);
//DBG("call libev break()");
ev_break(EV_A_ EVBREAK_ALL);
}
void proxy_deinit(proxy_context_t *ctx)
{
assert(NULL!=ctx);
if (ctx->listen.fd > 0) {
close (ctx->listen.fd);
ctx->listen.fd = -1;
}
while(ctx->backends.server) {
DBG("delete backend %u", ctx->backends.server->address.ifindex);
free_backend(ctx, ctx->backends.server);
}
while(ctx->clients.client) {
DBG("delete client %u", ctx->clients.client->index);
free_client(ctx, ctx->clients.client);
}
dtls_free_context(ctx->dtls);
free_keystore(ctx->psk);
}