cache.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /* $Id$ */
  2. /*
  3. * Copyright (c) 2001-2010 Aaron Turner <aturner at synfin dot net>
  4. * Copyright (c) 2013-2017 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. *comment = (char *)safe_malloc(header.comment_len + 1);
  84. dbgx(1, "Comment length: %d", header.comment_len);
  85. if ((read_size = read(cachefd, *comment, header.comment_len)) < 0)
  86. errx(-1, "Error reading comment: %s", strerror(errno));
  87. if (read_size != (ssize_t)header.comment_len)
  88. errx(-1, "Invalid comment read: expected=%u actual=%zd bytes",
  89. header.comment_len, read_size);
  90. dbgx(1, "Cache file comment: %s", *comment);
  91. /* malloc our cache block */
  92. header.num_packets = ntohll(header.num_packets);
  93. header.packets_per_byte = ntohs(header.packets_per_byte);
  94. cache_size = header.num_packets / header.packets_per_byte;
  95. /* deal with any remainder, because above division is integer */
  96. if (header.num_packets % header.packets_per_byte)
  97. cache_size ++;
  98. dbgx(1, "Cache file contains %" PRIu64 " packets in " COUNTER_SPEC " bytes",
  99. header.num_packets, cache_size);
  100. dbgx(1, "Cache uses %d packets per byte", header.packets_per_byte);
  101. *cachedata = (char *)safe_malloc(cache_size);
  102. /* read in the cache */
  103. if ((COUNTER)(read_size = read(cachefd, *cachedata, cache_size))
  104. != cache_size)
  105. errx(-1, "Cache data length (%zu bytes) doesn't match "
  106. "cache header (" COUNTER_SPEC " bytes)", read_size, cache_size);
  107. dbgx(1, "Loaded in %" PRIu64 " packets from cache.", header.num_packets);
  108. close(cachefd);
  109. return (header.num_packets);
  110. }
  111. /**
  112. * writes out the cache file header, comment and then the
  113. * contents of *cachedata to out_file and then returns the number
  114. * of cache entries written
  115. */
  116. COUNTER
  117. write_cache(tcpr_cache_t * cachedata, const int out_file, COUNTER numpackets,
  118. char *comment)
  119. {
  120. tcpr_cache_t *mycache = NULL;
  121. tcpr_cache_file_hdr_t *cache_header = NULL;
  122. uint32_t chars, last = 0;
  123. COUNTER packets = 0;
  124. ssize_t written = 0;
  125. assert(out_file);
  126. /* write a header to our file */
  127. cache_header = (tcpr_cache_file_hdr_t *)
  128. safe_malloc(sizeof(tcpr_cache_file_hdr_t));
  129. strncpy(cache_header->magic, CACHEMAGIC, strlen(CACHEMAGIC)+1);
  130. strncpy(cache_header->version, CACHEVERSION, strlen(CACHEVERSION)+1);
  131. cache_header->packets_per_byte = htons(CACHE_PACKETS_PER_BYTE);
  132. cache_header->num_packets = htonll((u_int64_t)numpackets);
  133. /* we can't strlen(NULL) so ... */
  134. if (comment != NULL) {
  135. cache_header->comment_len = htons((uint16_t)strlen(comment));
  136. } else {
  137. cache_header->comment_len = 0;
  138. }
  139. written = write(out_file, cache_header, sizeof(tcpr_cache_file_hdr_t));
  140. dbgx(1, "Wrote %zu bytes of cache file header", written);
  141. if (written != sizeof(tcpr_cache_file_hdr_t))
  142. errx(-1, "Only wrote %zu of %zu bytes of the cache file header!\n%s",
  143. written, sizeof(tcpr_cache_file_hdr_t),
  144. written == -1 ? strerror(errno) : "");
  145. /* don't write comment if there is none */
  146. if (comment != NULL) {
  147. written = write(out_file, comment, strlen(comment));
  148. dbgx(1, "Wrote %zu bytes of comment", written);
  149. if (written != (ssize_t)strlen(comment))
  150. errx(-1, "Only wrote %zu of %zu bytes of the comment!\n%s",
  151. written, 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,
  164. CACHE_PACKETS_PER_BYTE);
  165. }
  166. /* write to file, and verify it wrote properly */
  167. written = write(out_file, mycache->data, chars);
  168. dbgx(1, "Wrote %zu bytes of cache data", written);
  169. if (written != (ssize_t)chars)
  170. errx(-1, "Only wrote %zu of %i bytes to cache file!", written, chars);
  171. /*
  172. * if that was the last, stop processing, otherwise wash,
  173. * rinse, repeat
  174. */
  175. if (mycache->next != NULL) {
  176. mycache = mycache->next;
  177. }
  178. else {
  179. last = 1;
  180. }
  181. }
  182. }
  183. safe_free(cache_header);
  184. /* return number of packets written */
  185. return (packets);
  186. }
  187. /**
  188. * mallocs a new CACHE struct all pre-set to sane defaults
  189. */
  190. static tcpr_cache_t *
  191. new_cache(void)
  192. {
  193. tcpr_cache_t *newcache;
  194. /* malloc mem */
  195. newcache = (tcpr_cache_t *)safe_malloc(sizeof(tcpr_cache_t));
  196. return (newcache);
  197. }
  198. /**
  199. * adds the cache data for a packet to the given cachedata
  200. */
  201. tcpr_dir_t
  202. add_cache(tcpr_cache_t ** cachedata, const int send, const tcpr_dir_t interface)
  203. {
  204. static tcpr_cache_t *lastcache = NULL;
  205. u_char *byte = NULL;
  206. uint32_t bit;
  207. tcpr_dir_t result = TCPR_DIR_ERROR;
  208. COUNTER index;
  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) {
  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. index = (lastcache->packets - 1) / (COUNTER)CACHE_PACKETS_PER_BYTE;
  235. bit = (((lastcache->packets - 1) % (COUNTER)CACHE_PACKETS_PER_BYTE) *
  236. (COUNTER)CACHE_BITS_PER_PACKET) + 1;
  237. dbgx(3, "Bit: %d", bit);
  238. byte = (u_char *) & lastcache->data[index];
  239. *byte += (u_char) (1 << bit);
  240. dbgx(2, "set send bit: byte " COUNTER_SPEC " = 0x%x", index, *byte);
  241. /* if true, set low order bit. else, do squat */
  242. if (interface == TCPR_DIR_C2S) {
  243. *byte += (u_char)(1 << (bit - 1));
  244. dbgx(2, "set interface bit: byte " COUNTER_SPEC " = 0x%x", index, *byte);
  245. result = TCPR_DIR_C2S;
  246. }
  247. else {
  248. dbgx(2, "don't set interface bit: byte " COUNTER_SPEC " = 0x%x", index, *byte);
  249. result = TCPR_DIR_S2C;
  250. }
  251. #ifdef DEBUG
  252. /*
  253. * only build the byte string when not in debug mode since
  254. * the calculation is a bit expensive
  255. */
  256. dbgx(3, "Current cache byte: %c%c%c%c%c%c%c%c",
  257. BIT_STR(byte2bits(*byte, bitstring)));
  258. #endif
  259. }
  260. else {
  261. dbg(1, "not setting send bit");
  262. result = TCPR_DIR_NOSEND;
  263. }
  264. return result;
  265. }
  266. /**
  267. * returns the action for a given packet based on the CACHE
  268. */
  269. tcpr_dir_t
  270. check_cache(char *cachedata, COUNTER packetid)
  271. {
  272. COUNTER index = 0;
  273. uint32_t bit;
  274. assert(cachedata);
  275. if (packetid == 0)
  276. err(-1, "packetid must be > 0");
  277. index = (packetid - 1) / (COUNTER)CACHE_PACKETS_PER_BYTE;
  278. bit = (uint32_t)(((packetid - 1) % (COUNTER)CACHE_PACKETS_PER_BYTE) *
  279. (COUNTER)CACHE_BITS_PER_PACKET) + 1;
  280. #ifdef DEBUG
  281. dbgx(3, "Index: " COUNTER_SPEC "\tBit: %d\tByte: %hhu\tMask: %hhu", index, bit,
  282. cachedata[index], (uint8_t)(cachedata[index] & (char)(1 << bit)));
  283. #endif
  284. if (!(cachedata[index] & (char)(1 << bit))) {
  285. return TCPR_DIR_NOSEND;
  286. }
  287. /* go back a bit to get the interface */
  288. bit--;
  289. if (cachedata[index] & (char)(1 << bit)) {
  290. return TCPR_DIR_C2S;
  291. }
  292. else {
  293. return TCPR_DIR_S2C;
  294. }
  295. return TCPR_DIR_ERROR;
  296. }