flowbuff.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /* $Id: flowbuff.c 1518 2006-07-18 02:47:33Z 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 "defines.h"
  33. #include "common.h"
  34. #include <stdlib.h> /* malloc/free */
  35. #include "flowreplay.h"
  36. #include "flownode.h"
  37. extern flowreplay_opt_t options;
  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) > options.pernodebufflim) {
  49. warn("Unable to buffer next packet: per node buffer limit reached");
  50. return (NULL);
  51. }
  52. /* check total buffer limit */
  53. if (options.totalbufflim < len) {
  54. warn("Unable to buffer next packet: total buffer limit reached");
  55. return (NULL);
  56. }
  57. /* we got enough buffer space so, mark it off */
  58. options.totalbufflim -= len;
  59. /* prep the buffer header for the linked list */
  60. buffhdr = (struct pktbuffhdr_t *)safe_malloc(sizeof(struct pktbuffhdr_t));
  61. buffhdr->len = len;
  62. /* allocate memory for the packet data */
  63. buffhdr->packet = (u_char *)safe_malloc(len);
  64. /* copy over the packet */
  65. memcpy(buffhdr->packet, pktdata, len);
  66. /* is this the first packet ? */
  67. if (node->lastbuff == NULL) {
  68. /* start the chain by pointing both buffered and lastbuff to the new buffer */
  69. node->buffered = buffhdr;
  70. node->lastbuff = buffhdr;
  71. }
  72. else {
  73. /* otherwise add the buffer to the end of the list */
  74. node->lastbuff->next = buffhdr;
  75. node->lastbuff = buffhdr;
  76. }
  77. /* return a ptr to the packet */
  78. return (buffhdr);
  79. }
  80. /*
  81. * frees the last sent packet, relinks the linked list, and returns a
  82. * pointer to the packet. packet len is returned in len. Returns
  83. * NULL/len = 0 when last packet is reached.
  84. */
  85. const u_char *
  86. nextbuffpkt(struct session_t *node, u_int32_t len)
  87. {
  88. struct pktbuffhdr_t *packet = NULL;
  89. /* mode temp ptr to next packet, which may be NULL */
  90. packet = node->sentbuff->next;
  91. /* first thing first, free the last packet, update the node's
  92. * buffmem counter, the total buffer limit, and free the buffer header
  93. */
  94. if (node->sentbuff != NULL) {
  95. free(node->sentbuff->packet);
  96. node->buffmem -= node->sentbuff->len;
  97. options.totalbufflim += len;
  98. free(node->sentbuff);
  99. }
  100. /* relink the list */
  101. node->buffered = packet;
  102. /* was that the last packet ? */
  103. if (node->buffered == NULL) {
  104. len = 0;
  105. return (NULL);
  106. }
  107. /* otherwise we've got another packet, so update len and return it */
  108. len = node->buffered->len;
  109. return (node->buffered->packet);
  110. }
  111. /*
  112. Local Variables:
  113. mode:c
  114. indent-tabs-mode:nil
  115. c-basic-offset:4
  116. End:
  117. */