cache.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. /* $Id$ */
  2. /*
  3. * Copyright (c) 2001-2010 Aaron Turner <aturner at synfin dot net>
  4. * Copyright (c) 2013-2024 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. #include "defines.h"
  20. #include "config.h"
  21. #include "common.h"
  22. #include <errno.h>
  23. #include <fcntl.h>
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <sys/types.h>
  28. #include <unistd.h>
  29. static tcpr_cache_t *new_cache(void);
  30. /**
  31. * Takes a single char and returns a ptr to a string representation of the
  32. * 8 bits that make up that char. Use BIT_STR() to print it out
  33. */
  34. #ifdef DEBUG
  35. static char *
  36. byte2bits(char byte, char *bitstring)
  37. {
  38. int i, j = 7;
  39. for (i = 1; i <= 255; i = i << 1) {
  40. if (byte & i)
  41. bitstring[j] = '\061';
  42. j--;
  43. }
  44. return bitstring;
  45. }
  46. #endif
  47. /**
  48. * simple function to read in a cache file created with tcpprep this let's us
  49. * be really damn fast in picking an interface to send the packet out returns
  50. * number of cache entries read
  51. *
  52. * now also checks for the cache magic and version
  53. */
  54. COUNTER
  55. read_cache(char **cachedata, const char *cachefile, char **comment)
  56. {
  57. int cachefd;
  58. tcpr_cache_file_hdr_t header;
  59. ssize_t read_size;
  60. COUNTER cache_size;
  61. assert(cachedata);
  62. assert(comment);
  63. /* open the file or abort */
  64. if ((cachefd = open(cachefile, O_RDONLY)) == -1)
  65. errx(-1, "unable to open %s:%s", cachefile, strerror(errno));
  66. /* read the cache header and determine compatibility */
  67. if ((read_size = read(cachefd, &header, sizeof(header))) < 0)
  68. errx(-1, "unable to read from %s:%s,", cachefile, strerror(errno));
  69. if (read_size < (ssize_t)sizeof(header))
  70. errx(-1, "Cache file %s doesn't contain a full header", cachefile);
  71. /* verify our magic: tcpprep\0 */
  72. if (memcmp(header.magic, CACHEMAGIC, sizeof(CACHEMAGIC)) != 0)
  73. errx(-1, "Unable to process %s: not a tcpprep cache file", cachefile);
  74. /* verify version */
  75. if (strtol(header.version, NULL, 10) != strtol(CACHEVERSION, NULL, 10))
  76. errx(-1, "Unable to process %s: cache file version mismatch", cachefile);
  77. /* read the comment */
  78. header.comment_len = ntohs(header.comment_len);
  79. if (header.comment_len > 65534)
  80. errx(-1, "Unable to process %s: invalid comment length %u", cachefile, header.comment_len);
  81. *comment = (char *)safe_malloc(header.comment_len + 1);
  82. dbgx(1, "Comment length: %d", header.comment_len);
  83. if ((read_size = read(cachefd, *comment, header.comment_len)) < 0)
  84. errx(-1, "Error reading comment: %s", strerror(errno));
  85. if (read_size != (ssize_t)header.comment_len)
  86. errx(-1, "Invalid comment read: expected=%u actual=%zd bytes", header.comment_len, read_size);
  87. dbgx(1, "Cache file comment: %s", *comment);
  88. /* malloc our cache block */
  89. header.num_packets = ntohll(header.num_packets);
  90. header.packets_per_byte = ntohs(header.packets_per_byte);
  91. cache_size = header.num_packets / header.packets_per_byte;
  92. /* deal with any remainder, because above division is integer */
  93. if (header.num_packets % header.packets_per_byte)
  94. cache_size++;
  95. dbgx(1, "Cache file contains %" PRIu64 " packets in " COUNTER_SPEC " bytes", header.num_packets, cache_size);
  96. dbgx(1, "Cache uses %d packets per byte", header.packets_per_byte);
  97. *cachedata = (char *)safe_malloc(cache_size);
  98. /* read in the cache */
  99. if ((COUNTER)(read_size = read(cachefd, *cachedata, cache_size)) != cache_size)
  100. errx(-1,
  101. "Cache data length (%zu bytes) doesn't match "
  102. "cache header (" COUNTER_SPEC " bytes)",
  103. read_size,
  104. cache_size);
  105. dbgx(1, "Loaded in %" PRIu64 " packets from cache.", header.num_packets);
  106. close(cachefd);
  107. return (header.num_packets);
  108. }
  109. /**
  110. * writes out the cache file header, comment and then the
  111. * contents of *cachedata to out_file and then returns the number
  112. * of cache entries written
  113. */
  114. COUNTER
  115. write_cache(tcpr_cache_t *cachedata, const int out_file, COUNTER numpackets, char *comment)
  116. {
  117. tcpr_cache_t *mycache = NULL;
  118. tcpr_cache_file_hdr_t *cache_header = NULL;
  119. uint32_t chars, last = 0;
  120. COUNTER packets = 0;
  121. ssize_t written;
  122. assert(out_file);
  123. /* write a header to our file */
  124. cache_header = (tcpr_cache_file_hdr_t *)safe_malloc(sizeof(tcpr_cache_file_hdr_t));
  125. strncpy(cache_header->magic, CACHEMAGIC, strlen(CACHEMAGIC) + 1);
  126. strncpy(cache_header->version, CACHEVERSION, strlen(CACHEVERSION) + 1);
  127. cache_header->packets_per_byte = htons(CACHE_PACKETS_PER_BYTE);
  128. cache_header->num_packets = htonll((u_int64_t)numpackets);
  129. /* we can't strlen(NULL) so ... */
  130. if (comment != NULL) {
  131. cache_header->comment_len = htons((uint16_t)strlen(comment));
  132. } else {
  133. cache_header->comment_len = 0;
  134. }
  135. written = write(out_file, cache_header, sizeof(tcpr_cache_file_hdr_t));
  136. dbgx(1, "Wrote %zu bytes of cache file header", written);
  137. if (written != sizeof(tcpr_cache_file_hdr_t))
  138. errx(-1,
  139. "Only wrote %zu of %zu bytes of the cache file header!\n%s",
  140. written,
  141. sizeof(tcpr_cache_file_hdr_t),
  142. written == -1 ? strerror(errno) : "");
  143. /* don't write comment if there is none */
  144. if (comment != NULL) {
  145. written = write(out_file, comment, strlen(comment));
  146. dbgx(1, "Wrote %zu bytes of comment", written);
  147. if (written != (ssize_t)strlen(comment))
  148. errx(-1,
  149. "Only wrote %zu of %zu bytes of the comment!\n%s",
  150. written,
  151. strlen(comment),
  152. written == -1 ? strerror(errno) : "");
  153. }
  154. if (cachedata) {
  155. mycache = cachedata;
  156. while (!last) {
  157. /* increment total packets */
  158. packets += mycache->packets;
  159. /* calculate how many chars to write */
  160. chars = mycache->packets / CACHE_PACKETS_PER_BYTE;
  161. if (mycache->packets % CACHE_PACKETS_PER_BYTE) {
  162. chars++;
  163. dbgx(1, "Bumping up to the next byte: %d %% %d", mycache->packets, CACHE_PACKETS_PER_BYTE);
  164. }
  165. /* write to file, and verify it wrote properly */
  166. written = write(out_file, mycache->data, chars);
  167. dbgx(1, "Wrote %zu bytes of cache data", written);
  168. if (written != (ssize_t)chars)
  169. errx(-1, "Only wrote %zu of %i bytes to cache file!", written, chars);
  170. /*
  171. * if that was the last, stop processing, otherwise wash,
  172. * rinse, repeat
  173. */
  174. if (mycache->next != NULL) {
  175. mycache = mycache->next;
  176. } else {
  177. last = 1;
  178. }
  179. }
  180. }
  181. safe_free(cache_header);
  182. /* return number of packets written */
  183. return (packets);
  184. }
  185. /**
  186. * mallocs a new CACHE struct all pre-set to sane defaults
  187. */
  188. static tcpr_cache_t *
  189. new_cache(void)
  190. {
  191. tcpr_cache_t *newcache;
  192. /* malloc mem */
  193. newcache = (tcpr_cache_t *)safe_malloc(sizeof(tcpr_cache_t));
  194. return (newcache);
  195. }
  196. /**
  197. * adds the cache data for a packet to the given cachedata
  198. */
  199. tcpr_dir_t
  200. add_cache(tcpr_cache_t **cachedata, const int send, const tcpr_dir_t interface)
  201. {
  202. static tcpr_cache_t *lastcache = NULL;
  203. tcpr_dir_t result;
  204. #ifdef DEBUG
  205. char bitstring[9] = EIGHT_ZEROS;
  206. #endif
  207. assert(cachedata);
  208. /* first run? malloc our first entry, set bit count to 0 */
  209. if (*cachedata == NULL || lastcache == NULL) {
  210. *cachedata = new_cache();
  211. lastcache = *cachedata;
  212. } else {
  213. /* check to see if this is the last bit in this struct */
  214. if ((lastcache->packets + 1) > (CACHEDATASIZE * CACHE_PACKETS_PER_BYTE)) {
  215. /*
  216. * if so, we have to malloc a new one and set bit to 0
  217. */
  218. dbg(1, "Adding to cachedata linked list");
  219. lastcache->next = new_cache();
  220. lastcache = lastcache->next;
  221. }
  222. }
  223. /* always increment our bit count */
  224. lastcache->packets++;
  225. dbgx(1, "Cache array packet %d", lastcache->packets);
  226. /* send packet ? */
  227. if (send == SEND) {
  228. COUNTER index;
  229. uint32_t bit;
  230. u_char *byte;
  231. index = (lastcache->packets - 1) / (COUNTER)CACHE_PACKETS_PER_BYTE;
  232. bit = (((lastcache->packets - 1) % (COUNTER)CACHE_PACKETS_PER_BYTE) * (COUNTER)CACHE_BITS_PER_PACKET) + 1;
  233. dbgx(3, "Bit: %d", bit);
  234. byte = (u_char *)&lastcache->data[index];
  235. *byte += (u_char)(1 << bit);
  236. dbgx(2, "set send bit: byte " COUNTER_SPEC " = 0x%x", index, *byte);
  237. /* if true, set low order bit. else, do squat */
  238. if (interface == TCPR_DIR_C2S) {
  239. *byte += (u_char)(1 << (bit - 1));
  240. dbgx(2, "set interface bit: byte " COUNTER_SPEC " = 0x%x", index, *byte);
  241. result = TCPR_DIR_C2S;
  242. } else {
  243. dbgx(2, "don't set interface bit: byte " COUNTER_SPEC " = 0x%x", index, *byte);
  244. result = TCPR_DIR_S2C;
  245. }
  246. #ifdef DEBUG
  247. /*
  248. * only build the byte string when not in debug mode since
  249. * the calculation is a bit expensive
  250. */
  251. dbgx(3, "Current cache byte: %c%c%c%c%c%c%c%c", BIT_STR(byte2bits(*byte, bitstring)));
  252. #endif
  253. } else {
  254. dbg(1, "not setting send bit");
  255. result = TCPR_DIR_NOSEND;
  256. }
  257. return result;
  258. }
  259. /**
  260. * returns the action for a given packet based on the CACHE
  261. */
  262. tcpr_dir_t
  263. check_cache(char *cachedata, COUNTER packetid)
  264. {
  265. COUNTER index;
  266. uint32_t bit;
  267. assert(cachedata);
  268. if (packetid == 0)
  269. err(-1, "packetid must be > 0");
  270. index = (packetid - 1) / (COUNTER)CACHE_PACKETS_PER_BYTE;
  271. bit = (uint32_t)(((packetid - 1) % (COUNTER)CACHE_PACKETS_PER_BYTE) * (COUNTER)CACHE_BITS_PER_PACKET) + 1;
  272. #ifdef DEBUG
  273. dbgx(3,
  274. "Index: " COUNTER_SPEC "\tBit: %d\tByte: %hhu\tMask: %hhu",
  275. index,
  276. bit,
  277. cachedata[index],
  278. (uint8_t)(cachedata[index] & (char)(1 << bit)));
  279. #endif
  280. if (!(cachedata[index] & (char)(1 << bit))) {
  281. return TCPR_DIR_NOSEND;
  282. }
  283. /* go back a bit to get the interface */
  284. bit--;
  285. if (cachedata[index] & (char)(1 << bit)) {
  286. return TCPR_DIR_C2S;
  287. } else {
  288. return TCPR_DIR_S2C;
  289. }
  290. }