]>
git.proxmox.com Git - mirror_frr.git/blob - lib/stream.h
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
22 #ifndef _ZEBRA_STREAM_H
23 #define _ZEBRA_STREAM_H
27 #include "frratomic.h"
36 * A stream is an arbitrary buffer, whose contents generally are assumed to
37 * be in network order.
39 * A stream has the following attributes associated with it:
41 * - size: the allocated, invariant size of the buffer.
43 * - getp: the get position marker, denoting the offset in the stream where
44 * the next read (or 'get') will be from. This getp marker is
45 * automatically adjusted when data is read from the stream, the
46 * user may also manipulate this offset as they wish, within limits
49 * - endp: the end position marker, denoting the offset in the stream where
50 * valid data ends, and if the user attempted to write (or
51 * 'put') data where that data would be written (or 'put') to.
53 * These attributes are all size_t values.
57 * 1. getp can never exceed endp
59 * - hence if getp is equal to endp, there is no more valid data that can be
60 * gotten from the stream (though, the user may reposition getp to earlier in
61 * the stream, if they wish).
63 * 2. endp can never exceed size
65 * - hence, if endp is equal to size, then the stream is full, and no more
66 * data can be written to the stream.
68 * In other words the following must always be true, and the stream
69 * abstraction is allowed internally to assert that the following property
70 * holds true for a stream, as and when it wishes:
72 * getp <= endp <= size
74 * It is the users responsibility to ensure this property is never violated.
76 * A stream therefore can be thought of like this:
78 * ---------------------------------------------------
79 * |XXXXXXXXXXXXXXXXXXXXXXXX |
80 * ---------------------------------------------------
84 * This shows a stream containing data (shown as 'X') up to the endp offset.
85 * The stream is empty from endp to size. Without adjusting getp, there are
86 * still endp-getp bytes of valid data to be read from the stream.
88 * Methods are provided to get and put to/from the stream, as well as
89 * retrieve the values of the 3 markers and manipulate the getp marker.
92 * At the moment, newly allocated streams are zero filled. Hence, one can
93 * use stream_forward_endp() to effectively create arbitrary zero-fill
94 * padding. However, note that stream_reset() does *not* zero-out the
95 * stream. This property should **not** be relied upon.
97 * Best practice is to use stream_put (<stream *>, NULL, <size>) to zero out
98 * any part of a stream which isn't otherwise written to.
106 * Remainder is ***private*** to stream
107 * direct access is frowned upon!
108 * Use the appropriate functions/macros
110 size_t getp
; /* next get position */
111 size_t endp
; /* last valid data position */
112 size_t size
; /* size of data segment */
113 unsigned char data
[0]; /* data pointer */
116 /* First in first out queue structure. */
118 /* lock for mt-safe operations */
121 /* number of streams in this fifo */
123 #if defined DEV_BUILD
124 atomic_size_t max_count
;
131 /* Utility macros. */
132 #define STREAM_SIZE(S) ((S)->size)
133 /* number of bytes which can still be written */
134 #define STREAM_WRITEABLE(S) ((S)->size - (S)->endp)
135 /* number of bytes still to be read */
136 #define STREAM_READABLE(S) ((S)->endp - (S)->getp)
138 #define STREAM_CONCAT_REMAIN(S1, S2, size) ((size) - (S1)->endp - (S2)->endp)
140 /* this macro is deprecated, but not slated for removal anytime soon */
141 #define STREAM_DATA(S) ((S)->data)
143 /* Stream prototypes.
144 * For stream_{put,get}S, the S suffix mean:
146 * c: character (unsigned byte)
147 * w: word (two bytes)
148 * l: long (two words)
149 * q: quad (four words)
151 extern struct stream
*stream_new(size_t);
152 extern void stream_free(struct stream
*);
153 extern struct stream
*stream_copy(struct stream
*, struct stream
*src
);
154 extern struct stream
*stream_dup(struct stream
*);
156 #if CONFDATE > 20190821
157 CPP_NOTICE("lib: time to remove stream_resize_orig")
159 extern size_t stream_resize_orig(struct stream
*s
, size_t newsize
);
160 #define stream_resize stream_resize_orig
161 extern size_t stream_resize_inplace(struct stream
**sptr
, size_t newsize
);
163 extern size_t stream_get_getp(struct stream
*);
164 extern size_t stream_get_endp(struct stream
*);
165 extern size_t stream_get_size(struct stream
*);
166 extern uint8_t *stream_get_data(struct stream
*);
169 * Create a new stream structure; copy offset bytes from s1 to the new
170 * stream; copy s2 data to the new stream; copy rest of s1 data to the
173 extern struct stream
*stream_dupcat(struct stream
*s1
, struct stream
*s2
,
176 extern void stream_set_getp(struct stream
*, size_t);
177 extern void stream_set_endp(struct stream
*, size_t);
178 extern void stream_forward_getp(struct stream
*, size_t);
179 extern void stream_forward_endp(struct stream
*, size_t);
181 /* steam_put: NULL source zeroes out size_t bytes of stream */
182 extern void stream_put(struct stream
*, const void *, size_t);
183 extern int stream_putc(struct stream
*, uint8_t);
184 extern int stream_putc_at(struct stream
*, size_t, uint8_t);
185 extern int stream_putw(struct stream
*, uint16_t);
186 extern int stream_putw_at(struct stream
*, size_t, uint16_t);
187 extern int stream_put3(struct stream
*, uint32_t);
188 extern int stream_put3_at(struct stream
*, size_t, uint32_t);
189 extern int stream_putl(struct stream
*, uint32_t);
190 extern int stream_putl_at(struct stream
*, size_t, uint32_t);
191 extern int stream_putq(struct stream
*, uint64_t);
192 extern int stream_putq_at(struct stream
*, size_t, uint64_t);
193 extern int stream_put_ipv4(struct stream
*, uint32_t);
194 extern int stream_put_in_addr(struct stream
*, struct in_addr
*);
195 extern int stream_put_in_addr_at(struct stream
*, size_t, struct in_addr
*);
196 extern int stream_put_in6_addr_at(struct stream
*, size_t, struct in6_addr
*);
197 extern int stream_put_prefix_addpath(struct stream
*, struct prefix
*,
199 uint32_t addpath_tx_id
);
200 extern int stream_put_prefix(struct stream
*, struct prefix
*);
201 extern int stream_put_labeled_prefix(struct stream
*, struct prefix
*,
203 extern void stream_get(void *, struct stream
*, size_t);
204 extern bool stream_get2(void *data
, struct stream
*s
, size_t size
);
205 extern void stream_get_from(void *, struct stream
*, size_t, size_t);
206 extern uint8_t stream_getc(struct stream
*);
207 extern bool stream_getc2(struct stream
*s
, uint8_t *byte
);
208 extern uint8_t stream_getc_from(struct stream
*, size_t);
209 extern uint16_t stream_getw(struct stream
*);
210 extern bool stream_getw2(struct stream
*s
, uint16_t *word
);
211 extern uint16_t stream_getw_from(struct stream
*, size_t);
212 extern uint32_t stream_get3(struct stream
*);
213 extern uint32_t stream_get3_from(struct stream
*, size_t);
214 extern uint32_t stream_getl(struct stream
*);
215 extern bool stream_getl2(struct stream
*s
, uint32_t *l
);
216 extern uint32_t stream_getl_from(struct stream
*, size_t);
217 extern uint64_t stream_getq(struct stream
*);
218 extern uint64_t stream_getq_from(struct stream
*, size_t);
219 extern uint32_t stream_get_ipv4(struct stream
*);
221 /* IEEE-754 floats */
222 extern float stream_getf(struct stream
*);
223 extern double stream_getd(struct stream
*);
224 extern int stream_putf(struct stream
*, float);
225 extern int stream_putd(struct stream
*, double);
230 /* Deprecated: assumes blocking I/O. Will be removed.
231 Use stream_read_try instead. */
232 extern int stream_read(struct stream
*, int, size_t);
234 /* Read up to size bytes into the stream.
236 >0: number of bytes read
239 -2: transient error, should retry later (i.e. EAGAIN or EINTR)
240 This is suitable for use with non-blocking file descriptors.
242 extern ssize_t
stream_read_try(struct stream
*s
, int fd
, size_t size
);
244 extern ssize_t
stream_recvmsg(struct stream
*s
, int fd
, struct msghdr
*,
245 int flags
, size_t size
);
246 extern ssize_t
stream_recvfrom(struct stream
*s
, int fd
, size_t len
, int flags
,
247 struct sockaddr
*from
, socklen_t
*fromlen
);
248 extern size_t stream_write(struct stream
*, const void *, size_t);
250 /* reset the stream. See Note above */
251 extern void stream_reset(struct stream
*);
252 extern int stream_flush(struct stream
*, int);
253 extern int stream_empty(struct stream
*); /* is the stream empty? */
256 extern uint8_t *stream_pnt(struct stream
*);
259 * Operations on struct stream_fifo.
261 * Each function has a safe variant, which ensures that the operation performed
262 * is atomic with respect to the operations performed by all other safe
263 * variants. In other words, the safe variants lock the stream_fifo's mutex
264 * before performing their action. These are provided for convenience when
265 * using stream_fifo in a multithreaded context, to alleviate the need for the
266 * caller to implement their own synchronization around the stream_fifo.
268 * The following functions do not have safe variants. The caller must ensure
269 * that these operations are performed safely in a multithreaded context:
275 * Create a new stream_fifo.
278 * newly created stream_fifo
280 extern struct stream_fifo
*stream_fifo_new(void);
283 * Push a stream onto a stream_fifo.
286 * the stream_fifo to push onto
289 * the stream to push onto the stream_fifo
291 extern void stream_fifo_push(struct stream_fifo
*fifo
, struct stream
*s
);
292 extern void stream_fifo_push_safe(struct stream_fifo
*fifo
, struct stream
*s
);
295 * Pop a stream off a stream_fifo.
298 * the stream_fifo to pop from
301 * the next stream in the stream_fifo
303 extern struct stream
*stream_fifo_pop(struct stream_fifo
*fifo
);
304 extern struct stream
*stream_fifo_pop_safe(struct stream_fifo
*fifo
);
307 * Retrieve the next stream from a stream_fifo without popping it.
310 * the stream_fifo to operate on
313 * the next stream that would be returned from stream_fifo_pop
315 extern struct stream
*stream_fifo_head(struct stream_fifo
*fifo
);
316 extern struct stream
*stream_fifo_head_safe(struct stream_fifo
*fifo
);
319 * Remove all streams from a stream_fifo.
322 * the stream_fifo to clean
324 extern void stream_fifo_clean(struct stream_fifo
*fifo
);
325 extern void stream_fifo_clean_safe(struct stream_fifo
*fifo
);
328 * Retrieve number of streams on a stream_fifo.
331 * the stream_fifo to retrieve the count for
334 * the number of streams on the stream_fifo
336 extern size_t stream_fifo_count_safe(struct stream_fifo
*fifo
);
339 * Free a stream_fifo.
341 * Calls stream_fifo_clean, then deinitializes the stream_fifo and frees it.
344 * the stream_fifo to free
346 extern void stream_fifo_free(struct stream_fifo
*fifo
);
348 /* This is here because "<< 24" is particularly problematic in C.
349 * This is because the left operand of << is integer-promoted, which means
350 * an uint8_t gets converted into a *signed* int. Shifting into the sign
351 * bit of a signed int is theoretically undefined behaviour, so - the left
352 * operand needs to be cast to unsigned.
354 * This is not a problem for 16- or 8-bit values (they don't reach the sign
355 * bit), for 64-bit values (you need to cast them anyway), and neither for
356 * encoding (because it's downcasted.)
358 static inline uint8_t *ptr_get_be32(uint8_t *ptr
, uint32_t *out
)
361 memcpy(&tmp
, ptr
, sizeof(tmp
));
367 * so Normal stream_getX functions assert. Which is anathema
368 * to keeping a daemon up and running when something goes south
369 * Provide a stream_getX2 functions that do not assert.
370 * In addition provide these macro's that upon failure
371 * goto stream_failure. This is modeled upon some NL_XX
372 * macros in the linux kernel.
374 * This change allows for proper memory freeing
375 * after we've detected an error.
377 * In the future we will be removing the assert in
378 * the stream functions but we need a transition
381 #define STREAM_GETC(S, P) \
384 if (!stream_getc2((S), &_pval)) \
385 goto stream_failure; \
389 #define STREAM_GETW(S, P) \
392 if (!stream_getw2((S), &_pval)) \
393 goto stream_failure; \
397 #define STREAM_GETL(S, P) \
400 if (!stream_getl2((S), &_pval)) \
401 goto stream_failure; \
405 #define STREAM_GET(P, STR, SIZE) \
407 if (!stream_get2((P), (STR), (SIZE))) \
408 goto stream_failure; \
415 #endif /* _ZEBRA_STREAM_H */