]> git.proxmox.com Git - mirror_frr.git/blob - lib/stream.h
Merge pull request #2516 from rubenk/more-spec-cleanups
[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 /*
32 * A stream is an arbitrary buffer, whose contents generally are assumed to
33 * be in network order.
34 *
35 * A stream has the following attributes associated with it:
36 *
37 * - size: the allocated, invariant size of the buffer.
38 *
39 * - getp: the get position marker, denoting the offset in the stream where
40 * the next read (or 'get') will be from. This getp marker is
41 * automatically adjusted when data is read from the stream, the
42 * user may also manipulate this offset as they wish, within limits
43 * (see below)
44 *
45 * - endp: the end position marker, denoting the offset in the stream where
46 * valid data ends, and if the user attempted to write (or
47 * 'put') data where that data would be written (or 'put') to.
48 *
49 * These attributes are all size_t values.
50 *
51 * Constraints:
52 *
53 * 1. getp can never exceed endp
54 *
55 * - hence if getp is equal to endp, there is no more valid data that can be
56 * gotten from the stream (though, the user may reposition getp to earlier in
57 * the stream, if they wish).
58 *
59 * 2. endp can never exceed size
60 *
61 * - hence, if endp is equal to size, then the stream is full, and no more
62 * data can be written to the stream.
63 *
64 * In other words the following must always be true, and the stream
65 * abstraction is allowed internally to assert that the following property
66 * holds true for a stream, as and when it wishes:
67 *
68 * getp <= endp <= size
69 *
70 * It is the users responsibility to ensure this property is never violated.
71 *
72 * A stream therefore can be thought of like this:
73 *
74 * ---------------------------------------------------
75 * |XXXXXXXXXXXXXXXXXXXXXXXX |
76 * ---------------------------------------------------
77 * ^ ^ ^
78 * getp endp size
79 *
80 * This shows a stream containing data (shown as 'X') up to the endp offset.
81 * The stream is empty from endp to size. Without adjusting getp, there are
82 * still endp-getp bytes of valid data to be read from the stream.
83 *
84 * Methods are provided to get and put to/from the stream, as well as
85 * retrieve the values of the 3 markers and manipulate the getp marker.
86 *
87 * Note:
88 * At the moment, newly allocated streams are zero filled. Hence, one can
89 * use stream_forward_endp() to effectively create arbitrary zero-fill
90 * padding. However, note that stream_reset() does *not* zero-out the
91 * stream. This property should **not** be relied upon.
92 *
93 * Best practice is to use stream_put (<stream *>, NULL, <size>) to zero out
94 * any part of a stream which isn't otherwise written to.
95 */
96
97 /* Stream buffer. */
98 struct stream {
99 struct stream *next;
100
101 /* Remainder is ***private*** to stream
102 * direct access is frowned upon!
103 * Use the appropriate functions/macros
104 */
105 size_t getp; /* next get position */
106 size_t endp; /* last valid data position */
107 size_t size; /* size of data segment */
108 unsigned char *data; /* data pointer */
109 };
110
111 /* First in first out queue structure. */
112 struct stream_fifo {
113 /* lock for mt-safe operations */
114 pthread_mutex_t mtx;
115
116 /* number of streams in this fifo */
117 _Atomic size_t count;
118 #if defined DEV_BUILD
119 _Atomic size_t max_count;
120 #endif
121
122 struct stream *head;
123 struct stream *tail;
124 };
125
126 /* Utility macros. */
127 #define STREAM_SIZE(S) ((S)->size)
128 /* number of bytes which can still be written */
129 #define STREAM_WRITEABLE(S) ((S)->size - (S)->endp)
130 /* number of bytes still to be read */
131 #define STREAM_READABLE(S) ((S)->endp - (S)->getp)
132
133 #define STREAM_CONCAT_REMAIN(S1, S2, size) ((size) - (S1)->endp - (S2)->endp)
134
135 /* deprecated macros - do not use in new code */
136 #if defined(VERSION_TYPE_DEV) && CONFDATE > 20181128
137 CPP_NOTICE("lib: time to remove deprecated stream.h macros")
138 #endif
139 #define STREAM_PNT(S) stream_pnt((S))
140 #define STREAM_REMAIN(S) STREAM_WRITEABLE((S))
141
142 /* this macro is deprecated, but not slated for removal anytime soon */
143 #define STREAM_DATA(S) ((S)->data)
144
145 /* Stream prototypes.
146 * For stream_{put,get}S, the S suffix mean:
147 *
148 * c: character (unsigned byte)
149 * w: word (two bytes)
150 * l: long (two words)
151 * q: quad (four words)
152 */
153 extern struct stream *stream_new(size_t);
154 extern void stream_free(struct stream *);
155 extern struct stream *stream_copy(struct stream *, struct stream *src);
156 extern struct stream *stream_dup(struct stream *);
157 extern size_t stream_resize(struct stream *, size_t);
158 extern size_t stream_get_getp(struct stream *);
159 extern size_t stream_get_endp(struct stream *);
160 extern size_t stream_get_size(struct stream *);
161 extern uint8_t *stream_get_data(struct stream *);
162
163 /**
164 * Create a new stream structure; copy offset bytes from s1 to the new
165 * stream; copy s2 data to the new stream; copy rest of s1 data to the
166 * new stream.
167 */
168 extern struct stream *stream_dupcat(struct stream *s1, struct stream *s2,
169 size_t offset);
170
171 extern void stream_set_getp(struct stream *, size_t);
172 extern void stream_set_endp(struct stream *, size_t);
173 extern void stream_forward_getp(struct stream *, size_t);
174 extern void stream_forward_endp(struct stream *, size_t);
175
176 /* steam_put: NULL source zeroes out size_t bytes of stream */
177 extern void stream_put(struct stream *, const void *, size_t);
178 extern int stream_putc(struct stream *, uint8_t);
179 extern int stream_putc_at(struct stream *, size_t, uint8_t);
180 extern int stream_putw(struct stream *, uint16_t);
181 extern int stream_putw_at(struct stream *, size_t, uint16_t);
182 extern int stream_put3(struct stream *, uint32_t);
183 extern int stream_put3_at(struct stream *, size_t, uint32_t);
184 extern int stream_putl(struct stream *, uint32_t);
185 extern int stream_putl_at(struct stream *, size_t, uint32_t);
186 extern int stream_putq(struct stream *, uint64_t);
187 extern int stream_putq_at(struct stream *, size_t, uint64_t);
188 extern int stream_put_ipv4(struct stream *, uint32_t);
189 extern int stream_put_in_addr(struct stream *, struct in_addr *);
190 extern int stream_put_in_addr_at(struct stream *, size_t, struct in_addr *);
191 extern int stream_put_in6_addr_at(struct stream *, size_t, struct in6_addr *);
192 extern int stream_put_prefix_addpath(struct stream *, struct prefix *,
193 int addpath_encode,
194 uint32_t addpath_tx_id);
195 extern int stream_put_prefix(struct stream *, struct prefix *);
196 extern int stream_put_labeled_prefix(struct stream *, struct prefix *,
197 mpls_label_t *);
198 extern void stream_get(void *, struct stream *, size_t);
199 extern bool stream_get2(void *data, struct stream *s, size_t size);
200 extern void stream_get_from(void *, struct stream *, size_t, size_t);
201 extern uint8_t stream_getc(struct stream *);
202 extern bool stream_getc2(struct stream *s, uint8_t *byte);
203 extern uint8_t stream_getc_from(struct stream *, size_t);
204 extern uint16_t stream_getw(struct stream *);
205 extern bool stream_getw2(struct stream *s, uint16_t *word);
206 extern uint16_t stream_getw_from(struct stream *, size_t);
207 extern uint32_t stream_get3(struct stream *);
208 extern uint32_t stream_get3_from(struct stream *, size_t);
209 extern uint32_t stream_getl(struct stream *);
210 extern bool stream_getl2(struct stream *s, uint32_t *l);
211 extern uint32_t stream_getl_from(struct stream *, size_t);
212 extern uint64_t stream_getq(struct stream *);
213 extern uint64_t stream_getq_from(struct stream *, size_t);
214 extern uint32_t stream_get_ipv4(struct stream *);
215
216 /* IEEE-754 floats */
217 extern float stream_getf(struct stream *);
218 extern double stream_getd(struct stream *);
219 extern int stream_putf(struct stream *, float);
220 extern int stream_putd(struct stream *, double);
221
222 #undef stream_read
223 #undef stream_write
224
225 /* Deprecated: assumes blocking I/O. Will be removed.
226 Use stream_read_try instead. */
227 extern int stream_read(struct stream *, int, size_t);
228
229 /* Read up to size bytes into the stream.
230 Return code:
231 >0: number of bytes read
232 0: end-of-file
233 -1: fatal error
234 -2: transient error, should retry later (i.e. EAGAIN or EINTR)
235 This is suitable for use with non-blocking file descriptors.
236 */
237 extern ssize_t stream_read_try(struct stream *s, int fd, size_t size);
238
239 extern ssize_t stream_recvmsg(struct stream *s, int fd, struct msghdr *,
240 int flags, size_t size);
241 extern ssize_t stream_recvfrom(struct stream *s, int fd, size_t len, int flags,
242 struct sockaddr *from, socklen_t *fromlen);
243 extern size_t stream_write(struct stream *, const void *, size_t);
244
245 /* reset the stream. See Note above */
246 extern void stream_reset(struct stream *);
247 extern int stream_flush(struct stream *, int);
248 extern int stream_empty(struct stream *); /* is the stream empty? */
249
250 /* deprecated */
251 extern uint8_t *stream_pnt(struct stream *);
252
253 /*
254 * Operations on struct stream_fifo.
255 *
256 * Each function has a safe variant, which ensures that the operation performed
257 * is atomic with respect to the operations performed by all other safe
258 * variants. In other words, the safe variants lock the stream_fifo's mutex
259 * before performing their action. These are provided for convenience when
260 * using stream_fifo in a multithreaded context, to alleviate the need for the
261 * caller to implement their own synchronization around the stream_fifo.
262 *
263 * The following functions do not have safe variants. The caller must ensure
264 * that these operations are performed safely in a multithreaded context:
265 * - stream_fifo_new
266 * - stream_fifo_free
267 */
268
269 /*
270 * Create a new stream_fifo.
271 *
272 * Returns:
273 * newly created stream_fifo
274 */
275 extern struct stream_fifo *stream_fifo_new(void);
276
277 /*
278 * Push a stream onto a stream_fifo.
279 *
280 * fifo
281 * the stream_fifo to push onto
282 *
283 * s
284 * the stream to push onto the stream_fifo
285 */
286 extern void stream_fifo_push(struct stream_fifo *fifo, struct stream *s);
287 extern void stream_fifo_push_safe(struct stream_fifo *fifo, struct stream *s);
288
289 /*
290 * Pop a stream off a stream_fifo.
291 *
292 * fifo
293 * the stream_fifo to pop from
294 *
295 * Returns:
296 * the next stream in the stream_fifo
297 */
298 extern struct stream *stream_fifo_pop(struct stream_fifo *fifo);
299 extern struct stream *stream_fifo_pop_safe(struct stream_fifo *fifo);
300
301 /*
302 * Retrieve the next stream from a stream_fifo without popping it.
303 *
304 * fifo
305 * the stream_fifo to operate on
306 *
307 * Returns:
308 * the next stream that would be returned from stream_fifo_pop
309 */
310 extern struct stream *stream_fifo_head(struct stream_fifo *fifo);
311 extern struct stream *stream_fifo_head_safe(struct stream_fifo *fifo);
312
313 /*
314 * Remove all streams from a stream_fifo.
315 *
316 * fifo
317 * the stream_fifo to clean
318 */
319 extern void stream_fifo_clean(struct stream_fifo *fifo);
320 extern void stream_fifo_clean_safe(struct stream_fifo *fifo);
321
322 /*
323 * Retrieve number of streams on a stream_fifo.
324 *
325 * fifo
326 * the stream_fifo to retrieve the count for
327 *
328 * Returns:
329 * the number of streams on the stream_fifo
330 */
331 extern size_t stream_fifo_count_safe(struct stream_fifo *fifo);
332
333 /*
334 * Free a stream_fifo.
335 *
336 * Calls stream_fifo_clean, then deinitializes the stream_fifo and frees it.
337 *
338 * fifo
339 * the stream_fifo to free
340 */
341 extern void stream_fifo_free(struct stream_fifo *fifo);
342
343 /* This is here because "<< 24" is particularly problematic in C.
344 * This is because the left operand of << is integer-promoted, which means
345 * an uint8_t gets converted into a *signed* int. Shifting into the sign
346 * bit of a signed int is theoretically undefined behaviour, so - the left
347 * operand needs to be cast to unsigned.
348 *
349 * This is not a problem for 16- or 8-bit values (they don't reach the sign
350 * bit), for 64-bit values (you need to cast them anyway), and neither for
351 * encoding (because it's downcasted.)
352 */
353 static inline uint8_t *ptr_get_be32(uint8_t *ptr, uint32_t *out)
354 {
355 uint32_t tmp;
356 memcpy(&tmp, ptr, sizeof(tmp));
357 *out = ntohl(tmp);
358 return ptr + 4;
359 }
360
361 /*
362 * so Normal stream_getX functions assert. Which is anathema
363 * to keeping a daemon up and running when something goes south
364 * Provide a stream_getX2 functions that do not assert.
365 * In addition provide these macro's that upon failure
366 * goto stream_failure. This is modeled upon some NL_XX
367 * macros in the linux kernel.
368 *
369 * This change allows for proper memory freeing
370 * after we've detected an error.
371 *
372 * In the future we will be removing the assert in
373 * the stream functions but we need a transition
374 * plan.
375 */
376 #define STREAM_GETC(S, P) \
377 do { \
378 uint8_t _pval; \
379 if (!stream_getc2((S), &_pval)) \
380 goto stream_failure; \
381 (P) = _pval; \
382 } while (0)
383
384 #define STREAM_GETW(S, P) \
385 do { \
386 uint16_t _pval; \
387 if (!stream_getw2((S), &_pval)) \
388 goto stream_failure; \
389 (P) = _pval; \
390 } while (0)
391
392 #define STREAM_GETL(S, P) \
393 do { \
394 uint32_t _pval; \
395 if (!stream_getl2((S), &_pval)) \
396 goto stream_failure; \
397 (P) = _pval; \
398 } while (0)
399
400 #define STREAM_GET(P, STR, SIZE) \
401 do { \
402 if (!stream_get2((P), (STR), (SIZE))) \
403 goto stream_failure; \
404 } while (0)
405
406 #endif /* _ZEBRA_STREAM_H */