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