paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 1 | /* |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 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 | |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 23 | #include <stddef.h> |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 24 | #include <zebra.h> |
| 25 | |
| 26 | #include "stream.h" |
| 27 | #include "memory.h" |
| 28 | #include "network.h" |
| 29 | #include "prefix.h" |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 30 | #include "log.h" |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 31 | |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 32 | /* Tests whether a position is valid */ |
| 33 | #define GETP_VALID(S,G) \ |
| 34 | ((G) <= (S)->endp) |
| 35 | #define PUT_AT_VALID(S,G) GETP_VALID(S,G) |
| 36 | #define ENDP_VALID(S,E) \ |
| 37 | ((E) <= (S)->size) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 38 | |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 39 | /* asserting sanity checks. Following must be true before |
| 40 | * stream functions are called: |
| 41 | * |
| 42 | * Following must always be true of stream elements |
| 43 | * before and after calls to stream functions: |
| 44 | * |
| 45 | * getp <= endp <= size |
| 46 | * |
| 47 | * Note that after a stream function is called following may be true: |
| 48 | * if (getp == endp) then stream is no longer readable |
| 49 | * if (endp == size) then stream is no longer writeable |
| 50 | * |
| 51 | * It is valid to put to anywhere within the size of the stream, but only |
| 52 | * using stream_put..._at() functions. |
| 53 | */ |
| 54 | #define STREAM_WARN_OFFSETS(S) \ |
| 55 | zlog_warn ("&(struct stream): %p, size: %lu, endp: %lu, getp: %lu\n", \ |
| 56 | (S), \ |
| 57 | (unsigned long) (S)->size, \ |
| 58 | (unsigned long) (S)->getp, \ |
| 59 | (unsigned long) (S)->endp)\ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 60 | |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 61 | #define STREAM_VERIFY_SANE(S) \ |
| 62 | do { \ |
| 63 | if ( !(GETP_VALID(S, (S)->getp)) && ENDP_VALID(S, (S)->endp) ) \ |
| 64 | STREAM_WARN_OFFSETS(S); \ |
| 65 | assert ( GETP_VALID(S, (S)->getp) ); \ |
| 66 | assert ( ENDP_VALID(S, (S)->endp) ); \ |
| 67 | } while (0) |
| 68 | |
| 69 | #define STREAM_BOUND_WARN(S, WHAT) \ |
| 70 | do { \ |
| 71 | zlog_warn ("%s: Attempt to %s out of bounds", __func__, (WHAT)); \ |
| 72 | STREAM_WARN_OFFSETS(S); \ |
| 73 | assert (0); \ |
| 74 | } while (0) |
| 75 | |
| 76 | /* XXX: Deprecated macro: do not use */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 77 | #define CHECK_SIZE(S, Z) \ |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 78 | do { \ |
| 79 | if (((S)->endp + (Z)) > (S)->size) \ |
| 80 | { \ |
| 81 | zlog_warn ("CHECK_SIZE: truncating requested size %lu\n", \ |
| 82 | (unsigned long) (Z)); \ |
| 83 | STREAM_WARN_OFFSETS(S); \ |
| 84 | (Z) = (S)->size - (S)->endp; \ |
| 85 | } \ |
| 86 | } while (0); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 87 | |
| 88 | /* Make stream buffer. */ |
| 89 | struct stream * |
| 90 | stream_new (size_t size) |
| 91 | { |
| 92 | struct stream *s; |
| 93 | |
paul | 0e43a2b | 2004-12-22 00:15:34 +0000 | [diff] [blame] | 94 | assert (size > 0); |
| 95 | |
| 96 | if (size == 0) |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 97 | { |
| 98 | zlog_warn ("stream_new(): called with 0 size!"); |
| 99 | return NULL; |
| 100 | } |
paul | 0e43a2b | 2004-12-22 00:15:34 +0000 | [diff] [blame] | 101 | |
paul | 109ac96 | 2005-02-19 01:17:07 +0000 | [diff] [blame^] | 102 | s = XCALLOC (MTYPE_STREAM, offsetof(struct stream, data[size])); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 103 | |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 104 | if (s == NULL) |
| 105 | return s; |
| 106 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 107 | s->size = size; |
| 108 | return s; |
| 109 | } |
| 110 | |
| 111 | /* Free it now. */ |
| 112 | void |
| 113 | stream_free (struct stream *s) |
| 114 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 115 | XFREE (MTYPE_STREAM, s); |
| 116 | } |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 117 | |
| 118 | struct stream * |
| 119 | stream_copy (struct stream *new, struct stream *src) |
| 120 | { |
| 121 | STREAM_VERIFY_SANE (src); |
| 122 | |
| 123 | assert (new != NULL); |
| 124 | assert (STREAM_SIZE(new) >= src->endp); |
| 125 | |
| 126 | new->endp = src->endp; |
| 127 | new->getp = src->getp; |
| 128 | |
| 129 | memcpy (new->data, src->data, src->endp); |
| 130 | |
| 131 | return new; |
| 132 | } |
| 133 | |
| 134 | struct stream * |
| 135 | stream_dup (struct stream *s) |
| 136 | { |
| 137 | struct stream *new; |
| 138 | |
| 139 | STREAM_VERIFY_SANE (s); |
| 140 | |
| 141 | if ( (new = stream_new (s->endp)) == NULL) |
| 142 | return NULL; |
| 143 | |
| 144 | return (stream_copy (new, s)); |
| 145 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 146 | |
paul | f2e6c42 | 2005-02-12 14:35:49 +0000 | [diff] [blame] | 147 | size_t |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 148 | stream_get_getp (struct stream *s) |
| 149 | { |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 150 | STREAM_VERIFY_SANE(s); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 151 | return s->getp; |
| 152 | } |
| 153 | |
paul | f2e6c42 | 2005-02-12 14:35:49 +0000 | [diff] [blame] | 154 | size_t |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 155 | stream_get_endp (struct stream *s) |
| 156 | { |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 157 | STREAM_VERIFY_SANE(s); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 158 | return s->endp; |
| 159 | } |
| 160 | |
paul | f2e6c42 | 2005-02-12 14:35:49 +0000 | [diff] [blame] | 161 | size_t |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 162 | stream_get_size (struct stream *s) |
| 163 | { |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 164 | STREAM_VERIFY_SANE(s); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 165 | return s->size; |
| 166 | } |
| 167 | |
| 168 | /* Stream structre' stream pointer related functions. */ |
| 169 | void |
paul | f2e6c42 | 2005-02-12 14:35:49 +0000 | [diff] [blame] | 170 | stream_set_getp (struct stream *s, size_t pos) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 171 | { |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 172 | STREAM_VERIFY_SANE(s); |
| 173 | |
| 174 | if (!GETP_VALID (s, pos)) |
| 175 | { |
| 176 | STREAM_BOUND_WARN (s, "set getp"); |
| 177 | pos = s->endp; |
| 178 | } |
| 179 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 180 | s->getp = pos; |
| 181 | } |
| 182 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 183 | /* Forward pointer. */ |
| 184 | void |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 185 | stream_forward_getp (struct stream *s, size_t size) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 186 | { |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 187 | STREAM_VERIFY_SANE(s); |
| 188 | |
| 189 | if (!GETP_VALID (s, s->getp + size)) |
| 190 | { |
| 191 | STREAM_BOUND_WARN (s, "seek getp"); |
| 192 | return; |
| 193 | } |
| 194 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 195 | s->getp += size; |
| 196 | } |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 197 | |
| 198 | void |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 199 | stream_forward_endp (struct stream *s, size_t size) |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 200 | { |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 201 | STREAM_VERIFY_SANE(s); |
| 202 | |
| 203 | if (!ENDP_VALID (s, s->endp + size)) |
| 204 | { |
| 205 | STREAM_BOUND_WARN (s, "seek endp"); |
| 206 | return; |
| 207 | } |
| 208 | |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 209 | s->endp += size; |
| 210 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 211 | |
| 212 | /* Copy from stream to destination. */ |
| 213 | void |
| 214 | stream_get (void *dst, struct stream *s, size_t size) |
| 215 | { |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 216 | STREAM_VERIFY_SANE(s); |
| 217 | |
| 218 | if (STREAM_READABLE(s) < size) |
| 219 | { |
| 220 | STREAM_BOUND_WARN (s, "get"); |
| 221 | return; |
| 222 | } |
| 223 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 224 | memcpy (dst, s->data + s->getp, size); |
| 225 | s->getp += size; |
| 226 | } |
| 227 | |
| 228 | /* Get next character from the stream. */ |
| 229 | u_char |
| 230 | stream_getc (struct stream *s) |
| 231 | { |
| 232 | u_char c; |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 233 | |
| 234 | STREAM_VERIFY_SANE (s); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 235 | |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 236 | if (STREAM_READABLE(s) < sizeof (u_char)) |
| 237 | { |
| 238 | STREAM_BOUND_WARN (s, "get char"); |
| 239 | return 0; |
| 240 | } |
| 241 | c = s->data[s->getp++]; |
| 242 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 243 | return c; |
| 244 | } |
| 245 | |
| 246 | /* Get next character from the stream. */ |
| 247 | u_char |
paul | f2e6c42 | 2005-02-12 14:35:49 +0000 | [diff] [blame] | 248 | stream_getc_from (struct stream *s, size_t from) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 249 | { |
| 250 | u_char c; |
| 251 | |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 252 | STREAM_VERIFY_SANE(s); |
| 253 | |
| 254 | if (!GETP_VALID (s, from + sizeof (u_char))) |
| 255 | { |
| 256 | STREAM_BOUND_WARN (s, "get char"); |
| 257 | return 0; |
| 258 | } |
| 259 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 260 | c = s->data[from]; |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 261 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 262 | return c; |
| 263 | } |
| 264 | |
| 265 | /* Get next word from the stream. */ |
| 266 | u_int16_t |
| 267 | stream_getw (struct stream *s) |
| 268 | { |
| 269 | u_int16_t w; |
| 270 | |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 271 | STREAM_VERIFY_SANE (s); |
| 272 | |
| 273 | if (STREAM_READABLE (s) < sizeof (u_int16_t)) |
| 274 | { |
| 275 | STREAM_BOUND_WARN (s, "get "); |
| 276 | return 0; |
| 277 | } |
| 278 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 279 | w = s->data[s->getp++] << 8; |
| 280 | w |= s->data[s->getp++]; |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 281 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 282 | return w; |
| 283 | } |
| 284 | |
| 285 | /* Get next word from the stream. */ |
| 286 | u_int16_t |
paul | f2e6c42 | 2005-02-12 14:35:49 +0000 | [diff] [blame] | 287 | stream_getw_from (struct stream *s, size_t from) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 288 | { |
| 289 | u_int16_t w; |
| 290 | |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 291 | STREAM_VERIFY_SANE(s); |
| 292 | |
| 293 | if (!GETP_VALID (s, from + sizeof (u_int16_t))) |
| 294 | { |
| 295 | STREAM_BOUND_WARN (s, "get "); |
| 296 | return 0; |
| 297 | } |
| 298 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 299 | w = s->data[from++] << 8; |
| 300 | w |= s->data[from]; |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 301 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 302 | return w; |
| 303 | } |
| 304 | |
| 305 | /* Get next long word from the stream. */ |
| 306 | u_int32_t |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 307 | stream_getl_from (struct stream *s, size_t from) |
| 308 | { |
| 309 | u_int32_t l; |
| 310 | |
| 311 | STREAM_VERIFY_SANE(s); |
| 312 | |
| 313 | if (!GETP_VALID (s, from + sizeof (u_int32_t))) |
| 314 | { |
| 315 | STREAM_BOUND_WARN (s, "get long"); |
| 316 | return 0; |
| 317 | } |
| 318 | |
| 319 | l = s->data[from++] << 24; |
| 320 | l |= s->data[from++] << 16; |
| 321 | l |= s->data[from++] << 8; |
| 322 | l |= s->data[from]; |
| 323 | |
| 324 | return l; |
| 325 | } |
| 326 | |
| 327 | u_int32_t |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 328 | stream_getl (struct stream *s) |
| 329 | { |
| 330 | u_int32_t l; |
| 331 | |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 332 | STREAM_VERIFY_SANE(s); |
| 333 | |
| 334 | if (STREAM_READABLE (s) < sizeof (u_int32_t)) |
| 335 | { |
| 336 | STREAM_BOUND_WARN (s, "get long"); |
| 337 | return 0; |
| 338 | } |
| 339 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 340 | l = s->data[s->getp++] << 24; |
| 341 | l |= s->data[s->getp++] << 16; |
| 342 | l |= s->data[s->getp++] << 8; |
| 343 | l |= s->data[s->getp++]; |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 344 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 345 | return l; |
| 346 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 347 | /* Get next long word from the stream. */ |
| 348 | u_int32_t |
| 349 | stream_get_ipv4 (struct stream *s) |
| 350 | { |
| 351 | u_int32_t l; |
| 352 | |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 353 | STREAM_VERIFY_SANE(s); |
| 354 | |
| 355 | if (STREAM_READABLE (s) < sizeof(u_int32_t)) |
| 356 | { |
| 357 | STREAM_BOUND_WARN (s, "get ipv4"); |
| 358 | return 0; |
| 359 | } |
| 360 | |
| 361 | memcpy (&l, s->data + s->getp, sizeof(u_int32_t)); |
| 362 | s->getp += sizeof(u_int32_t); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 363 | |
| 364 | return l; |
| 365 | } |
| 366 | |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 367 | /* Copy to source to stream. |
| 368 | * |
| 369 | * XXX: This uses CHECK_SIZE and hence has funny semantics -> Size will wrap |
| 370 | * around. This should be fixed once the stream updates are working. |
| 371 | */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 372 | void |
| 373 | stream_put (struct stream *s, void *src, size_t size) |
| 374 | { |
| 375 | |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 376 | /* XXX: CHECK_SIZE has strange semantics. It should be deprecated */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 377 | CHECK_SIZE(s, size); |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 378 | |
| 379 | STREAM_VERIFY_SANE(s); |
| 380 | |
| 381 | if (STREAM_WRITEABLE (s) < size) |
| 382 | { |
| 383 | STREAM_BOUND_WARN (s, "put"); |
| 384 | return; |
| 385 | } |
| 386 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 387 | if (src) |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 388 | memcpy (s->data + s->endp, src, size); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 389 | else |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 390 | memset (s->data + s->endp, 0, size); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 391 | |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 392 | s->endp += size; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | /* Put character to the stream. */ |
| 396 | int |
| 397 | stream_putc (struct stream *s, u_char c) |
| 398 | { |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 399 | STREAM_VERIFY_SANE(s); |
| 400 | |
| 401 | if (STREAM_WRITEABLE (s) < sizeof(u_char)) |
| 402 | { |
| 403 | STREAM_BOUND_WARN (s, "put"); |
| 404 | return 0; |
| 405 | } |
| 406 | |
| 407 | s->data[s->endp++] = c; |
| 408 | return sizeof (u_char); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | /* Put word to the stream. */ |
| 412 | int |
| 413 | stream_putw (struct stream *s, u_int16_t w) |
| 414 | { |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 415 | STREAM_VERIFY_SANE (s); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 416 | |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 417 | if (STREAM_WRITEABLE (s) < sizeof (u_int16_t)) |
| 418 | { |
| 419 | STREAM_BOUND_WARN (s, "put"); |
| 420 | return 0; |
| 421 | } |
| 422 | |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 423 | s->data[s->endp++] = (u_char)(w >> 8); |
| 424 | s->data[s->endp++] = (u_char) w; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 425 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 426 | return 2; |
| 427 | } |
| 428 | |
| 429 | /* Put long word to the stream. */ |
| 430 | int |
| 431 | stream_putl (struct stream *s, u_int32_t l) |
| 432 | { |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 433 | STREAM_VERIFY_SANE (s); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 434 | |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 435 | if (STREAM_WRITEABLE (s) < sizeof (u_int32_t)) |
| 436 | { |
| 437 | STREAM_BOUND_WARN (s, "put"); |
| 438 | return 0; |
| 439 | } |
| 440 | |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 441 | s->data[s->endp++] = (u_char)(l >> 24); |
| 442 | s->data[s->endp++] = (u_char)(l >> 16); |
| 443 | s->data[s->endp++] = (u_char)(l >> 8); |
| 444 | s->data[s->endp++] = (u_char)l; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 445 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 446 | return 4; |
| 447 | } |
| 448 | |
| 449 | int |
paul | f2e6c42 | 2005-02-12 14:35:49 +0000 | [diff] [blame] | 450 | stream_putc_at (struct stream *s, size_t putp, u_char c) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 451 | { |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 452 | STREAM_VERIFY_SANE(s); |
| 453 | |
| 454 | if (!PUT_AT_VALID (s, putp + sizeof (u_char))) |
| 455 | { |
| 456 | STREAM_BOUND_WARN (s, "put"); |
| 457 | return 0; |
| 458 | } |
| 459 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 460 | s->data[putp] = c; |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 461 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 462 | return 1; |
| 463 | } |
| 464 | |
| 465 | int |
paul | f2e6c42 | 2005-02-12 14:35:49 +0000 | [diff] [blame] | 466 | stream_putw_at (struct stream *s, size_t putp, u_int16_t w) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 467 | { |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 468 | STREAM_VERIFY_SANE(s); |
| 469 | |
| 470 | if (!PUT_AT_VALID (s, putp + sizeof (u_int16_t))) |
| 471 | { |
| 472 | STREAM_BOUND_WARN (s, "put"); |
| 473 | return 0; |
| 474 | } |
| 475 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 476 | s->data[putp] = (u_char)(w >> 8); |
| 477 | s->data[putp + 1] = (u_char) w; |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 478 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 479 | return 2; |
| 480 | } |
| 481 | |
| 482 | int |
paul | f2e6c42 | 2005-02-12 14:35:49 +0000 | [diff] [blame] | 483 | stream_putl_at (struct stream *s, size_t putp, u_int32_t l) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 484 | { |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 485 | STREAM_VERIFY_SANE(s); |
| 486 | |
| 487 | if (!PUT_AT_VALID (s, putp + sizeof (u_int32_t))) |
| 488 | { |
| 489 | STREAM_BOUND_WARN (s, "put"); |
| 490 | return 0; |
| 491 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 492 | s->data[putp] = (u_char)(l >> 24); |
| 493 | s->data[putp + 1] = (u_char)(l >> 16); |
| 494 | s->data[putp + 2] = (u_char)(l >> 8); |
| 495 | s->data[putp + 3] = (u_char)l; |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 496 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 497 | return 4; |
| 498 | } |
| 499 | |
| 500 | /* Put long word to the stream. */ |
| 501 | int |
| 502 | stream_put_ipv4 (struct stream *s, u_int32_t l) |
| 503 | { |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 504 | STREAM_VERIFY_SANE(s); |
| 505 | |
| 506 | if (STREAM_WRITEABLE (s) < sizeof (u_int32_t)) |
| 507 | { |
| 508 | STREAM_BOUND_WARN (s, "put"); |
| 509 | return 0; |
| 510 | } |
| 511 | memcpy (s->data + s->endp, &l, sizeof (u_int32_t)); |
| 512 | s->endp += sizeof (u_int32_t); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 513 | |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 514 | return sizeof (u_int32_t); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | /* Put long word to the stream. */ |
| 518 | int |
| 519 | stream_put_in_addr (struct stream *s, struct in_addr *addr) |
| 520 | { |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 521 | STREAM_VERIFY_SANE(s); |
| 522 | |
| 523 | if (STREAM_WRITEABLE (s) < sizeof (u_int32_t)) |
| 524 | { |
| 525 | STREAM_BOUND_WARN (s, "put"); |
| 526 | return 0; |
| 527 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 528 | |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 529 | memcpy (s->data + s->endp, addr, sizeof (u_int32_t)); |
| 530 | s->endp += sizeof (u_int32_t); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 531 | |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 532 | return sizeof (u_int32_t); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 533 | } |
| 534 | |
| 535 | /* Put prefix by nlri type format. */ |
| 536 | int |
| 537 | stream_put_prefix (struct stream *s, struct prefix *p) |
| 538 | { |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 539 | size_t psize; |
| 540 | |
| 541 | STREAM_VERIFY_SANE(s); |
| 542 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 543 | psize = PSIZE (p->prefixlen); |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 544 | |
| 545 | if (STREAM_WRITEABLE (s) < psize) |
| 546 | { |
| 547 | STREAM_BOUND_WARN (s, "put"); |
| 548 | return 0; |
| 549 | } |
| 550 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 551 | stream_putc (s, p->prefixlen); |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 552 | memcpy (s->data + s->endp, &p->u.prefix, psize); |
| 553 | s->endp += psize; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 554 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 555 | return psize; |
| 556 | } |
| 557 | |
| 558 | /* Read size from fd. */ |
| 559 | int |
| 560 | stream_read (struct stream *s, int fd, size_t size) |
| 561 | { |
| 562 | int nbytes; |
| 563 | |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 564 | STREAM_VERIFY_SANE(s); |
| 565 | |
| 566 | if (STREAM_WRITEABLE (s) < size) |
| 567 | { |
| 568 | STREAM_BOUND_WARN (s, "put"); |
| 569 | return 0; |
| 570 | } |
| 571 | |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 572 | nbytes = readn (fd, s->data + s->endp, size); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 573 | |
| 574 | if (nbytes > 0) |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 575 | s->endp += nbytes; |
| 576 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 577 | return nbytes; |
| 578 | } |
| 579 | |
| 580 | /* Read size from fd. */ |
| 581 | int |
| 582 | stream_read_unblock (struct stream *s, int fd, size_t size) |
| 583 | { |
| 584 | int nbytes; |
| 585 | int val; |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 586 | |
| 587 | STREAM_VERIFY_SANE(s); |
| 588 | |
| 589 | if (STREAM_WRITEABLE (s) < size) |
| 590 | { |
| 591 | STREAM_BOUND_WARN (s, "put"); |
| 592 | return 0; |
| 593 | } |
| 594 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 595 | val = fcntl (fd, F_GETFL, 0); |
| 596 | fcntl (fd, F_SETFL, val|O_NONBLOCK); |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 597 | nbytes = read (fd, s->data + s->endp, size); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 598 | fcntl (fd, F_SETFL, val); |
| 599 | |
| 600 | if (nbytes > 0) |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 601 | s->endp += nbytes; |
| 602 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 603 | return nbytes; |
| 604 | } |
| 605 | |
ajs | 262feb1 | 2005-02-16 20:35:47 +0000 | [diff] [blame] | 606 | ssize_t |
| 607 | stream_read_try(struct stream *s, int fd, size_t size) |
| 608 | { |
| 609 | ssize_t nbytes; |
| 610 | |
| 611 | STREAM_VERIFY_SANE(s); |
| 612 | |
| 613 | if (STREAM_WRITEABLE(s) < size) |
| 614 | { |
| 615 | STREAM_BOUND_WARN (s, "put"); |
| 616 | /* Fatal (not transient) error, since retrying will not help |
| 617 | (stream is too small to contain the desired data). */ |
| 618 | return -1; |
| 619 | } |
| 620 | |
| 621 | if ((nbytes = read(fd, s->data + s->endp, size)) >= 0) |
| 622 | { |
| 623 | s->endp += nbytes; |
| 624 | return nbytes; |
| 625 | } |
| 626 | /* Error: was it transient (return -2) or fatal (return -1)? */ |
ajs | 5327011 | 2005-02-17 20:07:22 +0000 | [diff] [blame] | 627 | return ERRNO_IO_RETRY(errno) ? -2 : -1; |
ajs | 262feb1 | 2005-02-16 20:35:47 +0000 | [diff] [blame] | 628 | } |
| 629 | |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 630 | /* Read up to smaller of size or SIZE_REMAIN() bytes to the stream, starting |
| 631 | * from endp. |
| 632 | * First iovec will be used to receive the data. |
| 633 | * Stream need not be empty. |
| 634 | */ |
| 635 | int |
| 636 | stream_recvmsg (struct stream *s, int fd, struct msghdr *msgh, int flags, |
| 637 | size_t size) |
| 638 | { |
| 639 | int nbytes; |
| 640 | struct iovec *iov; |
| 641 | |
| 642 | STREAM_VERIFY_SANE(s); |
| 643 | assert (msgh->msg_iovlen > 0); |
| 644 | |
| 645 | if (STREAM_WRITEABLE (s) < size) |
| 646 | { |
| 647 | STREAM_BOUND_WARN (s, "put"); |
ajs | 262feb1 | 2005-02-16 20:35:47 +0000 | [diff] [blame] | 648 | /* This is a logic error in the calling code: the stream is too small |
| 649 | to hold the desired data! */ |
| 650 | return -1; |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 651 | } |
| 652 | |
| 653 | iov = &(msgh->msg_iov[0]); |
| 654 | iov->iov_base = (s->data + s->endp); |
| 655 | iov->iov_len = size; |
| 656 | |
| 657 | nbytes = recvmsg (fd, msgh, flags); |
| 658 | |
| 659 | if (nbytes > 0) |
| 660 | s->endp += nbytes; |
| 661 | |
| 662 | return nbytes; |
| 663 | } |
| 664 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 665 | /* Write data to buffer. */ |
| 666 | int |
| 667 | stream_write (struct stream *s, u_char *ptr, size_t size) |
| 668 | { |
| 669 | |
| 670 | CHECK_SIZE(s, size); |
| 671 | |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 672 | STREAM_VERIFY_SANE(s); |
| 673 | |
| 674 | if (STREAM_WRITEABLE (s) < size) |
| 675 | { |
| 676 | STREAM_BOUND_WARN (s, "put"); |
| 677 | return 0; |
| 678 | } |
| 679 | |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 680 | memcpy (s->data + s->endp, ptr, size); |
| 681 | s->endp += size; |
| 682 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 683 | return size; |
| 684 | } |
| 685 | |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 686 | /* Return current read pointer. |
| 687 | * DEPRECATED! |
| 688 | * Use stream_get_pnt_to if you must, but decoding streams properly |
| 689 | * is preferred |
| 690 | */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 691 | u_char * |
| 692 | stream_pnt (struct stream *s) |
| 693 | { |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 694 | STREAM_VERIFY_SANE(s); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 695 | return s->data + s->getp; |
| 696 | } |
| 697 | |
| 698 | /* Check does this stream empty? */ |
| 699 | int |
| 700 | stream_empty (struct stream *s) |
| 701 | { |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 702 | STREAM_VERIFY_SANE(s); |
| 703 | |
| 704 | return (s->endp == 0); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 705 | } |
| 706 | |
| 707 | /* Reset stream. */ |
| 708 | void |
| 709 | stream_reset (struct stream *s) |
| 710 | { |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 711 | STREAM_VERIFY_SANE (s); |
| 712 | |
| 713 | s->getp = s->endp = 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 714 | } |
| 715 | |
| 716 | /* Write stream contens to the file discriptor. */ |
| 717 | int |
| 718 | stream_flush (struct stream *s, int fd) |
| 719 | { |
| 720 | int nbytes; |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 721 | |
| 722 | STREAM_VERIFY_SANE(s); |
| 723 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 724 | nbytes = write (fd, s->data + s->getp, s->endp - s->getp); |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 725 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 726 | return nbytes; |
| 727 | } |
| 728 | |
| 729 | /* Stream first in first out queue. */ |
| 730 | |
| 731 | struct stream_fifo * |
| 732 | stream_fifo_new () |
| 733 | { |
| 734 | struct stream_fifo *new; |
| 735 | |
| 736 | new = XCALLOC (MTYPE_STREAM_FIFO, sizeof (struct stream_fifo)); |
| 737 | return new; |
| 738 | } |
| 739 | |
| 740 | /* Add new stream to fifo. */ |
| 741 | void |
| 742 | stream_fifo_push (struct stream_fifo *fifo, struct stream *s) |
| 743 | { |
| 744 | if (fifo->tail) |
| 745 | fifo->tail->next = s; |
| 746 | else |
| 747 | fifo->head = s; |
| 748 | |
| 749 | fifo->tail = s; |
| 750 | |
| 751 | fifo->count++; |
| 752 | } |
| 753 | |
| 754 | /* Delete first stream from fifo. */ |
| 755 | struct stream * |
| 756 | stream_fifo_pop (struct stream_fifo *fifo) |
| 757 | { |
| 758 | struct stream *s; |
| 759 | |
| 760 | s = fifo->head; |
| 761 | |
| 762 | if (s) |
| 763 | { |
| 764 | fifo->head = s->next; |
| 765 | |
| 766 | if (fifo->head == NULL) |
| 767 | fifo->tail = NULL; |
| 768 | } |
| 769 | |
| 770 | fifo->count--; |
| 771 | |
| 772 | return s; |
| 773 | } |
| 774 | |
| 775 | /* Return first fifo entry. */ |
| 776 | struct stream * |
| 777 | stream_fifo_head (struct stream_fifo *fifo) |
| 778 | { |
| 779 | return fifo->head; |
| 780 | } |
| 781 | |
| 782 | void |
| 783 | stream_fifo_clean (struct stream_fifo *fifo) |
| 784 | { |
| 785 | struct stream *s; |
| 786 | struct stream *next; |
| 787 | |
| 788 | for (s = fifo->head; s; s = next) |
| 789 | { |
| 790 | next = s->next; |
| 791 | stream_free (s); |
| 792 | } |
| 793 | fifo->head = fifo->tail = NULL; |
| 794 | fifo->count = 0; |
| 795 | } |
| 796 | |
| 797 | void |
| 798 | stream_fifo_free (struct stream_fifo *fifo) |
| 799 | { |
| 800 | stream_fifo_clean (fifo); |
| 801 | XFREE (MTYPE_STREAM_FIFO, fifo); |
| 802 | } |