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. |
| 89 | * |
| 90 | * A Good stream user should ensure it writes all bytes. (the zero-fill |
| 91 | * guarantee may be removed in future, however, the zero-filling may |
| 92 | * possibly be moved to stream_forward_endp() instead, maybe..) |
| 93 | */ |
| 94 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 95 | /* Stream buffer. */ |
| 96 | struct stream |
| 97 | { |
| 98 | struct stream *next; |
| 99 | |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 100 | /* Remainder is ***private*** to stream |
| 101 | * direct access is frowned upon! |
| 102 | * Use the appropriate functions/macros |
| 103 | */ |
| 104 | size_t getp; /* next get position */ |
| 105 | size_t endp; /* last valid data position */ |
| 106 | size_t size; /* size of data segment */ |
| 107 | unsigned char data[0]; /* data pointer */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 108 | }; |
| 109 | |
| 110 | /* First in first out queue structure. */ |
| 111 | struct stream_fifo |
| 112 | { |
paul | f2e6c42 | 2005-02-12 14:35:49 +0000 | [diff] [blame] | 113 | size_t count; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 114 | |
| 115 | struct stream *head; |
| 116 | struct stream *tail; |
| 117 | }; |
| 118 | |
| 119 | /* Utility macros. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 120 | #define STREAM_SIZE(S) ((S)->size) |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 121 | /* number of bytes which can still be written */ |
| 122 | #define STREAM_WRITEABLE(S) ((S)->size - (S)->endp) |
| 123 | /* number of bytes still to be read */ |
| 124 | #define STREAM_READABLE(S) ((S)->endp - (S)->getp) |
| 125 | |
| 126 | /* deprecated macros - do not use in new code */ |
| 127 | #define STREAM_PNT(S) stream_pnt((S)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 128 | #define STREAM_DATA(S) ((S)->data) |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 129 | #define STREAM_REMAIN(S) STREAM_WRITEABLE((S)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 130 | |
| 131 | /* Stream prototypes. */ |
| 132 | struct stream *stream_new (size_t); |
| 133 | void stream_free (struct stream *); |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 134 | struct stream * stream_copy (struct stream *new, struct stream *src); |
| 135 | struct stream *stream_dup (struct stream *); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 136 | |
paul | f2e6c42 | 2005-02-12 14:35:49 +0000 | [diff] [blame] | 137 | size_t stream_get_getp (struct stream *); |
| 138 | size_t stream_get_endp (struct stream *); |
| 139 | size_t stream_get_size (struct stream *); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 140 | u_char *stream_get_data (struct stream *); |
| 141 | |
paul | f2e6c42 | 2005-02-12 14:35:49 +0000 | [diff] [blame] | 142 | void stream_set_getp (struct stream *, size_t); |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 143 | void stream_forward_getp (struct stream *, size_t); |
| 144 | void stream_forward_endp (struct stream *, size_t); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 145 | |
| 146 | void stream_put (struct stream *, void *, size_t); |
| 147 | int stream_putc (struct stream *, u_char); |
paul | f2e6c42 | 2005-02-12 14:35:49 +0000 | [diff] [blame] | 148 | int stream_putc_at (struct stream *, size_t, u_char); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 149 | int stream_putw (struct stream *, u_int16_t); |
paul | f2e6c42 | 2005-02-12 14:35:49 +0000 | [diff] [blame] | 150 | int stream_putw_at (struct stream *, size_t, u_int16_t); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 151 | int stream_putl (struct stream *, u_int32_t); |
paul | f2e6c42 | 2005-02-12 14:35:49 +0000 | [diff] [blame] | 152 | int stream_putl_at (struct stream *, size_t, u_int32_t); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 153 | int stream_put_ipv4 (struct stream *, u_int32_t); |
| 154 | int stream_put_in_addr (struct stream *, struct in_addr *); |
paul | 02ff83c | 2004-06-11 11:27:03 +0000 | [diff] [blame] | 155 | int stream_put_prefix (struct stream *, struct prefix *); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 156 | |
| 157 | void stream_get (void *, struct stream *, size_t); |
| 158 | u_char stream_getc (struct stream *); |
paul | f2e6c42 | 2005-02-12 14:35:49 +0000 | [diff] [blame] | 159 | u_char stream_getc_from (struct stream *, size_t); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 160 | u_int16_t stream_getw (struct stream *); |
paul | f2e6c42 | 2005-02-12 14:35:49 +0000 | [diff] [blame] | 161 | u_int16_t stream_getw_from (struct stream *, size_t); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 162 | u_int32_t stream_getl (struct stream *); |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 163 | u_int32_t stream_getl_from (struct stream *, size_t); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 164 | u_int32_t stream_get_ipv4 (struct stream *); |
| 165 | |
| 166 | #undef stream_read |
| 167 | #undef stream_write |
ajs | 262feb1 | 2005-02-16 20:35:47 +0000 | [diff] [blame] | 168 | |
| 169 | /* Deprecated: assumes blocking I/O. Will be removed. |
| 170 | Use stream_read_try instead. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 171 | int stream_read (struct stream *, int, size_t); |
ajs | 262feb1 | 2005-02-16 20:35:47 +0000 | [diff] [blame] | 172 | |
| 173 | /* Deprecated: all file descriptors should already be non-blocking. |
| 174 | Will be removed. Use stream_read_try instead. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 175 | int stream_read_unblock (struct stream *, int, size_t); |
ajs | 262feb1 | 2005-02-16 20:35:47 +0000 | [diff] [blame] | 176 | |
| 177 | /* Read up to size bytes into the stream. |
| 178 | Return code: |
| 179 | >0: number of bytes read |
| 180 | 0: end-of-file |
| 181 | -1: fatal error |
| 182 | -2: transient error, should retry later (i.e. EAGAIN or EINTR) |
| 183 | This is suitable for use with non-blocking file descriptors. |
| 184 | */ |
| 185 | extern ssize_t stream_read_try(struct stream *s, int fd, size_t size); |
| 186 | |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 187 | int stream_recvmsg (struct stream *s, int fd, struct msghdr *, |
| 188 | int flags, size_t size); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 189 | int stream_write (struct stream *, u_char *, size_t); |
| 190 | |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 191 | void stream_reset (struct stream *); /* reset the stream. See Note above */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 192 | int stream_flush (struct stream *, int); |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 193 | int stream_empty (struct stream *); /* is the stream empty? */ |
| 194 | |
| 195 | /* deprecated */ |
| 196 | u_char *stream_pnt (struct stream *); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 197 | |
| 198 | /* Stream fifo. */ |
| 199 | struct stream_fifo *stream_fifo_new (); |
| 200 | void stream_fifo_push (struct stream_fifo *fifo, struct stream *s); |
| 201 | struct stream *stream_fifo_pop (struct stream_fifo *fifo); |
| 202 | struct stream *stream_fifo_head (struct stream_fifo *fifo); |
| 203 | void stream_fifo_clean (struct stream_fifo *fifo); |
| 204 | void stream_fifo_free (struct stream_fifo *fifo); |
| 205 | |
| 206 | #endif /* _ZEBRA_STREAM_H */ |