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 | #include <zebra.h> |
| 24 | |
| 25 | #include "stream.h" |
| 26 | #include "memory.h" |
| 27 | #include "network.h" |
| 28 | #include "prefix.h" |
| 29 | |
| 30 | |
| 31 | /*A macro to check pointers in order to not |
| 32 | go behind the allocated mem block |
| 33 | S -- stream reference |
| 34 | Z -- size of data to be written |
| 35 | */ |
| 36 | |
| 37 | #define CHECK_SIZE(S, Z) \ |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 38 | if (((S)->endp + (Z)) > (S)->size) \ |
| 39 | (Z) = (S)->size - (S)->endp; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 40 | |
| 41 | /* Stream is fixed length buffer for network output/input. */ |
| 42 | |
| 43 | /* Make stream buffer. */ |
| 44 | struct stream * |
| 45 | stream_new (size_t size) |
| 46 | { |
| 47 | struct stream *s; |
| 48 | |
paul | 0e43a2b | 2004-12-22 00:15:34 +0000 | [diff] [blame] | 49 | assert (size > 0); |
| 50 | |
| 51 | if (size == 0) |
| 52 | return NULL; |
| 53 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 54 | s = XCALLOC (MTYPE_STREAM, sizeof (struct stream)); |
| 55 | |
| 56 | s->data = XCALLOC (MTYPE_STREAM_DATA, size); |
| 57 | s->size = size; |
| 58 | return s; |
| 59 | } |
| 60 | |
| 61 | /* Free it now. */ |
| 62 | void |
| 63 | stream_free (struct stream *s) |
| 64 | { |
| 65 | XFREE (MTYPE_STREAM_DATA, s->data); |
| 66 | XFREE (MTYPE_STREAM, s); |
| 67 | } |
| 68 | |
paul | f2e6c42 | 2005-02-12 14:35:49 +0000 | [diff] [blame^] | 69 | size_t |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 70 | stream_get_getp (struct stream *s) |
| 71 | { |
| 72 | return s->getp; |
| 73 | } |
| 74 | |
paul | f2e6c42 | 2005-02-12 14:35:49 +0000 | [diff] [blame^] | 75 | size_t |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 76 | stream_get_endp (struct stream *s) |
| 77 | { |
| 78 | return s->endp; |
| 79 | } |
| 80 | |
paul | f2e6c42 | 2005-02-12 14:35:49 +0000 | [diff] [blame^] | 81 | size_t |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 82 | stream_get_size (struct stream *s) |
| 83 | { |
| 84 | return s->size; |
| 85 | } |
| 86 | |
| 87 | /* Stream structre' stream pointer related functions. */ |
| 88 | void |
paul | f2e6c42 | 2005-02-12 14:35:49 +0000 | [diff] [blame^] | 89 | stream_set_getp (struct stream *s, size_t pos) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 90 | { |
| 91 | s->getp = pos; |
| 92 | } |
| 93 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 94 | /* Forward pointer. */ |
| 95 | void |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 96 | stream_forward_getp (struct stream *s, int size) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 97 | { |
| 98 | s->getp += size; |
| 99 | } |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 100 | |
| 101 | void |
| 102 | stream_forward_endp (struct stream *s, int size) |
| 103 | { |
| 104 | s->endp += size; |
| 105 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 106 | |
| 107 | /* Copy from stream to destination. */ |
| 108 | void |
| 109 | stream_get (void *dst, struct stream *s, size_t size) |
| 110 | { |
| 111 | memcpy (dst, s->data + s->getp, size); |
| 112 | s->getp += size; |
| 113 | } |
| 114 | |
| 115 | /* Get next character from the stream. */ |
| 116 | u_char |
| 117 | stream_getc (struct stream *s) |
| 118 | { |
| 119 | u_char c; |
| 120 | |
| 121 | c = s->data[s->getp]; |
| 122 | s->getp++; |
| 123 | return c; |
| 124 | } |
| 125 | |
| 126 | /* Get next character from the stream. */ |
| 127 | u_char |
paul | f2e6c42 | 2005-02-12 14:35:49 +0000 | [diff] [blame^] | 128 | stream_getc_from (struct stream *s, size_t from) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 129 | { |
| 130 | u_char c; |
| 131 | |
| 132 | c = s->data[from]; |
| 133 | return c; |
| 134 | } |
| 135 | |
| 136 | /* Get next word from the stream. */ |
| 137 | u_int16_t |
| 138 | stream_getw (struct stream *s) |
| 139 | { |
| 140 | u_int16_t w; |
| 141 | |
| 142 | w = s->data[s->getp++] << 8; |
| 143 | w |= s->data[s->getp++]; |
| 144 | return w; |
| 145 | } |
| 146 | |
| 147 | /* Get next word from the stream. */ |
| 148 | u_int16_t |
paul | f2e6c42 | 2005-02-12 14:35:49 +0000 | [diff] [blame^] | 149 | stream_getw_from (struct stream *s, size_t from) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 150 | { |
| 151 | u_int16_t w; |
| 152 | |
| 153 | w = s->data[from++] << 8; |
| 154 | w |= s->data[from]; |
| 155 | return w; |
| 156 | } |
| 157 | |
| 158 | /* Get next long word from the stream. */ |
| 159 | u_int32_t |
| 160 | stream_getl (struct stream *s) |
| 161 | { |
| 162 | u_int32_t l; |
| 163 | |
| 164 | l = s->data[s->getp++] << 24; |
| 165 | l |= s->data[s->getp++] << 16; |
| 166 | l |= s->data[s->getp++] << 8; |
| 167 | l |= s->data[s->getp++]; |
| 168 | return l; |
| 169 | } |
| 170 | |
| 171 | /* Get next long word from the stream. */ |
| 172 | u_int32_t |
| 173 | stream_get_ipv4 (struct stream *s) |
| 174 | { |
| 175 | u_int32_t l; |
| 176 | |
| 177 | memcpy (&l, s->data + s->getp, 4); |
| 178 | s->getp += 4; |
| 179 | |
| 180 | return l; |
| 181 | } |
| 182 | |
| 183 | /* Copy to source to stream. */ |
| 184 | void |
| 185 | stream_put (struct stream *s, void *src, size_t size) |
| 186 | { |
| 187 | |
| 188 | CHECK_SIZE(s, size); |
| 189 | |
| 190 | if (src) |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 191 | memcpy (s->data + s->endp, src, size); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 192 | else |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 193 | memset (s->data + s->endp, 0, size); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 194 | |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 195 | s->endp += size; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | /* Put character to the stream. */ |
| 199 | int |
| 200 | stream_putc (struct stream *s, u_char c) |
| 201 | { |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 202 | if (s->endp >= s->size) return 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 203 | |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 204 | s->data[s->endp] = c; |
| 205 | s->endp++; |
| 206 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 207 | return 1; |
| 208 | } |
| 209 | |
| 210 | /* Put word to the stream. */ |
| 211 | int |
| 212 | stream_putw (struct stream *s, u_int16_t w) |
| 213 | { |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 214 | if ((s->size - s->endp) < 2) return 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 215 | |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 216 | s->data[s->endp++] = (u_char)(w >> 8); |
| 217 | s->data[s->endp++] = (u_char) w; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 218 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 219 | return 2; |
| 220 | } |
| 221 | |
| 222 | /* Put long word to the stream. */ |
| 223 | int |
| 224 | stream_putl (struct stream *s, u_int32_t l) |
| 225 | { |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 226 | if ((s->size - s->endp) < 4) return 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 227 | |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 228 | s->data[s->endp++] = (u_char)(l >> 24); |
| 229 | s->data[s->endp++] = (u_char)(l >> 16); |
| 230 | s->data[s->endp++] = (u_char)(l >> 8); |
| 231 | s->data[s->endp++] = (u_char)l; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 232 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 233 | return 4; |
| 234 | } |
| 235 | |
| 236 | int |
paul | f2e6c42 | 2005-02-12 14:35:49 +0000 | [diff] [blame^] | 237 | stream_putc_at (struct stream *s, size_t putp, u_char c) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 238 | { |
| 239 | s->data[putp] = c; |
| 240 | return 1; |
| 241 | } |
| 242 | |
| 243 | int |
paul | f2e6c42 | 2005-02-12 14:35:49 +0000 | [diff] [blame^] | 244 | stream_putw_at (struct stream *s, size_t putp, u_int16_t w) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 245 | { |
| 246 | s->data[putp] = (u_char)(w >> 8); |
| 247 | s->data[putp + 1] = (u_char) w; |
| 248 | return 2; |
| 249 | } |
| 250 | |
| 251 | int |
paul | f2e6c42 | 2005-02-12 14:35:49 +0000 | [diff] [blame^] | 252 | stream_putl_at (struct stream *s, size_t putp, u_int32_t l) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 253 | { |
| 254 | s->data[putp] = (u_char)(l >> 24); |
| 255 | s->data[putp + 1] = (u_char)(l >> 16); |
| 256 | s->data[putp + 2] = (u_char)(l >> 8); |
| 257 | s->data[putp + 3] = (u_char)l; |
| 258 | return 4; |
| 259 | } |
| 260 | |
| 261 | /* Put long word to the stream. */ |
| 262 | int |
| 263 | stream_put_ipv4 (struct stream *s, u_int32_t l) |
| 264 | { |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 265 | if ((s->size - s->endp) < 4) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 266 | return 0; |
| 267 | |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 268 | memcpy (s->data + s->endp, &l, 4); |
| 269 | s->endp += 4; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 270 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 271 | return 4; |
| 272 | } |
| 273 | |
| 274 | /* Put long word to the stream. */ |
| 275 | int |
| 276 | stream_put_in_addr (struct stream *s, struct in_addr *addr) |
| 277 | { |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 278 | if ((s->size - s->endp) < 4) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 279 | return 0; |
| 280 | |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 281 | memcpy (s->data + s->endp, addr, 4); |
| 282 | s->endp += 4; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 283 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 284 | return 4; |
| 285 | } |
| 286 | |
| 287 | /* Put prefix by nlri type format. */ |
| 288 | int |
| 289 | stream_put_prefix (struct stream *s, struct prefix *p) |
| 290 | { |
| 291 | u_char psize; |
| 292 | |
| 293 | psize = PSIZE (p->prefixlen); |
| 294 | |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 295 | if ((s->size - s->endp) < psize) return 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 296 | |
| 297 | stream_putc (s, p->prefixlen); |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 298 | memcpy (s->data + s->endp, &p->u.prefix, psize); |
| 299 | s->endp += psize; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 300 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 301 | return psize; |
| 302 | } |
| 303 | |
| 304 | /* Read size from fd. */ |
| 305 | int |
| 306 | stream_read (struct stream *s, int fd, size_t size) |
| 307 | { |
| 308 | int nbytes; |
| 309 | |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 310 | nbytes = readn (fd, s->data + s->endp, size); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 311 | |
| 312 | if (nbytes > 0) |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 313 | s->endp += nbytes; |
| 314 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 315 | return nbytes; |
| 316 | } |
| 317 | |
| 318 | /* Read size from fd. */ |
| 319 | int |
| 320 | stream_read_unblock (struct stream *s, int fd, size_t size) |
| 321 | { |
| 322 | int nbytes; |
| 323 | int val; |
| 324 | |
| 325 | val = fcntl (fd, F_GETFL, 0); |
| 326 | fcntl (fd, F_SETFL, val|O_NONBLOCK); |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 327 | nbytes = read (fd, s->data + s->endp, size); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 328 | fcntl (fd, F_SETFL, val); |
| 329 | |
| 330 | if (nbytes > 0) |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 331 | s->endp += nbytes; |
| 332 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 333 | return nbytes; |
| 334 | } |
| 335 | |
| 336 | /* Write data to buffer. */ |
| 337 | int |
| 338 | stream_write (struct stream *s, u_char *ptr, size_t size) |
| 339 | { |
| 340 | |
| 341 | CHECK_SIZE(s, size); |
| 342 | |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 343 | memcpy (s->data + s->endp, ptr, size); |
| 344 | s->endp += size; |
| 345 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 346 | return size; |
| 347 | } |
| 348 | |
| 349 | /* Return current read pointer. */ |
| 350 | u_char * |
| 351 | stream_pnt (struct stream *s) |
| 352 | { |
| 353 | return s->data + s->getp; |
| 354 | } |
| 355 | |
| 356 | /* Check does this stream empty? */ |
| 357 | int |
| 358 | stream_empty (struct stream *s) |
| 359 | { |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 360 | if (s->endp == 0 && s->getp == 0) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 361 | return 1; |
| 362 | else |
| 363 | return 0; |
| 364 | } |
| 365 | |
| 366 | /* Reset stream. */ |
| 367 | void |
| 368 | stream_reset (struct stream *s) |
| 369 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 370 | s->endp = 0; |
| 371 | s->getp = 0; |
| 372 | } |
| 373 | |
| 374 | /* Write stream contens to the file discriptor. */ |
| 375 | int |
| 376 | stream_flush (struct stream *s, int fd) |
| 377 | { |
| 378 | int nbytes; |
| 379 | |
| 380 | nbytes = write (fd, s->data + s->getp, s->endp - s->getp); |
| 381 | |
| 382 | return nbytes; |
| 383 | } |
| 384 | |
| 385 | /* Stream first in first out queue. */ |
| 386 | |
| 387 | struct stream_fifo * |
| 388 | stream_fifo_new () |
| 389 | { |
| 390 | struct stream_fifo *new; |
| 391 | |
| 392 | new = XCALLOC (MTYPE_STREAM_FIFO, sizeof (struct stream_fifo)); |
| 393 | return new; |
| 394 | } |
| 395 | |
| 396 | /* Add new stream to fifo. */ |
| 397 | void |
| 398 | stream_fifo_push (struct stream_fifo *fifo, struct stream *s) |
| 399 | { |
| 400 | if (fifo->tail) |
| 401 | fifo->tail->next = s; |
| 402 | else |
| 403 | fifo->head = s; |
| 404 | |
| 405 | fifo->tail = s; |
| 406 | |
| 407 | fifo->count++; |
| 408 | } |
| 409 | |
| 410 | /* Delete first stream from fifo. */ |
| 411 | struct stream * |
| 412 | stream_fifo_pop (struct stream_fifo *fifo) |
| 413 | { |
| 414 | struct stream *s; |
| 415 | |
| 416 | s = fifo->head; |
| 417 | |
| 418 | if (s) |
| 419 | { |
| 420 | fifo->head = s->next; |
| 421 | |
| 422 | if (fifo->head == NULL) |
| 423 | fifo->tail = NULL; |
| 424 | } |
| 425 | |
| 426 | fifo->count--; |
| 427 | |
| 428 | return s; |
| 429 | } |
| 430 | |
| 431 | /* Return first fifo entry. */ |
| 432 | struct stream * |
| 433 | stream_fifo_head (struct stream_fifo *fifo) |
| 434 | { |
| 435 | return fifo->head; |
| 436 | } |
| 437 | |
| 438 | void |
| 439 | stream_fifo_clean (struct stream_fifo *fifo) |
| 440 | { |
| 441 | struct stream *s; |
| 442 | struct stream *next; |
| 443 | |
| 444 | for (s = fifo->head; s; s = next) |
| 445 | { |
| 446 | next = s->next; |
| 447 | stream_free (s); |
| 448 | } |
| 449 | fifo->head = fifo->tail = NULL; |
| 450 | fifo->count = 0; |
| 451 | } |
| 452 | |
| 453 | void |
| 454 | stream_fifo_free (struct stream_fifo *fifo) |
| 455 | { |
| 456 | stream_fifo_clean (fifo); |
| 457 | XFREE (MTYPE_STREAM_FIFO, fifo); |
| 458 | } |