cache.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /* $Id$ */
  2. /*
  3. * Copyright (c) 2001-2010 Aaron Turner <aturner at synfin dot net>
  4. * Copyright (c) 2013-2018 Fred Klassen <tcpreplay at appneta dot com> - AppNeta
  5. *
  6. * The Tcpreplay Suite of tools is free software: you can redistribute it
  7. * and/or modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation, either version 3 of the
  9. * License, or with the authors permission any later version.
  10. *
  11. * The Tcpreplay Suite is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with the Tcpreplay Suite. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #ifndef __CACHE_H__
  20. #define __CACHE_H__
  21. #define CACHEMAGIC "tcpprep"
  22. #define CACHEVERSION "04"
  23. #define CACHEDATASIZE 255
  24. #define CACHE_PACKETS_PER_BYTE 4 /* number of packets / byte */
  25. #define CACHE_BITS_PER_PACKET 2 /* number of bits / packet */
  26. #define SEND 1
  27. #define DONT_SEND 0
  28. /*
  29. * CACHEVERSION History:
  30. * 01 - Initial release. 1 bit of data/packet (primary or secondary nic)
  31. * 02 - 2 bits of data/packet (drop/send & primary or secondary nic)
  32. * 03 - Write integers in network-byte order
  33. * 04 - Increase num_packets from 32 to 64 bit integer
  34. */
  35. struct tcpr_cache_s {
  36. char data[CACHEDATASIZE];
  37. unsigned int packets; /* number of packets tracked in data */
  38. struct tcpr_cache_s *next;
  39. };
  40. typedef struct tcpr_cache_s tcpr_cache_t;
  41. /*
  42. * Each byte in cache_type.data represents CACHE_PACKETS_PER_BYTE (4) number of packets
  43. * Each packet has CACHE_BITS_PER_PACKETS (2) bits of data.
  44. * High Bit: 1 = send, 0 = don't send
  45. * Low Bit: 1 = primary interface, 0 = secondary interface
  46. */
  47. /*
  48. * cache_file_header Data structure defining a file as a tcpprep cache file
  49. * and it's version
  50. *
  51. * If you need to enhance this struct, do so AFTER the version field and be sure
  52. * to increment CACHEVERSION
  53. */
  54. struct tcpr_cache_file_hdr_s {
  55. char magic[8];
  56. char version[4];
  57. /* begin version 2 features */
  58. /* version 3 puts everything in network-byte order */
  59. /* version 4 makes num_packets a 64 bit int */
  60. u_int64_t num_packets; /* total # of packets in file */
  61. u_int16_t packets_per_byte;
  62. u_int16_t comment_len; /* how long is the user comment? */
  63. } __attribute__((__packed__));
  64. typedef struct tcpr_cache_file_hdr_s tcpr_cache_file_hdr_t;
  65. enum tcpr_dir_e {
  66. TCPR_DIR_ERROR = -1,
  67. TCPR_DIR_NOSEND = 0,
  68. TCPR_DIR_C2S = 1, /* aka PRIMARY */
  69. TCPR_DIR_S2C = 2 /* aka SECONDARY */
  70. };
  71. typedef enum tcpr_dir_e tcpr_dir_t;
  72. COUNTER write_cache(tcpr_cache_t *, const int, COUNTER, char *);
  73. tcpr_dir_t add_cache(tcpr_cache_t **, const int, const tcpr_dir_t);
  74. COUNTER read_cache(char **, const char *, char **);
  75. tcpr_dir_t check_cache(char *, COUNTER);
  76. /* return values for check_cache
  77. #define CACHE_ERROR -1
  78. #define CACHE_NOSEND 0 // NULL
  79. #define CACHE_PRIMARY 1
  80. #define CACHE_SECONDARY 2
  81. */
  82. /* macro to change a bitstring to 8 bits */
  83. #define BIT_STR(x) x[0], x[1], x[2], x[3], x[4], x[5], x[6], x[7]
  84. /* string of 8 zeros */
  85. #define EIGHT_ZEROS "\060\060\060\060\060\060\060\060"
  86. #endif