The FreeRADIUS server $Id: a4e7f8c94e8038b7627bcf52356b7ed15955c1d7 $
Loading...
Searching...
No Matches
mem.c
Go to the documentation of this file.
1/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or (at
5 * your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
15 */
16
17/**
18 * $Id: c03fa87796f1bbb54f13b9d9f8de65f9b6cd02fe $
19 * @file lib/bio/mem.c
20 * @brief BIO abstractions for memory buffers
21 *
22 * @copyright 2024 Network RADIUS SAS (legal@networkradius.com)
23 */
24
25#include <freeradius-devel/bio/bio_priv.h>
26#include <freeradius-devel/bio/null.h>
27#include <freeradius-devel/bio/buf.h>
28
29#include <freeradius-devel/bio/mem.h>
30
31typedef enum : uint8_t {
33 FR_BIO_MEM_SOURCE, /* the application writes to it, something else reads */
34 FR_BIO_MEM_SINK, /* something else writes to it, the application reads */
35 FR_BIO_MEM_VERIFY, /* verification only, with no buffers */
36 FR_BIO_MEM_BUFFER, /* we manage local buffers */
38
39/** The memory buffer bio
40 *
41 * It is used to buffer reads / writes to a streaming socket.
42 */
43typedef struct fr_bio_mem_s {
45
46 fr_bio_mem_type_t type; //!< source, sink, etc.
47
48 fr_bio_verify_t verify; //!< verify data to see if we have a packet.
49 void *verify_ctx; //!< verify context
50
51 fr_bio_buf_t read_buffer; //!< buffering for reads
52 fr_bio_buf_t write_buffer; //!< buffering for writes
54
55static ssize_t fr_bio_mem_write_buffer(fr_bio_t *bio, void *packet_ctx, void const *buffer, size_t size);
56
57static int fr_bio_mem_call_verify(fr_bio_t *bio, void *packet_ctx, size_t *size) CC_HINT(nonnull(1,3));
58
59/** At EOF, read data from the buffer until it is empty.
60 *
61 * When "next" bio returns EOF, there may still be pending data in the memory buffer. Return that until it's
62 * empty, and then EOF from then on.
63 */
64static ssize_t fr_bio_mem_read_eof(fr_bio_t *bio, UNUSED void *packet_ctx, void *buffer, size_t size)
65{
66 fr_bio_mem_t *my = talloc_get_type_abort(bio, fr_bio_mem_t);
67
68 /*
69 * No more data: return EOF from now on.
70 */
71 if (fr_bio_buf_used(&my->read_buffer) == 0) {
72
73 /*
74 * Don't call our EOF function. But do tell the other BIOs that we're at EOF.
75 */
76 my->priv_cb.eof = NULL;
77 fr_bio_eof(&my->bio);
78 return 0;
79 }
80
81 /*
82 * Return whatever data we have available. One the buffer is empty, the next read will get EOF.
83 */
84 return fr_bio_buf_read(&my->read_buffer, buffer, size);
85}
86
87static int fr_bio_mem_eof(fr_bio_t *bio)
88{
89 fr_bio_mem_t *my = talloc_get_type_abort(bio, fr_bio_mem_t);
90
91 /*
92 * Nothing more for us to read, tell fr_bio_eof() that it can continue with poking other BIOs.
93 */
94 if (fr_bio_buf_used(&my->read_buffer) == 0) {
95 return 1;
96 }
97
98 my->bio.read = fr_bio_mem_read_eof;
99
100 return 0;
101}
102
103/** Read from a memory BIO
104 *
105 * This bio reads as much data as possible into the memory buffer. On the theory that a few memcpy() or
106 * memmove() calls are much cheaper than a system call.
107 *
108 * If the read buffer has enough data to satisfy the read, then it is returned.
109 *
110 * Otherwise the next bio is called to re-fill the buffer. The next read call will try to get as much data
111 * as possible into the buffer, even if that results in reading more than "size" bytes.
112 *
113 * Once the next read has been done, then the data from the buffer is returned, even if it is less than
114 * "size".
115 */
116static ssize_t fr_bio_mem_read(fr_bio_t *bio, void *packet_ctx, void *buffer, size_t size)
117{
118 ssize_t rcode;
119 size_t used, room;
120 uint8_t *p;
121 fr_bio_mem_t *my = talloc_get_type_abort(bio, fr_bio_mem_t);
122 fr_bio_t *next;
123
124 /*
125 * We can satisfy the read from the memory buffer: do so.
126 */
128 if (size <= used) {
129 return fr_bio_buf_read(&my->read_buffer, buffer, size);
130 }
131
132 /*
133 * There must be a next bio.
134 */
135 next = fr_bio_next(&my->bio);
136 fr_assert(next != NULL);
137
138 /*
139 * If there's no room to store more data in the buffer. Just return whatever data we have in the
140 * buffer.
141 */
143 if (!room) return fr_bio_buf_read(&my->read_buffer, buffer, size);
144
145 /*
146 * We try to fill the buffer as much as possible from the network, even if that means reading
147 * more than "size" amount of data.
148 */
150 fr_assert(p != NULL); /* otherwise room would be zero */
151
152 rcode = next->read(next, packet_ctx, p, room);
153 if (rcode < 0) return rcode;
154
155 /*
156 * Ensure that whatever data we have read (zero or more) is marked as "used" in the buffer, and
157 * then return whatever data is available back to the caller.
158 */
159 (void) fr_bio_buf_write_alloc(&my->read_buffer, (size_t) rcode);
160
161 return fr_bio_buf_read(&my->read_buffer, buffer, size);
162}
163
164/** See is there's a full packet in the buffer.
165 *
166 */
167static ssize_t fr_bio_mem_read_packet(fr_bio_t *bio, void *packet_ctx, void *buffer, size_t size)
168{
169 ssize_t rcode;
170 size_t want;
171 fr_bio_mem_t *my = talloc_get_type_abort(bio, fr_bio_mem_t);
172
173 /*
174 * See if there are valid packets in the buffer.
175 */
176 rcode = fr_bio_mem_call_verify(bio, packet_ctx, &want);
177 if (rcode < 0) return fr_bio_error(VERIFY);
178
179 /*
180 * No valid packet.
181 */
182 if (rcode == 0) return 0;
183
184 /*
185 * There's at least one valid packet, return it.
186 *
187 * Not enough room isn't a fatal error. The caller should check how much room is needed by
188 * calling fr_bio_mem_call_verify(), and retry.
189 *
190 * But in general, the caller should make sure that the output buffer has enough room for at
191 * least one packet. The verify() function should also ensure that the packet is no larger than
192 * our application maximum, even if the protocol allows for it to be larger.
193 */
194 if (want > size) return fr_bio_error(BUFFER_TOO_SMALL);
195
196 return fr_bio_buf_read(&my->read_buffer, buffer, want);
197}
198
199/** Return data only if we have a complete packet.
200 *
201 */
202static ssize_t fr_bio_mem_read_verify_stream(fr_bio_t *bio, void *packet_ctx, void *buffer, size_t size)
203{
204 ssize_t rcode;
205 size_t used, room;
206 uint8_t *p;
207 fr_bio_mem_t *my = talloc_get_type_abort(bio, fr_bio_mem_t);
208 fr_bio_t *next;
209
210 /*
211 * We may be able to satisfy the read from the memory buffer.
212 */
214 if (used) {
215 rcode = fr_bio_mem_read_packet(bio, packet_ctx, buffer, size);
216
217 /*
218 * A packet, or an error. Zero means that the buffer holds only part of a packet, and
219 * we need to read more data before we can return anything.
220 */
221 if (rcode != 0) return rcode;
222 }
223
224 /*
225 * There must be a next bio.
226 */
227 next = fr_bio_next(&my->bio);
228 fr_assert(next != NULL);
229
230 /*
231 * If there's no room to store more data in the buffer, try to make some room.
232 */
234 if (!room) {
236
237 /*
238 * We've tried to make room and failed. Which means that the buffer is full, AND there
239 * still isn't a complete packet in the buffer. This is therefore an error. The
240 * application has not supplied us with enough read_buffer space to store a complete
241 * packet.
242 *
243 * @todo - allow the application to increase the size of the buffer.
244 */
245 if (!room) return fr_bio_error(BUFFER_FULL);
246 }
247
248 /*
249 * We try to fill the buffer as much as possible from the network. The theory is that a few
250 * extra memcpy() or memmove()s are cheaper than a system call for reading each packet.
251 */
253 fr_assert(p != NULL); /* otherwise room would be zero */
254
255 rcode = next->read(next, packet_ctx, p, room);
256 if (rcode <= 0) return rcode;
257
258 /*
259 * The next bio returned some data. Bump up what's used in the buffer, and see if it's a valid packet.
260 */
261 (void) fr_bio_buf_write_alloc(&my->read_buffer, (size_t) rcode);
262 return fr_bio_mem_read_packet(bio, packet_ctx, buffer, size);
263}
264
265/** Return data only if we have a complete packet.
266 *
267 */
268static ssize_t fr_bio_mem_read_verify_datagram(fr_bio_t *bio, void *packet_ctx, void *buffer, size_t size)
269{
270 ssize_t rcode;
271 size_t want;
272 fr_bio_mem_t *my = talloc_get_type_abort(bio, fr_bio_mem_t);
273 fr_bio_t *next;
274
275 /*
276 * There must be a next bio.
277 */
278 next = fr_bio_next(&my->bio);
279 fr_assert(next != NULL);
280
281 rcode = next->read(next, packet_ctx, buffer, size);
282 if (rcode <= 0) return rcode;
283
284 /*
285 * We have some data.
286 */
287 want = rcode;
288
289 /*
290 * It's a datagram socket, there can only be one packet in the buffer.
291 *
292 * @todo - if we're allowed more than one packet in the buffer, we should just call
293 * fr_bio_mem_read_verify(), or this function should call fr_bio_mem_call_verify().
294 */
295 switch (my->verify((fr_bio_t *) my, my->verify_ctx, packet_ctx, buffer, &want)) {
296 /*
297 * The data in the buffer is exactly a packet. Return that.
298 *
299 * @todo - if there are multiple packets, return the total size of packets?
300 */
301 case FR_BIO_VERIFY_OK:
302 fr_assert(want <= (size_t) rcode);
303 return want;
304
305 /*
306 * The data in the buffer doesn't make up a complete packet, discard it. The
307 * called verify function should take care of logging.
308 */
310 return 0;
311
313 return 0;
314
315 /*
316 * The verification routine told us to close the socket, so we do that.
317 *
318 * This is typically because the application-layer got something which is invalid.
319 */
321 break;
322 }
323
324 (void) fr_bio_shutdown_discard(bio); /* the buffered data is what failed */
325 return fr_bio_error(VERIFY);
326}
327
328
329/** Pass writes to the next BIO
330 *
331 * For speed, we try to bypass the memory buffer and write directly to the next bio. However, if the next
332 * bio returns EWOULDBLOCK, we write the data to the memory buffer, even if it is partial data.
333 */
334static ssize_t fr_bio_mem_write_next(fr_bio_t *bio, void *packet_ctx, void const *buffer, size_t size)
335{
336 int error;
337 ssize_t rcode;
338 size_t room, leftover;
339 fr_bio_mem_t *my = talloc_get_type_abort(bio, fr_bio_mem_t);
340 fr_bio_t *next;
341
342 /*
343 * We can't call the next bio if there's still cached data to flush.
344 *
345 * There must be a next bio.
346 */
348
349 next = fr_bio_next(&my->bio);
350 fr_assert(next != NULL);
351
352 rcode = next->write(next, packet_ctx, buffer, size);
353 if (rcode < 0) return rcode;
354
355 /*
356 * The next bio may write all of the data. If so, we return that,
357 */
358 if ((size_t) rcode == size) return rcode;
359
360 /*
361 * We were flushing the BIO, return however much data we managed to write.
362 *
363 * Note that flushes should never block.
364 */
365 if (!buffer) return rcode;
366
367 /*
368 * Tell previous BIOs in the chain that they are blocked.
369 */
370 error = fr_bio_write_blocked(bio);
371 if (error < 0) return error;
372
373 /*
374 * We had WOULD BLOCK, or wrote partial bytes. Save the data to the memory buffer, and ensure
375 * that future writes are ordered. i.e. they write to the memory buffer before writing to the
376 * next bio.
377 */
379
380 /*
381 * Clamp the write to however much data is available in the buffer.
382 */
383 leftover = size - rcode;
385
386 /*
387 * If we have "used == 0" above, then we must also have "room > 0".
388 */
389 fr_assert(room > 0);
390
391 if (room < leftover) leftover = room;
392
393 /*
394 * Since we've clamped the write, this call can never fail.
395 */
396 (void) fr_bio_buf_write(&my->write_buffer, ((uint8_t const *) buffer) + rcode, leftover);
397
398 /*
399 * The next bio blocked, so the buffer has data.
400 */
402
403 /*
404 * Some of the data base been written to the next bio, and some to our cache. The caller has to
405 * ensure that the first subsequent write will send over the rest of the data.
406 *
407 * However, we tell the caller that we wrote the entire packet. Because we are now responsible
408 * for writing the remaining bytes.
409 */
410 return size;
411}
412
413/** Flush the memory buffer.
414 *
415 * Note that this can't be used for unconnected FD sockets, as we
416 * don't have a packet_ctx to pass to the next write function!
417 */
419{
420 ssize_t rcode;
421 size_t used;
422 fr_bio_t *next;
423
424 /*
425 * Nothing to flush, don't do any writes.
426 *
427 * Instead, set the write function to write next, where data will be sent directly to the next
428 * bio, and will bypass the write buffer.
429 */
431 if (!used) {
432 my->bio.write = fr_bio_mem_write_next;
433 return 0;
434 }
435
436 next = fr_bio_next(&my->bio);
437 fr_assert(next != NULL);
438
439 /*
440 * Clamp the amount of data written. If the caller wants to write everything, it should
441 * pass SIZE_MAX.
442 */
443 if (used < size) size = used;
444
445 /*
446 * Flush the buffer to the next bio in line. That function will write as much data as possible,
447 * but may return a partial write.
448 */
449 rcode = next->write(next, NULL, my->write_buffer.read, size);
450
451 /*
452 * No write, or a "would block" error, that's a "would block" error.
453 */
454 if ((rcode == 0) || (rcode == fr_bio_error(IO_WOULD_BLOCK))) return fr_bio_error(IO_WOULD_BLOCK);
455
456 if (rcode < 0) return rcode;
457
458 /*
459 * Tell the buffer that we've read a certain amount of data from it.
460 */
461 (void) fr_bio_buf_read(&my->write_buffer, NULL, (size_t) rcode);
462
463 /*
464 * We haven't emptied the buffer, any further IO is blocked.
465 */
466 if ((size_t) rcode < used) return fr_bio_error(IO_WOULD_BLOCK);
467
468 /*
469 * We've flushed all of the buffer. Revert back to "pass through" writing.
470 */
472 my->bio.write = fr_bio_mem_write_next;
473 return rcode;
474}
475
476/** Write to the memory buffer.
477 *
478 * The special buffer pointer of NULL means flush(). On flush, we call next->read(), and if that succeeds,
479 * go back to "pass through" mode for the buffers.
480 *
481 * This can only be called for streams, not for datagrams.
482 */
483static ssize_t fr_bio_mem_write_buffer(fr_bio_t *bio, UNUSED void *packet_ctx, void const *buffer, size_t size)
484{
485 size_t room;
486 fr_bio_mem_t *my = talloc_get_type_abort(bio, fr_bio_mem_t);
487
488 /*
489 * Flush the output buffer.
490 */
491 if (unlikely(!buffer)) return fr_bio_mem_write_flush(my, size);
492
493 /*
494 * Clamp the write to however much data is available in the buffer.
495 */
497
498 /*
499 * The buffer is full, we can't write anything.
500 */
501 if (!room) return fr_bio_error(IO_WOULD_BLOCK);
502
503 /*
504 * If we're asked to write more bytes than are available in the buffer, then tell the caller that
505 * writes are now blocked, and we can't write any more data.
506 *
507 * Return an WOULD_BLOCK error instead of breaking our promise by writing part of the data,
508 * instead of accepting a full application write.
509 */
510 if (room < size) {
511 int rcode;
512
513 rcode = fr_bio_write_blocked(bio);
514 if (rcode < 0) return rcode;
515
516 return fr_bio_error(IO_WOULD_BLOCK);
517 }
518
519 /*
520 * As we have clamped the write, we know that this call must succeed.
521 */
522 (void) fr_bio_buf_write(&my->write_buffer, buffer, size);
523
524 /*
525 * If we've filled the buffer, tell the caller that writes are now blocked, and we can't write
526 * any more data. However, we still return the amount of data we wrote.
527 */
528 if (room == size) {
529 int rcode;
530
532
533 rcode = fr_bio_write_blocked(bio);
534 if (rcode < 0) return rcode;
535 }
536
537 return size;
538}
539
540/** Peek at the data in the read buffer
541 *
542 * Peeking at the data allows us to avoid many memory copies.
543 */
544uint8_t const *fr_bio_mem_read_peek(fr_bio_t *bio, size_t *size)
545{
546 size_t used;
547 fr_bio_mem_t *my = talloc_get_type_abort(bio, fr_bio_mem_t);
548
550
551 if (!used) return NULL;
552
553 *size = used;
554 return my->read_buffer.read;
555}
556
557/** Discard data from the read buffer.
558 *
559 * Discarding allows the caller to silently omit packets, so that
560 * they are not passed up to previous bios.
561 */
562void fr_bio_mem_read_discard(fr_bio_t *bio, size_t size)
563{
564 fr_bio_mem_t *my = talloc_get_type_abort(bio, fr_bio_mem_t);
565
566 (void) fr_bio_buf_read(&my->read_buffer, NULL, size);
567}
568
569/** Verify that a packet is OK.
570 *
571 * @todo - have this as a parameter to the read routines, so that they only return complete packets?
572 *
573 * @param bio the #fr_bio_mem_t
574 * @param packet_ctx the packet ctx
575 * @param[out] size how big the verified packet is
576 * @return
577 * - <0 for a fatal error
578 * - 0 for "we have a partial packet", the size to read is in *size
579 * - 1 for "we have at least one good packet", the size of it is in *size
580 */
581static int fr_bio_mem_call_verify(fr_bio_t *bio, void *packet_ctx, size_t *size)
582{
583 uint8_t *packet, *end;
584 fr_bio_mem_t *my = talloc_get_type_abort(bio, fr_bio_mem_t);
585
586 packet = my->read_buffer.read;
587 end = my->read_buffer.write;
588
589 while (packet < end) {
590 size_t want;
591#ifndef NDEBUG
592 size_t used;
593
594 used = end - packet;
595#endif
596
597 want = end - packet;
598
599 switch (my->verify((fr_bio_t *) my, my->verify_ctx, packet_ctx, packet, &want)) {
600 /*
601 * The data in the buffer is exactly a packet. Return that.
602 *
603 * @todo - if there are multiple packets, return the total size of packets?
604 */
605 case FR_BIO_VERIFY_OK:
606 fr_assert(want <= used);
607 *size = want;
608 return 1;
609
610 /*
611 * The packet needs more data. Return how much data we need for one packet.
612 */
614 fr_assert(want > used);
615 *size = want;
616 return 0;
617
619 /*
620 * We don't call fr_bio_buf_read(), because that will move the memory around, and
621 * we want to avoid that if at all possible.
622 */
623 fr_assert(want <= used);
624 fr_assert(packet == my->read_buffer.read);
625 packet = my->read_buffer.read += want;
626 continue;
627
628 /*
629 * Some kind of fatal validation error.
630 */
632 (void) fr_bio_shutdown_discard(bio); /* the buffered data is what failed */
633 return -1;
634 }
635 }
636
637 /*
638 * We haven't returned a full packet, partial packet, or error. We must have discarded
639 * everything in the buffer.
640 */
642
643 *size = 0;
644 return 0;
645}
646
647/** Allocate a memory buffer bio for either reading or writing.
648 *
649 * Frees "my" on failure, so that we don't need to put that in all of the callers.
650 */
651static bool fr_bio_mem_buf_alloc(fr_bio_mem_t *my, fr_bio_buf_t *buf, size_t size)
652{
653 if (size < 1024) size = 1024;
654 if (size > (1 << 20)) size = 1 << 20;
655
656 if (fr_bio_buf_alloc(my, buf, size) < 0) {
657 talloc_free(my);
658 return false;
659 }
660
661 return true;
662}
663
664/** Allocate a memory buffer bio
665 *
666 * The "read buffer" will cache reads from the next bio in the chain. If the next bio returns more data than
667 * the caller asked for, the extra data is cached in the read buffer.
668 *
669 * The "write buffer" will buffer writes to the next bio in the chain. If the caller writes more data than
670 * the next bio can process, the extra data is cached in the write buffer.
671 *
672 * When the bio is closed (or freed) any pending data in the buffers is lost. The same happens if the next
673 * bio returns a fatal error.
674 *
675 * At some point during a read, the next bio may return EOF. When that happens, the caller should not rely
676 * on the next FD being readable or writable. Instead, it should keep reading from the memory bio until it
677 * returns EOF. See fr_bio_fd_eof() for details.
678 *
679 * @param ctx the talloc ctx
680 * @param read_size size of the read buffer. Must be 1024..2^20
681 * @param write_size size of the write buffer. Can be zero. If non-zero, must be 1024..2^20
682 * @param next the next bio which will perform the underlying reads and writes.
683 * - NULL on error, memory allocation failed
684 * - !NULL the bio
685 */
686fr_bio_t *fr_bio_mem_alloc(TALLOC_CTX *ctx, size_t read_size, size_t write_size, fr_bio_t *next)
687{
688 fr_bio_mem_t *my;
689
690 my = talloc_zero(ctx, fr_bio_mem_t);
691 if (!my) return NULL;
692
693 /*
694 * We can do 0-sized buffers for read, and the application should then set the verify function.
695 */
696 if (!read_size) {
698 my->bio.read = fr_bio_fail_read; /* can't read anything until the verify routine is put in place */
699 my->bio.write = fr_bio_next_write;
700
701 if (write_size > 0) {
702 talloc_free(my);
703 fr_strerror_const("Invalid write size. If read size is zero, then write size must also be zero");
704 return NULL;
705 }
706
707 } else {
708 /*
709 * We have a read buffer, and potentially a write buffer.
710 */
712
713 if (!fr_bio_mem_buf_alloc(my, &my->read_buffer, read_size)) {
714 oom:
715 fr_strerror_const("Out of memory");
716 return NULL;
717 }
718 my->bio.read = fr_bio_mem_read;
719
720 if (write_size) {
721 if (!fr_bio_mem_buf_alloc(my, &my->write_buffer, write_size)) goto oom;
722
723 /*
724 * Default to passing writes straight through, but buffer them locally if the
725 * write blocks.
726 *
727 * We also only need to resume writes if we're buffering data.
728 */
729 my->bio.write = fr_bio_mem_write_next;
730 my->priv_cb.write_resume = fr_bio_mem_write_resume;
731
732 } else {
733 my->bio.write = fr_bio_next_write;
734 }
735
736 /*
737 * EOF and shutdown are needed if there's a read buffer, but not when the memory bio is
738 * just doing packet verification/
739 */
740 my->priv_cb.eof = fr_bio_mem_eof;
741 }
742
743 fr_bio_chain(&my->bio, next);
744
745 talloc_set_destructor((fr_bio_t *) my, fr_bio_destructor); /* always use a common destructor */
746 return (fr_bio_t *) my;
747}
748
749
750/** Allocate a memory buffer which sources data from the callers application into the bio system.
751 *
752 * The caller writes data to the buffer, but never reads from it. This bio will call the "next" bio to write
753 * the data. somewhere.
754 */
755fr_bio_t *fr_bio_mem_source_alloc(TALLOC_CTX *ctx, size_t write_size, fr_bio_t *next)
756{
757 fr_bio_mem_t *my;
758
759 /*
760 * The caller has to state that the API is caching data.
761 */
762 if (!write_size) return NULL;
763
764 my = talloc_zero(ctx, fr_bio_mem_t);
765 if (!my) return NULL;
766
767 if (!fr_bio_mem_buf_alloc(my, &my->write_buffer, write_size)) {
768 return NULL;
769 }
770
772 my->bio.read = fr_bio_fail_read; /* reading FROM this bio is not possible */
773 my->bio.write = fr_bio_mem_write_next;
774
775 /*
776 * @todo - have write pause / write resume callbacks?
777 */
778
779 fr_bio_chain(&my->bio, next);
780
781 talloc_set_destructor((fr_bio_t *) my, fr_bio_destructor);
782 return (fr_bio_t *) my;
783}
784
785/** Read from a buffer which a previous bio has filled.
786 *
787 * This function is called by the application which wants to read from a sink.
788 */
789static ssize_t fr_bio_mem_read_buffer(fr_bio_t *bio, UNUSED void *packet_ctx, void *buffer, size_t size)
790{
791 fr_bio_mem_t *my = talloc_get_type_abort(bio, fr_bio_mem_t);
792
793 return fr_bio_buf_read(&my->read_buffer, buffer, size);
794}
795
796/** Write to the read buffer.
797 *
798 * This function is called by an upstream function which writes into our local buffer.
799 */
800static ssize_t fr_bio_mem_write_read_buffer(fr_bio_t *bio, UNUSED void *packet_ctx, void const *buffer, size_t size)
801{
802 size_t room;
803 fr_bio_mem_t *my = talloc_get_type_abort(bio, fr_bio_mem_t);
804
805 /*
806 * Clamp the write to however much data is available in the buffer.
807 */
809
810 /*
811 * The buffer is full. We're now blocked.
812 */
813 if (!room) return fr_bio_error(IO_WOULD_BLOCK);
814
815 if (room < size) size = room;
816
817 /*
818 * As we have clamped the write, we know that this call must succeed.
819 */
820 return fr_bio_buf_write(&my->read_buffer, buffer, size);
821}
822
823/** Allocate a memory buffer which sinks data from a bio system into the callers application.
824 *
825 * The caller reads data from this bio, but never writes to it. Upstream BIOs will source the data.
826 */
827fr_bio_t *fr_bio_mem_sink_alloc(TALLOC_CTX *ctx, size_t read_size)
828{
829 fr_bio_mem_t *my;
830
831 /*
832 * The caller has to state that the API is caching data.
833 */
834 if (!read_size) return NULL;
835
836 my = talloc_zero(ctx, fr_bio_mem_t);
837 if (!my) return NULL;
838
839 if (!fr_bio_mem_buf_alloc(my, &my->read_buffer, read_size)) {
840 return NULL;
841 }
842
843 my->type = FR_BIO_MEM_SINK;
844 my->bio.read = fr_bio_mem_read_buffer;
845 my->bio.write = fr_bio_mem_write_read_buffer; /* the upstream will write to our read buffer */
846
847 talloc_set_destructor((fr_bio_t *) my, fr_bio_destructor); /* always use a common destructor */
848 return (fr_bio_t *) my;
849}
850
851/** Set the verification function for memory bios.
852 *
853 * It is possible to add a verification function. It is not currently possible to remove one.
854 *
855 * @param bio the binary IO handler
856 * @param verify the verification function
857 * @param verify_ctx to pass to the verification function
858 * @param datagram whether or not this bio is a datagram one.
859 * @return
860 * - <0 on error
861 * - 0 on success
862 */
863int fr_bio_mem_set_verify(fr_bio_t *bio, fr_bio_verify_t verify, void *verify_ctx, bool datagram)
864{
865 fr_bio_mem_t *my = talloc_get_type_abort(bio, fr_bio_mem_t);
866
867 switch (my->type) {
869 fr_assert(0);
870 return -1;
871
873 case FR_BIO_MEM_SINK:
874 fr_strerror_const("Cannot add verify to this memory BIO");
875 return fr_bio_error(GENERIC);
876
878 fr_assert(my->bio.write == fr_bio_next_write);
879 if (!datagram) {
880 fr_strerror_const("Invalid memory BIO - we need a read buffer for verifying packets from a stream socket");
881 return -1;
882 }
884
886 break;
887 }
888
889 my->verify = verify;
890 my->verify_ctx = verify_ctx;
891
892 /*
893 * For reading datagrams, we just verify the packet in place. This works both when we have local
894 * buffers, and when we are verifying packets in the application-supplied buffer.
895 *
896 * For writing datagrams, then we cannot buffer individual datagrams. We must write
897 * either all of the datagram out, or none of it.
898 */
899 if (datagram) {
900 my->bio.read = fr_bio_mem_read_verify_datagram;
901 my->bio.write = fr_bio_next_write;
902
903 /*
904 * Might as well free the memory for the write buffer. It won't be used.
905 */
906 if (my->write_buffer.start) {
908 my->write_buffer = (fr_bio_buf_t) {};
909 }
910 } else {
911 my->bio.read = fr_bio_mem_read_verify_stream;
912 /* don't touch the write function or the write buffer. */
913 }
914
915 return 0;
916}
917
918/*
919 * There's no fr_bio_mem_write_blocked()
920 */
921
922/** See if we can resume writes to the memory bio.
923 *
924 * Note that there is no equivalent fr_bio_mem_write_blocked(), as that function wouldn't do anything.
925 * Perhaps it could swap the write function to fr_bio_mem_write_buffer(), but the fr_bio_mem_write_next()
926 * function should automatically do that when the write to the next bio only writes part of the data,
927 * or if it returns fr_bio_error(IO_WOULD_BLOCK)
928 */
930{
931 fr_bio_mem_t *my = talloc_get_type_abort(bio, fr_bio_mem_t);
932 ssize_t rcode;
933
934 if (bio->write != fr_bio_mem_write_buffer) return 1;
935
937
938 /*
939 * Flush the buffer, and then reset the write routine if we were successful.
940 */
941 rcode = fr_bio_mem_write_flush(my, SIZE_MAX);
942 if (rcode < 0) {
943 /*
944 * Still blocked, can't resume.
945 */
946 if (rcode == fr_bio_error(IO_WOULD_BLOCK)) return 0;
947
948 return (int)rcode; /* fr_bio_error() codes fit in an int */
949 }
950
951 /*
952 * There's no EWOULDBLOCK, so we try to resume.
953 */
954 fr_assert((rcode == 0) || (fr_bio_buf_used(&my->write_buffer) == 0));
955
956 /*
957 * Check for an application hook to check if we can resume writes.
958 */
959 if (!my->cb.write_resume) return 1;
960
961 return my->cb.write_resume(bio);
962}
963
964/** Pause writes.
965 *
966 * Calls to fr_bio_write() will write to the memory buffer, and not
967 * to the next bio. You MUST call fr_bio_mem_write_resume() after
968 * this to flush any data.
969 */
971{
972 fr_bio_mem_t *my = talloc_get_type_abort(bio, fr_bio_mem_t);
973
974 if (my->bio.write == fr_bio_mem_write_buffer) return 0;
975
976 if (!my->bio.write || !fr_bio_buf_initialized(&my->write_buffer)) {
977 fr_strerror_const("Cannot pause writes on a BIO with no write buffer");
978 return fr_bio_error(GENERIC);
979 }
980
981 /*
982 * Sink only, can't pause.
983 */
984 if (my->bio.write == fr_bio_mem_write_read_buffer) return -1;
985
986 my->bio.write = fr_bio_mem_write_buffer;
987
988 return 0;
989}
static int const char char buffer[256]
Definition acutest.h:641
fr_bio_write_t _CONST write
write to the underlying bio
Definition base.h:117
fr_bio_read_t _CONST read
read from the underlying bio
Definition base.h:116
static fr_bio_t * fr_bio_next(fr_bio_t *bio)
Definition base.h:131
#define fr_bio_error(_x)
Definition base.h:200
static int packet_ctx
the tests pass only the address of packet_ctx
static void fr_bio_chain(fr_bio_t *first, fr_bio_t *second)
Chain one bio after another.
Definition bio_priv.h:86
size_t fr_bio_buf_make_room(fr_bio_buf_t *bio_buf)
Definition buf.c:28
size_t fr_bio_buf_read(fr_bio_buf_t *bio_buf, void *buffer, size_t size)
Definition buf.c:48
int fr_bio_buf_alloc(TALLOC_CTX *ctx, fr_bio_buf_t *bio_buf, size_t size)
Definition buf.c:117
ssize_t fr_bio_buf_write(fr_bio_buf_t *bio_buf, const void *buffer, size_t size)
Definition buf.c:84
static bool fr_bio_buf_initialized(fr_bio_buf_t const *bio_buf)
Definition buf.h:68
static size_t fr_bio_buf_write_room(fr_bio_buf_t const *bio_buf)
Definition buf.h:82
uint8_t * start
start of the buffer
Definition buf.h:30
static size_t fr_bio_buf_used(fr_bio_buf_t const *bio_buf)
Definition buf.h:73
static void fr_bio_buf_reset(fr_bio_buf_t *bio_buf)
Definition buf.h:61
static uint8_t * fr_bio_buf_write_reserve(fr_bio_buf_t *bio_buf, size_t size)
Definition buf.h:89
uint8_t * read
where in the buffer reads are taken from
Definition buf.h:33
static int fr_bio_buf_write_alloc(fr_bio_buf_t *bio_buf, size_t size)
Definition buf.h:98
uint8_t * write
where in the buffer writes are sent to
Definition buf.h:34
#define FALL_THROUGH
clang 10 doesn't recognised the FALL-THROUGH comment anymore
Definition build.h:422
#define unlikely(_x)
Definition build.h:486
#define UNUSED
Definition build.h:415
talloc_free(hp)
int fr_bio_write_blocked(fr_bio_t *bio)
Internal BIO function to tell all BIOs that it's blocked.
Definition base.c:354
int fr_bio_shutdown_discard(fr_bio_t *bio)
Shut down the chain now, discarding anything which is still buffered.
Definition base.c:207
int fr_bio_destructor(fr_bio_t *bio)
Free this bio.
Definition base.c:35
fr_bio_t * fr_bio_eof(fr_bio_t *bio)
Internal BIO function to run EOF callbacks.
Definition base.c:280
ssize_t fr_bio_next_write(fr_bio_t *bio, void *packet_ctx, void const *buffer, size_t size)
Internal bio function which just writes to the "next" bio.
Definition base.c:64
fr_bio_t * fr_bio_mem_alloc(TALLOC_CTX *ctx, size_t read_size, size_t write_size, fr_bio_t *next)
Allocate a memory buffer bio.
Definition mem.c:686
static ssize_t fr_bio_mem_write_read_buffer(fr_bio_t *bio, UNUSED void *packet_ctx, void const *buffer, size_t size)
Write to the read buffer.
Definition mem.c:800
static ssize_t fr_bio_mem_read_verify_stream(fr_bio_t *bio, void *packet_ctx, void *buffer, size_t size)
Return data only if we have a complete packet.
Definition mem.c:202
void * verify_ctx
verify context
Definition mem.c:49
void fr_bio_mem_read_discard(fr_bio_t *bio, size_t size)
Discard data from the read buffer.
Definition mem.c:562
fr_bio_t * fr_bio_mem_sink_alloc(TALLOC_CTX *ctx, size_t read_size)
Allocate a memory buffer which sinks data from a bio system into the callers application.
Definition mem.c:827
fr_bio_buf_t read_buffer
buffering for reads
Definition mem.c:51
static int fr_bio_mem_eof(fr_bio_t *bio)
Definition mem.c:87
struct fr_bio_mem_s fr_bio_mem_t
The memory buffer bio.
static ssize_t fr_bio_mem_write_next(fr_bio_t *bio, void *packet_ctx, void const *buffer, size_t size)
Pass writes to the next BIO.
Definition mem.c:334
static ssize_t fr_bio_mem_read_verify_datagram(fr_bio_t *bio, void *packet_ctx, void *buffer, size_t size)
Return data only if we have a complete packet.
Definition mem.c:268
fr_bio_mem_type_t
Definition mem.c:31
@ FR_BIO_MEM_SINK
Definition mem.c:34
@ FR_BIO_MEM_INVALID
Definition mem.c:32
@ FR_BIO_MEM_SOURCE
Definition mem.c:33
@ FR_BIO_MEM_BUFFER
Definition mem.c:36
@ FR_BIO_MEM_VERIFY
Definition mem.c:35
fr_bio_t * fr_bio_mem_source_alloc(TALLOC_CTX *ctx, size_t write_size, fr_bio_t *next)
Allocate a memory buffer which sources data from the callers application into the bio system.
Definition mem.c:755
static ssize_t fr_bio_mem_read_buffer(fr_bio_t *bio, UNUSED void *packet_ctx, void *buffer, size_t size)
Read from a buffer which a previous bio has filled.
Definition mem.c:789
int fr_bio_mem_write_resume(fr_bio_t *bio)
See if we can resume writes to the memory bio.
Definition mem.c:929
fr_bio_buf_t write_buffer
buffering for writes
Definition mem.c:52
fr_bio_verify_t verify
verify data to see if we have a packet.
Definition mem.c:48
FR_BIO_COMMON
Definition mem.c:44
static ssize_t fr_bio_mem_read(fr_bio_t *bio, void *packet_ctx, void *buffer, size_t size)
Read from a memory BIO.
Definition mem.c:116
int fr_bio_mem_set_verify(fr_bio_t *bio, fr_bio_verify_t verify, void *verify_ctx, bool datagram)
Set the verification function for memory bios.
Definition mem.c:863
fr_bio_mem_type_t type
source, sink, etc.
Definition mem.c:46
static ssize_t fr_bio_mem_read_eof(fr_bio_t *bio, UNUSED void *packet_ctx, void *buffer, size_t size)
At EOF, read data from the buffer until it is empty.
Definition mem.c:64
static bool fr_bio_mem_buf_alloc(fr_bio_mem_t *my, fr_bio_buf_t *buf, size_t size)
Allocate a memory buffer bio for either reading or writing.
Definition mem.c:651
static ssize_t fr_bio_mem_write_flush(fr_bio_mem_t *my, size_t size)
Flush the memory buffer.
Definition mem.c:418
static int fr_bio_mem_call_verify(fr_bio_t *bio, void *packet_ctx, size_t *size))
Verify that a packet is OK.
Definition mem.c:581
int fr_bio_mem_write_pause(fr_bio_t *bio)
Pause writes.
Definition mem.c:970
uint8_t const * fr_bio_mem_read_peek(fr_bio_t *bio, size_t *size)
Peek at the data in the read buffer.
Definition mem.c:544
static ssize_t fr_bio_mem_read_packet(fr_bio_t *bio, void *packet_ctx, void *buffer, size_t size)
See is there's a full packet in the buffer.
Definition mem.c:167
static ssize_t fr_bio_mem_write_buffer(fr_bio_t *bio, void *packet_ctx, void const *buffer, size_t size)
The memory buffer bio.
Definition mem.c:43
fr_bio_verify_action_t(* fr_bio_verify_t)(fr_bio_t *bio, void *verify_ctx, void *packet_ctx, const void *buffer, size_t *size)
Verifies the packet.
Definition mem.h:51
@ FR_BIO_VERIFY_ERROR_CLOSE
fatal error, the bio should be closed.
Definition mem.h:36
@ FR_BIO_VERIFY_DISCARD
the packet should be discarded
Definition mem.h:34
@ FR_BIO_VERIFY_OK
packet is OK
Definition mem.h:33
@ FR_BIO_VERIFY_WANT_MORE
not enough data for one packet
Definition mem.h:35
long int ssize_t
unsigned char uint8_t
static size_t used
ssize_t fr_bio_fail_read(UNUSED fr_bio_t *bio, UNUSED void *packet_ctx, UNUSED void *buffer, UNUSED size_t size)
Always return error on read.
Definition null.c:47
#define fr_assert(_expr)
Definition rad_assert.h:37
#define VERIFY(_x)
Definition trie.c:130
#define fr_strerror_const(_msg)
Definition strerror.h:223
int nonnull(2, 5))