Debian Codegraph @

src/reader.c

raw
1 /*
2 * Author Jerry Lundström <[email protected]>
3 * Copyright (c) 2019-2026 OARC, Inc.
4 * All rights reserved.
5 *
6 * This file is part of the dnswire library.
7 *
8 * dnswire library is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * dnswire library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with dnswire library. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "config.h"
23
24 #include "dnswire/reader.h"
25 #include "dnswire/trace.h"
26
27 #include <assert.h>
28 #include <stdlib.h>
29
30 const char* const dnswire_reader_state_string[] = {
31 "reading_control",
32 "decoding_control",
33 "encoding_accept",
34 "writing_accept",
35 "reading",
36 "decoding",
37 "encoding_finish",
38 "writing_finish",
39 "done",
40 };
41
42 #define __state(h, s) \
43 __trace("state %s => %s", dnswire_reader_state_string[(h)->state], dnswire_reader_state_string[s]); \
44 (h)->state = s;
45
46 static struct dnswire_reader _defaults = {
47 .state = dnswire_reader_reading_control,
48
49 .decoder = DNSWIRE_DECODER_INITIALIZER,
50 .buf = 0,
51 .size = DNSWIRE_DEFAULT_BUF_SIZE,
52 .inc = DNSWIRE_DEFAULT_BUF_SIZE,
53 .max = DNSWIRE_MAXIMUM_BUF_SIZE,
54 .at = 0,
55 .left = 0,
56 .pushed = 0,
57
58 .encoder = DNSWIRE_ENCODER_INITIALIZER,
59 .write_buf = 0,
60 .write_size = DNSWIRE_DEFAULT_BUF_SIZE,
61 .write_inc = DNSWIRE_DEFAULT_BUF_SIZE,
62 .write_max = DNSWIRE_MAXIMUM_BUF_SIZE,
63 .write_at = 0,
64 .write_left = 0,
65
66 .allow_bidirectional = false,
67 .is_bidirectional = false,
68 };
69
70 enum dnswire_result dnswire_reader_init(struct dnswire_reader* handle)
71 {
72 assert(handle);
73
74 *handle = _defaults;
75
76 if (!(handle->buf = malloc(handle->size))) {
77 return dnswire_error;
78 }
79
80 return dnswire_ok;
81 }
82
83 enum dnswire_result dnswire_reader_allow_bidirectional(struct dnswire_reader* handle, bool allow_bidirectional)
84 {
85 assert(handle);
86
87 if (allow_bidirectional) {
88 if (!handle->write_buf) {
89 if (!(handle->write_buf = malloc(handle->write_size))) {
90 return dnswire_error;
91 }
92 }
93
94 handle->encoder.state = dnswire_encoder_control_accept;
95 } else {
96 handle->encoder.state = dnswire_encoder_control_start;
97 }
98
99 handle->allow_bidirectional = allow_bidirectional;
100
101 return dnswire_ok;
102 }
103
104 enum dnswire_result dnswire_reader_set_bufsize(struct dnswire_reader* handle, size_t size)
105 {
106 assert(handle);
107 assert(size);
108 assert(handle->buf);
109
110 if (handle->left > size) {
111 // we got data and it doesn't fit in the new size
112 return dnswire_error;
113 }
114 if (size > handle->max) {
115 // don't expand over max
116 return dnswire_error;
117 }
118
119 if (handle->at + handle->left > size) {
120 // move what's left to the start
121 if (handle->left) {
122 memmove(handle->buf, &handle->buf[handle->at], handle->left);
123 }
124 handle->at = 0;
125 }
126
127 uint8_t* buf = realloc(handle->buf, size);
128 if (!buf) {
129 return dnswire_error;
130 }
131
132 handle->buf = buf;
133 handle->size = size;
134
135 return dnswire_ok;
136 }
137
138 enum dnswire_result dnswire_reader_set_bufinc(struct dnswire_reader* handle, size_t inc)
139 {
140 assert(handle);
141 assert(inc);
142
143 handle->inc = inc;
144
145 return dnswire_ok;
146 }
147
148 enum dnswire_result dnswire_reader_set_bufmax(struct dnswire_reader* handle, size_t max)
149 {
150 assert(handle);
151 assert(max);
152
153 if (max < handle->size) {
154 return dnswire_error;
155 }
156
157 handle->max = max;
158
159 return dnswire_ok;
160 }
161
162 static enum dnswire_result _encoding(struct dnswire_reader* handle)
163 {
164 enum dnswire_result res;
165
166 while (1) {
167 res = dnswire_encoder_encode(&handle->encoder, &handle->write_buf[handle->write_at], handle->write_size - handle->write_at);
168 __trace("encode %s", dnswire_result_string[res]);
169
170 switch (res) {
171 case dnswire_ok:
172 case dnswire_again:
173 case dnswire_endofdata:
174 handle->write_at += dnswire_encoder_encoded(handle->encoder);
175 handle->write_left += dnswire_encoder_encoded(handle->encoder);
176 break;
177
178 case dnswire_need_more: {
179 if (handle->write_size >= handle->write_max) {
180 // already at max size and it's not enough
181 return dnswire_error;
182 }
183
184 // no space left, expand
185 size_t size = handle->write_size + handle->write_inc > handle->write_max ? handle->write_max : handle->write_size + handle->write_inc;
186 uint8_t* buf = realloc(handle->write_buf, size);
187 if (!buf) {
188 return dnswire_error;
189 }
190 handle->write_buf = buf;
191 handle->write_size = size;
192 continue;
193 }
194 default:
195 break;
196 }
197 break;
198 }
199 return res;
200 }
201
202 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)
203 {
204 assert(handle);
205 assert(data);
206 assert(handle->buf);
207 assert(!handle->allow_bidirectional || out_data);
208 assert(!handle->allow_bidirectional || out_len);
209
210 handle->pushed = 0;
211 size_t out_len_orig = 0;
212 if (out_len) {
213 out_len_orig = *out_len;
214 *out_len = 0;
215 }
216
217 switch (handle->state) {
218 case dnswire_reader_reading_control:
219 if (!len) {
220 return dnswire_need_more;
221 }
222 // if the space we have left is less then len we only move that amount
223 handle->pushed = handle->size - handle->at - handle->left < len ? handle->size - handle->at - handle->left : len;
224 // if we can't add more we fallthrough and let it decode some or expand the buffer
225 if (handle->pushed) {
226 memcpy(&handle->buf[handle->at + handle->left], data, handle->pushed);
227 __trace("%s", __printable_string(&handle->buf[handle->at + handle->left], handle->pushed));
228 handle->left += handle->pushed;
229 }
230 __state(handle, dnswire_reader_decoding_control);
231 // fallthrough
232
233 case dnswire_reader_decoding_control:
234 switch (dnswire_decoder_decode(&handle->decoder, &handle->buf[handle->at], handle->left)) {
235 case dnswire_bidirectional:
236 handle->at += dnswire_decoder_decoded(handle->decoder);
237 handle->left -= dnswire_decoder_decoded(handle->decoder);
238 if (!handle->left) {
239 handle->at = 0;
240 __state(handle, dnswire_reader_reading_control);
241 }
242
243 if (!handle->allow_bidirectional) {
244 return dnswire_error;
245 }
246
247 handle->is_bidirectional = true;
248
249 if (!handle->decoder.ready_support_dnstap_protobuf) {
250 return dnswire_error;
251 }
252
253 __state(handle, dnswire_reader_encoding_accept);
254 return dnswire_again;
255
256 case dnswire_again:
257 handle->at += dnswire_decoder_decoded(handle->decoder);
258 handle->left -= dnswire_decoder_decoded(handle->decoder);
259 if (!handle->left) {
260 handle->at = 0;
261 __state(handle, dnswire_reader_reading_control);
262 }
263 return dnswire_again;
264
265 case dnswire_need_more:
266 if (handle->left < handle->size) {
267 // still space left to fill
268 if (handle->at) {
269 // move what's left to the start
270 if (handle->left) {
271 memmove(handle->buf, &handle->buf[handle->at], handle->left);
272 }
273 handle->at = 0;
274 }
275 } else if (handle->size < handle->max) {
276 // no space left, expand
277 size_t size = handle->size + handle->inc > handle->max ? handle->max : handle->size + handle->inc;
278 uint8_t* buf = realloc(handle->buf, size);
279 if (!buf) {
280 return dnswire_error;
281 }
282 handle->buf = buf;
283 handle->size = size;
284 } else {
285 // already at max size, and full
286 return dnswire_error;
287 }
288 __state(handle, dnswire_reader_reading_control);
289 if (len && handle->pushed < len) {
290 // we had more to push but won't loop, call again please
291 return dnswire_again;
292 }
293 return dnswire_need_more;
294
295 case dnswire_have_dnstap:
296 handle->at += dnswire_decoder_decoded(handle->decoder);
297 handle->left -= dnswire_decoder_decoded(handle->decoder);
298 if (handle->left) {
299 __state(handle, dnswire_reader_decoding);
300 } else {
301 handle->at = 0;
302 __state(handle, dnswire_reader_reading);
303 }
304 return dnswire_have_dnstap;
305
306 case dnswire_endofdata:
307 if (handle->is_bidirectional) {
308 __state(handle, dnswire_reader_encoding_finish);
309 return dnswire_again;
310 }
311 __state(handle, dnswire_reader_done);
312 return dnswire_endofdata;
313
314 default:
315 break;
316 }
317 return dnswire_error;
318
319 case dnswire_reader_encoding_accept:
320 switch (_encoding(handle)) {
321 case dnswire_again:
322 __state(handle, dnswire_reader_writing_accept);
323 break;
324 // fallthrough
325
326 default:
327 return dnswire_error;
328 }
329
330 case dnswire_reader_writing_accept:
331 // if what we have left to write is less then the space available, we write it all
332 assert(out_len);
333 *out_len = handle->write_left < out_len_orig ? handle->write_left : out_len_orig;
334 memcpy(out_data, &handle->write_buf[handle->write_at - handle->write_left], *out_len);
335 __trace("pushed %zu", *out_len);
336 handle->write_left -= *out_len;
337 __trace("left %zu", handle->write_left);
338 if (!handle->write_left) {
339 handle->write_at = 0;
340 __state(handle, dnswire_reader_reading_control);
341 }
342 return dnswire_again;
343
344 case dnswire_reader_reading:
345 if (!len) {
346 return dnswire_need_more;
347 }
348 // if the space we have left is less then len we only move that amount
349 handle->pushed = handle->size - handle->at - handle->left < len ? handle->size - handle->at - handle->left : len;
350 // if we can't add more we fallthrough and let it decode some or expand the buffer
351 if (handle->pushed) {
352 memcpy(&handle->buf[handle->at + handle->left], data, handle->pushed);
353 __trace("%s", __printable_string(&handle->buf[handle->at + handle->left], handle->pushed));
354 handle->left += handle->pushed;
355 }
356 __state(handle, dnswire_reader_decoding);
357 // fallthrough
358
359 case dnswire_reader_decoding:
360 switch (dnswire_decoder_decode(&handle->decoder, &handle->buf[handle->at], handle->left)) {
361 case dnswire_again:
362 handle->at += dnswire_decoder_decoded(handle->decoder);
363 handle->left -= dnswire_decoder_decoded(handle->decoder);
364 if (!handle->left) {
365 handle->at = 0;
366 __state(handle, dnswire_reader_reading);
367 }
368 return dnswire_again;
369
370 case dnswire_need_more:
371 if (handle->left < handle->size) {
372 // still space left to fill
373 if (handle->at) {
374 // move what's left to the start
375 if (handle->left) {
376 memmove(handle->buf, &handle->buf[handle->at], handle->left);
377 }
378 handle->at = 0;
379 }
380 } else if (handle->size < handle->max) {
381 // no space left, expand
382 size_t size = handle->size + handle->inc > handle->max ? handle->max : handle->size + handle->inc;
383 uint8_t* buf = realloc(handle->buf, size);
384 if (!buf) {
385 return dnswire_error;
386 }
387 handle->buf = buf;
388 handle->size = size;
389 } else {
390 // already at max size, and full
391 return dnswire_error;
392 }
393 __state(handle, dnswire_reader_reading);
394 if (len && handle->pushed < len) {
395 // we had more to push but won't loop, call again please
396 return dnswire_again;
397 }
398 return dnswire_need_more;
399
400 case dnswire_have_dnstap:
401 handle->at += dnswire_decoder_decoded(handle->decoder);
402 handle->left -= dnswire_decoder_decoded(handle->decoder);
403 if (!handle->left) {
404 handle->at = 0;
405 __state(handle, dnswire_reader_reading);
406 }
407 return dnswire_have_dnstap;
408
409 case dnswire_endofdata:
410 if (handle->is_bidirectional) {
411 __state(handle, dnswire_reader_encoding_finish);
412 return dnswire_again;
413 }
414 __state(handle, dnswire_reader_done);
415 return dnswire_endofdata;
416
417 default:
418 break;
419 }
420 return dnswire_error;
421
422 case dnswire_reader_encoding_finish:
423 switch (_encoding(handle)) {
424 case dnswire_endofdata:
425 __state(handle, dnswire_reader_writing_finish);
426 break;
427 // fallthrough
428
429 default:
430 return dnswire_error;
431 }
432
433 case dnswire_reader_writing_finish:
434 // if what we have left to write is less then the space available, we write it all
435 assert(out_len);
436 *out_len = handle->write_left < out_len_orig ? handle->write_left : out_len_orig;
437 memcpy(out_data, &handle->write_buf[handle->write_at - handle->write_left], *out_len);
438 __trace("pushed %zu", *out_len);
439 handle->write_left -= *out_len;
440 __trace("left %zu", handle->write_left);
441 if (!handle->write_left) {
442 handle->write_at = 0;
443 __state(handle, dnswire_reader_done);
444 return dnswire_endofdata;
445 }
446 return dnswire_again;
447
448 case dnswire_reader_done:
449 break;
450 }
451
452 return dnswire_error;
453 }
454
455 enum dnswire_result dnswire_reader_read(struct dnswire_reader* handle, int fd)
456 {
457 assert(handle);
458 assert(handle->buf);
459
460 switch (handle->state) {
461 case dnswire_reader_reading_control: {
462 ssize_t nread = read(fd, &handle->buf[handle->at + handle->left], handle->size - handle->at - handle->left);
463 if (nread < 0) {
464 // TODO
465 return dnswire_error;
466 } else if (!nread) {
467 // TODO
468 return dnswire_error;
469 }
470 __trace("%s", __printable_string(&handle->buf[handle->at + handle->left], nread));
471 handle->left += nread;
472 __state(handle, dnswire_reader_decoding_control);
473 // fallthrough
474 }
475
476 case dnswire_reader_decoding_control:
477 switch (dnswire_decoder_decode(&handle->decoder, &handle->buf[handle->at], handle->left)) {
478 case dnswire_bidirectional:
479 handle->at += dnswire_decoder_decoded(handle->decoder);
480 handle->left -= dnswire_decoder_decoded(handle->decoder);
481 if (!handle->left) {
482 handle->at = 0;
483 __state(handle, dnswire_reader_reading_control);
484 }
485
486 if (!handle->allow_bidirectional) {
487 return dnswire_error;
488 }
489
490 handle->is_bidirectional = true;
491
492 if (!handle->decoder.ready_support_dnstap_protobuf) {
493 return dnswire_error;
494 }
495
496 __state(handle, dnswire_reader_encoding_accept);
497 return dnswire_again;
498
499 case dnswire_again:
500 handle->at += dnswire_decoder_decoded(handle->decoder);
501 handle->left -= dnswire_decoder_decoded(handle->decoder);
502 if (!handle->left) {
503 handle->at = 0;
504 __state(handle, dnswire_reader_reading_control);
505 }
506 return dnswire_again;
507
508 case dnswire_need_more:
509 if (handle->left < handle->size) {
510 // still space left to fill
511 if (handle->at) {
512 // move what's left to the start
513 if (handle->left) {
514 memmove(handle->buf, &handle->buf[handle->at], handle->left);
515 }
516 handle->at = 0;
517 }
518 } else if (handle->size < handle->max) {
519 // no space left, expand
520 size_t size = handle->size + handle->inc > handle->max ? handle->max : handle->size + handle->inc;
521 uint8_t* buf = realloc(handle->buf, size);
522 if (!buf) {
523 return dnswire_error;
524 }
525 handle->buf = buf;
526 handle->size = size;
527 } else {
528 // already at max size, and full
529 return dnswire_error;
530 }
531 __state(handle, dnswire_reader_reading_control);
532 return dnswire_need_more;
533
534 case dnswire_have_dnstap:
535 handle->at += dnswire_decoder_decoded(handle->decoder);
536 handle->left -= dnswire_decoder_decoded(handle->decoder);
537 if (handle->left) {
538 __state(handle, dnswire_reader_decoding);
539 } else {
540 handle->at = 0;
541 __state(handle, dnswire_reader_reading);
542 }
543 return dnswire_have_dnstap;
544
545 case dnswire_endofdata:
546 if (handle->is_bidirectional) {
547 __state(handle, dnswire_reader_encoding_finish);
548 return dnswire_again;
549 }
550 __state(handle, dnswire_reader_done);
551 return dnswire_endofdata;
552
553 default:
554 break;
555 }
556 return dnswire_error;
557
558 case dnswire_reader_encoding_accept:
559 switch (_encoding(handle)) {
560 case dnswire_again:
561 __state(handle, dnswire_reader_writing_accept);
562 break;
563 // fallthrough
564
565 default:
566 return dnswire_error;
567 }
568
569 case dnswire_reader_writing_accept: {
570 ssize_t nwrote = write(fd, &handle->write_buf[handle->write_at - handle->write_left], handle->write_left);
571 __trace("wrote %zd", nwrote);
572 if (nwrote < 0) {
573 // TODO
574 return dnswire_error;
575 } else if (!nwrote) {
576 // TODO
577 return dnswire_error;
578 }
579
580 handle->write_left -= nwrote;
581 __trace("left %zu", handle->write_left);
582 if (!handle->write_left) {
583 handle->write_at = 0;
584 __state(handle, dnswire_reader_reading_control);
585 }
586 return dnswire_again;
587 }
588
589 case dnswire_reader_reading: {
590 ssize_t nread = read(fd, &handle->buf[handle->at + handle->left], handle->size - handle->at - handle->left);
591 if (nread < 0) {
592 // TODO
593 return dnswire_error;
594 } else if (!nread) {
595 // TODO
596 return dnswire_error;
597 }
598 __trace("%s", __printable_string(&handle->buf[handle->at + handle->left], nread));
599 handle->left += nread;
600 __state(handle, dnswire_reader_decoding);
601 // fallthrough
602 }
603
604 case dnswire_reader_decoding:
605 switch (dnswire_decoder_decode(&handle->decoder, &handle->buf[handle->at], handle->left)) {
606 case dnswire_again:
607 handle->at += dnswire_decoder_decoded(handle->decoder);
608 handle->left -= dnswire_decoder_decoded(handle->decoder);
609 if (!handle->left) {
610 handle->at = 0;
611 __state(handle, dnswire_reader_reading);
612 }
613 return dnswire_again;
614
615 case dnswire_need_more:
616 if (handle->left < handle->size) {
617 // still space left to fill
618 if (handle->at) {
619 // move what's left to the start
620 if (handle->left) {
621 memmove(handle->buf, &handle->buf[handle->at], handle->left);
622 }
623 handle->at = 0;
624 }
625 } else if (handle->size < handle->max) {
626 // no space left, expand
627 size_t size = handle->size + handle->inc > handle->max ? handle->max : handle->size + handle->inc;
628 uint8_t* buf = realloc(handle->buf, size);
629 if (!buf) {
630 return dnswire_error;
631 }
632 handle->buf = buf;
633 handle->size = size;
634 } else {
635 // already at max size, and full
636 return dnswire_error;
637 }
638 __state(handle, dnswire_reader_reading);
639 return dnswire_need_more;
640
641 case dnswire_have_dnstap:
642 handle->at += dnswire_decoder_decoded(handle->decoder);
643 handle->left -= dnswire_decoder_decoded(handle->decoder);
644 if (!handle->left) {
645 handle->at = 0;
646 __state(handle, dnswire_reader_reading);
647 }
648 return dnswire_have_dnstap;
649
650 case dnswire_endofdata:
651 if (handle->is_bidirectional) {
652 __state(handle, dnswire_reader_encoding_finish);
653 return dnswire_again;
654 }
655 __state(handle, dnswire_reader_done);
656 return dnswire_endofdata;
657
658 default:
659 break;
660 }
661 return dnswire_error;
662
663 case dnswire_reader_encoding_finish:
664 switch (_encoding(handle)) {
665 case dnswire_endofdata:
666 __state(handle, dnswire_reader_writing_finish);
667 break;
668 // fallthrough
669
670 default:
671 return dnswire_error;
672 }
673
674 case dnswire_reader_writing_finish: {
675 ssize_t nwrote = write(fd, &handle->write_buf[handle->write_at - handle->write_left], handle->write_left);
676 __trace("wrote %zd", nwrote);
677 if (nwrote < 0) {
678 // TODO
679 return dnswire_error;
680 } else if (!nwrote) {
681 // TODO
682 return dnswire_error;
683 }
684
685 handle->write_left -= nwrote;
686 __trace("left %zu", handle->write_left);
687 if (!handle->write_left) {
688 handle->write_at = 0;
689 __state(handle, dnswire_reader_done);
690 return dnswire_endofdata;
691 }
692 return dnswire_again;
693 }
694
695 case dnswire_reader_done:
696 break;
697 }
698
699 return dnswire_error;
700 }