bgpdump_mstream.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. Copyright (c) 2007 - 2010 RIPE NCC - All Rights Reserved
  3. Permission to use, copy, modify, and distribute this software and its
  4. documentation for any purpose and without fee is hereby granted, provided
  5. that the above copyright notice appear in all copies and that both that
  6. copyright notice and this permission notice appear in supporting
  7. documentation, and that the name of the author not be used in advertising or
  8. publicity pertaining to distribution of the software without specific,
  9. written prior permission.
  10. THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  11. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS; IN NO EVENT SHALL
  12. AUTHOR BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
  13. DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
  14. AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  15. OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. Parts of this code have been engineered after analiyzing GNU Zebra's
  17. source code and therefore might contain declarations/code from GNU
  18. Zebra, Copyright (C) 1999 Kunihiro Ishiguro. Zebra is a free routing
  19. software, distributed under the GNU General Public License. A copy of
  20. this license is included with libbgpdump.
  21. Original Author: Dan Ardelean (dan@ripe.net)
  22. */
  23. #include "bgpdump-config.h"
  24. #include "bgpdump_mstream.h"
  25. #include <stdio.h>
  26. #include <string.h>
  27. #include <netinet/in.h>
  28. void mstream_init(struct mstream *s, u_char *buffer, u_int32_t len) {
  29. s->start=buffer;
  30. s->position=0;
  31. s->len=len;
  32. }
  33. u_char mstream_getc(struct mstream *s, u_char *d) {
  34. u_char data;
  35. mstream_get(s, &data, sizeof(data));
  36. if(d!=NULL) memcpy(d,&data,sizeof(data));
  37. return data;
  38. }
  39. u_int16_t mstream_getw(struct mstream *s, u_int16_t *d) {
  40. u_int16_t data;
  41. mstream_get(s, &data, sizeof(data));
  42. data=ntohs(data);
  43. if(d!=NULL) memcpy(d,&data,sizeof(data));
  44. return data;
  45. }
  46. u_int32_t mstream_getl(struct mstream *s, u_int32_t *d) {
  47. u_int32_t data;
  48. mstream_get(s, &data, sizeof(data));
  49. data=ntohl(data);
  50. if(d!=NULL) memcpy(d,&data,sizeof(data));
  51. return data;
  52. }
  53. struct in_addr mstream_get_ipv4(struct mstream *s) {
  54. struct in_addr addr;
  55. mstream_get(s, &addr.s_addr, 4);
  56. return addr;
  57. }
  58. u_int32_t mstream_can_read(struct mstream *s) {
  59. return s->len - s->position;
  60. }
  61. // construct a partial mstream
  62. mstream_t mstream_copy(mstream_t *s, int len) {
  63. mstream_t copy = {0};
  64. copy.start = s->start + s->position;
  65. copy.len = mstream_get(s, NULL, len);
  66. return copy;
  67. }
  68. u_int32_t mstream_get (struct mstream *s, void *d, u_int32_t len) {
  69. int room = mstream_can_read(s);
  70. if(room >= len) {
  71. if(d) memcpy(d, s->start + s->position, len);
  72. s->position += len;
  73. return len;
  74. } else {
  75. /* Reading past end of buffer!
  76. Zero out extra bytes and set position to end of buffer */
  77. if(d) {
  78. memcpy(d, s->start + s->position, room);
  79. memset((char *)d + room, 0, len - room);
  80. }
  81. s->position = s->len;
  82. return room;
  83. }
  84. }