]>
Commit | Line | Data |
---|---|---|
718e3744 | 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 | * | |
896014f4 DL |
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 | |
718e3744 | 20 | */ |
21 | ||
22 | #ifndef _ZEBRA_STREAM_H | |
23 | #define _ZEBRA_STREAM_H | |
24 | ||
363e24c6 QY |
25 | #include <pthread.h> |
26 | ||
27 | #include "frratomic.h" | |
9bedbb1e | 28 | #include "mpls.h" |
02ff83c5 | 29 | #include "prefix.h" |
30 | ||
050c013a | 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 | * | |
d62a17ae | 84 | * Methods are provided to get and put to/from the stream, as well as |
050c013a | 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. | |
0dab9303 | 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. | |
050c013a | 95 | */ |
96 | ||
718e3744 | 97 | /* Stream buffer. */ |
d62a17ae | 98 | struct stream { |
99 | struct stream *next; | |
100 | ||
de75223e DS |
101 | /* |
102 | * Remainder is ***private*** to stream | |
d62a17ae | 103 | * direct access is frowned upon! |
104 | * Use the appropriate functions/macros | |
105 | */ | |
de75223e DS |
106 | size_t getp; /* next get position */ |
107 | size_t endp; /* last valid data position */ | |
108 | size_t size; /* size of data segment */ | |
109 | unsigned char data[0]; /* data pointer */ | |
718e3744 | 110 | }; |
111 | ||
112 | /* First in first out queue structure. */ | |
d62a17ae | 113 | struct stream_fifo { |
363e24c6 QY |
114 | /* lock for mt-safe operations */ |
115 | pthread_mutex_t mtx; | |
116 | ||
117 | /* number of streams in this fifo */ | |
118 | _Atomic size_t count; | |
03ed85a6 DS |
119 | #if defined DEV_BUILD |
120 | _Atomic size_t max_count; | |
121 | #endif | |
718e3744 | 122 | |
d62a17ae | 123 | struct stream *head; |
124 | struct stream *tail; | |
718e3744 | 125 | }; |
126 | ||
127 | /* Utility macros. */ | |
718e3744 | 128 | #define STREAM_SIZE(S) ((S)->size) |
d62a17ae | 129 | /* number of bytes which can still be written */ |
050c013a | 130 | #define STREAM_WRITEABLE(S) ((S)->size - (S)->endp) |
d62a17ae | 131 | /* number of bytes still to be read */ |
050c013a | 132 | #define STREAM_READABLE(S) ((S)->endp - (S)->getp) |
133 | ||
d62a17ae | 134 | #define STREAM_CONCAT_REMAIN(S1, S2, size) ((size) - (S1)->endp - (S2)->endp) |
8c71e481 | 135 | |
2d34fb80 QY |
136 | /* this macro is deprecated, but not slated for removal anytime soon */ |
137 | #define STREAM_DATA(S) ((S)->data) | |
138 | ||
d62a17ae | 139 | /* Stream prototypes. |
4b201d46 | 140 | * For stream_{put,get}S, the S suffix mean: |
141 | * | |
142 | * c: character (unsigned byte) | |
143 | * w: word (two bytes) | |
144 | * l: long (two words) | |
145 | * q: quad (four words) | |
146 | */ | |
d62a17ae | 147 | extern struct stream *stream_new(size_t); |
148 | extern void stream_free(struct stream *); | |
149 | extern struct stream *stream_copy(struct stream *, struct stream *src); | |
150 | extern struct stream *stream_dup(struct stream *); | |
43888669 DS |
151 | |
152 | #if CONFDATE > 20190821 | |
153 | CPP_NOTICE("lib: time to remove stream_resize_orig") | |
154 | #endif | |
155 | extern size_t stream_resize_orig(struct stream *s, size_t newsize); | |
156 | #define stream_resize stream_resize_orig | |
157 | extern size_t stream_resize_inplace(struct stream **sptr, size_t newsize); | |
158 | ||
d62a17ae | 159 | extern size_t stream_get_getp(struct stream *); |
160 | extern size_t stream_get_endp(struct stream *); | |
161 | extern size_t stream_get_size(struct stream *); | |
d7c0a89a | 162 | extern uint8_t *stream_get_data(struct stream *); |
8cc4198f | 163 | |
8c71e481 PM |
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(struct stream *s1, struct stream *s2, | |
170 | size_t offset); | |
171 | ||
d62a17ae | 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 void stream_forward_endp(struct stream *, size_t); | |
8cc4198f | 176 | |
177 | /* steam_put: NULL source zeroes out size_t bytes of stream */ | |
d62a17ae | 178 | extern void stream_put(struct stream *, const void *, size_t); |
d7c0a89a QY |
179 | extern int stream_putc(struct stream *, uint8_t); |
180 | extern int stream_putc_at(struct stream *, size_t, uint8_t); | |
181 | extern int stream_putw(struct stream *, uint16_t); | |
182 | extern int stream_putw_at(struct stream *, size_t, uint16_t); | |
183 | extern int stream_put3(struct stream *, uint32_t); | |
184 | extern int stream_put3_at(struct stream *, size_t, uint32_t); | |
185 | extern int stream_putl(struct stream *, uint32_t); | |
186 | extern int stream_putl_at(struct stream *, size_t, uint32_t); | |
d62a17ae | 187 | extern int stream_putq(struct stream *, uint64_t); |
188 | extern int stream_putq_at(struct stream *, size_t, uint64_t); | |
d7c0a89a | 189 | extern int stream_put_ipv4(struct stream *, uint32_t); |
d62a17ae | 190 | extern int stream_put_in_addr(struct stream *, struct in_addr *); |
191 | extern int stream_put_in_addr_at(struct stream *, size_t, struct in_addr *); | |
192 | extern int stream_put_in6_addr_at(struct stream *, size_t, struct in6_addr *); | |
193 | extern int stream_put_prefix_addpath(struct stream *, struct prefix *, | |
194 | int addpath_encode, | |
d7c0a89a | 195 | uint32_t addpath_tx_id); |
d62a17ae | 196 | extern int stream_put_prefix(struct stream *, struct prefix *); |
197 | extern int stream_put_labeled_prefix(struct stream *, struct prefix *, | |
198 | mpls_label_t *); | |
199 | extern void stream_get(void *, struct stream *, size_t); | |
051cc28c | 200 | extern bool stream_get2(void *data, struct stream *s, size_t size); |
d62a17ae | 201 | extern void stream_get_from(void *, struct stream *, size_t, size_t); |
d7c0a89a QY |
202 | extern uint8_t stream_getc(struct stream *); |
203 | extern bool stream_getc2(struct stream *s, uint8_t *byte); | |
204 | extern uint8_t stream_getc_from(struct stream *, size_t); | |
205 | extern uint16_t stream_getw(struct stream *); | |
051cc28c | 206 | extern bool stream_getw2(struct stream *s, uint16_t *word); |
d7c0a89a QY |
207 | extern uint16_t stream_getw_from(struct stream *, size_t); |
208 | extern uint32_t stream_get3(struct stream *); | |
209 | extern uint32_t stream_get3_from(struct stream *, size_t); | |
210 | extern uint32_t stream_getl(struct stream *); | |
051cc28c | 211 | extern bool stream_getl2(struct stream *s, uint32_t *l); |
d7c0a89a | 212 | extern uint32_t stream_getl_from(struct stream *, size_t); |
d62a17ae | 213 | extern uint64_t stream_getq(struct stream *); |
214 | extern uint64_t stream_getq_from(struct stream *, size_t); | |
d7c0a89a | 215 | extern uint32_t stream_get_ipv4(struct stream *); |
718e3744 | 216 | |
16f1b9ee | 217 | /* IEEE-754 floats */ |
d62a17ae | 218 | extern float stream_getf(struct stream *); |
219 | extern double stream_getd(struct stream *); | |
220 | extern int stream_putf(struct stream *, float); | |
221 | extern int stream_putd(struct stream *, double); | |
16f1b9ee | 222 | |
718e3744 | 223 | #undef stream_read |
224 | #undef stream_write | |
262feb1a | 225 | |
d62a17ae | 226 | /* Deprecated: assumes blocking I/O. Will be removed. |
262feb1a | 227 | Use stream_read_try instead. */ |
d62a17ae | 228 | extern int stream_read(struct stream *, int, size_t); |
262feb1a | 229 | |
262feb1a | 230 | /* Read up to size bytes into the stream. |
231 | Return code: | |
232 | >0: number of bytes read | |
233 | 0: end-of-file | |
234 | -1: fatal error | |
235 | -2: transient error, should retry later (i.e. EAGAIN or EINTR) | |
236 | This is suitable for use with non-blocking file descriptors. | |
237 | */ | |
238 | extern ssize_t stream_read_try(struct stream *s, int fd, size_t size); | |
239 | ||
d62a17ae | 240 | extern ssize_t stream_recvmsg(struct stream *s, int fd, struct msghdr *, |
241 | int flags, size_t size); | |
242 | extern ssize_t stream_recvfrom(struct stream *s, int fd, size_t len, int flags, | |
243 | struct sockaddr *from, socklen_t *fromlen); | |
244 | extern size_t stream_write(struct stream *, const void *, size_t); | |
718e3744 | 245 | |
8cc4198f | 246 | /* reset the stream. See Note above */ |
d62a17ae | 247 | extern void stream_reset(struct stream *); |
248 | extern int stream_flush(struct stream *, int); | |
249 | extern int stream_empty(struct stream *); /* is the stream empty? */ | |
050c013a | 250 | |
251 | /* deprecated */ | |
d7c0a89a | 252 | extern uint8_t *stream_pnt(struct stream *); |
718e3744 | 253 | |
363e24c6 QY |
254 | /* |
255 | * Operations on struct stream_fifo. | |
256 | * | |
257 | * Each function has a safe variant, which ensures that the operation performed | |
258 | * is atomic with respect to the operations performed by all other safe | |
259 | * variants. In other words, the safe variants lock the stream_fifo's mutex | |
260 | * before performing their action. These are provided for convenience when | |
261 | * using stream_fifo in a multithreaded context, to alleviate the need for the | |
262 | * caller to implement their own synchronization around the stream_fifo. | |
263 | * | |
264 | * The following functions do not have safe variants. The caller must ensure | |
265 | * that these operations are performed safely in a multithreaded context: | |
266 | * - stream_fifo_new | |
267 | * - stream_fifo_free | |
268 | */ | |
269 | ||
270 | /* | |
271 | * Create a new stream_fifo. | |
272 | * | |
273 | * Returns: | |
274 | * newly created stream_fifo | |
275 | */ | |
d62a17ae | 276 | extern struct stream_fifo *stream_fifo_new(void); |
363e24c6 QY |
277 | |
278 | /* | |
279 | * Push a stream onto a stream_fifo. | |
280 | * | |
281 | * fifo | |
282 | * the stream_fifo to push onto | |
283 | * | |
284 | * s | |
285 | * the stream to push onto the stream_fifo | |
286 | */ | |
d62a17ae | 287 | extern void stream_fifo_push(struct stream_fifo *fifo, struct stream *s); |
363e24c6 QY |
288 | extern void stream_fifo_push_safe(struct stream_fifo *fifo, struct stream *s); |
289 | ||
290 | /* | |
291 | * Pop a stream off a stream_fifo. | |
292 | * | |
293 | * fifo | |
294 | * the stream_fifo to pop from | |
295 | * | |
296 | * Returns: | |
297 | * the next stream in the stream_fifo | |
298 | */ | |
d62a17ae | 299 | extern struct stream *stream_fifo_pop(struct stream_fifo *fifo); |
363e24c6 QY |
300 | extern struct stream *stream_fifo_pop_safe(struct stream_fifo *fifo); |
301 | ||
302 | /* | |
303 | * Retrieve the next stream from a stream_fifo without popping it. | |
304 | * | |
305 | * fifo | |
306 | * the stream_fifo to operate on | |
307 | * | |
308 | * Returns: | |
309 | * the next stream that would be returned from stream_fifo_pop | |
310 | */ | |
d62a17ae | 311 | extern struct stream *stream_fifo_head(struct stream_fifo *fifo); |
363e24c6 QY |
312 | extern struct stream *stream_fifo_head_safe(struct stream_fifo *fifo); |
313 | ||
314 | /* | |
315 | * Remove all streams from a stream_fifo. | |
316 | * | |
317 | * fifo | |
318 | * the stream_fifo to clean | |
319 | */ | |
d62a17ae | 320 | extern void stream_fifo_clean(struct stream_fifo *fifo); |
363e24c6 QY |
321 | extern void stream_fifo_clean_safe(struct stream_fifo *fifo); |
322 | ||
323 | /* | |
324 | * Retrieve number of streams on a stream_fifo. | |
325 | * | |
326 | * fifo | |
327 | * the stream_fifo to retrieve the count for | |
328 | * | |
329 | * Returns: | |
330 | * the number of streams on the stream_fifo | |
331 | */ | |
332 | extern size_t stream_fifo_count_safe(struct stream_fifo *fifo); | |
333 | ||
334 | /* | |
335 | * Free a stream_fifo. | |
336 | * | |
337 | * Calls stream_fifo_clean, then deinitializes the stream_fifo and frees it. | |
338 | * | |
339 | * fifo | |
340 | * the stream_fifo to free | |
341 | */ | |
d62a17ae | 342 | extern void stream_fifo_free(struct stream_fifo *fifo); |
718e3744 | 343 | |
937652c6 DL |
344 | /* This is here because "<< 24" is particularly problematic in C. |
345 | * This is because the left operand of << is integer-promoted, which means | |
346 | * an uint8_t gets converted into a *signed* int. Shifting into the sign | |
347 | * bit of a signed int is theoretically undefined behaviour, so - the left | |
348 | * operand needs to be cast to unsigned. | |
349 | * | |
350 | * This is not a problem for 16- or 8-bit values (they don't reach the sign | |
351 | * bit), for 64-bit values (you need to cast them anyway), and neither for | |
352 | * encoding (because it's downcasted.) | |
353 | */ | |
354 | static inline uint8_t *ptr_get_be32(uint8_t *ptr, uint32_t *out) | |
355 | { | |
356 | uint32_t tmp; | |
357 | memcpy(&tmp, ptr, sizeof(tmp)); | |
358 | *out = ntohl(tmp); | |
359 | return ptr + 4; | |
360 | } | |
361 | ||
051cc28c DS |
362 | /* |
363 | * so Normal stream_getX functions assert. Which is anathema | |
364 | * to keeping a daemon up and running when something goes south | |
365 | * Provide a stream_getX2 functions that do not assert. | |
366 | * In addition provide these macro's that upon failure | |
367 | * goto stream_failure. This is modeled upon some NL_XX | |
368 | * macros in the linux kernel. | |
369 | * | |
370 | * This change allows for proper memory freeing | |
371 | * after we've detected an error. | |
372 | * | |
373 | * In the future we will be removing the assert in | |
374 | * the stream functions but we need a transition | |
375 | * plan. | |
376 | */ | |
996c9314 LB |
377 | #define STREAM_GETC(S, P) \ |
378 | do { \ | |
379 | uint8_t _pval; \ | |
380 | if (!stream_getc2((S), &_pval)) \ | |
381 | goto stream_failure; \ | |
382 | (P) = _pval; \ | |
051cc28c DS |
383 | } while (0) |
384 | ||
996c9314 LB |
385 | #define STREAM_GETW(S, P) \ |
386 | do { \ | |
387 | uint16_t _pval; \ | |
388 | if (!stream_getw2((S), &_pval)) \ | |
389 | goto stream_failure; \ | |
390 | (P) = _pval; \ | |
051cc28c DS |
391 | } while (0) |
392 | ||
996c9314 LB |
393 | #define STREAM_GETL(S, P) \ |
394 | do { \ | |
395 | uint32_t _pval; \ | |
396 | if (!stream_getl2((S), &_pval)) \ | |
397 | goto stream_failure; \ | |
398 | (P) = _pval; \ | |
051cc28c DS |
399 | } while (0) |
400 | ||
996c9314 LB |
401 | #define STREAM_GET(P, STR, SIZE) \ |
402 | do { \ | |
403 | if (!stream_get2((P), (STR), (SIZE))) \ | |
404 | goto stream_failure; \ | |
051cc28c DS |
405 | } while (0) |
406 | ||
718e3744 | 407 | #endif /* _ZEBRA_STREAM_H */ |