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