]>
git.proxmox.com Git - mirror_frr.git/blob - lib/stream.c
3 * Copyright (C) 1999 Kunihiro Ishiguro
5 * This file is part of GNU Zebra.
7 * GNU Zebra is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2, or (at your option) any
12 * GNU Zebra is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; see the file COPYING; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
31 #include "frr_pthread.h"
32 #include "lib_errors.h"
34 DEFINE_MTYPE_STATIC(LIB
, STREAM
, "Stream")
35 DEFINE_MTYPE_STATIC(LIB
, STREAM_FIFO
, "Stream FIFO")
37 /* Tests whether a position is valid */
38 #define GETP_VALID(S, G) ((G) <= (S)->endp)
39 #define PUT_AT_VALID(S,G) GETP_VALID(S,G)
40 #define ENDP_VALID(S, E) ((E) <= (S)->size)
42 /* asserting sanity checks. Following must be true before
43 * stream functions are called:
45 * Following must always be true of stream elements
46 * before and after calls to stream functions:
48 * getp <= endp <= size
50 * Note that after a stream function is called following may be true:
51 * if (getp == endp) then stream is no longer readable
52 * if (endp == size) then stream is no longer writeable
54 * It is valid to put to anywhere within the size of the stream, but only
55 * using stream_put..._at() functions.
57 #define STREAM_WARN_OFFSETS(S) \
58 flog_warn(EC_LIB_STREAM, \
59 "&(struct stream): %p, size: %lu, getp: %lu, endp: %lu\n", \
60 (void *)(S), (unsigned long)(S)->size, \
61 (unsigned long)(S)->getp, (unsigned long)(S)->endp)
63 #define STREAM_VERIFY_SANE(S) \
65 if (!(GETP_VALID(S, (S)->getp) && ENDP_VALID(S, (S)->endp))) \
66 STREAM_WARN_OFFSETS(S); \
67 assert(GETP_VALID(S, (S)->getp)); \
68 assert(ENDP_VALID(S, (S)->endp)); \
71 #define STREAM_BOUND_WARN(S, WHAT) \
73 flog_warn(EC_LIB_STREAM, "%s: Attempt to %s out of bounds", \
75 STREAM_WARN_OFFSETS(S); \
79 #define STREAM_BOUND_WARN2(S, WHAT) \
81 flog_warn(EC_LIB_STREAM, "%s: Attempt to %s out of bounds", \
83 STREAM_WARN_OFFSETS(S); \
86 /* XXX: Deprecated macro: do not use */
87 #define CHECK_SIZE(S, Z) \
89 if (((S)->endp + (Z)) > (S)->size) { \
92 "CHECK_SIZE: truncating requested size %lu\n", \
93 (unsigned long)(Z)); \
94 STREAM_WARN_OFFSETS(S); \
95 (Z) = (S)->size - (S)->endp; \
99 /* Make stream buffer. */
100 struct stream
*stream_new(size_t size
)
106 s
= XMALLOC(MTYPE_STREAM
, sizeof(struct stream
) + size
);
108 s
->getp
= s
->endp
= 0;
115 void stream_free(struct stream
*s
)
120 XFREE(MTYPE_STREAM
, s
);
123 struct stream
*stream_copy(struct stream
*new, struct stream
*src
)
125 STREAM_VERIFY_SANE(src
);
128 assert(STREAM_SIZE(new) >= src
->endp
);
130 new->endp
= src
->endp
;
131 new->getp
= src
->getp
;
133 memcpy(new->data
, src
->data
, src
->endp
);
138 struct stream
*stream_dup(struct stream
*s
)
142 STREAM_VERIFY_SANE(s
);
144 if ((new = stream_new(s
->endp
)) == NULL
)
147 return (stream_copy(new, s
));
150 struct stream
*stream_dupcat(struct stream
*s1
, struct stream
*s2
,
155 STREAM_VERIFY_SANE(s1
);
156 STREAM_VERIFY_SANE(s2
);
158 if ((new = stream_new(s1
->endp
+ s2
->endp
)) == NULL
)
161 memcpy(new->data
, s1
->data
, offset
);
162 memcpy(new->data
+ offset
, s2
->data
, s2
->endp
);
163 memcpy(new->data
+ offset
+ s2
->endp
, s1
->data
+ offset
,
164 (s1
->endp
- offset
));
165 new->endp
= s1
->endp
+ s2
->endp
;
169 size_t stream_resize_inplace(struct stream
**sptr
, size_t newsize
)
171 struct stream
*orig
= *sptr
;
173 STREAM_VERIFY_SANE(orig
);
175 orig
= XREALLOC(MTYPE_STREAM
, orig
, sizeof(struct stream
) + newsize
);
177 orig
->size
= newsize
;
179 if (orig
->endp
> orig
->size
)
180 orig
->endp
= orig
->size
;
181 if (orig
->getp
> orig
->endp
)
182 orig
->getp
= orig
->endp
;
184 STREAM_VERIFY_SANE(orig
);
190 size_t stream_get_getp(struct stream
*s
)
192 STREAM_VERIFY_SANE(s
);
196 size_t stream_get_endp(struct stream
*s
)
198 STREAM_VERIFY_SANE(s
);
202 size_t stream_get_size(struct stream
*s
)
204 STREAM_VERIFY_SANE(s
);
208 /* Stream structre' stream pointer related functions. */
209 void stream_set_getp(struct stream
*s
, size_t pos
)
211 STREAM_VERIFY_SANE(s
);
213 if (!GETP_VALID(s
, pos
)) {
214 STREAM_BOUND_WARN(s
, "set getp");
221 void stream_set_endp(struct stream
*s
, size_t pos
)
223 STREAM_VERIFY_SANE(s
);
225 if (!ENDP_VALID(s
, pos
)) {
226 STREAM_BOUND_WARN(s
, "set endp");
231 * Make sure the current read pointer is not beyond the new endp.
234 STREAM_BOUND_WARN(s
, "set endp");
239 STREAM_VERIFY_SANE(s
);
242 /* Forward pointer. */
243 void stream_forward_getp(struct stream
*s
, size_t size
)
245 STREAM_VERIFY_SANE(s
);
247 if (!GETP_VALID(s
, s
->getp
+ size
)) {
248 STREAM_BOUND_WARN(s
, "seek getp");
255 void stream_forward_endp(struct stream
*s
, size_t size
)
257 STREAM_VERIFY_SANE(s
);
259 if (!ENDP_VALID(s
, s
->endp
+ size
)) {
260 STREAM_BOUND_WARN(s
, "seek endp");
267 /* Copy from stream to destination. */
268 bool stream_get2(void *dst
, struct stream
*s
, size_t size
)
270 STREAM_VERIFY_SANE(s
);
272 if (STREAM_READABLE(s
) < size
) {
273 STREAM_BOUND_WARN2(s
, "get");
277 memcpy(dst
, s
->data
+ s
->getp
, size
);
283 void stream_get(void *dst
, struct stream
*s
, size_t size
)
285 STREAM_VERIFY_SANE(s
);
287 if (STREAM_READABLE(s
) < size
) {
288 STREAM_BOUND_WARN(s
, "get");
292 memcpy(dst
, s
->data
+ s
->getp
, size
);
296 /* Get next character from the stream. */
297 bool stream_getc2(struct stream
*s
, uint8_t *byte
)
299 STREAM_VERIFY_SANE(s
);
301 if (STREAM_READABLE(s
) < sizeof(uint8_t)) {
302 STREAM_BOUND_WARN2(s
, "get char");
305 *byte
= s
->data
[s
->getp
++];
310 uint8_t stream_getc(struct stream
*s
)
314 STREAM_VERIFY_SANE(s
);
316 if (STREAM_READABLE(s
) < sizeof(uint8_t)) {
317 STREAM_BOUND_WARN(s
, "get char");
320 c
= s
->data
[s
->getp
++];
325 /* Get next character from the stream. */
326 uint8_t stream_getc_from(struct stream
*s
, size_t from
)
330 STREAM_VERIFY_SANE(s
);
332 if (!GETP_VALID(s
, from
+ sizeof(uint8_t))) {
333 STREAM_BOUND_WARN(s
, "get char");
342 bool stream_getw2(struct stream
*s
, uint16_t *word
)
344 STREAM_VERIFY_SANE(s
);
346 if (STREAM_READABLE(s
) < sizeof(uint16_t)) {
347 STREAM_BOUND_WARN2(s
, "get ");
351 *word
= s
->data
[s
->getp
++] << 8;
352 *word
|= s
->data
[s
->getp
++];
357 /* Get next word from the stream. */
358 uint16_t stream_getw(struct stream
*s
)
362 STREAM_VERIFY_SANE(s
);
364 if (STREAM_READABLE(s
) < sizeof(uint16_t)) {
365 STREAM_BOUND_WARN(s
, "get ");
369 w
= s
->data
[s
->getp
++] << 8;
370 w
|= s
->data
[s
->getp
++];
375 /* Get next word from the stream. */
376 uint16_t stream_getw_from(struct stream
*s
, size_t from
)
380 STREAM_VERIFY_SANE(s
);
382 if (!GETP_VALID(s
, from
+ sizeof(uint16_t))) {
383 STREAM_BOUND_WARN(s
, "get ");
387 w
= s
->data
[from
++] << 8;
393 /* Get next 3-byte from the stream. */
394 uint32_t stream_get3_from(struct stream
*s
, size_t from
)
398 STREAM_VERIFY_SANE(s
);
400 if (!GETP_VALID(s
, from
+ 3)) {
401 STREAM_BOUND_WARN(s
, "get 3byte");
405 l
= s
->data
[from
++] << 16;
406 l
|= s
->data
[from
++] << 8;
412 uint32_t stream_get3(struct stream
*s
)
416 STREAM_VERIFY_SANE(s
);
418 if (STREAM_READABLE(s
) < 3) {
419 STREAM_BOUND_WARN(s
, "get 3byte");
423 l
= s
->data
[s
->getp
++] << 16;
424 l
|= s
->data
[s
->getp
++] << 8;
425 l
|= s
->data
[s
->getp
++];
430 /* Get next long word from the stream. */
431 uint32_t stream_getl_from(struct stream
*s
, size_t from
)
435 STREAM_VERIFY_SANE(s
);
437 if (!GETP_VALID(s
, from
+ sizeof(uint32_t))) {
438 STREAM_BOUND_WARN(s
, "get long");
442 l
= (unsigned)(s
->data
[from
++]) << 24;
443 l
|= s
->data
[from
++] << 16;
444 l
|= s
->data
[from
++] << 8;
450 /* Copy from stream at specific location to destination. */
451 void stream_get_from(void *dst
, struct stream
*s
, size_t from
, size_t size
)
453 STREAM_VERIFY_SANE(s
);
455 if (!GETP_VALID(s
, from
+ size
)) {
456 STREAM_BOUND_WARN(s
, "get from");
460 memcpy(dst
, s
->data
+ from
, size
);
463 bool stream_getl2(struct stream
*s
, uint32_t *l
)
465 STREAM_VERIFY_SANE(s
);
467 if (STREAM_READABLE(s
) < sizeof(uint32_t)) {
468 STREAM_BOUND_WARN2(s
, "get long");
472 *l
= (unsigned int)(s
->data
[s
->getp
++]) << 24;
473 *l
|= s
->data
[s
->getp
++] << 16;
474 *l
|= s
->data
[s
->getp
++] << 8;
475 *l
|= s
->data
[s
->getp
++];
480 uint32_t stream_getl(struct stream
*s
)
484 STREAM_VERIFY_SANE(s
);
486 if (STREAM_READABLE(s
) < sizeof(uint32_t)) {
487 STREAM_BOUND_WARN(s
, "get long");
491 l
= (unsigned)(s
->data
[s
->getp
++]) << 24;
492 l
|= s
->data
[s
->getp
++] << 16;
493 l
|= s
->data
[s
->getp
++] << 8;
494 l
|= s
->data
[s
->getp
++];
499 /* Get next quad word from the stream. */
500 uint64_t stream_getq_from(struct stream
*s
, size_t from
)
504 STREAM_VERIFY_SANE(s
);
506 if (!GETP_VALID(s
, from
+ sizeof(uint64_t))) {
507 STREAM_BOUND_WARN(s
, "get quad");
511 q
= ((uint64_t)s
->data
[from
++]) << 56;
512 q
|= ((uint64_t)s
->data
[from
++]) << 48;
513 q
|= ((uint64_t)s
->data
[from
++]) << 40;
514 q
|= ((uint64_t)s
->data
[from
++]) << 32;
515 q
|= ((uint64_t)s
->data
[from
++]) << 24;
516 q
|= ((uint64_t)s
->data
[from
++]) << 16;
517 q
|= ((uint64_t)s
->data
[from
++]) << 8;
518 q
|= ((uint64_t)s
->data
[from
++]);
523 uint64_t stream_getq(struct stream
*s
)
527 STREAM_VERIFY_SANE(s
);
529 if (STREAM_READABLE(s
) < sizeof(uint64_t)) {
530 STREAM_BOUND_WARN(s
, "get quad");
534 q
= ((uint64_t)s
->data
[s
->getp
++]) << 56;
535 q
|= ((uint64_t)s
->data
[s
->getp
++]) << 48;
536 q
|= ((uint64_t)s
->data
[s
->getp
++]) << 40;
537 q
|= ((uint64_t)s
->data
[s
->getp
++]) << 32;
538 q
|= ((uint64_t)s
->data
[s
->getp
++]) << 24;
539 q
|= ((uint64_t)s
->data
[s
->getp
++]) << 16;
540 q
|= ((uint64_t)s
->data
[s
->getp
++]) << 8;
541 q
|= ((uint64_t)s
->data
[s
->getp
++]);
546 /* Get next long word from the stream. */
547 uint32_t stream_get_ipv4(struct stream
*s
)
551 STREAM_VERIFY_SANE(s
);
553 if (STREAM_READABLE(s
) < sizeof(uint32_t)) {
554 STREAM_BOUND_WARN(s
, "get ipv4");
558 memcpy(&l
, s
->data
+ s
->getp
, sizeof(uint32_t));
559 s
->getp
+= sizeof(uint32_t);
564 float stream_getf(struct stream
*s
)
570 u
.d
= stream_getl(s
);
574 double stream_getd(struct stream
*s
)
580 u
.d
= stream_getq(s
);
584 /* Copy to source to stream.
586 * XXX: This uses CHECK_SIZE and hence has funny semantics -> Size will wrap
587 * around. This should be fixed once the stream updates are working.
589 * stream_write() is saner
591 void stream_put(struct stream
*s
, const void *src
, size_t size
)
594 /* XXX: CHECK_SIZE has strange semantics. It should be deprecated */
597 STREAM_VERIFY_SANE(s
);
599 if (STREAM_WRITEABLE(s
) < size
) {
600 STREAM_BOUND_WARN(s
, "put");
605 memcpy(s
->data
+ s
->endp
, src
, size
);
607 memset(s
->data
+ s
->endp
, 0, size
);
612 /* Put character to the stream. */
613 int stream_putc(struct stream
*s
, uint8_t c
)
615 STREAM_VERIFY_SANE(s
);
617 if (STREAM_WRITEABLE(s
) < sizeof(uint8_t)) {
618 STREAM_BOUND_WARN(s
, "put");
622 s
->data
[s
->endp
++] = c
;
623 return sizeof(uint8_t);
626 /* Put word to the stream. */
627 int stream_putw(struct stream
*s
, uint16_t w
)
629 STREAM_VERIFY_SANE(s
);
631 if (STREAM_WRITEABLE(s
) < sizeof(uint16_t)) {
632 STREAM_BOUND_WARN(s
, "put");
636 s
->data
[s
->endp
++] = (uint8_t)(w
>> 8);
637 s
->data
[s
->endp
++] = (uint8_t)w
;
642 /* Put long word to the stream. */
643 int stream_put3(struct stream
*s
, uint32_t l
)
645 STREAM_VERIFY_SANE(s
);
647 if (STREAM_WRITEABLE(s
) < 3) {
648 STREAM_BOUND_WARN(s
, "put");
652 s
->data
[s
->endp
++] = (uint8_t)(l
>> 16);
653 s
->data
[s
->endp
++] = (uint8_t)(l
>> 8);
654 s
->data
[s
->endp
++] = (uint8_t)l
;
659 /* Put long word to the stream. */
660 int stream_putl(struct stream
*s
, uint32_t l
)
662 STREAM_VERIFY_SANE(s
);
664 if (STREAM_WRITEABLE(s
) < sizeof(uint32_t)) {
665 STREAM_BOUND_WARN(s
, "put");
669 s
->data
[s
->endp
++] = (uint8_t)(l
>> 24);
670 s
->data
[s
->endp
++] = (uint8_t)(l
>> 16);
671 s
->data
[s
->endp
++] = (uint8_t)(l
>> 8);
672 s
->data
[s
->endp
++] = (uint8_t)l
;
677 /* Put quad word to the stream. */
678 int stream_putq(struct stream
*s
, uint64_t q
)
680 STREAM_VERIFY_SANE(s
);
682 if (STREAM_WRITEABLE(s
) < sizeof(uint64_t)) {
683 STREAM_BOUND_WARN(s
, "put quad");
687 s
->data
[s
->endp
++] = (uint8_t)(q
>> 56);
688 s
->data
[s
->endp
++] = (uint8_t)(q
>> 48);
689 s
->data
[s
->endp
++] = (uint8_t)(q
>> 40);
690 s
->data
[s
->endp
++] = (uint8_t)(q
>> 32);
691 s
->data
[s
->endp
++] = (uint8_t)(q
>> 24);
692 s
->data
[s
->endp
++] = (uint8_t)(q
>> 16);
693 s
->data
[s
->endp
++] = (uint8_t)(q
>> 8);
694 s
->data
[s
->endp
++] = (uint8_t)q
;
699 int stream_putf(struct stream
*s
, float f
)
706 return stream_putl(s
, u
.o
);
709 int stream_putd(struct stream
*s
, double d
)
716 return stream_putq(s
, u
.o
);
719 int stream_putc_at(struct stream
*s
, size_t putp
, uint8_t c
)
721 STREAM_VERIFY_SANE(s
);
723 if (!PUT_AT_VALID(s
, putp
+ sizeof(uint8_t))) {
724 STREAM_BOUND_WARN(s
, "put");
733 int stream_putw_at(struct stream
*s
, size_t putp
, uint16_t w
)
735 STREAM_VERIFY_SANE(s
);
737 if (!PUT_AT_VALID(s
, putp
+ sizeof(uint16_t))) {
738 STREAM_BOUND_WARN(s
, "put");
742 s
->data
[putp
] = (uint8_t)(w
>> 8);
743 s
->data
[putp
+ 1] = (uint8_t)w
;
748 int stream_put3_at(struct stream
*s
, size_t putp
, uint32_t l
)
750 STREAM_VERIFY_SANE(s
);
752 if (!PUT_AT_VALID(s
, putp
+ 3)) {
753 STREAM_BOUND_WARN(s
, "put");
756 s
->data
[putp
] = (uint8_t)(l
>> 16);
757 s
->data
[putp
+ 1] = (uint8_t)(l
>> 8);
758 s
->data
[putp
+ 2] = (uint8_t)l
;
763 int stream_putl_at(struct stream
*s
, size_t putp
, uint32_t l
)
765 STREAM_VERIFY_SANE(s
);
767 if (!PUT_AT_VALID(s
, putp
+ sizeof(uint32_t))) {
768 STREAM_BOUND_WARN(s
, "put");
771 s
->data
[putp
] = (uint8_t)(l
>> 24);
772 s
->data
[putp
+ 1] = (uint8_t)(l
>> 16);
773 s
->data
[putp
+ 2] = (uint8_t)(l
>> 8);
774 s
->data
[putp
+ 3] = (uint8_t)l
;
779 int stream_putq_at(struct stream
*s
, size_t putp
, uint64_t q
)
781 STREAM_VERIFY_SANE(s
);
783 if (!PUT_AT_VALID(s
, putp
+ sizeof(uint64_t))) {
784 STREAM_BOUND_WARN(s
, "put");
787 s
->data
[putp
] = (uint8_t)(q
>> 56);
788 s
->data
[putp
+ 1] = (uint8_t)(q
>> 48);
789 s
->data
[putp
+ 2] = (uint8_t)(q
>> 40);
790 s
->data
[putp
+ 3] = (uint8_t)(q
>> 32);
791 s
->data
[putp
+ 4] = (uint8_t)(q
>> 24);
792 s
->data
[putp
+ 5] = (uint8_t)(q
>> 16);
793 s
->data
[putp
+ 6] = (uint8_t)(q
>> 8);
794 s
->data
[putp
+ 7] = (uint8_t)q
;
799 /* Put long word to the stream. */
800 int stream_put_ipv4(struct stream
*s
, uint32_t l
)
802 STREAM_VERIFY_SANE(s
);
804 if (STREAM_WRITEABLE(s
) < sizeof(uint32_t)) {
805 STREAM_BOUND_WARN(s
, "put");
808 memcpy(s
->data
+ s
->endp
, &l
, sizeof(uint32_t));
809 s
->endp
+= sizeof(uint32_t);
811 return sizeof(uint32_t);
814 /* Put long word to the stream. */
815 int stream_put_in_addr(struct stream
*s
, const struct in_addr
*addr
)
817 STREAM_VERIFY_SANE(s
);
819 if (STREAM_WRITEABLE(s
) < sizeof(uint32_t)) {
820 STREAM_BOUND_WARN(s
, "put");
824 memcpy(s
->data
+ s
->endp
, addr
, sizeof(uint32_t));
825 s
->endp
+= sizeof(uint32_t);
827 return sizeof(uint32_t);
830 /* Put in_addr at location in the stream. */
831 int stream_put_in_addr_at(struct stream
*s
, size_t putp
,
832 const struct in_addr
*addr
)
834 STREAM_VERIFY_SANE(s
);
836 if (!PUT_AT_VALID(s
, putp
+ 4)) {
837 STREAM_BOUND_WARN(s
, "put");
841 memcpy(&s
->data
[putp
], addr
, 4);
845 /* Put in6_addr at location in the stream. */
846 int stream_put_in6_addr_at(struct stream
*s
, size_t putp
,
847 const struct in6_addr
*addr
)
849 STREAM_VERIFY_SANE(s
);
851 if (!PUT_AT_VALID(s
, putp
+ 16)) {
852 STREAM_BOUND_WARN(s
, "put");
856 memcpy(&s
->data
[putp
], addr
, 16);
860 /* Put prefix by nlri type format. */
861 int stream_put_prefix_addpath(struct stream
*s
, const struct prefix
*p
,
862 int addpath_encode
, uint32_t addpath_tx_id
)
865 size_t psize_with_addpath
;
867 STREAM_VERIFY_SANE(s
);
869 psize
= PSIZE(p
->prefixlen
);
872 psize_with_addpath
= psize
+ 4;
874 psize_with_addpath
= psize
;
876 if (STREAM_WRITEABLE(s
) < (psize_with_addpath
+ sizeof(uint8_t))) {
877 STREAM_BOUND_WARN(s
, "put");
881 if (addpath_encode
) {
882 s
->data
[s
->endp
++] = (uint8_t)(addpath_tx_id
>> 24);
883 s
->data
[s
->endp
++] = (uint8_t)(addpath_tx_id
>> 16);
884 s
->data
[s
->endp
++] = (uint8_t)(addpath_tx_id
>> 8);
885 s
->data
[s
->endp
++] = (uint8_t)addpath_tx_id
;
888 s
->data
[s
->endp
++] = p
->prefixlen
;
889 memcpy(s
->data
+ s
->endp
, &p
->u
.prefix
, psize
);
895 int stream_put_prefix(struct stream
*s
, const struct prefix
*p
)
897 return stream_put_prefix_addpath(s
, p
, 0, 0);
900 /* Put NLRI with label */
901 int stream_put_labeled_prefix(struct stream
*s
, struct prefix
*p
,
902 mpls_label_t
*label
, int addpath_encode
,
903 uint32_t addpath_tx_id
)
906 size_t psize_with_addpath
;
907 uint8_t *label_pnt
= (uint8_t *)label
;
909 STREAM_VERIFY_SANE(s
);
911 psize
= PSIZE(p
->prefixlen
);
912 psize_with_addpath
= psize
+ (addpath_encode
? 4 : 0);
914 if (STREAM_WRITEABLE(s
) < (psize_with_addpath
+ 3)) {
915 STREAM_BOUND_WARN(s
, "put");
919 if (addpath_encode
) {
920 s
->data
[s
->endp
++] = (uint8_t)(addpath_tx_id
>> 24);
921 s
->data
[s
->endp
++] = (uint8_t)(addpath_tx_id
>> 16);
922 s
->data
[s
->endp
++] = (uint8_t)(addpath_tx_id
>> 8);
923 s
->data
[s
->endp
++] = (uint8_t)addpath_tx_id
;
926 stream_putc(s
, (p
->prefixlen
+ 24));
927 stream_putc(s
, label_pnt
[0]);
928 stream_putc(s
, label_pnt
[1]);
929 stream_putc(s
, label_pnt
[2]);
930 memcpy(s
->data
+ s
->endp
, &p
->u
.prefix
, psize
);
936 /* Read size from fd. */
937 int stream_read(struct stream
*s
, int fd
, size_t size
)
941 STREAM_VERIFY_SANE(s
);
943 if (STREAM_WRITEABLE(s
) < size
) {
944 STREAM_BOUND_WARN(s
, "put");
948 nbytes
= readn(fd
, s
->data
+ s
->endp
, size
);
956 ssize_t
stream_read_try(struct stream
*s
, int fd
, size_t size
)
960 STREAM_VERIFY_SANE(s
);
962 if (STREAM_WRITEABLE(s
) < size
) {
963 STREAM_BOUND_WARN(s
, "put");
964 /* Fatal (not transient) error, since retrying will not help
965 (stream is too small to contain the desired data). */
969 if ((nbytes
= read(fd
, s
->data
+ s
->endp
, size
)) >= 0) {
973 /* Error: was it transient (return -2) or fatal (return -1)? */
974 if (ERRNO_IO_RETRY(errno
))
976 flog_err(EC_LIB_SOCKET
, "%s: read failed on fd %d: %s", __func__
, fd
,
977 safe_strerror(errno
));
981 /* Read up to size bytes into the stream from the fd, using recvmsgfrom
982 * whose arguments match the remaining arguments to this function
984 ssize_t
stream_recvfrom(struct stream
*s
, int fd
, size_t size
, int flags
,
985 struct sockaddr
*from
, socklen_t
*fromlen
)
989 STREAM_VERIFY_SANE(s
);
991 if (STREAM_WRITEABLE(s
) < size
) {
992 STREAM_BOUND_WARN(s
, "put");
993 /* Fatal (not transient) error, since retrying will not help
994 (stream is too small to contain the desired data). */
998 if ((nbytes
= recvfrom(fd
, s
->data
+ s
->endp
, size
, flags
, from
,
1004 /* Error: was it transient (return -2) or fatal (return -1)? */
1005 if (ERRNO_IO_RETRY(errno
))
1007 flog_err(EC_LIB_SOCKET
, "%s: read failed on fd %d: %s", __func__
, fd
,
1008 safe_strerror(errno
));
1012 /* Read up to smaller of size or SIZE_REMAIN() bytes to the stream, starting
1014 * First iovec will be used to receive the data.
1015 * Stream need not be empty.
1017 ssize_t
stream_recvmsg(struct stream
*s
, int fd
, struct msghdr
*msgh
, int flags
,
1023 STREAM_VERIFY_SANE(s
);
1024 assert(msgh
->msg_iovlen
> 0);
1026 if (STREAM_WRITEABLE(s
) < size
) {
1027 STREAM_BOUND_WARN(s
, "put");
1028 /* This is a logic error in the calling code: the stream is too
1030 to hold the desired data! */
1034 iov
= &(msgh
->msg_iov
[0]);
1035 iov
->iov_base
= (s
->data
+ s
->endp
);
1036 iov
->iov_len
= size
;
1038 nbytes
= recvmsg(fd
, msgh
, flags
);
1046 /* Write data to buffer. */
1047 size_t stream_write(struct stream
*s
, const void *ptr
, size_t size
)
1050 CHECK_SIZE(s
, size
);
1052 STREAM_VERIFY_SANE(s
);
1054 if (STREAM_WRITEABLE(s
) < size
) {
1055 STREAM_BOUND_WARN(s
, "put");
1059 memcpy(s
->data
+ s
->endp
, ptr
, size
);
1065 /* Return current read pointer.
1067 * Use stream_get_pnt_to if you must, but decoding streams properly
1070 uint8_t *stream_pnt(struct stream
*s
)
1072 STREAM_VERIFY_SANE(s
);
1073 return s
->data
+ s
->getp
;
1076 /* Check does this stream empty? */
1077 int stream_empty(struct stream
*s
)
1079 STREAM_VERIFY_SANE(s
);
1081 return (s
->endp
== 0);
1085 void stream_reset(struct stream
*s
)
1087 STREAM_VERIFY_SANE(s
);
1089 s
->getp
= s
->endp
= 0;
1092 /* Write stream contens to the file discriptor. */
1093 int stream_flush(struct stream
*s
, int fd
)
1097 STREAM_VERIFY_SANE(s
);
1099 nbytes
= write(fd
, s
->data
+ s
->getp
, s
->endp
- s
->getp
);
1104 /* Stream first in first out queue. */
1106 struct stream_fifo
*stream_fifo_new(void)
1108 struct stream_fifo
*new;
1110 new = XCALLOC(MTYPE_STREAM_FIFO
, sizeof(struct stream_fifo
));
1111 pthread_mutex_init(&new->mtx
, NULL
);
1115 /* Add new stream to fifo. */
1116 void stream_fifo_push(struct stream_fifo
*fifo
, struct stream
*s
)
1118 #if defined DEV_BUILD
1123 fifo
->tail
->next
= s
;
1128 fifo
->tail
->next
= NULL
;
1129 #if !defined DEV_BUILD
1130 atomic_fetch_add_explicit(&fifo
->count
, 1, memory_order_release
);
1132 max
= atomic_fetch_add_explicit(&fifo
->count
, 1, memory_order_release
);
1133 curmax
= atomic_load_explicit(&fifo
->max_count
, memory_order_relaxed
);
1135 atomic_store_explicit(&fifo
->max_count
, max
,
1136 memory_order_relaxed
);
1140 void stream_fifo_push_safe(struct stream_fifo
*fifo
, struct stream
*s
)
1142 frr_with_mutex(&fifo
->mtx
) {
1143 stream_fifo_push(fifo
, s
);
1147 /* Delete first stream from fifo. */
1148 struct stream
*stream_fifo_pop(struct stream_fifo
*fifo
)
1155 fifo
->head
= s
->next
;
1157 if (fifo
->head
== NULL
)
1160 atomic_fetch_sub_explicit(&fifo
->count
, 1,
1161 memory_order_release
);
1163 /* ensure stream is scrubbed of references to this fifo */
1170 struct stream
*stream_fifo_pop_safe(struct stream_fifo
*fifo
)
1174 frr_with_mutex(&fifo
->mtx
) {
1175 ret
= stream_fifo_pop(fifo
);
1181 struct stream
*stream_fifo_head(struct stream_fifo
*fifo
)
1186 struct stream
*stream_fifo_head_safe(struct stream_fifo
*fifo
)
1190 frr_with_mutex(&fifo
->mtx
) {
1191 ret
= stream_fifo_head(fifo
);
1197 void stream_fifo_clean(struct stream_fifo
*fifo
)
1200 struct stream
*next
;
1202 for (s
= fifo
->head
; s
; s
= next
) {
1206 fifo
->head
= fifo
->tail
= NULL
;
1207 atomic_store_explicit(&fifo
->count
, 0, memory_order_release
);
1210 void stream_fifo_clean_safe(struct stream_fifo
*fifo
)
1212 frr_with_mutex(&fifo
->mtx
) {
1213 stream_fifo_clean(fifo
);
1217 size_t stream_fifo_count_safe(struct stream_fifo
*fifo
)
1219 return atomic_load_explicit(&fifo
->count
, memory_order_acquire
);
1222 void stream_fifo_free(struct stream_fifo
*fifo
)
1224 stream_fifo_clean(fifo
);
1225 pthread_mutex_destroy(&fifo
->mtx
);
1226 XFREE(MTYPE_STREAM_FIFO
, fifo
);