]> git.proxmox.com Git - mirror_frr.git/blob - lib/stream.h
Merge pull request #7053 from mjstapp/fix_ospf6_intf_sa
[mirror_frr.git] / lib / stream.h
1 /*
2 * Packet interface
3 * Copyright (C) 1999 Kunihiro Ishiguro
4 *
5 * This file is part of GNU Zebra.
6 *
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
10 * later version.
11 *
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.
16 *
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
20 */
21
22 #ifndef _ZEBRA_STREAM_H
23 #define _ZEBRA_STREAM_H
24
25 #include <pthread.h>
26
27 #include "frratomic.h"
28 #include "mpls.h"
29 #include "prefix.h"
30
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34
35 /*
36 * A stream is an arbitrary buffer, whose contents generally are assumed to
37 * be in network order.
38 *
39 * A stream has the following attributes associated with it:
40 *
41 * - size: the allocated, invariant size of the buffer.
42 *
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
47 * (see below)
48 *
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.
52 *
53 * These attributes are all size_t values.
54 *
55 * Constraints:
56 *
57 * 1. getp can never exceed endp
58 *
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).
62 *
63 * 2. endp can never exceed size
64 *
65 * - hence, if endp is equal to size, then the stream is full, and no more
66 * data can be written to the stream.
67 *
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:
71 *
72 * getp <= endp <= size
73 *
74 * It is the users responsibility to ensure this property is never violated.
75 *
76 * A stream therefore can be thought of like this:
77 *
78 * ---------------------------------------------------
79 * |XXXXXXXXXXXXXXXXXXXXXXXX |
80 * ---------------------------------------------------
81 * ^ ^ ^
82 * getp endp size
83 *
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.
87 *
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.
90 *
91 * Note:
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.
96 *
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.
99 */
100
101 /* Stream buffer. */
102 struct stream {
103 struct stream *next;
104
105 /*
106 * Remainder is ***private*** to stream
107 * direct access is frowned upon!
108 * Use the appropriate functions/macros
109 */
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[]; /* data pointer */
114 };
115
116 /* First in first out queue structure. */
117 struct stream_fifo {
118 /* lock for mt-safe operations */
119 pthread_mutex_t mtx;
120
121 /* number of streams in this fifo */
122 atomic_size_t count;
123 #if defined DEV_BUILD
124 atomic_size_t max_count;
125 #endif
126
127 struct stream *head;
128 struct stream *tail;
129 };
130
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)
137
138 #define STREAM_CONCAT_REMAIN(S1, S2, size) ((size) - (S1)->endp - (S2)->endp)
139
140 /* this macro is deprecated, but not slated for removal anytime soon */
141 #define STREAM_DATA(S) ((S)->data)
142
143 /* Stream prototypes.
144 * For stream_{put,get}S, the S suffix mean:
145 *
146 * c: character (unsigned byte)
147 * w: word (two bytes)
148 * l: long (two words)
149 * q: quad (four words)
150 */
151 extern struct stream *stream_new(size_t);
152 extern void stream_free(struct stream *);
153 /* Copy 'src' into 'dest', returns 'dest' */
154 extern struct stream *stream_copy(struct stream *dest,
155 const struct stream *src);
156 extern struct stream *stream_dup(const struct stream *s);
157
158 extern size_t stream_resize_inplace(struct stream **sptr, size_t newsize);
159
160 extern size_t stream_get_getp(const struct stream *s);
161 extern size_t stream_get_endp(const struct stream *s);
162 extern size_t stream_get_size(const struct stream *s);
163
164 /**
165 * Create a new stream structure; copy offset bytes from s1 to the new
166 * stream; copy s2 data to the new stream; copy rest of s1 data to the
167 * new stream.
168 */
169 extern struct stream *stream_dupcat(const struct stream *s1,
170 const struct stream *s2, size_t offset);
171
172 extern void stream_set_getp(struct stream *, size_t);
173 extern void stream_set_endp(struct stream *, size_t);
174 extern void stream_forward_getp(struct stream *, size_t);
175 extern bool stream_forward_getp2(struct stream *, size_t);
176 extern void stream_forward_endp(struct stream *, size_t);
177 extern bool stream_forward_endp2(struct stream *, size_t);
178
179 /* steam_put: NULL source zeroes out size_t bytes of stream */
180 extern void stream_put(struct stream *, const void *, size_t);
181 extern int stream_putc(struct stream *, uint8_t);
182 extern int stream_putc_at(struct stream *, size_t, uint8_t);
183 extern int stream_putw(struct stream *, uint16_t);
184 extern int stream_putw_at(struct stream *, size_t, uint16_t);
185 extern int stream_put3(struct stream *, uint32_t);
186 extern int stream_put3_at(struct stream *, size_t, uint32_t);
187 extern int stream_putl(struct stream *, uint32_t);
188 extern int stream_putl_at(struct stream *, size_t, uint32_t);
189 extern int stream_putq(struct stream *, uint64_t);
190 extern int stream_putq_at(struct stream *, size_t, uint64_t);
191 extern int stream_put_ipv4(struct stream *, uint32_t);
192 extern int stream_put_in_addr(struct stream *s, const struct in_addr *addr);
193 extern bool stream_put_ipaddr(struct stream *s, struct ipaddr *ip);
194 extern int stream_put_in_addr_at(struct stream *s, size_t putp,
195 const struct in_addr *addr);
196 extern int stream_put_in6_addr_at(struct stream *s, size_t putp,
197 const struct in6_addr *addr);
198 extern int stream_put_prefix_addpath(struct stream *s,
199 const struct prefix *p,
200 int addpath_encode,
201 uint32_t addpath_tx_id);
202 extern int stream_put_prefix(struct stream *s, const struct prefix *p);
203 extern int stream_put_labeled_prefix(struct stream *, const struct prefix *,
204 mpls_label_t *, int addpath_encode,
205 uint32_t addpath_tx_id);
206 extern void stream_get(void *, struct stream *, size_t);
207 extern bool stream_get2(void *data, struct stream *s, size_t size);
208 extern void stream_get_from(void *, struct stream *, size_t, size_t);
209 extern uint8_t stream_getc(struct stream *);
210 extern bool stream_getc2(struct stream *s, uint8_t *byte);
211 extern uint8_t stream_getc_from(struct stream *, size_t);
212 extern uint16_t stream_getw(struct stream *);
213 extern bool stream_getw2(struct stream *s, uint16_t *word);
214 extern uint16_t stream_getw_from(struct stream *, size_t);
215 extern uint32_t stream_get3(struct stream *);
216 extern uint32_t stream_get3_from(struct stream *, size_t);
217 extern uint32_t stream_getl(struct stream *);
218 extern bool stream_getl2(struct stream *s, uint32_t *l);
219 extern uint32_t stream_getl_from(struct stream *, size_t);
220 extern uint64_t stream_getq(struct stream *);
221 extern uint64_t stream_getq_from(struct stream *, size_t);
222 bool stream_getq2(struct stream *s, uint64_t *q);
223 extern uint32_t stream_get_ipv4(struct stream *);
224 extern bool stream_get_ipaddr(struct stream *s, struct ipaddr *ip);
225
226 /* IEEE-754 floats */
227 extern float stream_getf(struct stream *);
228 extern double stream_getd(struct stream *);
229 extern int stream_putf(struct stream *, float);
230 extern int stream_putd(struct stream *, double);
231
232 #undef stream_read
233 #undef stream_write
234
235 /* Deprecated: assumes blocking I/O. Will be removed.
236 Use stream_read_try instead. */
237 extern int stream_read(struct stream *, int, size_t);
238
239 /* Read up to size bytes into the stream.
240 Return code:
241 >0: number of bytes read
242 0: end-of-file
243 -1: fatal error
244 -2: transient error, should retry later (i.e. EAGAIN or EINTR)
245 This is suitable for use with non-blocking file descriptors.
246 */
247 extern ssize_t stream_read_try(struct stream *s, int fd, size_t size);
248
249 extern ssize_t stream_recvmsg(struct stream *s, int fd, struct msghdr *,
250 int flags, size_t size);
251 extern ssize_t stream_recvfrom(struct stream *s, int fd, size_t len, int flags,
252 struct sockaddr *from, socklen_t *fromlen);
253 extern size_t stream_write(struct stream *, const void *, size_t);
254
255 /* reset the stream. See Note above */
256 extern void stream_reset(struct stream *);
257 extern int stream_flush(struct stream *, int);
258 extern int stream_empty(struct stream *); /* is the stream empty? */
259
260 /* debugging */
261 extern void stream_hexdump(const struct stream *s);
262
263 /* deprecated */
264 extern uint8_t *stream_pnt(struct stream *);
265
266 /*
267 * Operations on struct stream_fifo.
268 *
269 * Each function has a safe variant, which ensures that the operation performed
270 * is atomic with respect to the operations performed by all other safe
271 * variants. In other words, the safe variants lock the stream_fifo's mutex
272 * before performing their action. These are provided for convenience when
273 * using stream_fifo in a multithreaded context, to alleviate the need for the
274 * caller to implement their own synchronization around the stream_fifo.
275 *
276 * The following functions do not have safe variants. The caller must ensure
277 * that these operations are performed safely in a multithreaded context:
278 * - stream_fifo_new
279 * - stream_fifo_free
280 */
281
282 /*
283 * Create a new stream_fifo.
284 *
285 * Returns:
286 * newly created stream_fifo
287 */
288 extern struct stream_fifo *stream_fifo_new(void);
289
290 /*
291 * Init or re-init an on-stack fifo. This allows use of a fifo struct without
292 * requiring a malloc/free cycle.
293 * Note well that the fifo must be de-inited with the 'fifo_deinit' api.
294 */
295 void stream_fifo_init(struct stream_fifo *fifo);
296
297 /*
298 * Deinit an on-stack fifo.
299 */
300 void stream_fifo_deinit(struct stream_fifo *fifo);
301
302 /*
303 * Push a stream onto a stream_fifo.
304 *
305 * fifo
306 * the stream_fifo to push onto
307 *
308 * s
309 * the stream to push onto the stream_fifo
310 */
311 extern void stream_fifo_push(struct stream_fifo *fifo, struct stream *s);
312 extern void stream_fifo_push_safe(struct stream_fifo *fifo, struct stream *s);
313
314 /*
315 * Pop a stream off a stream_fifo.
316 *
317 * fifo
318 * the stream_fifo to pop from
319 *
320 * Returns:
321 * the next stream in the stream_fifo
322 */
323 extern struct stream *stream_fifo_pop(struct stream_fifo *fifo);
324 extern struct stream *stream_fifo_pop_safe(struct stream_fifo *fifo);
325
326 /*
327 * Retrieve the next stream from a stream_fifo without popping it.
328 *
329 * fifo
330 * the stream_fifo to operate on
331 *
332 * Returns:
333 * the next stream that would be returned from stream_fifo_pop
334 */
335 extern struct stream *stream_fifo_head(struct stream_fifo *fifo);
336 extern struct stream *stream_fifo_head_safe(struct stream_fifo *fifo);
337
338 /*
339 * Remove all streams from a stream_fifo.
340 *
341 * fifo
342 * the stream_fifo to clean
343 */
344 extern void stream_fifo_clean(struct stream_fifo *fifo);
345 extern void stream_fifo_clean_safe(struct stream_fifo *fifo);
346
347 /*
348 * Retrieve number of streams on a stream_fifo.
349 *
350 * fifo
351 * the stream_fifo to retrieve the count for
352 *
353 * Returns:
354 * the number of streams on the stream_fifo
355 */
356 extern size_t stream_fifo_count_safe(struct stream_fifo *fifo);
357
358 /*
359 * Free a stream_fifo.
360 *
361 * Calls stream_fifo_clean, then deinitializes the stream_fifo and frees it.
362 *
363 * fifo
364 * the stream_fifo to free
365 */
366 extern void stream_fifo_free(struct stream_fifo *fifo);
367
368 /* This is here because "<< 24" is particularly problematic in C.
369 * This is because the left operand of << is integer-promoted, which means
370 * an uint8_t gets converted into a *signed* int. Shifting into the sign
371 * bit of a signed int is theoretically undefined behaviour, so - the left
372 * operand needs to be cast to unsigned.
373 *
374 * This is not a problem for 16- or 8-bit values (they don't reach the sign
375 * bit), for 64-bit values (you need to cast them anyway), and neither for
376 * encoding (because it's downcasted.)
377 */
378 static inline const uint8_t *ptr_get_be32(const uint8_t *ptr, uint32_t *out)
379 {
380 uint32_t tmp;
381
382 memcpy(&tmp, ptr, sizeof(tmp));
383 *out = ntohl(tmp);
384 return ptr + 4;
385 }
386
387 /*
388 * so Normal stream_getX functions assert. Which is anathema
389 * to keeping a daemon up and running when something goes south
390 * Provide a stream_getX2 functions that do not assert.
391 * In addition provide these macro's that upon failure
392 * goto stream_failure. This is modeled upon some NL_XX
393 * macros in the linux kernel.
394 *
395 * This change allows for proper memory freeing
396 * after we've detected an error.
397 *
398 * In the future we will be removing the assert in
399 * the stream functions but we need a transition
400 * plan.
401 */
402 #define STREAM_GETC(S, P) \
403 do { \
404 uint8_t _pval; \
405 if (!stream_getc2((S), &_pval)) \
406 goto stream_failure; \
407 (P) = _pval; \
408 } while (0)
409
410 #define STREAM_GETW(S, P) \
411 do { \
412 uint16_t _pval; \
413 if (!stream_getw2((S), &_pval)) \
414 goto stream_failure; \
415 (P) = _pval; \
416 } while (0)
417
418 #define STREAM_GETL(S, P) \
419 do { \
420 uint32_t _pval; \
421 if (!stream_getl2((S), &_pval)) \
422 goto stream_failure; \
423 (P) = _pval; \
424 } while (0)
425
426 #define STREAM_GETF(S, P) \
427 do { \
428 union { \
429 float r; \
430 uint32_t d; \
431 } _pval; \
432 if (!stream_getl2((S), &_pval.d)) \
433 goto stream_failure; \
434 (P) = _pval.r; \
435 } while (0)
436
437 #define STREAM_GETQ(S, P) \
438 do { \
439 uint64_t _pval; \
440 if (!stream_getq2((S), &_pval)) \
441 goto stream_failure; \
442 (P) = _pval; \
443 } while (0)
444
445 #define STREAM_GET_IPADDR(S, P) \
446 do { \
447 if (!stream_get_ipaddr((S), (P))) \
448 goto stream_failure; \
449 } while (0)
450
451 #define STREAM_GET(P, STR, SIZE) \
452 do { \
453 if (!stream_get2((P), (STR), (SIZE))) \
454 goto stream_failure; \
455 } while (0)
456
457 #define STREAM_FORWARD_GETP(STR, SIZE) \
458 do { \
459 if (!stream_forward_getp2((STR), (SIZE))) \
460 goto stream_failure; \
461 } while (0)
462
463 #define STREAM_FORWARD_ENDP(STR, SIZE) \
464 do { \
465 if (!stream_forward_endp2((STR), (SIZE))) \
466 goto stream_failure; \
467 } while (0)
468
469 #ifdef __cplusplus
470 }
471 #endif
472
473 #endif /* _ZEBRA_STREAM_H */