-
Notifications
You must be signed in to change notification settings - Fork 31
/
fqd_ccs.c
384 lines (368 loc) · 12.2 KB
/
fqd_ccs.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
/*
* Copyright (c) 2013 OmniTI Computer Consulting, Inc.
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#include "fqd.h"
#include "fqd_private.h"
#include "fq_dtrace.h"
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <poll.h>
#include <errno.h>
#include <uuid/uuid.h>
#ifdef __MACH__
static int mkkey(void *ptr, int len) {
static FILE *_random;
if(_random == NULL) _random = fopen("/dev/random", "r");
if(_random == NULL) return -1;
unsigned char *ucp = ptr;
for(int i=0; i<len; i++) ucp[i] = fgetc(_random);
return 0;
}
#else
#include <openssl/rand.h>
static int mkkey(void *ptr, int len) {
if(RAND_bytes(ptr, len) != 1) {
#if OPENSSL_VERSION_NUMBER < 0x1010100fL
if(RAND_pseudo_bytes(ptr, len) != 1) {
return -1;
}
#else
return -1;
#endif
}
return 0;
}
#endif
static int
fqd_ccs_auth(remote_client *client) {
uint16_t cmd, method;
fq_rk queue_name;
if(fq_read_uint16(client->fd, &cmd) ||
ntohs(cmd) != FQ_PROTO_AUTH_CMD) {
ERRTOFD(client->fd, "auth command expected");
return -1;
}
if(fq_read_uint16(client->fd, &method)) {
ERRTOFD(client->fd, "auth method read failed");
return -2;
}
method = ntohs(method);
if(method == 0) {
char buf[128];
unsigned char pass[10240];
char queue_detail[1024], *end_of_qd;
char *qtype = NULL, *qparams = NULL;
char *replace_params = NULL;
int len;
len = fq_read_short_cmd(client->fd, sizeof(client->user.name),
client->user.name);
if(len < 0 || len > (int)sizeof(client->user.name)) {
ERRTOFD(client->fd, "user name is too long");
return -3;
}
client->user.len = len & 0xff;
len = fq_read_short_cmd(client->fd, sizeof(queue_detail)-1,
queue_detail);
if(len < 0) return -4;
if(len >= sizeof(queue_detail)) {
ERRTOFD(client->fd, "queue detail is too long");
return -4;
}
queue_detail[len] = '\0';
end_of_qd = memchr(queue_detail, '\0', len);
if(!end_of_qd) {
if(len < 0 || len > (int)sizeof(queue_name.name)) {
ERRTOFD(client->fd, "queue name is too long");
return -4;
}
queue_name.len = len & 0xff;
memcpy(queue_name.name, queue_detail, queue_name.len);
if(queue_name.len < sizeof(queue_name.name))
memset(queue_name.name + queue_name.len, 0,
sizeof(queue_name.name) - queue_name.len);
}
else if(end_of_qd - queue_detail <= 0xff) {
queue_name.len = end_of_qd - queue_detail;
memcpy(queue_name.name, queue_detail, queue_name.len);
if(queue_name.len < sizeof(queue_name.name))
memset(queue_name.name + queue_name.len, 0,
sizeof(queue_name.name) - queue_name.len);
qtype = end_of_qd + 1;
if(*qtype) qparams = strchr(qtype, ':');
else qtype = NULL;
if(qparams) *qparams++ = '\0';
}
else {
ERRTOFD(client->fd, "pass field is too long");
return -4;
}
if(queue_name.len == 0) {
uuid_t autogen;
static const char *DYNAMIC_QUEUE_FORCE_OPTIONS = "transient,private";
int rlen = strlen(DYNAMIC_QUEUE_FORCE_OPTIONS)+1;
if(!qparams || *qparams == '\0') {
replace_params = malloc(rlen);
memcpy(replace_params, DYNAMIC_QUEUE_FORCE_OPTIONS, rlen);
}
else {
rlen += strlen(qparams)+1;
replace_params = malloc(rlen);
snprintf(replace_params, rlen, "%s,%s",
qparams, DYNAMIC_QUEUE_FORCE_OPTIONS);
}
qparams = replace_params;
uuid_generate(autogen);
memcpy(queue_name.name, "auto-", 5);
uuid_unparse_lower(autogen, (void *)(queue_name.name+5));
queue_name.len = 5 + 36; /* 5 + 36 uuid, no trailing \0 */
}
len = fq_read_short_cmd(client->fd, sizeof(pass), pass);
if(len < 0 || len > (int)sizeof(pass)) {
ERRTOFD(client->fd, "queue name is too long");
free(replace_params);
return -4;
}
client->queue = fqd_queue_get(&queue_name, qtype, qparams,
sizeof(buf), buf);
if(client->queue == NULL) {
ERRTOFD(client->fd, buf);
free(replace_params);
return -6;
}
/* do AUTH */
buf[0] = '\0';
inet_ntop(AF_INET, &client->remote.sin_addr, buf, sizeof(buf));
snprintf(client->pretty, sizeof(client->pretty), "%.*s/%.*s@%s:%d",
client->user.len, client->user.name,
queue_name.len, queue_name.name,
buf, ntohs(client->remote.sin_port));
if(FQ_CLIENT_AUTH_ENABLED()) {
fq_dtrace_remote_client_t dclient;
DTRACE_PACK_CLIENT(&dclient, client);
FQ_CLIENT_AUTH(&dclient);
}
free(replace_params);
return 0;
}
ERRTOFD(client->fd, "unsupported auth method");
return -1;
}
static int
fqd_ccs_key_client(remote_client *client) {
int fd = client->fd;
client->key.len = sizeof(client->key.name);
if(mkkey(client->key.name, client->key.len) != 0) {
ERRTOFD(fd, "can't generate random key");
return -1;
}
if(fqd_queue_register_client(client->queue, client)) {
ERRTOFD(fd, "can't add you to queue");
return -1;
}
if(fq_write_uint16(client->fd, FQ_PROTO_AUTH_RESP) ||
fq_write_short_cmd(client->fd,
client->key.len, client->key.name) < 0) {
return -2;
}
#ifdef DEBUG
{
char hex[260];
if(fq_rk_to_hex(hex, sizeof(hex), &client->key) >= 0)
fq_debug(FQ_DEBUG_CONN, "client keyed:\n%s\n", hex);
}
#endif
return 0;
}
static int
fqd_ccs_heartbeat(remote_client *client) {
#ifdef DEBUG
fq_debug(FQ_DEBUG_CONN, "heartbeat -> %s\n", client->pretty);
#endif
return fq_write_uint16(client->fd, FQ_PROTO_HB);
}
static int
fqd_css_status(remote_client *client) {
remote_data_client *data = client->data;
#ifdef DEBUG
fq_debug(FQ_DEBUG_CONN, "status -> %s\n", client->pretty);
#endif
if(fq_write_uint16(client->fd, FQ_PROTO_STATUS) < 0) return -1;
#define write_uintkey(name, v) do { \
if(fq_write_short_cmd(client->fd, strlen(name), name) < 0) return -1; \
if(fq_write_uint32(client->fd, v) < 0) return -1; \
} while(0)
if(client->queue) write_uintkey("dropped_in", client->queue->dropped_to);
if(data) {
write_uintkey("no_exchange", data->no_exchange);
write_uintkey("no_route", data->no_route);
write_uintkey("routed", data->routed);
write_uintkey("dropped", data->dropped);
write_uintkey("size_dropped", data->size_dropped);
write_uintkey("msgs_in", data->msgs_in);
write_uintkey("msgs_out", data->msgs_out);
write_uintkey("octets_in", data->octets_in);
write_uintkey("octets_out", data->octets_out);
}
if(fq_write_uint16(client->fd, 0) < 0) return -1;
return 0;
}
static int
fqd_ccs_loop(remote_client *client) {
int poll_timeout = 10;
while(1) {
int rv;
struct pollfd pfd;
uint16_t cmd;
hrtime_t t;
pfd.fd = client->fd;
pfd.events = POLLIN|POLLHUP;
pfd.revents = 0;
rv = poll(&pfd, 1, poll_timeout);
if(rv < 0) {
#ifdef DEBUG
fq_debug(FQ_DEBUG_CONN, "poll() failed on %s: %s\n", client->pretty,
strerror(errno));
#endif
break;
}
if(rv > 0) poll_timeout = 10;
else poll_timeout *= 2;
if(poll_timeout > 4000) poll_timeout = 4000;
/* we must wake up often enough to emit our heartbeat */
if(client->heartbeat_ms && poll_timeout > client->heartbeat_ms) poll_timeout = client->heartbeat_ms;
t = fq_gethrtime();
unsigned long long hb_ns = ((unsigned long long)client->heartbeat_ms) * 1000000ULL;
long long hb_age_ns = t - client->last_heartbeat;
if(client->heartbeat_ms && hb_age_ns > hb_ns) {
if(fqd_ccs_heartbeat(client)) break;
client->last_heartbeat = t;
}
if(hb_ns && client->last_activity < (t - hb_ns * 3)) {
ERRTOFD(client->fd, "client heartbeat failed");
#ifdef DEBUG
fq_debug(FQ_DEBUG_CONN, "heartbeat [%dms] failed from %s [%lld]\n", client->heartbeat_ms,
client->pretty, hb_age_ns);
#endif
break;
}
if(rv > 0) {
if(fq_read_uint16(client->fd, &cmd) != 0) break;
client->last_activity = fq_gethrtime();
switch(cmd) {
case FQ_PROTO_HB:
#ifdef DEBUG
fq_debug(FQ_DEBUG_CONN, "heartbeat <- %s\n", client->pretty);
#endif
break;
case FQ_PROTO_HBREQ:
{
uint16_t ms;
if(fq_read_uint16(client->fd, &ms) < 0) return -1;
#ifdef DEBUG
fq_debug(FQ_DEBUG_CONN, "setting client(%p) heartbeat to %d\n",
(void *)client, ms);
#endif
client->heartbeat_ms = ms;
break;
}
case FQ_PROTO_STATUSREQ:
if(fqd_css_status(client)) return -1;
break;
case FQ_PROTO_BINDREQ:
{
int len;
uint16_t flags;
uint32_t route_id;
uint64_t cgen;
char program[0xffff];
fq_rk exchange;
if(fq_read_uint16(client->fd, &flags)) return -1;
len = fq_read_short_cmd(client->fd, sizeof(exchange.name),
exchange.name);
if(len < 0 || len > (int)sizeof(exchange.name)) return -3;
exchange.len = len & 0xff;
len = fq_read_short_cmd(client->fd, sizeof(program)-1, program);
if(len < 0 || len > (int)sizeof(program)-1) return -1;
program[len] = '\0';
route_id = fqd_config_bind(&exchange, flags, program,
client->queue, &cgen);
if(route_id != FQ_BIND_ILLEGAL)
fqd_config_wait(cgen, 100);
if(fq_write_uint16(client->fd, FQ_PROTO_BIND) != 0) return -1;
if(fq_write_uint32(client->fd, route_id) != 0) return -1;
break;
}
case FQ_PROTO_UNBINDREQ:
{
uint32_t route_id;
fq_rk exchange;
int success, len;
if(fq_read_uint32(client->fd, &route_id)) return -1;
len = fq_read_short_cmd(client->fd, sizeof(exchange.name),
exchange.name);
if(len < 0 || len > (int)sizeof(exchange.name)) return -1;
exchange.len = len & 0xff;
success = fqd_config_unbind(&exchange, route_id, client->queue, NULL);
if(fq_write_uint16(client->fd, FQ_PROTO_UNBIND) != 0) return -1;
if(fq_write_uint32(client->fd, success ? route_id : FQ_BIND_ILLEGAL))
return -1;
break;
}
default:
return -1;
}
}
}
return -1;
}
extern void
fqd_command_and_control_server(remote_client *client) {
/* auth */
int rv, registered = 0;
uint64_t cgen;
fq_debug(FQ_DEBUG_CONN, "--> ccs thread\n");
if((rv = fqd_ccs_auth(client)) != 0) {
fq_debug(FQ_DEBUG_CONN, "client auth failed: %d\n", rv);
(void)rv;
goto out;
}
if(fqd_config_register_client(client, &cgen)) {
fq_debug(FQ_DEBUG_CONN, "client registration failed\n");
goto out;
}
fq_thread_setname("fqd:ccs:%s", client->pretty);
registered = 1;
fqd_config_wait(cgen, 100);
if(fqd_ccs_key_client(client) != 0) {
fq_debug(FQ_DEBUG_CONN, "client keying failed: %d\n", rv);
goto out;
}
fqd_ccs_heartbeat(client);
fqd_ccs_loop(client);
out:
if(registered) fqd_config_deregister_client(client, NULL);
fq_debug(FQ_DEBUG_CONN, "<-- ccs thread\n");
}