]> git.proxmox.com Git - mirror_frr.git/blame - lib/stream.h
debianpkg: Remove -werror from Ubuntu 14.04 and 12.04 build to skip warnings from...
[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 *
17 * You should have received a copy of the GNU General Public License
18 * along with GNU Zebra; see the file COPYING. If not, write to the Free
19 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
ac4d0be5 20 * 02111-1307, USA.
718e3744 21 */
22
23#ifndef _ZEBRA_STREAM_H
24#define _ZEBRA_STREAM_H
25
02ff83c5 26#include "prefix.h"
27
050c013a 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 *
ac4d0be5 81 * Methods are provided to get and put to/from the stream, as well as
050c013a 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.
0dab9303 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.
050c013a 92 */
93
718e3744 94/* Stream buffer. */
ac4d0be5 95struct 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 */
718e3744 106};
107
108/* First in first out queue structure. */
ac4d0be5 109struct stream_fifo {
110 size_t count;
718e3744 111
ac4d0be5 112 struct stream *head;
113 struct stream *tail;
718e3744 114};
115
116/* Utility macros. */
718e3744 117#define STREAM_SIZE(S) ((S)->size)
ac4d0be5 118/* number of bytes which can still be written */
050c013a 119#define STREAM_WRITEABLE(S) ((S)->size - (S)->endp)
ac4d0be5 120/* number of bytes still to be read */
050c013a 121#define STREAM_READABLE(S) ((S)->endp - (S)->getp)
122
ac4d0be5 123#define STREAM_CONCAT_REMAIN(S1, S2, size) ((size) - (S1)->endp - (S2)->endp)
8c71e481 124
050c013a 125/* deprecated macros - do not use in new code */
126#define STREAM_PNT(S) stream_pnt((S))
718e3744 127#define STREAM_DATA(S) ((S)->data)
050c013a 128#define STREAM_REMAIN(S) STREAM_WRITEABLE((S))
718e3744 129
ac4d0be5 130/* Stream prototypes.
4b201d46 131 * For stream_{put,get}S, the S suffix mean:
132 *
133 * c: character (unsigned byte)
134 * w: word (two bytes)
135 * l: long (two words)
136 * q: quad (four words)
137 */
ac4d0be5 138extern struct stream *stream_new(size_t);
139extern void stream_free(struct stream *);
140extern struct stream *stream_copy(struct stream *, struct stream *src);
141extern struct stream *stream_dup(struct stream *);
142extern size_t stream_resize(struct stream *, size_t);
143extern size_t stream_get_getp(struct stream *);
144extern size_t stream_get_endp(struct stream *);
145extern size_t stream_get_size(struct stream *);
146extern u_char *stream_get_data(struct stream *);
8cc4198f 147
8c71e481
PM
148/**
149 * Create a new stream structure; copy offset bytes from s1 to the new
150 * stream; copy s2 data to the new stream; copy rest of s1 data to the
151 * new stream.
152 */
153extern struct stream *stream_dupcat(struct stream *s1, struct stream *s2,
154 size_t offset);
155
ac4d0be5 156extern void stream_set_getp(struct stream *, size_t);
157extern void stream_set_endp(struct stream *, size_t);
158extern void stream_forward_getp(struct stream *, size_t);
159extern void stream_forward_endp(struct stream *, size_t);
8cc4198f 160
161/* steam_put: NULL source zeroes out size_t bytes of stream */
ac4d0be5 162extern void stream_put(struct stream *, const void *, size_t);
163extern int stream_putc(struct stream *, u_char);
164extern int stream_putc_at(struct stream *, size_t, u_char);
165extern int stream_putw(struct stream *, u_int16_t);
166extern int stream_putw_at(struct stream *, size_t, u_int16_t);
167extern int stream_put3(struct stream *, u_int32_t);
168extern int stream_put3_at(struct stream *, size_t, u_int32_t);
169extern int stream_putl(struct stream *, u_int32_t);
170extern int stream_putl_at(struct stream *, size_t, u_int32_t);
171extern int stream_putq(struct stream *, uint64_t);
172extern int stream_putq_at(struct stream *, size_t, uint64_t);
173extern int stream_put_ipv4(struct stream *, u_int32_t);
174extern int stream_put_in_addr(struct stream *, struct in_addr *);
175extern int stream_put_in_addr_at(struct stream *, size_t, struct in_addr *);
176extern int stream_put_in6_addr_at(struct stream *, size_t, struct in6_addr *);
177extern int stream_put_prefix_addpath(struct stream *, struct prefix *,
178 int addpath_encode,
179 u_int32_t addpath_tx_id);
180extern int stream_put_prefix(struct stream *, struct prefix *);
181
182extern void stream_get(void *, struct stream *, size_t);
183extern void stream_get_from(void *, struct stream *, size_t, size_t);
184extern u_char stream_getc(struct stream *);
185extern u_char stream_getc_from(struct stream *, size_t);
186extern u_int16_t stream_getw(struct stream *);
187extern u_int16_t stream_getw_from(struct stream *, size_t);
188extern u_int32_t stream_get3(struct stream *);
189extern u_int32_t stream_get3_from(struct stream *, size_t);
190extern u_int32_t stream_getl(struct stream *);
191extern u_int32_t stream_getl_from(struct stream *, size_t);
192extern uint64_t stream_getq(struct stream *);
193extern uint64_t stream_getq_from(struct stream *, size_t);
194extern u_int32_t stream_get_ipv4(struct stream *);
718e3744 195
16f1b9ee 196/* IEEE-754 floats */
ac4d0be5 197extern float stream_getf(struct stream *);
198extern double stream_getd(struct stream *);
199extern int stream_putf(struct stream *, float);
200extern int stream_putd(struct stream *, double);
16f1b9ee 201
718e3744 202#undef stream_read
203#undef stream_write
262feb1a 204
ac4d0be5 205/* Deprecated: assumes blocking I/O. Will be removed.
262feb1a 206 Use stream_read_try instead. */
ac4d0be5 207extern int stream_read(struct stream *, int, size_t);
262feb1a 208
262feb1a 209/* Read up to size bytes into the stream.
210 Return code:
211 >0: number of bytes read
212 0: end-of-file
213 -1: fatal error
214 -2: transient error, should retry later (i.e. EAGAIN or EINTR)
215 This is suitable for use with non-blocking file descriptors.
216 */
217extern ssize_t stream_read_try(struct stream *s, int fd, size_t size);
218
ac4d0be5 219extern ssize_t stream_recvmsg(struct stream *s, int fd, struct msghdr *,
220 int flags, size_t size);
221extern ssize_t stream_recvfrom(struct stream *s, int fd, size_t len, int flags,
222 struct sockaddr *from, socklen_t *fromlen);
223extern size_t stream_write(struct stream *, const void *, size_t);
718e3744 224
8cc4198f 225/* reset the stream. See Note above */
ac4d0be5 226extern void stream_reset(struct stream *);
227extern int stream_flush(struct stream *, int);
228extern int stream_empty(struct stream *); /* is the stream empty? */
050c013a 229
230/* deprecated */
ac4d0be5 231extern u_char *stream_pnt(struct stream *);
718e3744 232
233/* Stream fifo. */
ac4d0be5 234extern struct stream_fifo *stream_fifo_new(void);
235extern void stream_fifo_push(struct stream_fifo *fifo, struct stream *s);
236extern struct stream *stream_fifo_pop(struct stream_fifo *fifo);
237extern struct stream *stream_fifo_head(struct stream_fifo *fifo);
238extern void stream_fifo_clean(struct stream_fifo *fifo);
239extern void stream_fifo_free(struct stream_fifo *fifo);
718e3744 240
241#endif /* _ZEBRA_STREAM_H */