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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
|
/* Copyright (C) 2024 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <assert.h>
#include <poll.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "libknot/control/control.h"
#include "libknot/attribute.h"
#include "libknot/error.h"
#include "contrib/mempattern.h"
#include "contrib/net.h"
#include "contrib/sockaddr.h"
#include "contrib/string.h"
#include "contrib/ucw/mempool.h"
#include "contrib/wire_ctx.h"
/*! Size of the input and output buffers. */
#ifndef CTL_BUFF_SIZE
#define CTL_BUFF_SIZE (256 * 1024)
#endif
/*! Listen backlog size. */
#define DEFAULT_LISTEN_BACKLOG 5
/*! Default socket operations timeout in milliseconds. */
#define DEFAULT_TIMEOUT (30 * 1000)
/*! Accept poll timeout in milliseconds. */
#define ACCEPT_TIMEOUT (5 * 1000)
/*! The first data item code. */
#define DATA_CODE_OFFSET 16
/*! Control context structure. */
struct knot_ctl {
/*! Memory pool context. */
knot_mm_t mm;
/*! Network operations timeout. */
int timeout;
/*! Server listening socket. */
int listen_sock;
/*! Remote server/client socket. */
int sock;
/*! The latter read data. */
knot_ctl_data_t data;
/*! Write wire context. */
wire_ctx_t wire_out;
/*! Read wire context. */
wire_ctx_t wire_in;
/*! Write buffer. */
uint8_t buff_out[CTL_BUFF_SIZE];
/*! Read buffer. */
uint8_t buff_in[CTL_BUFF_SIZE];
};
static int type_to_code(knot_ctl_type_t type)
{
switch (type) {
case KNOT_CTL_TYPE_END: return 0;
case KNOT_CTL_TYPE_DATA: return 1;
case KNOT_CTL_TYPE_EXTRA: return 2;
case KNOT_CTL_TYPE_BLOCK: return 3;
default: return -1;
}
}
static int code_to_type(uint8_t code)
{
switch (code) {
case 0: return KNOT_CTL_TYPE_END;
case 1: return KNOT_CTL_TYPE_DATA;
case 2: return KNOT_CTL_TYPE_EXTRA;
case 3: return KNOT_CTL_TYPE_BLOCK;
default: return -1;
}
}
static bool is_data_type(knot_ctl_type_t type)
{
switch (type) {
case KNOT_CTL_TYPE_DATA:
case KNOT_CTL_TYPE_EXTRA:
return true;
default:
return false;
}
}
static int idx_to_code(knot_ctl_idx_t idx)
{
if (idx >= KNOT_CTL_IDX__COUNT) {
return -1;
}
return DATA_CODE_OFFSET + idx;
}
static int code_to_idx(uint8_t code)
{
if (code < DATA_CODE_OFFSET ||
code >= DATA_CODE_OFFSET + KNOT_CTL_IDX__COUNT) {
return -1;
}
return code - DATA_CODE_OFFSET;
}
static void reset_buffers(knot_ctl_t *ctx)
{
ctx->wire_out = wire_ctx_init(ctx->buff_out, CTL_BUFF_SIZE);
ctx->wire_in = wire_ctx_init(ctx->buff_in, 0);
}
static void clean_data(knot_ctl_t *ctx)
{
mp_flush(ctx->mm.ctx);
memzero(ctx->data, sizeof(ctx->data));
}
static void close_sock(int *sock)
{
if (*sock < 0) {
return;
}
close(*sock);
*sock = -1;
}
_public_
knot_ctl_t* knot_ctl_alloc(void)
{
knot_ctl_t *ctx = calloc(1, sizeof(*ctx));
if (ctx == NULL) {
return NULL;
}
mm_ctx_mempool(&ctx->mm, MM_DEFAULT_BLKSIZE);
ctx->timeout = DEFAULT_TIMEOUT;
ctx->listen_sock = -1;
ctx->sock = -1;
reset_buffers(ctx);
return ctx;
}
_public_
void knot_ctl_free(knot_ctl_t *ctx)
{
if (ctx == NULL) {
return;
}
close_sock(&ctx->listen_sock);
close_sock(&ctx->sock);
clean_data(ctx);
mp_delete(ctx->mm.ctx);
memzero(ctx, sizeof(*ctx));
free(ctx);
}
_public_
void knot_ctl_set_timeout(knot_ctl_t *ctx, int timeout_ms)
{
if (ctx == NULL) {
return;
}
ctx->timeout = (timeout_ms > 0) ? timeout_ms : -1;
}
_public_
int knot_ctl_bind(knot_ctl_t *ctx, const char *path)
{
return knot_ctl_bind2(ctx, path, DEFAULT_LISTEN_BACKLOG);
}
_public_
int knot_ctl_bind2(knot_ctl_t *ctx, const char *path, unsigned backlog)
{
if (ctx == NULL || path == NULL) {
return KNOT_EINVAL;
}
// Prepare socket address.
struct sockaddr_storage addr;
int ret = sockaddr_set(&addr, AF_UNIX, path, 0);
if (ret != KNOT_EOK) {
return ret;
}
// Bind the socket.
mode_t mode = S_IWUSR | S_IWGRP;
ctx->listen_sock = net_bound_socket(SOCK_STREAM, &addr, 0, mode);
if (ctx->listen_sock < 0) {
return ctx->listen_sock;
}
// Start listening.
if (listen(ctx->listen_sock, backlog) != 0) {
close_sock(&ctx->listen_sock);
return knot_map_errno();
}
return KNOT_EOK;
}
_public_
void knot_ctl_unbind(knot_ctl_t *ctx)
{
if (ctx == NULL || ctx->listen_sock < 0) {
return;
}
// Remove the control socket file.
struct sockaddr_storage addr;
socklen_t addr_len = sizeof(addr);
if (getsockname(ctx->listen_sock, (struct sockaddr *)&addr, &addr_len) == 0) {
char addr_str[SOCKADDR_STRLEN] = { 0 };
if (sockaddr_tostr(addr_str, sizeof(addr_str), &addr) > 0) {
(void)unlink(addr_str);
}
}
// Close the listening socket.
close_sock(&ctx->listen_sock);
}
_public_
int knot_ctl_accept(knot_ctl_t *ctx)
{
if (ctx == NULL) {
return KNOT_EINVAL;
}
knot_ctl_close(ctx);
// Control interface.
struct pollfd pfd = { .fd = ctx->listen_sock, .events = POLLIN };
int ret = poll(&pfd, 1, ACCEPT_TIMEOUT);
if (ret <= 0) {
return (ret == 0) ? KNOT_ETIMEOUT : knot_map_errno();
}
int client = net_accept(ctx->listen_sock, NULL);
if (client < 0) {
return client;
}
ctx->sock = client;
reset_buffers(ctx);
return KNOT_EOK;
}
_public_
int knot_ctl_connect(knot_ctl_t *ctx, const char *path)
{
if (ctx == NULL || path == NULL) {
return KNOT_EINVAL;
}
// Prepare socket address.
struct sockaddr_storage addr;
int ret = sockaddr_set(&addr, AF_UNIX, path, 0);
if (ret != KNOT_EOK) {
return ret;
}
// Connect to socket.
ctx->sock = net_connected_socket(SOCK_STREAM, &addr, NULL, false);
if (ctx->sock < 0) {
return ctx->sock;
}
reset_buffers(ctx);
return KNOT_EOK;
}
_public_
void knot_ctl_close(knot_ctl_t *ctx)
{
if (ctx == NULL) {
return;
}
close_sock(&ctx->sock);
}
static int ensure_output(knot_ctl_t *ctx, uint16_t len)
{
wire_ctx_t *w = &ctx->wire_out;
// Check for enough available room in the output buffer.
size_t available = wire_ctx_available(w);
if (available >= len) {
return KNOT_EOK;
}
// Flush the buffer.
int ret = net_stream_send(ctx->sock, w->wire, wire_ctx_offset(w),
ctx->timeout);
if (ret < 0) {
return ret;
}
*w = wire_ctx_init(w->wire, CTL_BUFF_SIZE);
return KNOT_EOK;
}
static int send_item(knot_ctl_t *ctx, uint8_t code, const char *data, bool flush)
{
wire_ctx_t *w = &ctx->wire_out;
// Write the control block code.
int ret = ensure_output(ctx, sizeof(uint8_t));
if (ret != KNOT_EOK) {
return ret;
}
wire_ctx_write_u8(w, code);
if (w->error != KNOT_EOK) {
return w->error;
}
// Control block data is optional.
if (data != NULL) {
// Get the data length.
size_t data_len = strlen(data);
if (data_len > UINT16_MAX) {
return KNOT_ERANGE;
}
// Write the data length.
ret = ensure_output(ctx, sizeof(uint16_t));
if (ret != KNOT_EOK) {
return ret;
}
wire_ctx_write_u16(w, data_len);
if (w->error != KNOT_EOK) {
return w->error;
}
// Write the data.
ret = ensure_output(ctx, data_len);
if (ret != KNOT_EOK) {
return ret;
}
wire_ctx_write(w, (uint8_t *)data, data_len);
if (w->error != KNOT_EOK) {
return w->error;
}
}
// Send finalized buffer.
if (flush && wire_ctx_offset(w) > 0) {
ret = net_stream_send(ctx->sock, w->wire, wire_ctx_offset(w),
ctx->timeout);
if (ret < 0) {
return ret;
}
*w = wire_ctx_init(w->wire, CTL_BUFF_SIZE);
}
return KNOT_EOK;
}
_public_
int knot_ctl_send(knot_ctl_t *ctx, knot_ctl_type_t type, knot_ctl_data_t *data)
{
if (ctx == NULL) {
return KNOT_EINVAL;
}
// Get the type code.
int code = type_to_code(type);
if (code == -1) {
return KNOT_EINVAL;
}
// Send unit type.
int ret = send_item(ctx, code, NULL, !is_data_type(type));
if (ret != KNOT_EOK) {
return ret;
}
// Send unit data.
if (is_data_type(type) && data != NULL) {
// Send all non-empty data items.
for (knot_ctl_idx_t i = 0; i < KNOT_CTL_IDX__COUNT; i++) {
const char *value = (*data)[i];
if (value == NULL) {
continue;
}
ret = send_item(ctx, idx_to_code(i), value, false);
if (ret != KNOT_EOK) {
return ret;
}
}
}
return KNOT_EOK;
}
static int ensure_input(knot_ctl_t *ctx, uint16_t len)
{
wire_ctx_t *w = &ctx->wire_in;
// Check for enough available room in the input buffer.
size_t available = wire_ctx_available(w);
if (available >= len) {
return KNOT_EOK;
}
// Move unprocessed data to the beginning of the buffer.
memmove(w->wire, w->wire + wire_ctx_offset(w), available);
// Receive enough data.
while (available < len) {
int ret = net_stream_recv(ctx->sock, w->wire + available,
CTL_BUFF_SIZE - available,
ctx->timeout);
if (ret < 0) {
return ret;
}
assert(ret > 0);
available += ret;
}
ctx->wire_in = wire_ctx_init(w->wire, available);
return KNOT_EOK;
}
static int receive_item_code(knot_ctl_t *ctx, uint8_t *code)
{
wire_ctx_t *w = &ctx->wire_in;
// Read the type.
int ret = ensure_input(ctx, sizeof(uint8_t));
if (ret != KNOT_EOK) {
return ret;
}
*code = wire_ctx_read_u8(w);
if (w->error != KNOT_EOK) {
return w->error;
}
return KNOT_EOK;
}
static int receive_item_value(knot_ctl_t *ctx, char **value)
{
wire_ctx_t *w = &ctx->wire_in;
// Read value length.
int ret = ensure_input(ctx, sizeof(uint16_t));
if (ret != KNOT_EOK) {
return ret;
}
uint16_t data_len = wire_ctx_read_u16(w);
if (w->error != KNOT_EOK) {
return w->error;
}
// Read the value.
ret = ensure_input(ctx, data_len);
if (ret != KNOT_EOK) {
return ret;
}
*value = mm_alloc(&ctx->mm, data_len + 1);
if (*value == NULL) {
return KNOT_ENOMEM;
}
wire_ctx_read(w, *value, data_len);
if (w->error != KNOT_EOK) {
return w->error;
}
(*value)[data_len] = '\0';
return KNOT_EOK;
}
_public_
int knot_ctl_receive(knot_ctl_t *ctx, knot_ctl_type_t *type, knot_ctl_data_t *data)
{
if (ctx == NULL || type == NULL) {
return KNOT_EINVAL;
}
wire_ctx_t *w = &ctx->wire_in;
// Reset output variables.
*type = KNOT_CTL_TYPE_END;
clean_data(ctx);
// Read data units until end of message.
bool have_type = false;
while (true) {
uint8_t code;
int ret = receive_item_code(ctx, &code);
if (ret != KNOT_EOK) {
return ret;
}
// Process unit type.
int current_type = code_to_type(code);
if (current_type != -1) {
if (have_type) {
// Revert parsed type.
wire_ctx_skip(w, -sizeof(uint8_t));
assert(w->error == KNOT_EOK);
break;
}
// Set the unit type.
*type = current_type;
if (is_data_type(current_type)) {
have_type = true;
continue;
} else {
break;
}
}
// Check for data item code.
int idx = code_to_idx(code);
if (idx == -1) {
return KNOT_EINVAL;
}
// Store the item data value.
ret = receive_item_value(ctx, (char **)&ctx->data[idx]);
if (ret != KNOT_EOK) {
return ret;
}
}
// Set the output data.
if (data != NULL) {
memcpy(*data, ctx->data, sizeof(*data));
}
return KNOT_EOK;
}
|