cache.c 11 KB

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