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