-
Notifications
You must be signed in to change notification settings - Fork 20
/
dgram_router.c
314 lines (261 loc) · 10.1 KB
/
dgram_router.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
#include <uwsgi.h>
extern struct uwsgi_server uwsgi;
/*
the datagram router (dgram_router) allows you to forward datagrams to one or more peers.
It is used in the uwsgi.it infrastructure to forward subscription packets to the various fastrouters
in the cluster
uwsgi --dgram-router /subscribe/dgram --dgram-router-to 192.68.0.6:2100 --dgram-router-to 192.68.0.7:2100 --dgram-router-bind 192.68.0.5.2100
*/
static struct dgram_router {
char *addr;
struct uwsgi_string_list *to;
char *bind;
int fd;
int bind_fd;
int queue;
char *psk_in;
char *psk_out;
EVP_CIPHER_CTX *encrypt_ctx;
EVP_CIPHER_CTX *decrypt_ctx;
char *encrypt_buf;
char *decrypt_buf;
} dgr;
static struct uwsgi_option uwsgi_dgram_router_options[] = {
{"dgram-router", required_argument, 0, "run the dgram router on the specified address", uwsgi_opt_set_str, &dgr.addr, 0},
{"dgram-router-to", required_argument, 0, "forward each received dgram packet to the specified peer", uwsgi_opt_add_string_list, &dgr.to, 0},
{"dgram-router-bind", required_argument, 0, "bind to the specified address when forwrding dgram packets", uwsgi_opt_set_str, &dgr.bind, 0},
{"dgram-router-psk-in", required_argument, 0, "set a preshared key for decrypting incoming traffic (syntax: algo:secret [iv])", uwsgi_opt_set_str, &dgr.psk_in, 0},
{"dgram-router-psk-out", required_argument, 0, "set a preshared key for encrypting outgoing traffic (syntax: algo:secret [iv])", uwsgi_opt_set_str, &dgr.psk_out, 0},
UWSGI_END_OF_OPTIONS
};
static const EVP_CIPHER *setup_secret_and_iv(char *arg, char **secret, char **iv) {
char *colon = strchr(arg, ':');
if (!colon) {
uwsgi_log("invalid dgram-router psk syntax, must be: <algo>:<secret> [iv]\n");
exit(1);
}
*colon = 0;
const EVP_CIPHER *cipher = EVP_get_cipherbyname(arg);
if (!cipher) {
uwsgi_log("unable to find algorithm/cipher %s\n", arg);
exit(1);
}
*colon = ':';
*secret = colon+1;
*iv = NULL;
char *space = strchr(colon+1, ' ');
if (space) {
*space = 0;
*iv = space+1;
}
int cipher_len = EVP_CIPHER_key_length(cipher);
size_t s_len = strlen(*secret);
if ((unsigned int) cipher_len > s_len) {
char *secret_tmp = uwsgi_malloc(cipher_len);
memcpy(secret_tmp, *secret, s_len);
memset(secret_tmp + s_len, 0, cipher_len - s_len);
*secret = secret_tmp;
}
int iv_len = EVP_CIPHER_iv_length(cipher);
size_t s_iv_len = 0;
if (*iv) {
s_iv_len = strlen(*iv);
}
if ((unsigned int) iv_len > s_iv_len) {
char *secret_tmp = uwsgi_malloc(iv_len);
memcpy(secret_tmp, *iv, s_iv_len);
memset(secret_tmp + s_iv_len, '0', iv_len - s_iv_len);
*iv = secret_tmp;
}
if (space) *space = ' ';
return cipher;
}
static ssize_t encrypt_packet(char *buf, size_t len) {
if (EVP_EncryptInit_ex(dgr.encrypt_ctx, NULL, NULL, NULL, NULL) <= 0) {
uwsgi_error("EVP_EncryptInit_ex()");
return -1;
}
int e_len = 0;
if (EVP_EncryptUpdate(dgr.encrypt_ctx, (unsigned char *) dgr.encrypt_buf, &e_len, (unsigned char *) buf, len) <= 0) {
uwsgi_error("EVP_EncryptUpdate()");
return -1;
}
int tmplen = 0;
if (EVP_EncryptFinal_ex(dgr.encrypt_ctx, (unsigned char *) (dgr.encrypt_buf + e_len), &tmplen) <= 0) {
uwsgi_error("EVP_EncryptFinal_ex()");
return -1;
}
return e_len + tmplen;
}
static ssize_t decrypt_packet(char *buf, size_t len) {
if (EVP_DecryptInit_ex(dgr.decrypt_ctx, NULL, NULL, NULL, NULL) <= 0) {
uwsgi_error("EVP_DecryptInit_ex()");
return -1;
}
int d_len = 0;
if (EVP_DecryptUpdate(dgr.decrypt_ctx, (unsigned char *)dgr.decrypt_buf, &d_len, (unsigned char *) buf, len) <= 0) {
uwsgi_error("EVP_DecryptUpdate()");
return -1;
}
int tmplen = 0;
if (EVP_DecryptFinal_ex(dgr.decrypt_ctx, (unsigned char *) (dgr.decrypt_buf + d_len), &tmplen) <= 0) {
ERR_print_errors_fp(stderr);
uwsgi_error("EVP_DecryptFinal_ex()");
return -1;
}
return d_len + tmplen;
}
static void uwsgi_dgram_router_loop(int id, void *arg) {
char *colon = strchr(dgr.addr, ':');
if (colon) {
dgr.fd = bind_to_udp(dgr.addr, 0, 0);
}
else {
dgr.fd = bind_to_unix_dgram(dgr.addr);
}
uwsgi_socket_nb(dgr.fd);
dgr.bind_fd = -1;
if (dgr.bind) {
colon = strchr(dgr.bind, ':');
if (colon) {
dgr.bind_fd = bind_to_udp(dgr.bind, 0, 0);
}
else {
dgr.bind_fd = bind_to_unix_dgram(dgr.bind);
}
uwsgi_socket_nb(dgr.bind_fd);
}
// prepare sockets and addresses
struct uwsgi_string_list *usl;
uwsgi_foreach(usl, dgr.to) {
char *udp_port = strchr(usl->value, ':');
if (udp_port) {
*udp_port = 0;
struct sockaddr_in *udp_addr = uwsgi_calloc(sizeof(struct sockaddr_in));
udp_addr->sin_family = AF_INET;
udp_addr->sin_port = htons(atoi(udp_port + 1));
udp_addr->sin_addr.s_addr = inet_addr(usl->value);
*udp_port = ':';
if (dgr.bind_fd > -1) {
usl->custom = (uint64_t) dgr.bind_fd;
}
else {
int bfd = socket(AF_INET, SOCK_DGRAM, 0);
if (bfd < 0) {
uwsgi_error("uwsgi_dgram_router_init()/socket()");
exit(1);
}
uwsgi_socket_nb(bfd);
usl->custom = (uint64_t) bfd;
}
usl->custom_ptr = udp_addr;
usl->custom2 = sizeof(struct sockaddr_in);
}
else {
struct sockaddr_un *un_addr = uwsgi_calloc(sizeof(struct sockaddr_un));
un_addr->sun_family = AF_UNIX;
// use 102 as the magic number
strncat(un_addr->sun_path, usl->value, 102);
if (dgr.bind_fd > -1) {
usl->custom = (uint64_t) dgr.bind_fd;
}
else {
int bfd = socket(AF_UNIX, SOCK_DGRAM, 0);
if (bfd < 0) {
uwsgi_error("uwsgi_dgram_router_init()/socket()");
exit(1);
}
uwsgi_socket_nb(bfd);
usl->custom = (uint64_t) bfd;
}
usl->custom_ptr = un_addr;
usl->custom2 = sizeof(struct sockaddr_un);
}
}
dgr.queue = event_queue_init();
void *events = event_queue_alloc(64);
if (event_queue_add_fd_read(dgr.queue, dgr.fd))
exit(1);
for (;;) {
int i;
int nevents = event_queue_wait_multi(dgr.queue, -1, events, 64);
for (i = 0; i < nevents; i++) {
int interesting_fd = event_queue_interesting_fd(events, i);
if (interesting_fd == dgr.fd) {
char buf[8192];
ssize_t rlen = read(interesting_fd, buf, 8192);
if (rlen < 0) {
if (uwsgi_is_again()) continue;
uwsgi_error("uwsgi_dgram_router_loop()/read()");
exit(1);
}
char *buf2 = buf;
if (dgr.psk_in) {
rlen = decrypt_packet(buf, rlen);
if (rlen < 0) continue;
buf2 = dgr.decrypt_buf;
}
if (dgr.psk_out) {
rlen = encrypt_packet(buf2, rlen);
if (rlen < 0) continue;
buf2 = dgr.encrypt_buf;
}
if (rlen > 0) {
struct uwsgi_string_list *usl;
uwsgi_foreach(usl, dgr.to) {
if (sendto((int) usl->custom, buf2, rlen, 0, (const struct sockaddr *) usl->custom_ptr, (socklen_t) usl->custom2) < 0) {
uwsgi_error("uwsgi_dgram_router_loop()/sendto()");
}
}
}
}
}
}
}
static int uwsgi_dgram_router_init() {
if (!dgr.addr) return 0;
if (dgr.psk_in) {
if (!uwsgi.ssl_initialized) uwsgi_ssl_init();
char *secret = NULL;
char *iv = NULL;
#if OPENSSL_VERSION_NUMBER < 0x10100000L
dgr.decrypt_ctx = uwsgi_malloc(sizeof(EVP_CIPHER_CTX));
EVP_CIPHER_CTX_init(dgr.decrypt_ctx);
#else
dgr.decrypt_ctx = EVP_CIPHER_CTX_new();
#endif
const EVP_CIPHER *cipher = setup_secret_and_iv(dgr.psk_in, &secret, &iv);
if (EVP_DecryptInit_ex(dgr.decrypt_ctx, cipher, NULL, (const unsigned char *) secret, (const unsigned char *) iv) <= 0) {
uwsgi_error("EVP_DecryptInit_ex()");
exit(1);
}
dgr.decrypt_buf = uwsgi_malloc(8192 + EVP_MAX_BLOCK_LENGTH);
}
if (dgr.psk_out) {
if (!uwsgi.ssl_initialized) uwsgi_ssl_init();
char *secret = NULL;
char *iv = NULL;
#if OPENSSL_VERSION_NUMBER < 0x10100000L
dgr.encrypt_ctx = uwsgi_malloc(sizeof(EVP_CIPHER_CTX));
EVP_CIPHER_CTX_init(dgr.encrypt_ctx);
#else
dgr.encrypt_ctx = EVP_CIPHER_CTX_new();
#endif
const EVP_CIPHER *cipher = setup_secret_and_iv(dgr.psk_out, &secret, &iv);
if (EVP_EncryptInit_ex(dgr.encrypt_ctx, cipher, NULL, (const unsigned char *) secret, (const unsigned char *) iv) <= 0) {
uwsgi_error("EVP_EncryptInit_ex()");
exit(1);
}
dgr.encrypt_buf = uwsgi_malloc(8192 + EVP_MAX_BLOCK_LENGTH);
}
if (register_gateway("uWSGI dgram router", uwsgi_dgram_router_loop, NULL) == NULL) {
uwsgi_log("unable to register the dgram router gateway\n");
exit(1);
}
return 0;
}
struct uwsgi_plugin dgram_router_plugin = {
.name = "dgram_router",
.options = uwsgi_dgram_router_options,
.init = uwsgi_dgram_router_init,
};