cache.c 12 KB

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