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 | |
| 23 | #include <zebra.h> |
David Lamparter | 821df2c | 2015-09-15 01:53:09 -0700 | [diff] [blame] | 24 | #include <stddef.h> |
Paul Jakma | d41e7b9 | 2015-05-19 15:38:43 +0100 | [diff] [blame^] | 25 | /* primarily for __STDC_IEC_559__ with clang */ |
| 26 | #include <features.h> |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 27 | |
| 28 | #include "stream.h" |
| 29 | #include "memory.h" |
| 30 | #include "network.h" |
| 31 | #include "prefix.h" |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 32 | #include "log.h" |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 33 | |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 34 | /* Tests whether a position is valid */ |
| 35 | #define GETP_VALID(S,G) \ |
| 36 | ((G) <= (S)->endp) |
| 37 | #define PUT_AT_VALID(S,G) GETP_VALID(S,G) |
| 38 | #define ENDP_VALID(S,E) \ |
| 39 | ((E) <= (S)->size) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 40 | |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 41 | /* asserting sanity checks. Following must be true before |
| 42 | * stream functions are called: |
| 43 | * |
| 44 | * Following must always be true of stream elements |
| 45 | * before and after calls to stream functions: |
| 46 | * |
| 47 | * getp <= endp <= size |
| 48 | * |
| 49 | * Note that after a stream function is called following may be true: |
| 50 | * if (getp == endp) then stream is no longer readable |
| 51 | * if (endp == size) then stream is no longer writeable |
| 52 | * |
| 53 | * It is valid to put to anywhere within the size of the stream, but only |
| 54 | * using stream_put..._at() functions. |
| 55 | */ |
| 56 | #define STREAM_WARN_OFFSETS(S) \ |
Subbaiah Venkata | d531050 | 2012-03-24 13:10:19 -0700 | [diff] [blame] | 57 | zlog_warn ("&(struct stream): %p, size: %lu, getp: %lu, endp: %lu\n", \ |
David Lamparter | eed3c48 | 2015-03-03 08:51:53 +0100 | [diff] [blame] | 58 | (void *)(S), \ |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 59 | (unsigned long) (S)->size, \ |
| 60 | (unsigned long) (S)->getp, \ |
| 61 | (unsigned long) (S)->endp)\ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 62 | |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 63 | #define STREAM_VERIFY_SANE(S) \ |
| 64 | do { \ |
Wenjian Ma | 1ed8ce4 | 2015-06-19 10:53:26 +0800 | [diff] [blame] | 65 | if ( !(GETP_VALID(S, (S)->getp) && ENDP_VALID(S, (S)->endp)) ) \ |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 66 | STREAM_WARN_OFFSETS(S); \ |
| 67 | assert ( GETP_VALID(S, (S)->getp) ); \ |
| 68 | assert ( ENDP_VALID(S, (S)->endp) ); \ |
| 69 | } while (0) |
| 70 | |
| 71 | #define STREAM_BOUND_WARN(S, WHAT) \ |
| 72 | do { \ |
| 73 | zlog_warn ("%s: Attempt to %s out of bounds", __func__, (WHAT)); \ |
| 74 | STREAM_WARN_OFFSETS(S); \ |
| 75 | assert (0); \ |
| 76 | } while (0) |
| 77 | |
| 78 | /* XXX: Deprecated macro: do not use */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 79 | #define CHECK_SIZE(S, Z) \ |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 80 | do { \ |
| 81 | if (((S)->endp + (Z)) > (S)->size) \ |
| 82 | { \ |
| 83 | zlog_warn ("CHECK_SIZE: truncating requested size %lu\n", \ |
| 84 | (unsigned long) (Z)); \ |
| 85 | STREAM_WARN_OFFSETS(S); \ |
| 86 | (Z) = (S)->size - (S)->endp; \ |
| 87 | } \ |
| 88 | } while (0); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 89 | |
| 90 | /* Make stream buffer. */ |
| 91 | struct stream * |
| 92 | stream_new (size_t size) |
| 93 | { |
| 94 | struct stream *s; |
| 95 | |
paul | 0e43a2b | 2004-12-22 00:15:34 +0000 | [diff] [blame] | 96 | assert (size > 0); |
| 97 | |
| 98 | if (size == 0) |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 99 | { |
| 100 | zlog_warn ("stream_new(): called with 0 size!"); |
| 101 | return NULL; |
| 102 | } |
paul | 0e43a2b | 2004-12-22 00:15:34 +0000 | [diff] [blame] | 103 | |
paul | 4b201d4 | 2006-01-10 14:35:19 +0000 | [diff] [blame] | 104 | s = XCALLOC (MTYPE_STREAM, sizeof (struct stream)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 105 | |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 106 | if (s == NULL) |
| 107 | return s; |
| 108 | |
paul | 4b201d4 | 2006-01-10 14:35:19 +0000 | [diff] [blame] | 109 | if ( (s->data = XMALLOC (MTYPE_STREAM_DATA, size)) == NULL) |
| 110 | { |
| 111 | XFREE (MTYPE_STREAM, s); |
| 112 | return NULL; |
| 113 | } |
| 114 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 115 | s->size = size; |
| 116 | return s; |
| 117 | } |
| 118 | |
| 119 | /* Free it now. */ |
| 120 | void |
| 121 | stream_free (struct stream *s) |
| 122 | { |
paul | 4b201d4 | 2006-01-10 14:35:19 +0000 | [diff] [blame] | 123 | if (!s) |
| 124 | return; |
| 125 | |
| 126 | XFREE (MTYPE_STREAM_DATA, s->data); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 127 | XFREE (MTYPE_STREAM, s); |
| 128 | } |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 129 | |
| 130 | struct stream * |
| 131 | stream_copy (struct stream *new, struct stream *src) |
| 132 | { |
| 133 | STREAM_VERIFY_SANE (src); |
| 134 | |
| 135 | assert (new != NULL); |
| 136 | assert (STREAM_SIZE(new) >= src->endp); |
| 137 | |
| 138 | new->endp = src->endp; |
| 139 | new->getp = src->getp; |
| 140 | |
| 141 | memcpy (new->data, src->data, src->endp); |
| 142 | |
| 143 | return new; |
| 144 | } |
| 145 | |
| 146 | struct stream * |
| 147 | stream_dup (struct stream *s) |
| 148 | { |
| 149 | struct stream *new; |
| 150 | |
| 151 | STREAM_VERIFY_SANE (s); |
| 152 | |
| 153 | if ( (new = stream_new (s->endp)) == NULL) |
| 154 | return NULL; |
| 155 | |
| 156 | return (stream_copy (new, s)); |
| 157 | } |
paul | 4b201d4 | 2006-01-10 14:35:19 +0000 | [diff] [blame] | 158 | |
Pradosh Mohapatra | 8c71e48 | 2014-01-15 06:57:57 +0000 | [diff] [blame] | 159 | struct stream * |
| 160 | stream_dupcat (struct stream *s1, struct stream *s2, size_t offset) |
| 161 | { |
| 162 | struct stream *new; |
| 163 | |
| 164 | STREAM_VERIFY_SANE (s1); |
| 165 | STREAM_VERIFY_SANE (s2); |
| 166 | |
| 167 | if ( (new = stream_new (s1->endp + s2->endp)) == NULL) |
| 168 | return NULL; |
| 169 | |
| 170 | memcpy (new->data, s1->data, offset); |
| 171 | memcpy (new->data + offset, s2->data, s2->endp); |
| 172 | memcpy (new->data + offset + s2->endp, s1->data + offset, |
| 173 | (s1->endp - offset)); |
| 174 | new->endp = s1->endp + s2->endp; |
| 175 | return new; |
| 176 | } |
| 177 | |
paul | 4b201d4 | 2006-01-10 14:35:19 +0000 | [diff] [blame] | 178 | size_t |
| 179 | stream_resize (struct stream *s, size_t newsize) |
| 180 | { |
| 181 | u_char *newdata; |
| 182 | STREAM_VERIFY_SANE (s); |
| 183 | |
| 184 | newdata = XREALLOC (MTYPE_STREAM_DATA, s->data, newsize); |
| 185 | |
| 186 | if (newdata == NULL) |
| 187 | return s->size; |
| 188 | |
| 189 | s->data = newdata; |
| 190 | s->size = newsize; |
| 191 | |
| 192 | if (s->endp > s->size) |
| 193 | s->endp = s->size; |
| 194 | if (s->getp > s->endp) |
| 195 | s->getp = s->endp; |
| 196 | |
| 197 | STREAM_VERIFY_SANE (s); |
| 198 | |
| 199 | return s->size; |
| 200 | } |
David Lamparter | 6b0655a | 2014-06-04 06:53:35 +0200 | [diff] [blame] | 201 | |
paul | f2e6c42 | 2005-02-12 14:35:49 +0000 | [diff] [blame] | 202 | size_t |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 203 | stream_get_getp (struct stream *s) |
| 204 | { |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 205 | STREAM_VERIFY_SANE(s); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 206 | return s->getp; |
| 207 | } |
| 208 | |
paul | f2e6c42 | 2005-02-12 14:35:49 +0000 | [diff] [blame] | 209 | size_t |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 210 | stream_get_endp (struct stream *s) |
| 211 | { |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 212 | STREAM_VERIFY_SANE(s); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 213 | return s->endp; |
| 214 | } |
| 215 | |
paul | f2e6c42 | 2005-02-12 14:35:49 +0000 | [diff] [blame] | 216 | size_t |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 217 | stream_get_size (struct stream *s) |
| 218 | { |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 219 | STREAM_VERIFY_SANE(s); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 220 | return s->size; |
| 221 | } |
| 222 | |
| 223 | /* Stream structre' stream pointer related functions. */ |
| 224 | void |
paul | f2e6c42 | 2005-02-12 14:35:49 +0000 | [diff] [blame] | 225 | stream_set_getp (struct stream *s, size_t pos) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 226 | { |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 227 | STREAM_VERIFY_SANE(s); |
| 228 | |
| 229 | if (!GETP_VALID (s, pos)) |
| 230 | { |
| 231 | STREAM_BOUND_WARN (s, "set getp"); |
| 232 | pos = s->endp; |
| 233 | } |
| 234 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 235 | s->getp = pos; |
| 236 | } |
| 237 | |
Subbaiah Venkata | d531050 | 2012-03-24 13:10:19 -0700 | [diff] [blame] | 238 | void |
| 239 | stream_set_endp (struct stream *s, size_t pos) |
| 240 | { |
| 241 | STREAM_VERIFY_SANE(s); |
| 242 | |
Avneesh Sachdev | 4effc29 | 2012-05-05 17:42:43 -0700 | [diff] [blame] | 243 | if (!ENDP_VALID(s, pos)) |
Subbaiah Venkata | d531050 | 2012-03-24 13:10:19 -0700 | [diff] [blame] | 244 | { |
| 245 | STREAM_BOUND_WARN (s, "set endp"); |
Avneesh Sachdev | 4effc29 | 2012-05-05 17:42:43 -0700 | [diff] [blame] | 246 | return; |
| 247 | } |
| 248 | |
| 249 | /* |
| 250 | * Make sure the current read pointer is not beyond the new endp. |
| 251 | */ |
| 252 | if (s->getp > pos) |
| 253 | { |
| 254 | STREAM_BOUND_WARN(s, "set endp"); |
| 255 | return; |
Subbaiah Venkata | d531050 | 2012-03-24 13:10:19 -0700 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | s->endp = pos; |
Avneesh Sachdev | 4effc29 | 2012-05-05 17:42:43 -0700 | [diff] [blame] | 259 | STREAM_VERIFY_SANE(s); |
Subbaiah Venkata | d531050 | 2012-03-24 13:10:19 -0700 | [diff] [blame] | 260 | } |
| 261 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 262 | /* Forward pointer. */ |
| 263 | void |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 264 | stream_forward_getp (struct stream *s, size_t size) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 265 | { |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 266 | STREAM_VERIFY_SANE(s); |
| 267 | |
| 268 | if (!GETP_VALID (s, s->getp + size)) |
| 269 | { |
| 270 | STREAM_BOUND_WARN (s, "seek getp"); |
| 271 | return; |
| 272 | } |
| 273 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 274 | s->getp += size; |
| 275 | } |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 276 | |
| 277 | void |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 278 | stream_forward_endp (struct stream *s, size_t size) |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 279 | { |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 280 | STREAM_VERIFY_SANE(s); |
| 281 | |
| 282 | if (!ENDP_VALID (s, s->endp + size)) |
| 283 | { |
| 284 | STREAM_BOUND_WARN (s, "seek endp"); |
| 285 | return; |
| 286 | } |
| 287 | |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 288 | s->endp += size; |
| 289 | } |
David Lamparter | 6b0655a | 2014-06-04 06:53:35 +0200 | [diff] [blame] | 290 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 291 | /* Copy from stream to destination. */ |
| 292 | void |
| 293 | stream_get (void *dst, struct stream *s, size_t size) |
| 294 | { |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 295 | STREAM_VERIFY_SANE(s); |
| 296 | |
| 297 | if (STREAM_READABLE(s) < size) |
| 298 | { |
| 299 | STREAM_BOUND_WARN (s, "get"); |
| 300 | return; |
| 301 | } |
| 302 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 303 | memcpy (dst, s->data + s->getp, size); |
| 304 | s->getp += size; |
| 305 | } |
| 306 | |
| 307 | /* Get next character from the stream. */ |
| 308 | u_char |
| 309 | stream_getc (struct stream *s) |
| 310 | { |
| 311 | u_char c; |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 312 | |
| 313 | STREAM_VERIFY_SANE (s); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 314 | |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 315 | if (STREAM_READABLE(s) < sizeof (u_char)) |
| 316 | { |
| 317 | STREAM_BOUND_WARN (s, "get char"); |
| 318 | return 0; |
| 319 | } |
| 320 | c = s->data[s->getp++]; |
| 321 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 322 | return c; |
| 323 | } |
| 324 | |
| 325 | /* Get next character from the stream. */ |
| 326 | u_char |
paul | f2e6c42 | 2005-02-12 14:35:49 +0000 | [diff] [blame] | 327 | stream_getc_from (struct stream *s, size_t from) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 328 | { |
| 329 | u_char c; |
| 330 | |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 331 | STREAM_VERIFY_SANE(s); |
| 332 | |
| 333 | if (!GETP_VALID (s, from + sizeof (u_char))) |
| 334 | { |
| 335 | STREAM_BOUND_WARN (s, "get char"); |
| 336 | return 0; |
| 337 | } |
| 338 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 339 | c = s->data[from]; |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 340 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 341 | return c; |
| 342 | } |
| 343 | |
| 344 | /* Get next word from the stream. */ |
| 345 | u_int16_t |
| 346 | stream_getw (struct stream *s) |
| 347 | { |
| 348 | u_int16_t w; |
| 349 | |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 350 | STREAM_VERIFY_SANE (s); |
| 351 | |
| 352 | if (STREAM_READABLE (s) < sizeof (u_int16_t)) |
| 353 | { |
| 354 | STREAM_BOUND_WARN (s, "get "); |
| 355 | return 0; |
| 356 | } |
| 357 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 358 | w = s->data[s->getp++] << 8; |
| 359 | w |= s->data[s->getp++]; |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 360 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 361 | return w; |
| 362 | } |
| 363 | |
| 364 | /* Get next word from the stream. */ |
| 365 | u_int16_t |
paul | f2e6c42 | 2005-02-12 14:35:49 +0000 | [diff] [blame] | 366 | stream_getw_from (struct stream *s, size_t from) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 367 | { |
| 368 | u_int16_t w; |
| 369 | |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 370 | STREAM_VERIFY_SANE(s); |
| 371 | |
| 372 | if (!GETP_VALID (s, from + sizeof (u_int16_t))) |
| 373 | { |
| 374 | STREAM_BOUND_WARN (s, "get "); |
| 375 | return 0; |
| 376 | } |
| 377 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 378 | w = s->data[from++] << 8; |
| 379 | w |= s->data[from]; |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 380 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 381 | return w; |
| 382 | } |
| 383 | |
| 384 | /* Get next long word from the stream. */ |
| 385 | u_int32_t |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 386 | stream_getl_from (struct stream *s, size_t from) |
| 387 | { |
| 388 | u_int32_t l; |
| 389 | |
| 390 | STREAM_VERIFY_SANE(s); |
| 391 | |
| 392 | if (!GETP_VALID (s, from + sizeof (u_int32_t))) |
| 393 | { |
| 394 | STREAM_BOUND_WARN (s, "get long"); |
| 395 | return 0; |
| 396 | } |
| 397 | |
| 398 | l = s->data[from++] << 24; |
| 399 | l |= s->data[from++] << 16; |
| 400 | l |= s->data[from++] << 8; |
| 401 | l |= s->data[from]; |
| 402 | |
| 403 | return l; |
| 404 | } |
| 405 | |
| 406 | u_int32_t |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 407 | stream_getl (struct stream *s) |
| 408 | { |
| 409 | u_int32_t l; |
| 410 | |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 411 | STREAM_VERIFY_SANE(s); |
| 412 | |
| 413 | if (STREAM_READABLE (s) < sizeof (u_int32_t)) |
| 414 | { |
| 415 | STREAM_BOUND_WARN (s, "get long"); |
| 416 | return 0; |
| 417 | } |
| 418 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 419 | l = s->data[s->getp++] << 24; |
| 420 | l |= s->data[s->getp++] << 16; |
| 421 | l |= s->data[s->getp++] << 8; |
| 422 | l |= s->data[s->getp++]; |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 423 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 424 | return l; |
| 425 | } |
paul | 4b201d4 | 2006-01-10 14:35:19 +0000 | [diff] [blame] | 426 | |
| 427 | /* Get next quad word from the stream. */ |
| 428 | uint64_t |
| 429 | stream_getq_from (struct stream *s, size_t from) |
| 430 | { |
paul | 581a02a | 2006-01-19 20:22:16 +0000 | [diff] [blame] | 431 | uint64_t q; |
paul | 4b201d4 | 2006-01-10 14:35:19 +0000 | [diff] [blame] | 432 | |
| 433 | STREAM_VERIFY_SANE(s); |
| 434 | |
| 435 | if (!GETP_VALID (s, from + sizeof (uint64_t))) |
| 436 | { |
| 437 | STREAM_BOUND_WARN (s, "get quad"); |
| 438 | return 0; |
| 439 | } |
| 440 | |
| 441 | q = ((uint64_t) s->data[from++]) << 56; |
| 442 | q |= ((uint64_t) s->data[from++]) << 48; |
| 443 | q |= ((uint64_t) s->data[from++]) << 40; |
| 444 | q |= ((uint64_t) s->data[from++]) << 32; |
| 445 | q |= ((uint64_t) s->data[from++]) << 24; |
| 446 | q |= ((uint64_t) s->data[from++]) << 16; |
| 447 | q |= ((uint64_t) s->data[from++]) << 8; |
| 448 | q |= ((uint64_t) s->data[from++]); |
| 449 | |
| 450 | return q; |
| 451 | } |
| 452 | |
| 453 | uint64_t |
| 454 | stream_getq (struct stream *s) |
| 455 | { |
| 456 | uint64_t q; |
| 457 | |
| 458 | STREAM_VERIFY_SANE(s); |
| 459 | |
| 460 | if (STREAM_READABLE (s) < sizeof (uint64_t)) |
| 461 | { |
| 462 | STREAM_BOUND_WARN (s, "get quad"); |
| 463 | return 0; |
| 464 | } |
| 465 | |
| 466 | q = ((uint64_t) s->data[s->getp++]) << 56; |
| 467 | q |= ((uint64_t) s->data[s->getp++]) << 48; |
| 468 | q |= ((uint64_t) s->data[s->getp++]) << 40; |
| 469 | q |= ((uint64_t) s->data[s->getp++]) << 32; |
| 470 | q |= ((uint64_t) s->data[s->getp++]) << 24; |
| 471 | q |= ((uint64_t) s->data[s->getp++]) << 16; |
| 472 | q |= ((uint64_t) s->data[s->getp++]) << 8; |
| 473 | q |= ((uint64_t) s->data[s->getp++]); |
| 474 | |
| 475 | return q; |
| 476 | } |
| 477 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 478 | /* Get next long word from the stream. */ |
| 479 | u_int32_t |
| 480 | stream_get_ipv4 (struct stream *s) |
| 481 | { |
| 482 | u_int32_t l; |
| 483 | |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 484 | STREAM_VERIFY_SANE(s); |
| 485 | |
| 486 | if (STREAM_READABLE (s) < sizeof(u_int32_t)) |
| 487 | { |
| 488 | STREAM_BOUND_WARN (s, "get ipv4"); |
| 489 | return 0; |
| 490 | } |
| 491 | |
| 492 | memcpy (&l, s->data + s->getp, sizeof(u_int32_t)); |
| 493 | s->getp += sizeof(u_int32_t); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 494 | |
| 495 | return l; |
| 496 | } |
David Lamparter | 6b0655a | 2014-06-04 06:53:35 +0200 | [diff] [blame] | 497 | |
Paul Jakma | d41e7b9 | 2015-05-19 15:38:43 +0100 | [diff] [blame^] | 498 | float |
| 499 | stream_getf (struct stream *s) |
| 500 | { |
| 501 | #if defined(__STDC_IEC_559__) || __GCC_IEC_559 >= 1 |
| 502 | /* we can safely assume 'float' is in the single precision |
| 503 | IEC 60559 binary format in host order */ |
| 504 | union { |
| 505 | float r; |
| 506 | uint32_t d; |
| 507 | } u; |
| 508 | u.d = stream_getl (s); |
| 509 | return u.r; |
| 510 | #else |
| 511 | #error "Please supply stream_getf implementation for this platform" |
| 512 | #endif |
| 513 | } |
| 514 | |
| 515 | double |
| 516 | stream_getd (struct stream *s) |
| 517 | { |
| 518 | #if defined(__STDC_IEC_559__) || __GCC_IEC_559 >= 1 |
| 519 | union { |
| 520 | double r; |
| 521 | uint64_t d; |
| 522 | } u; |
| 523 | u.d = stream_getq (s); |
| 524 | return u.r; |
| 525 | #else |
| 526 | #error "Please supply stream_getd implementation for this platform" |
| 527 | #endif |
| 528 | } |
| 529 | |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 530 | /* Copy to source to stream. |
| 531 | * |
| 532 | * XXX: This uses CHECK_SIZE and hence has funny semantics -> Size will wrap |
| 533 | * around. This should be fixed once the stream updates are working. |
paul | 0dab930 | 2005-05-03 09:07:56 +0000 | [diff] [blame] | 534 | * |
| 535 | * stream_write() is saner |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 536 | */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 537 | void |
Paul Jakma | 3d52bb8 | 2008-06-07 20:42:07 +0000 | [diff] [blame] | 538 | stream_put (struct stream *s, const void *src, size_t size) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 539 | { |
| 540 | |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 541 | /* XXX: CHECK_SIZE has strange semantics. It should be deprecated */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 542 | CHECK_SIZE(s, size); |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 543 | |
| 544 | STREAM_VERIFY_SANE(s); |
| 545 | |
| 546 | if (STREAM_WRITEABLE (s) < size) |
| 547 | { |
| 548 | STREAM_BOUND_WARN (s, "put"); |
| 549 | return; |
| 550 | } |
| 551 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 552 | if (src) |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 553 | memcpy (s->data + s->endp, src, size); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 554 | else |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 555 | memset (s->data + s->endp, 0, size); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 556 | |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 557 | s->endp += size; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | /* Put character to the stream. */ |
| 561 | int |
| 562 | stream_putc (struct stream *s, u_char c) |
| 563 | { |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 564 | STREAM_VERIFY_SANE(s); |
| 565 | |
| 566 | if (STREAM_WRITEABLE (s) < sizeof(u_char)) |
| 567 | { |
| 568 | STREAM_BOUND_WARN (s, "put"); |
| 569 | return 0; |
| 570 | } |
| 571 | |
| 572 | s->data[s->endp++] = c; |
| 573 | return sizeof (u_char); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | /* Put word to the stream. */ |
| 577 | int |
| 578 | stream_putw (struct stream *s, u_int16_t w) |
| 579 | { |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 580 | STREAM_VERIFY_SANE (s); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 581 | |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 582 | if (STREAM_WRITEABLE (s) < sizeof (u_int16_t)) |
| 583 | { |
| 584 | STREAM_BOUND_WARN (s, "put"); |
| 585 | return 0; |
| 586 | } |
| 587 | |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 588 | s->data[s->endp++] = (u_char)(w >> 8); |
| 589 | s->data[s->endp++] = (u_char) w; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 590 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 591 | return 2; |
| 592 | } |
| 593 | |
| 594 | /* Put long word to the stream. */ |
| 595 | int |
| 596 | stream_putl (struct stream *s, u_int32_t l) |
| 597 | { |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 598 | STREAM_VERIFY_SANE (s); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 599 | |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 600 | if (STREAM_WRITEABLE (s) < sizeof (u_int32_t)) |
| 601 | { |
| 602 | STREAM_BOUND_WARN (s, "put"); |
| 603 | return 0; |
| 604 | } |
| 605 | |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 606 | s->data[s->endp++] = (u_char)(l >> 24); |
| 607 | s->data[s->endp++] = (u_char)(l >> 16); |
| 608 | s->data[s->endp++] = (u_char)(l >> 8); |
| 609 | s->data[s->endp++] = (u_char)l; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 610 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 611 | return 4; |
| 612 | } |
| 613 | |
paul | 4b201d4 | 2006-01-10 14:35:19 +0000 | [diff] [blame] | 614 | /* Put quad word to the stream. */ |
| 615 | int |
| 616 | stream_putq (struct stream *s, uint64_t q) |
| 617 | { |
| 618 | STREAM_VERIFY_SANE (s); |
| 619 | |
| 620 | if (STREAM_WRITEABLE (s) < sizeof (uint64_t)) |
| 621 | { |
| 622 | STREAM_BOUND_WARN (s, "put quad"); |
| 623 | return 0; |
| 624 | } |
| 625 | |
| 626 | s->data[s->endp++] = (u_char)(q >> 56); |
| 627 | s->data[s->endp++] = (u_char)(q >> 48); |
| 628 | s->data[s->endp++] = (u_char)(q >> 40); |
| 629 | s->data[s->endp++] = (u_char)(q >> 32); |
| 630 | s->data[s->endp++] = (u_char)(q >> 24); |
| 631 | s->data[s->endp++] = (u_char)(q >> 16); |
| 632 | s->data[s->endp++] = (u_char)(q >> 8); |
| 633 | s->data[s->endp++] = (u_char)q; |
| 634 | |
| 635 | return 8; |
| 636 | } |
| 637 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 638 | int |
Paul Jakma | d41e7b9 | 2015-05-19 15:38:43 +0100 | [diff] [blame^] | 639 | stream_putf (struct stream *s, float f) |
| 640 | { |
| 641 | #if defined(__STDC_IEC_559__) || __GCC_IEC_559 >= 1 |
| 642 | /* we can safely assume 'float' is in the single precision |
| 643 | IEC 60559 binary format in host order */ |
| 644 | union { |
| 645 | float i; |
| 646 | uint32_t o; |
| 647 | } u; |
| 648 | u.i = f; |
| 649 | return stream_putl (s, u.o); |
| 650 | #else |
| 651 | #error "Please supply stream_putf implementation for this platform" |
| 652 | #endif |
| 653 | } |
| 654 | |
| 655 | int |
| 656 | stream_putd (struct stream *s, double d) |
| 657 | { |
| 658 | #if defined(__STDC_IEC_559__) || __GCC_IEC_559 >= 1 |
| 659 | union { |
| 660 | double i; |
| 661 | uint64_t o; |
| 662 | } u; |
| 663 | u.i = d; |
| 664 | return stream_putq (s, u.o); |
| 665 | #else |
| 666 | #error "Please supply stream_putd implementation for this platform" |
| 667 | #endif |
| 668 | } |
| 669 | |
| 670 | int |
paul | f2e6c42 | 2005-02-12 14:35:49 +0000 | [diff] [blame] | 671 | stream_putc_at (struct stream *s, size_t putp, u_char c) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 672 | { |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 673 | STREAM_VERIFY_SANE(s); |
| 674 | |
| 675 | if (!PUT_AT_VALID (s, putp + sizeof (u_char))) |
| 676 | { |
| 677 | STREAM_BOUND_WARN (s, "put"); |
| 678 | return 0; |
| 679 | } |
| 680 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 681 | s->data[putp] = c; |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 682 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 683 | return 1; |
| 684 | } |
| 685 | |
| 686 | int |
paul | f2e6c42 | 2005-02-12 14:35:49 +0000 | [diff] [blame] | 687 | stream_putw_at (struct stream *s, size_t putp, u_int16_t w) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 688 | { |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 689 | STREAM_VERIFY_SANE(s); |
| 690 | |
| 691 | if (!PUT_AT_VALID (s, putp + sizeof (u_int16_t))) |
| 692 | { |
| 693 | STREAM_BOUND_WARN (s, "put"); |
| 694 | return 0; |
| 695 | } |
| 696 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 697 | s->data[putp] = (u_char)(w >> 8); |
| 698 | s->data[putp + 1] = (u_char) w; |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 699 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 700 | return 2; |
| 701 | } |
| 702 | |
| 703 | int |
paul | f2e6c42 | 2005-02-12 14:35:49 +0000 | [diff] [blame] | 704 | stream_putl_at (struct stream *s, size_t putp, u_int32_t l) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 705 | { |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 706 | STREAM_VERIFY_SANE(s); |
| 707 | |
| 708 | if (!PUT_AT_VALID (s, putp + sizeof (u_int32_t))) |
| 709 | { |
| 710 | STREAM_BOUND_WARN (s, "put"); |
| 711 | return 0; |
| 712 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 713 | s->data[putp] = (u_char)(l >> 24); |
| 714 | s->data[putp + 1] = (u_char)(l >> 16); |
| 715 | s->data[putp + 2] = (u_char)(l >> 8); |
| 716 | s->data[putp + 3] = (u_char)l; |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 717 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 718 | return 4; |
| 719 | } |
| 720 | |
paul | 4b201d4 | 2006-01-10 14:35:19 +0000 | [diff] [blame] | 721 | int |
| 722 | stream_putq_at (struct stream *s, size_t putp, uint64_t q) |
| 723 | { |
| 724 | STREAM_VERIFY_SANE(s); |
| 725 | |
| 726 | if (!PUT_AT_VALID (s, putp + sizeof (uint64_t))) |
| 727 | { |
| 728 | STREAM_BOUND_WARN (s, "put"); |
| 729 | return 0; |
| 730 | } |
| 731 | s->data[putp] = (u_char)(q >> 56); |
| 732 | s->data[putp + 1] = (u_char)(q >> 48); |
| 733 | s->data[putp + 2] = (u_char)(q >> 40); |
| 734 | s->data[putp + 3] = (u_char)(q >> 32); |
| 735 | s->data[putp + 4] = (u_char)(q >> 24); |
| 736 | s->data[putp + 5] = (u_char)(q >> 16); |
| 737 | s->data[putp + 6] = (u_char)(q >> 8); |
| 738 | s->data[putp + 7] = (u_char)q; |
| 739 | |
| 740 | return 8; |
| 741 | } |
| 742 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 743 | /* Put long word to the stream. */ |
| 744 | int |
| 745 | stream_put_ipv4 (struct stream *s, u_int32_t l) |
| 746 | { |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 747 | STREAM_VERIFY_SANE(s); |
| 748 | |
| 749 | if (STREAM_WRITEABLE (s) < sizeof (u_int32_t)) |
| 750 | { |
| 751 | STREAM_BOUND_WARN (s, "put"); |
| 752 | return 0; |
| 753 | } |
| 754 | memcpy (s->data + s->endp, &l, sizeof (u_int32_t)); |
| 755 | s->endp += sizeof (u_int32_t); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 756 | |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 757 | return sizeof (u_int32_t); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 758 | } |
| 759 | |
| 760 | /* Put long word to the stream. */ |
| 761 | int |
| 762 | stream_put_in_addr (struct stream *s, struct in_addr *addr) |
| 763 | { |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 764 | STREAM_VERIFY_SANE(s); |
| 765 | |
| 766 | if (STREAM_WRITEABLE (s) < sizeof (u_int32_t)) |
| 767 | { |
| 768 | STREAM_BOUND_WARN (s, "put"); |
| 769 | return 0; |
| 770 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 771 | |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 772 | memcpy (s->data + s->endp, addr, sizeof (u_int32_t)); |
| 773 | s->endp += sizeof (u_int32_t); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 774 | |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 775 | return sizeof (u_int32_t); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 776 | } |
| 777 | |
| 778 | /* Put prefix by nlri type format. */ |
| 779 | int |
| 780 | stream_put_prefix (struct stream *s, struct prefix *p) |
| 781 | { |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 782 | size_t psize; |
| 783 | |
| 784 | STREAM_VERIFY_SANE(s); |
| 785 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 786 | psize = PSIZE (p->prefixlen); |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 787 | |
Jorge Boncompte [DTI2] | af51477 | 2013-07-31 16:16:05 +0000 | [diff] [blame] | 788 | if (STREAM_WRITEABLE (s) < (psize + sizeof (u_char))) |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 789 | { |
| 790 | STREAM_BOUND_WARN (s, "put"); |
| 791 | return 0; |
| 792 | } |
| 793 | |
Jorge Boncompte [DTI2] | af51477 | 2013-07-31 16:16:05 +0000 | [diff] [blame] | 794 | s->data[s->endp++] = p->prefixlen; |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 795 | memcpy (s->data + s->endp, &p->u.prefix, psize); |
| 796 | s->endp += psize; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 797 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 798 | return psize; |
| 799 | } |
David Lamparter | 6b0655a | 2014-06-04 06:53:35 +0200 | [diff] [blame] | 800 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 801 | /* Read size from fd. */ |
| 802 | int |
| 803 | stream_read (struct stream *s, int fd, size_t size) |
| 804 | { |
| 805 | int nbytes; |
| 806 | |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 807 | STREAM_VERIFY_SANE(s); |
| 808 | |
| 809 | if (STREAM_WRITEABLE (s) < size) |
| 810 | { |
| 811 | STREAM_BOUND_WARN (s, "put"); |
| 812 | return 0; |
| 813 | } |
| 814 | |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 815 | nbytes = readn (fd, s->data + s->endp, size); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 816 | |
| 817 | if (nbytes > 0) |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 818 | s->endp += nbytes; |
| 819 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 820 | return nbytes; |
| 821 | } |
| 822 | |
ajs | 262feb1 | 2005-02-16 20:35:47 +0000 | [diff] [blame] | 823 | ssize_t |
| 824 | stream_read_try(struct stream *s, int fd, size_t size) |
| 825 | { |
| 826 | ssize_t nbytes; |
| 827 | |
| 828 | STREAM_VERIFY_SANE(s); |
| 829 | |
| 830 | if (STREAM_WRITEABLE(s) < size) |
| 831 | { |
| 832 | STREAM_BOUND_WARN (s, "put"); |
| 833 | /* Fatal (not transient) error, since retrying will not help |
| 834 | (stream is too small to contain the desired data). */ |
| 835 | return -1; |
| 836 | } |
| 837 | |
| 838 | if ((nbytes = read(fd, s->data + s->endp, size)) >= 0) |
| 839 | { |
| 840 | s->endp += nbytes; |
| 841 | return nbytes; |
| 842 | } |
| 843 | /* Error: was it transient (return -2) or fatal (return -1)? */ |
ajs | 81fb324 | 2005-02-24 16:02:53 +0000 | [diff] [blame] | 844 | if (ERRNO_IO_RETRY(errno)) |
| 845 | return -2; |
| 846 | zlog_warn("%s: read failed on fd %d: %s", __func__, fd, safe_strerror(errno)); |
| 847 | return -1; |
ajs | 262feb1 | 2005-02-16 20:35:47 +0000 | [diff] [blame] | 848 | } |
| 849 | |
paul | 0dab930 | 2005-05-03 09:07:56 +0000 | [diff] [blame] | 850 | /* Read up to size bytes into the stream from the fd, using recvmsgfrom |
| 851 | * whose arguments match the remaining arguments to this function |
| 852 | */ |
| 853 | ssize_t |
| 854 | stream_recvfrom (struct stream *s, int fd, size_t size, int flags, |
| 855 | struct sockaddr *from, socklen_t *fromlen) |
| 856 | { |
| 857 | ssize_t nbytes; |
| 858 | |
| 859 | STREAM_VERIFY_SANE(s); |
| 860 | |
| 861 | if (STREAM_WRITEABLE(s) < size) |
| 862 | { |
| 863 | STREAM_BOUND_WARN (s, "put"); |
| 864 | /* Fatal (not transient) error, since retrying will not help |
| 865 | (stream is too small to contain the desired data). */ |
| 866 | return -1; |
| 867 | } |
| 868 | |
| 869 | if ((nbytes = recvfrom (fd, s->data + s->endp, size, |
| 870 | flags, from, fromlen)) >= 0) |
| 871 | { |
| 872 | s->endp += nbytes; |
| 873 | return nbytes; |
| 874 | } |
| 875 | /* Error: was it transient (return -2) or fatal (return -1)? */ |
| 876 | if (ERRNO_IO_RETRY(errno)) |
| 877 | return -2; |
| 878 | zlog_warn("%s: read failed on fd %d: %s", __func__, fd, safe_strerror(errno)); |
| 879 | return -1; |
| 880 | } |
| 881 | |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 882 | /* Read up to smaller of size or SIZE_REMAIN() bytes to the stream, starting |
| 883 | * from endp. |
| 884 | * First iovec will be used to receive the data. |
| 885 | * Stream need not be empty. |
| 886 | */ |
paul | 0dab930 | 2005-05-03 09:07:56 +0000 | [diff] [blame] | 887 | ssize_t |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 888 | stream_recvmsg (struct stream *s, int fd, struct msghdr *msgh, int flags, |
| 889 | size_t size) |
| 890 | { |
| 891 | int nbytes; |
| 892 | struct iovec *iov; |
| 893 | |
| 894 | STREAM_VERIFY_SANE(s); |
| 895 | assert (msgh->msg_iovlen > 0); |
| 896 | |
| 897 | if (STREAM_WRITEABLE (s) < size) |
| 898 | { |
| 899 | STREAM_BOUND_WARN (s, "put"); |
ajs | 262feb1 | 2005-02-16 20:35:47 +0000 | [diff] [blame] | 900 | /* This is a logic error in the calling code: the stream is too small |
| 901 | to hold the desired data! */ |
| 902 | return -1; |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 903 | } |
| 904 | |
| 905 | iov = &(msgh->msg_iov[0]); |
| 906 | iov->iov_base = (s->data + s->endp); |
| 907 | iov->iov_len = size; |
| 908 | |
| 909 | nbytes = recvmsg (fd, msgh, flags); |
| 910 | |
| 911 | if (nbytes > 0) |
| 912 | s->endp += nbytes; |
| 913 | |
| 914 | return nbytes; |
| 915 | } |
| 916 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 917 | /* Write data to buffer. */ |
paul | 0dab930 | 2005-05-03 09:07:56 +0000 | [diff] [blame] | 918 | size_t |
Paul Jakma | 3d52bb8 | 2008-06-07 20:42:07 +0000 | [diff] [blame] | 919 | stream_write (struct stream *s, const void *ptr, size_t size) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 920 | { |
| 921 | |
| 922 | CHECK_SIZE(s, size); |
| 923 | |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 924 | STREAM_VERIFY_SANE(s); |
| 925 | |
| 926 | if (STREAM_WRITEABLE (s) < size) |
| 927 | { |
| 928 | STREAM_BOUND_WARN (s, "put"); |
| 929 | return 0; |
| 930 | } |
| 931 | |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 932 | memcpy (s->data + s->endp, ptr, size); |
| 933 | s->endp += size; |
| 934 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 935 | return size; |
| 936 | } |
| 937 | |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 938 | /* Return current read pointer. |
| 939 | * DEPRECATED! |
| 940 | * Use stream_get_pnt_to if you must, but decoding streams properly |
| 941 | * is preferred |
| 942 | */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 943 | u_char * |
| 944 | stream_pnt (struct stream *s) |
| 945 | { |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 946 | STREAM_VERIFY_SANE(s); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 947 | return s->data + s->getp; |
| 948 | } |
| 949 | |
| 950 | /* Check does this stream empty? */ |
| 951 | int |
| 952 | stream_empty (struct stream *s) |
| 953 | { |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 954 | STREAM_VERIFY_SANE(s); |
| 955 | |
| 956 | return (s->endp == 0); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 957 | } |
| 958 | |
| 959 | /* Reset stream. */ |
| 960 | void |
| 961 | stream_reset (struct stream *s) |
| 962 | { |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 963 | STREAM_VERIFY_SANE (s); |
| 964 | |
| 965 | s->getp = s->endp = 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 966 | } |
| 967 | |
| 968 | /* Write stream contens to the file discriptor. */ |
| 969 | int |
| 970 | stream_flush (struct stream *s, int fd) |
| 971 | { |
| 972 | int nbytes; |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 973 | |
| 974 | STREAM_VERIFY_SANE(s); |
| 975 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 976 | nbytes = write (fd, s->data + s->getp, s->endp - s->getp); |
paul | 050c013 | 2005-02-14 23:47:47 +0000 | [diff] [blame] | 977 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 978 | return nbytes; |
| 979 | } |
David Lamparter | 6b0655a | 2014-06-04 06:53:35 +0200 | [diff] [blame] | 980 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 981 | /* Stream first in first out queue. */ |
| 982 | |
| 983 | struct stream_fifo * |
ajs | 81fb324 | 2005-02-24 16:02:53 +0000 | [diff] [blame] | 984 | stream_fifo_new (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 985 | { |
| 986 | struct stream_fifo *new; |
| 987 | |
| 988 | new = XCALLOC (MTYPE_STREAM_FIFO, sizeof (struct stream_fifo)); |
| 989 | return new; |
| 990 | } |
| 991 | |
| 992 | /* Add new stream to fifo. */ |
| 993 | void |
| 994 | stream_fifo_push (struct stream_fifo *fifo, struct stream *s) |
| 995 | { |
| 996 | if (fifo->tail) |
| 997 | fifo->tail->next = s; |
| 998 | else |
| 999 | fifo->head = s; |
| 1000 | |
| 1001 | fifo->tail = s; |
| 1002 | |
| 1003 | fifo->count++; |
| 1004 | } |
| 1005 | |
| 1006 | /* Delete first stream from fifo. */ |
| 1007 | struct stream * |
| 1008 | stream_fifo_pop (struct stream_fifo *fifo) |
| 1009 | { |
| 1010 | struct stream *s; |
| 1011 | |
| 1012 | s = fifo->head; |
| 1013 | |
| 1014 | if (s) |
| 1015 | { |
| 1016 | fifo->head = s->next; |
| 1017 | |
| 1018 | if (fifo->head == NULL) |
| 1019 | fifo->tail = NULL; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1020 | |
Subbaiah Venkata | d531050 | 2012-03-24 13:10:19 -0700 | [diff] [blame] | 1021 | fifo->count--; |
| 1022 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1023 | |
| 1024 | return s; |
| 1025 | } |
| 1026 | |
| 1027 | /* Return first fifo entry. */ |
| 1028 | struct stream * |
| 1029 | stream_fifo_head (struct stream_fifo *fifo) |
| 1030 | { |
| 1031 | return fifo->head; |
| 1032 | } |
| 1033 | |
| 1034 | void |
| 1035 | stream_fifo_clean (struct stream_fifo *fifo) |
| 1036 | { |
| 1037 | struct stream *s; |
| 1038 | struct stream *next; |
| 1039 | |
| 1040 | for (s = fifo->head; s; s = next) |
| 1041 | { |
| 1042 | next = s->next; |
| 1043 | stream_free (s); |
| 1044 | } |
| 1045 | fifo->head = fifo->tail = NULL; |
| 1046 | fifo->count = 0; |
| 1047 | } |
| 1048 | |
| 1049 | void |
| 1050 | stream_fifo_free (struct stream_fifo *fifo) |
| 1051 | { |
| 1052 | stream_fifo_clean (fifo); |
| 1053 | XFREE (MTYPE_STREAM_FIFO, fifo); |
| 1054 | } |