paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 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 |
| 20 | * 02111-1307, USA. |
| 21 | */ |
| 22 | |
| 23 | #ifndef _ZEBRA_STREAM_H |
| 24 | #define _ZEBRA_STREAM_H |
| 25 | |
paul | 02ff83c | 2004-06-11 11:27:03 +0000 | [diff] [blame] | 26 | #include "prefix.h" |
| 27 | |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 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. |
paul | 0dab930 | 2005-05-03 09:07:56 +0000 | [diff] [blame] | 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. |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 92 | */ |
| 93 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 94 | /* Stream buffer. */ |
| 95 | struct stream |
| 96 | { |
| 97 | struct stream *next; |
| 98 | |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 99 | /* Remainder is ***private*** to stream |
| 100 | * direct access is frowned upon! |
| 101 | * Use the appropriate functions/macros |
| 102 | */ |
| 103 | size_t getp; /* next get position */ |
| 104 | size_t endp; /* last valid data position */ |
| 105 | size_t size; /* size of data segment */ |
paul | 4b201d4 | 2006-01-10 14:35:19 +0000 | [diff] [blame] | 106 | unsigned char *data; /* data pointer */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 107 | }; |
| 108 | |
| 109 | /* First in first out queue structure. */ |
| 110 | struct stream_fifo |
| 111 | { |
paul | f2e6c42 | 2005-02-12 14:35:49 +0000 | [diff] [blame] | 112 | size_t count; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 113 | |
| 114 | struct stream *head; |
| 115 | struct stream *tail; |
| 116 | }; |
| 117 | |
| 118 | /* Utility macros. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 119 | #define STREAM_SIZE(S) ((S)->size) |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 120 | /* number of bytes which can still be written */ |
| 121 | #define STREAM_WRITEABLE(S) ((S)->size - (S)->endp) |
| 122 | /* number of bytes still to be read */ |
| 123 | #define STREAM_READABLE(S) ((S)->endp - (S)->getp) |
| 124 | |
| 125 | /* deprecated macros - do not use in new code */ |
| 126 | #define STREAM_PNT(S) stream_pnt((S)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 127 | #define STREAM_DATA(S) ((S)->data) |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 128 | #define STREAM_REMAIN(S) STREAM_WRITEABLE((S)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 129 | |
paul | 4b201d4 | 2006-01-10 14:35:19 +0000 | [diff] [blame] | 130 | /* Stream prototypes. |
| 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 | */ |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 138 | extern struct stream *stream_new (size_t); |
| 139 | extern void stream_free (struct stream *); |
Paul Jakma | dea0444 | 2008-02-26 09:16:09 +0000 | [diff] [blame] | 140 | extern struct stream * stream_copy (struct stream *, struct stream *src); |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 141 | extern struct stream *stream_dup (struct stream *); |
paul | 4b201d4 | 2006-01-10 14:35:19 +0000 | [diff] [blame] | 142 | extern size_t stream_resize (struct stream *, size_t); |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 143 | extern size_t stream_get_getp (struct stream *); |
| 144 | extern size_t stream_get_endp (struct stream *); |
| 145 | extern size_t stream_get_size (struct stream *); |
| 146 | extern u_char *stream_get_data (struct stream *); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 147 | |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 148 | extern void stream_set_getp (struct stream *, size_t); |
Subbaiah Venkata | d531050 | 2012-03-24 13:10:19 -0700 | [diff] [blame^] | 149 | extern void stream_set_endp (struct stream *, size_t); |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 150 | extern void stream_forward_getp (struct stream *, size_t); |
| 151 | extern void stream_forward_endp (struct stream *, size_t); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 152 | |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 153 | /* steam_put: NULL source zeroes out size_t bytes of stream */ |
Paul Jakma | 3d52bb8 | 2008-06-07 20:42:07 +0000 | [diff] [blame] | 154 | extern void stream_put (struct stream *, const void *, size_t); |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 155 | extern int stream_putc (struct stream *, u_char); |
| 156 | extern int stream_putc_at (struct stream *, size_t, u_char); |
| 157 | extern int stream_putw (struct stream *, u_int16_t); |
| 158 | extern int stream_putw_at (struct stream *, size_t, u_int16_t); |
| 159 | extern int stream_putl (struct stream *, u_int32_t); |
| 160 | extern int stream_putl_at (struct stream *, size_t, u_int32_t); |
paul | 4b201d4 | 2006-01-10 14:35:19 +0000 | [diff] [blame] | 161 | extern int stream_putq (struct stream *, uint64_t); |
| 162 | extern int stream_putq_at (struct stream *, size_t, uint64_t); |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 163 | extern int stream_put_ipv4 (struct stream *, u_int32_t); |
| 164 | extern int stream_put_in_addr (struct stream *, struct in_addr *); |
| 165 | extern int stream_put_prefix (struct stream *, struct prefix *); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 166 | |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 167 | extern void stream_get (void *, struct stream *, size_t); |
| 168 | extern u_char stream_getc (struct stream *); |
| 169 | extern u_char stream_getc_from (struct stream *, size_t); |
| 170 | extern u_int16_t stream_getw (struct stream *); |
| 171 | extern u_int16_t stream_getw_from (struct stream *, size_t); |
| 172 | extern u_int32_t stream_getl (struct stream *); |
| 173 | extern u_int32_t stream_getl_from (struct stream *, size_t); |
paul | 4b201d4 | 2006-01-10 14:35:19 +0000 | [diff] [blame] | 174 | extern uint64_t stream_getq (struct stream *); |
| 175 | extern uint64_t stream_getq_from (struct stream *, size_t); |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 176 | extern u_int32_t stream_get_ipv4 (struct stream *); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 177 | |
| 178 | #undef stream_read |
| 179 | #undef stream_write |
ajs | 262feb1 | 2005-02-16 20:35:47 +0000 | [diff] [blame] | 180 | |
| 181 | /* Deprecated: assumes blocking I/O. Will be removed. |
| 182 | Use stream_read_try instead. */ |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 183 | extern int stream_read (struct stream *, int, size_t); |
ajs | 262feb1 | 2005-02-16 20:35:47 +0000 | [diff] [blame] | 184 | |
| 185 | /* Deprecated: all file descriptors should already be non-blocking. |
| 186 | Will be removed. Use stream_read_try instead. */ |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 187 | extern int stream_read_unblock (struct stream *, int, size_t); |
ajs | 262feb1 | 2005-02-16 20:35:47 +0000 | [diff] [blame] | 188 | |
| 189 | /* Read up to size bytes into the stream. |
| 190 | Return code: |
| 191 | >0: number of bytes read |
| 192 | 0: end-of-file |
| 193 | -1: fatal error |
| 194 | -2: transient error, should retry later (i.e. EAGAIN or EINTR) |
| 195 | This is suitable for use with non-blocking file descriptors. |
| 196 | */ |
| 197 | extern ssize_t stream_read_try(struct stream *s, int fd, size_t size); |
| 198 | |
paul | 0dab930 | 2005-05-03 09:07:56 +0000 | [diff] [blame] | 199 | extern ssize_t stream_recvmsg (struct stream *s, int fd, struct msghdr *, |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 200 | int flags, size_t size); |
| 201 | extern ssize_t stream_recvfrom (struct stream *s, int fd, size_t len, |
| 202 | int flags, struct sockaddr *from, |
| 203 | socklen_t *fromlen); |
Paul Jakma | 3d52bb8 | 2008-06-07 20:42:07 +0000 | [diff] [blame] | 204 | extern size_t stream_write (struct stream *, const void *, size_t); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 205 | |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 206 | /* reset the stream. See Note above */ |
| 207 | extern void stream_reset (struct stream *); |
| 208 | extern int stream_flush (struct stream *, int); |
| 209 | extern int stream_empty (struct stream *); /* is the stream empty? */ |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 210 | |
| 211 | /* deprecated */ |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 212 | extern u_char *stream_pnt (struct stream *); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 213 | |
| 214 | /* Stream fifo. */ |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 215 | extern struct stream_fifo *stream_fifo_new (void); |
| 216 | extern void stream_fifo_push (struct stream_fifo *fifo, struct stream *s); |
| 217 | extern struct stream *stream_fifo_pop (struct stream_fifo *fifo); |
| 218 | extern struct stream *stream_fifo_head (struct stream_fifo *fifo); |
| 219 | extern void stream_fifo_clean (struct stream_fifo *fifo); |
| 220 | extern void stream_fifo_free (struct stream_fifo *fifo); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 221 | |
| 222 | #endif /* _ZEBRA_STREAM_H */ |