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
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
|
/*
* Author Jerry Lundström <jerry@dns-oarc.net>
* Copyright (c) 2019-2023, OARC, Inc.
* All rights reserved.
*
* This file is part of the dnswire library.
*
* dnswire library is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* dnswire library 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with dnswire library. If not, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include "dnswire/reader.h"
#include "dnswire/trace.h"
#include <assert.h>
#include <stdlib.h>
const char* const dnswire_reader_state_string[] = {
"reading_control",
"decoding_control",
"encoding_accept",
"writing_accept",
"reading",
"decoding",
"encoding_finish",
"writing_finish",
"done",
};
#define __state(h, s) \
__trace("state %s => %s", dnswire_reader_state_string[(h)->state], dnswire_reader_state_string[s]); \
(h)->state = s;
static struct dnswire_reader _defaults = {
.state = dnswire_reader_reading_control,
.decoder = DNSWIRE_DECODER_INITIALIZER,
.buf = 0,
.size = DNSWIRE_DEFAULT_BUF_SIZE,
.inc = DNSWIRE_DEFAULT_BUF_SIZE,
.max = DNSWIRE_MAXIMUM_BUF_SIZE,
.at = 0,
.left = 0,
.pushed = 0,
.encoder = DNSWIRE_ENCODER_INITIALIZER,
.write_buf = 0,
.write_size = DNSWIRE_DEFAULT_BUF_SIZE,
.write_inc = DNSWIRE_DEFAULT_BUF_SIZE,
.write_max = DNSWIRE_MAXIMUM_BUF_SIZE,
.write_at = 0,
.write_left = 0,
.allow_bidirectional = false,
.is_bidirectional = false,
};
enum dnswire_result dnswire_reader_init(struct dnswire_reader* handle)
{
assert(handle);
*handle = _defaults;
if (!(handle->buf = malloc(handle->size))) {
return dnswire_error;
}
return dnswire_ok;
}
enum dnswire_result dnswire_reader_allow_bidirectional(struct dnswire_reader* handle, bool allow_bidirectional)
{
assert(handle);
if (allow_bidirectional) {
if (!handle->write_buf) {
if (!(handle->write_buf = malloc(handle->write_size))) {
return dnswire_error;
}
}
handle->encoder.state = dnswire_encoder_control_accept;
} else {
handle->encoder.state = dnswire_encoder_control_start;
}
handle->allow_bidirectional = allow_bidirectional;
return dnswire_ok;
}
enum dnswire_result dnswire_reader_set_bufsize(struct dnswire_reader* handle, size_t size)
{
assert(handle);
assert(size);
assert(handle->buf);
if (handle->left > size) {
// we got data and it doesn't fit in the new size
return dnswire_error;
}
if (size > handle->max) {
// don't expand over max
return dnswire_error;
}
if (handle->at + handle->left > size) {
// move what's left to the start
if (handle->left) {
memmove(handle->buf, &handle->buf[handle->at], handle->left);
}
handle->at = 0;
}
uint8_t* buf = realloc(handle->buf, size);
if (!buf) {
return dnswire_error;
}
handle->buf = buf;
handle->size = size;
return dnswire_ok;
}
enum dnswire_result dnswire_reader_set_bufinc(struct dnswire_reader* handle, size_t inc)
{
assert(handle);
assert(inc);
handle->inc = inc;
return dnswire_ok;
}
enum dnswire_result dnswire_reader_set_bufmax(struct dnswire_reader* handle, size_t max)
{
assert(handle);
assert(max);
if (max < handle->size) {
return dnswire_error;
}
handle->max = max;
return dnswire_ok;
}
static enum dnswire_result _encoding(struct dnswire_reader* handle)
{
enum dnswire_result res;
while (1) {
res = dnswire_encoder_encode(&handle->encoder, &handle->write_buf[handle->write_at], handle->write_size - handle->write_at);
__trace("encode %s", dnswire_result_string[res]);
switch (res) {
case dnswire_ok:
case dnswire_again:
case dnswire_endofdata:
handle->write_at += dnswire_encoder_encoded(handle->encoder);
handle->write_left += dnswire_encoder_encoded(handle->encoder);
break;
case dnswire_need_more: {
if (handle->write_size >= handle->write_max) {
// already at max size and it's not enough
return dnswire_error;
}
// no space left, expand
size_t size = handle->write_size + handle->write_inc > handle->write_max ? handle->write_max : handle->write_size + handle->write_inc;
uint8_t* buf = realloc(handle->write_buf, size);
if (!buf) {
return dnswire_error;
}
handle->write_buf = buf;
handle->write_size = size;
continue;
}
default:
break;
}
break;
}
return res;
}
enum dnswire_result dnswire_reader_push(struct dnswire_reader* handle, const uint8_t* data, size_t len, uint8_t* out_data, size_t* out_len)
{
assert(handle);
assert(data);
assert(handle->buf);
assert(!handle->allow_bidirectional || out_data);
assert(!handle->allow_bidirectional || out_len);
handle->pushed = 0;
size_t out_len_orig = 0;
if (out_len) {
out_len_orig = *out_len;
*out_len = 0;
}
switch (handle->state) {
case dnswire_reader_reading_control:
if (!len) {
return dnswire_need_more;
}
// if the space we have left is less then len we only move that amount
handle->pushed = handle->size - handle->at - handle->left < len ? handle->size - handle->at - handle->left : len;
// if we can't add more we fallthrough and let it decode some or expand the buffer
if (handle->pushed) {
memcpy(&handle->buf[handle->at + handle->left], data, handle->pushed);
__trace("%s", __printable_string(&handle->buf[handle->at + handle->left], handle->pushed));
handle->left += handle->pushed;
}
__state(handle, dnswire_reader_decoding_control);
// fallthrough
case dnswire_reader_decoding_control:
switch (dnswire_decoder_decode(&handle->decoder, &handle->buf[handle->at], handle->left)) {
case dnswire_bidirectional:
handle->at += dnswire_decoder_decoded(handle->decoder);
handle->left -= dnswire_decoder_decoded(handle->decoder);
if (!handle->left) {
handle->at = 0;
__state(handle, dnswire_reader_reading_control);
}
if (!handle->allow_bidirectional) {
return dnswire_error;
}
handle->is_bidirectional = true;
if (!handle->decoder.ready_support_dnstap_protobuf) {
return dnswire_error;
}
__state(handle, dnswire_reader_encoding_accept);
return dnswire_again;
case dnswire_again:
handle->at += dnswire_decoder_decoded(handle->decoder);
handle->left -= dnswire_decoder_decoded(handle->decoder);
if (!handle->left) {
handle->at = 0;
__state(handle, dnswire_reader_reading_control);
}
return dnswire_again;
case dnswire_need_more:
if (handle->left < handle->size) {
// still space left to fill
if (handle->at) {
// move what's left to the start
if (handle->left) {
memmove(handle->buf, &handle->buf[handle->at], handle->left);
}
handle->at = 0;
}
} else if (handle->size < handle->max) {
// no space left, expand
size_t size = handle->size + handle->inc > handle->max ? handle->max : handle->size + handle->inc;
uint8_t* buf = realloc(handle->buf, size);
if (!buf) {
return dnswire_error;
}
handle->buf = buf;
handle->size = size;
} else {
// already at max size, and full
return dnswire_error;
}
__state(handle, dnswire_reader_reading_control);
if (len && handle->pushed < len) {
// we had more to push but won't loop, call again please
return dnswire_again;
}
return dnswire_need_more;
case dnswire_have_dnstap:
handle->at += dnswire_decoder_decoded(handle->decoder);
handle->left -= dnswire_decoder_decoded(handle->decoder);
if (handle->left) {
__state(handle, dnswire_reader_decoding);
} else {
handle->at = 0;
__state(handle, dnswire_reader_reading);
}
return dnswire_have_dnstap;
case dnswire_endofdata:
if (handle->is_bidirectional) {
__state(handle, dnswire_reader_encoding_finish);
return dnswire_again;
}
__state(handle, dnswire_reader_done);
return dnswire_endofdata;
default:
break;
}
return dnswire_error;
case dnswire_reader_encoding_accept:
switch (_encoding(handle)) {
case dnswire_again:
__state(handle, dnswire_reader_writing_accept);
break;
// fallthrough
default:
return dnswire_error;
}
case dnswire_reader_writing_accept:
// if what we have left to write is less then the space available, we write it all
assert(out_len);
*out_len = handle->write_left < out_len_orig ? handle->write_left : out_len_orig;
memcpy(out_data, &handle->write_buf[handle->write_at - handle->write_left], *out_len);
__trace("pushed %zu", *out_len);
handle->write_left -= *out_len;
__trace("left %zu", handle->write_left);
if (!handle->write_left) {
handle->write_at = 0;
__state(handle, dnswire_reader_reading_control);
}
return dnswire_again;
case dnswire_reader_reading:
if (!len) {
return dnswire_need_more;
}
// if the space we have left is less then len we only move that amount
handle->pushed = handle->size - handle->at - handle->left < len ? handle->size - handle->at - handle->left : len;
// if we can't add more we fallthrough and let it decode some or expand the buffer
if (handle->pushed) {
memcpy(&handle->buf[handle->at + handle->left], data, handle->pushed);
__trace("%s", __printable_string(&handle->buf[handle->at + handle->left], handle->pushed));
handle->left += handle->pushed;
}
__state(handle, dnswire_reader_decoding);
// fallthrough
case dnswire_reader_decoding:
switch (dnswire_decoder_decode(&handle->decoder, &handle->buf[handle->at], handle->left)) {
case dnswire_again:
handle->at += dnswire_decoder_decoded(handle->decoder);
handle->left -= dnswire_decoder_decoded(handle->decoder);
if (!handle->left) {
handle->at = 0;
__state(handle, dnswire_reader_reading);
}
return dnswire_again;
case dnswire_need_more:
if (handle->left < handle->size) {
// still space left to fill
if (handle->at) {
// move what's left to the start
if (handle->left) {
memmove(handle->buf, &handle->buf[handle->at], handle->left);
}
handle->at = 0;
}
} else if (handle->size < handle->max) {
// no space left, expand
size_t size = handle->size + handle->inc > handle->max ? handle->max : handle->size + handle->inc;
uint8_t* buf = realloc(handle->buf, size);
if (!buf) {
return dnswire_error;
}
handle->buf = buf;
handle->size = size;
} else {
// already at max size, and full
return dnswire_error;
}
__state(handle, dnswire_reader_reading);
if (len && handle->pushed < len) {
// we had more to push but won't loop, call again please
return dnswire_again;
}
return dnswire_need_more;
case dnswire_have_dnstap:
handle->at += dnswire_decoder_decoded(handle->decoder);
handle->left -= dnswire_decoder_decoded(handle->decoder);
if (!handle->left) {
handle->at = 0;
__state(handle, dnswire_reader_reading);
}
return dnswire_have_dnstap;
case dnswire_endofdata:
if (handle->is_bidirectional) {
__state(handle, dnswire_reader_encoding_finish);
return dnswire_again;
}
__state(handle, dnswire_reader_done);
return dnswire_endofdata;
default:
break;
}
return dnswire_error;
case dnswire_reader_encoding_finish:
switch (_encoding(handle)) {
case dnswire_endofdata:
__state(handle, dnswire_reader_writing_finish);
break;
// fallthrough
default:
return dnswire_error;
}
case dnswire_reader_writing_finish:
// if what we have left to write is less then the space available, we write it all
assert(out_len);
*out_len = handle->write_left < out_len_orig ? handle->write_left : out_len_orig;
memcpy(out_data, &handle->write_buf[handle->write_at - handle->write_left], *out_len);
__trace("pushed %zu", *out_len);
handle->write_left -= *out_len;
__trace("left %zu", handle->write_left);
if (!handle->write_left) {
handle->write_at = 0;
__state(handle, dnswire_reader_done);
return dnswire_endofdata;
}
return dnswire_again;
case dnswire_reader_done:
break;
}
return dnswire_error;
}
enum dnswire_result dnswire_reader_read(struct dnswire_reader* handle, int fd)
{
assert(handle);
assert(handle->buf);
switch (handle->state) {
case dnswire_reader_reading_control: {
ssize_t nread = read(fd, &handle->buf[handle->at + handle->left], handle->size - handle->at - handle->left);
if (nread < 0) {
// TODO
return dnswire_error;
} else if (!nread) {
// TODO
return dnswire_error;
}
__trace("%s", __printable_string(&handle->buf[handle->at + handle->left], nread));
handle->left += nread;
__state(handle, dnswire_reader_decoding_control);
// fallthrough
}
case dnswire_reader_decoding_control:
switch (dnswire_decoder_decode(&handle->decoder, &handle->buf[handle->at], handle->left)) {
case dnswire_bidirectional:
handle->at += dnswire_decoder_decoded(handle->decoder);
handle->left -= dnswire_decoder_decoded(handle->decoder);
if (!handle->left) {
handle->at = 0;
__state(handle, dnswire_reader_reading_control);
}
if (!handle->allow_bidirectional) {
return dnswire_error;
}
handle->is_bidirectional = true;
if (!handle->decoder.ready_support_dnstap_protobuf) {
return dnswire_error;
}
__state(handle, dnswire_reader_encoding_accept);
return dnswire_again;
case dnswire_again:
handle->at += dnswire_decoder_decoded(handle->decoder);
handle->left -= dnswire_decoder_decoded(handle->decoder);
if (!handle->left) {
handle->at = 0;
__state(handle, dnswire_reader_reading_control);
}
return dnswire_again;
case dnswire_need_more:
if (handle->left < handle->size) {
// still space left to fill
if (handle->at) {
// move what's left to the start
if (handle->left) {
memmove(handle->buf, &handle->buf[handle->at], handle->left);
}
handle->at = 0;
}
} else if (handle->size < handle->max) {
// no space left, expand
size_t size = handle->size + handle->inc > handle->max ? handle->max : handle->size + handle->inc;
uint8_t* buf = realloc(handle->buf, size);
if (!buf) {
return dnswire_error;
}
handle->buf = buf;
handle->size = size;
} else {
// already at max size, and full
return dnswire_error;
}
__state(handle, dnswire_reader_reading_control);
return dnswire_need_more;
case dnswire_have_dnstap:
handle->at += dnswire_decoder_decoded(handle->decoder);
handle->left -= dnswire_decoder_decoded(handle->decoder);
if (handle->left) {
__state(handle, dnswire_reader_decoding);
} else {
handle->at = 0;
__state(handle, dnswire_reader_reading);
}
return dnswire_have_dnstap;
case dnswire_endofdata:
if (handle->is_bidirectional) {
__state(handle, dnswire_reader_encoding_finish);
return dnswire_again;
}
__state(handle, dnswire_reader_done);
return dnswire_endofdata;
default:
break;
}
return dnswire_error;
case dnswire_reader_encoding_accept:
switch (_encoding(handle)) {
case dnswire_again:
__state(handle, dnswire_reader_writing_accept);
break;
// fallthrough
default:
return dnswire_error;
}
case dnswire_reader_writing_accept: {
ssize_t nwrote = write(fd, &handle->write_buf[handle->write_at - handle->write_left], handle->write_left);
__trace("wrote %zd", nwrote);
if (nwrote < 0) {
// TODO
return dnswire_error;
} else if (!nwrote) {
// TODO
return dnswire_error;
}
handle->write_left -= nwrote;
__trace("left %zu", handle->write_left);
if (!handle->write_left) {
handle->write_at = 0;
__state(handle, dnswire_reader_reading_control);
}
return dnswire_again;
}
case dnswire_reader_reading: {
ssize_t nread = read(fd, &handle->buf[handle->at + handle->left], handle->size - handle->at - handle->left);
if (nread < 0) {
// TODO
return dnswire_error;
} else if (!nread) {
// TODO
return dnswire_error;
}
__trace("%s", __printable_string(&handle->buf[handle->at + handle->left], nread));
handle->left += nread;
__state(handle, dnswire_reader_decoding);
// fallthrough
}
case dnswire_reader_decoding:
switch (dnswire_decoder_decode(&handle->decoder, &handle->buf[handle->at], handle->left)) {
case dnswire_again:
handle->at += dnswire_decoder_decoded(handle->decoder);
handle->left -= dnswire_decoder_decoded(handle->decoder);
if (!handle->left) {
handle->at = 0;
__state(handle, dnswire_reader_reading);
}
return dnswire_again;
case dnswire_need_more:
if (handle->left < handle->size) {
// still space left to fill
if (handle->at) {
// move what's left to the start
if (handle->left) {
memmove(handle->buf, &handle->buf[handle->at], handle->left);
}
handle->at = 0;
}
} else if (handle->size < handle->max) {
// no space left, expand
size_t size = handle->size + handle->inc > handle->max ? handle->max : handle->size + handle->inc;
uint8_t* buf = realloc(handle->buf, size);
if (!buf) {
return dnswire_error;
}
handle->buf = buf;
handle->size = size;
} else {
// already at max size, and full
return dnswire_error;
}
__state(handle, dnswire_reader_reading);
return dnswire_need_more;
case dnswire_have_dnstap:
handle->at += dnswire_decoder_decoded(handle->decoder);
handle->left -= dnswire_decoder_decoded(handle->decoder);
if (!handle->left) {
handle->at = 0;
__state(handle, dnswire_reader_reading);
}
return dnswire_have_dnstap;
case dnswire_endofdata:
if (handle->is_bidirectional) {
__state(handle, dnswire_reader_encoding_finish);
return dnswire_again;
}
__state(handle, dnswire_reader_done);
return dnswire_endofdata;
default:
break;
}
return dnswire_error;
case dnswire_reader_encoding_finish:
switch (_encoding(handle)) {
case dnswire_endofdata:
__state(handle, dnswire_reader_writing_finish);
break;
// fallthrough
default:
return dnswire_error;
}
case dnswire_reader_writing_finish: {
ssize_t nwrote = write(fd, &handle->write_buf[handle->write_at - handle->write_left], handle->write_left);
__trace("wrote %zd", nwrote);
if (nwrote < 0) {
// TODO
return dnswire_error;
} else if (!nwrote) {
// TODO
return dnswire_error;
}
handle->write_left -= nwrote;
__trace("left %zu", handle->write_left);
if (!handle->write_left) {
handle->write_at = 0;
__state(handle, dnswire_reader_done);
return dnswire_endofdata;
}
return dnswire_again;
}
case dnswire_reader_done:
break;
}
return dnswire_error;
}
|