cache.c 11 KB

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