flowbuff.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /* $Id: flowbuff.c 767 2004-10-06 12:48:49Z aturner $ */
  2. /*
  3. * Copyright (c) 2001-2004 Aaron Turner.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the names of the copyright owners nor the names of its
  16. * contributors may be used to endorse or promote products derived from
  17. * this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  20. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  21. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  22. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  23. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  24. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  25. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  27. * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  28. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  29. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. #include "config.h"
  32. #include <stdlib.h> /* malloc/free */
  33. #include "flowreplay.h"
  34. #include "flownode.h"
  35. #include "err.h"
  36. extern int32_t pernodebufflim;
  37. extern int32_t totalbufflim;
  38. /*
  39. * adds a packet read from pcap_next() to the chain of buffered
  40. * packets for the given node. Mallocs memory. Returns a ptr to
  41. * the new buffer or NULL on fail
  42. */
  43. struct pktbuffhdr_t *
  44. addpkt2buff(struct session_t *node, u_char * pktdata, u_int32_t len)
  45. {
  46. struct pktbuffhdr_t *buffhdr = NULL; /* packet buffer hdr */
  47. /* check per node buffer limit */
  48. if ((node->buffmem + len) > pernodebufflim) {
  49. warnx("Unable to buffer next packet: per node buffer limit reached");
  50. return (NULL);
  51. }
  52. /* check total buffer limit */
  53. totalbufflim -= len;
  54. if (totalbufflim < 0) {
  55. warnx("Unable to buffer next packet: total buffer limit reached");
  56. totalbufflim += len; /* reset */
  57. return (NULL);
  58. }
  59. /* prep the buffer header for the linked list */
  60. if ((buffhdr =
  61. (struct pktbuffhdr_t *)malloc(sizeof(struct pktbuffhdr_t))) == NULL)
  62. errx(1, "Unable to malloc *pktbuffhdr in addpkt2buff()");
  63. buffhdr->len = len;
  64. /* allocate memory for the packet data */
  65. if ((buffhdr->packet = (u_char *) malloc(len)) == NULL)
  66. errx(1, "Unable to malloc *buff in addpkt2buff()");
  67. /* copy over the packet */
  68. memcpy(buffhdr->packet, pktdata, len);
  69. /* is this the first packet ? */
  70. if (node->lastbuff == NULL) {
  71. /* start the chain by pointing both buffered and lastbuff to the new buffer */
  72. node->buffered = buffhdr;
  73. node->lastbuff = buffhdr;
  74. }
  75. else {
  76. /* otherwise add the buffer to the end of the list */
  77. node->lastbuff->next = buffhdr;
  78. node->lastbuff = buffhdr;
  79. }
  80. /* return a ptr to the packet */
  81. return (buffhdr);
  82. }
  83. /*
  84. * frees the last sent packet, relinks the linked list, and returns a
  85. * pointer to the packet. packet len is returned in len. Returns
  86. * NULL/len = 0 when last packet is reached.
  87. */
  88. const u_char *
  89. nextbuffpkt(struct session_t *node, u_int32_t len)
  90. {
  91. struct pktbuffhdr_t *packet = NULL;
  92. /* mode temp ptr to next packet, which may be NULL */
  93. packet = node->sentbuff->next;
  94. /* first thing first, free the last packet, update the node's
  95. * buffmem counter, the total buffer limit, and free the buffer header
  96. */
  97. if (node->sentbuff != NULL) {
  98. free(node->sentbuff->packet);
  99. node->buffmem -= node->sentbuff->len;
  100. totalbufflim += len;
  101. free(node->sentbuff);
  102. }
  103. /* relink the list */
  104. node->buffered = packet;
  105. /* was that the last packet ? */
  106. if (node->buffered == NULL) {
  107. len = 0;
  108. return (NULL);
  109. }
  110. /* otherwise we've got another packet, so update len and return it */
  111. len = node->buffered->len;
  112. return (node->buffered->packet);
  113. }