]> git.proxmox.com Git - mirror_frr.git/blame - lib/stream.h
Merge remote-tracking branch 'origin/stable/2.0'
[mirror_frr.git] / lib / stream.h
CommitLineData
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
02ff83c5 25#include "prefix.h"
26
050c013a 27/*
28 * A stream is an arbitrary buffer, whose contents generally are assumed to
29 * be in network order.
30 *
31 * A stream has the following attributes associated with it:
32 *
33 * - size: the allocated, invariant size of the buffer.
34 *
35 * - getp: the get position marker, denoting the offset in the stream where
36 * the next read (or 'get') will be from. This getp marker is
37 * automatically adjusted when data is read from the stream, the
38 * user may also manipulate this offset as they wish, within limits
39 * (see below)
40 *
41 * - endp: the end position marker, denoting the offset in the stream where
42 * valid data ends, and if the user attempted to write (or
43 * 'put') data where that data would be written (or 'put') to.
44 *
45 * These attributes are all size_t values.
46 *
47 * Constraints:
48 *
49 * 1. getp can never exceed endp
50 *
51 * - hence if getp is equal to endp, there is no more valid data that can be
52 * gotten from the stream (though, the user may reposition getp to earlier in
53 * the stream, if they wish).
54 *
55 * 2. endp can never exceed size
56 *
57 * - hence, if endp is equal to size, then the stream is full, and no more
58 * data can be written to the stream.
59 *
60 * In other words the following must always be true, and the stream
61 * abstraction is allowed internally to assert that the following property
62 * holds true for a stream, as and when it wishes:
63 *
64 * getp <= endp <= size
65 *
66 * It is the users responsibility to ensure this property is never violated.
67 *
68 * A stream therefore can be thought of like this:
69 *
70 * ---------------------------------------------------
71 * |XXXXXXXXXXXXXXXXXXXXXXXX |
72 * ---------------------------------------------------
73 * ^ ^ ^
74 * getp endp size
75 *
76 * This shows a stream containing data (shown as 'X') up to the endp offset.
77 * The stream is empty from endp to size. Without adjusting getp, there are
78 * still endp-getp bytes of valid data to be read from the stream.
79 *
80 * Methods are provided to get and put to/from the stream, as well as
81 * retrieve the values of the 3 markers and manipulate the getp marker.
82 *
83 * Note:
84 * At the moment, newly allocated streams are zero filled. Hence, one can
85 * use stream_forward_endp() to effectively create arbitrary zero-fill
86 * padding. However, note that stream_reset() does *not* zero-out the
87 * stream. This property should **not** be relied upon.
0dab9303 88 *
89 * Best practice is to use stream_put (<stream *>, NULL, <size>) to zero out
90 * any part of a stream which isn't otherwise written to.
050c013a 91 */
92
718e3744 93/* Stream buffer. */
94struct stream
95{
96 struct stream *next;
97
050c013a 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 */
4b201d46 105 unsigned char *data; /* data pointer */
718e3744 106};
107
108/* First in first out queue structure. */
109struct stream_fifo
110{
f2e6c429 111 size_t count;
718e3744 112
113 struct stream *head;
114 struct stream *tail;
115};
116
117/* Utility macros. */
718e3744 118#define STREAM_SIZE(S) ((S)->size)
050c013a 119 /* number of bytes which can still be written */
120#define STREAM_WRITEABLE(S) ((S)->size - (S)->endp)
121 /* number of bytes still to be read */
122#define STREAM_READABLE(S) ((S)->endp - (S)->getp)
123
8c71e481
PM
124#define STREAM_CONCAT_REMAIN(S1, S2, size) \
125 ((size) - (S1)->endp - (S2)->endp)
126
050c013a 127/* deprecated macros - do not use in new code */
128#define STREAM_PNT(S) stream_pnt((S))
718e3744 129#define STREAM_DATA(S) ((S)->data)
050c013a 130#define STREAM_REMAIN(S) STREAM_WRITEABLE((S))
718e3744 131
4b201d46 132/* Stream prototypes.
133 * For stream_{put,get}S, the S suffix mean:
134 *
135 * c: character (unsigned byte)
136 * w: word (two bytes)
137 * l: long (two words)
138 * q: quad (four words)
139 */
8cc4198f 140extern struct stream *stream_new (size_t);
141extern void stream_free (struct stream *);
dea04441 142extern struct stream * stream_copy (struct stream *, struct stream *src);
8cc4198f 143extern struct stream *stream_dup (struct stream *);
4b201d46 144extern size_t stream_resize (struct stream *, size_t);
8cc4198f 145extern size_t stream_get_getp (struct stream *);
146extern size_t stream_get_endp (struct stream *);
147extern size_t stream_get_size (struct stream *);
148extern u_char *stream_get_data (struct stream *);
149
8c71e481
PM
150/**
151 * Create a new stream structure; copy offset bytes from s1 to the new
152 * stream; copy s2 data to the new stream; copy rest of s1 data to the
153 * new stream.
154 */
155extern struct stream *stream_dupcat(struct stream *s1, struct stream *s2,
156 size_t offset);
157
8cc4198f 158extern void stream_set_getp (struct stream *, size_t);
d531050b 159extern void stream_set_endp (struct stream *, size_t);
8cc4198f 160extern void stream_forward_getp (struct stream *, size_t);
161extern void stream_forward_endp (struct stream *, size_t);
162
163/* steam_put: NULL source zeroes out size_t bytes of stream */
3d52bb80 164extern void stream_put (struct stream *, const void *, size_t);
8cc4198f 165extern int stream_putc (struct stream *, u_char);
166extern int stream_putc_at (struct stream *, size_t, u_char);
167extern int stream_putw (struct stream *, u_int16_t);
168extern int stream_putw_at (struct stream *, size_t, u_int16_t);
d6f4a61d
DL
169extern int stream_put3 (struct stream *, u_int32_t);
170extern int stream_put3_at (struct stream *, size_t, u_int32_t);
8cc4198f 171extern int stream_putl (struct stream *, u_int32_t);
172extern int stream_putl_at (struct stream *, size_t, u_int32_t);
4b201d46 173extern int stream_putq (struct stream *, uint64_t);
174extern int stream_putq_at (struct stream *, size_t, uint64_t);
8cc4198f 175extern int stream_put_ipv4 (struct stream *, u_int32_t);
176extern int stream_put_in_addr (struct stream *, struct in_addr *);
3f9c7369
DS
177extern int stream_put_in_addr_at (struct stream *, size_t, struct in_addr *);
178extern int stream_put_in6_addr_at (struct stream *, size_t, struct in6_addr *);
adbac85e
DW
179extern int stream_put_prefix_addpath (struct stream *, struct prefix *,
180 int addpath_encode,
181 u_int32_t addpath_tx_id);
8cc4198f 182extern int stream_put_prefix (struct stream *, struct prefix *);
cd1964ff
DS
183extern int stream_put_labeled_prefix (struct stream *, struct prefix *,
184 u_char *);
8cc4198f 185extern void stream_get (void *, struct stream *, size_t);
3f9c7369 186extern void stream_get_from (void *, struct stream *, size_t, size_t);
8cc4198f 187extern u_char stream_getc (struct stream *);
188extern u_char stream_getc_from (struct stream *, size_t);
189extern u_int16_t stream_getw (struct stream *);
190extern u_int16_t stream_getw_from (struct stream *, size_t);
d6f4a61d
DL
191extern u_int32_t stream_get3 (struct stream *);
192extern u_int32_t stream_get3_from (struct stream *, size_t);
8cc4198f 193extern u_int32_t stream_getl (struct stream *);
194extern u_int32_t stream_getl_from (struct stream *, size_t);
4b201d46 195extern uint64_t stream_getq (struct stream *);
196extern uint64_t stream_getq_from (struct stream *, size_t);
8cc4198f 197extern u_int32_t stream_get_ipv4 (struct stream *);
718e3744 198
16f1b9ee
OD
199/* IEEE-754 floats */
200extern float stream_getf (struct stream *);
201extern double stream_getd (struct stream *);
202extern int stream_putf (struct stream *, float);
203extern int stream_putd (struct stream *, double);
204
718e3744 205#undef stream_read
206#undef stream_write
262feb1a 207
208/* Deprecated: assumes blocking I/O. Will be removed.
209 Use stream_read_try instead. */
8cc4198f 210extern int stream_read (struct stream *, int, size_t);
262feb1a 211
262feb1a 212/* Read up to size bytes into the stream.
213 Return code:
214 >0: number of bytes read
215 0: end-of-file
216 -1: fatal error
217 -2: transient error, should retry later (i.e. EAGAIN or EINTR)
218 This is suitable for use with non-blocking file descriptors.
219 */
220extern ssize_t stream_read_try(struct stream *s, int fd, size_t size);
221
0dab9303 222extern ssize_t stream_recvmsg (struct stream *s, int fd, struct msghdr *,
8cc4198f 223 int flags, size_t size);
224extern ssize_t stream_recvfrom (struct stream *s, int fd, size_t len,
225 int flags, struct sockaddr *from,
226 socklen_t *fromlen);
3d52bb80 227extern size_t stream_write (struct stream *, const void *, size_t);
718e3744 228
8cc4198f 229/* reset the stream. See Note above */
230extern void stream_reset (struct stream *);
231extern int stream_flush (struct stream *, int);
232extern int stream_empty (struct stream *); /* is the stream empty? */
050c013a 233
234/* deprecated */
8cc4198f 235extern u_char *stream_pnt (struct stream *);
718e3744 236
237/* Stream fifo. */
8cc4198f 238extern struct stream_fifo *stream_fifo_new (void);
239extern void stream_fifo_push (struct stream_fifo *fifo, struct stream *s);
240extern struct stream *stream_fifo_pop (struct stream_fifo *fifo);
241extern struct stream *stream_fifo_head (struct stream_fifo *fifo);
242extern void stream_fifo_clean (struct stream_fifo *fifo);
243extern void stream_fifo_free (struct stream_fifo *fifo);
718e3744 244
245#endif /* _ZEBRA_STREAM_H */