cache.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /* $Id: cache.c 1907 2007-10-05 23:24:38Z 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. /* deal with any remainder, becuase above divsion is integer */
  106. if (header.num_packets % header.packets_per_byte)
  107. cache_size ++;
  108. dbgx(1, "Cache file contains %llu packets in %llu bytes",
  109. header.num_packets, cache_size);
  110. dbgx(1, "Cache uses %d packets per byte", header.packets_per_byte);
  111. *cachedata = (char *)safe_malloc(cache_size);
  112. /* read in the cache */
  113. if ((COUNTER)(read_size = read(cachefd, *cachedata, cache_size))
  114. != cache_size)
  115. errx(1, "Cache data length (%ld bytes) doesn't match "
  116. "cache header (%ld bytes)", read_size, cache_size);
  117. dbgx(1, "Loaded in %llu packets from cache.", header.num_packets);
  118. close(cachefd);
  119. return (header.num_packets);
  120. }
  121. /**
  122. * writes out the cache file header, comment and then the
  123. * contents of *cachedata to out_file and then returns the number
  124. * of cache entries written
  125. */
  126. COUNTER
  127. write_cache(tcpr_cache_t * cachedata, const int out_file, COUNTER numpackets,
  128. char *comment)
  129. {
  130. tcpr_cache_t *mycache = NULL;
  131. tcpr_cache_file_hdr_t *cache_header = NULL;
  132. u_int32_t chars, last = 0;
  133. COUNTER packets = 0;
  134. ssize_t written = 0;
  135. assert(cachedata);
  136. assert(out_file);
  137. /* write a header to our file */
  138. cache_header = (tcpr_cache_file_hdr_t *)
  139. safe_malloc(sizeof(tcpr_cache_file_hdr_t));
  140. strncpy(cache_header->magic, CACHEMAGIC, strlen(CACHEMAGIC));
  141. strncpy(cache_header->version, CACHEVERSION, strlen(CACHEMAGIC));
  142. cache_header->packets_per_byte = htons(CACHE_PACKETS_PER_BYTE);
  143. cache_header->num_packets = htonll((u_int64_t)numpackets);
  144. /* we can't strlen(NULL) so ... */
  145. if (comment != NULL) {
  146. cache_header->comment_len = htons((u_int16_t)strlen(comment));
  147. } else {
  148. cache_header->comment_len = 0;
  149. }
  150. written = write(out_file, cache_header, sizeof(tcpr_cache_file_hdr_t));
  151. dbgx(1, "Wrote %d bytes of cache file header", written);
  152. if (written != sizeof(tcpr_cache_file_hdr_t))
  153. errx(1, "Only wrote %d of %d bytes of the cache file header!\n%s",
  154. written, sizeof(tcpr_cache_file_hdr_t),
  155. written == -1 ? strerror(errno) : "");
  156. /* don't write comment if there is none */
  157. if (comment != NULL) {
  158. written = write(out_file, comment, strlen(comment));
  159. dbgx(1, "Wrote %d bytes of comment", written);
  160. if (written != (ssize_t)strlen(comment))
  161. errx(1, "Only wrote %d of %d bytes of the comment!\n%s",
  162. written, strlen(comment),
  163. written == -1 ? strerror(errno) : "");
  164. }
  165. mycache = cachedata;
  166. while (!last) {
  167. /* increment total packets */
  168. packets += mycache->packets;
  169. /* calculate how many chars to write */
  170. chars = mycache->packets / CACHE_PACKETS_PER_BYTE;
  171. if (mycache->packets % CACHE_PACKETS_PER_BYTE) {
  172. chars++;
  173. dbgx(1, "Bumping up to the next byte: %d %% %d", mycache->packets,
  174. CACHE_PACKETS_PER_BYTE);
  175. }
  176. /* write to file, and verify it wrote properly */
  177. written = write(out_file, mycache->data, chars);
  178. dbgx(1, "Wrote %i bytes of cache data", written);
  179. if (written != (ssize_t)chars)
  180. errx(1, "Only wrote %i of %i bytes to cache file!", written, chars);
  181. /*
  182. * if that was the last, stop processing, otherwise wash,
  183. * rinse, repeat
  184. */
  185. if (mycache->next != NULL) {
  186. mycache = mycache->next;
  187. }
  188. else {
  189. last = 1;
  190. }
  191. }
  192. /* return number of packets written */
  193. return (packets);
  194. }
  195. /**
  196. * mallocs a new CACHE struct all pre-set to sane defaults
  197. */
  198. static tcpr_cache_t *
  199. new_cache(void)
  200. {
  201. tcpr_cache_t *newcache;
  202. /* malloc mem */
  203. newcache = (tcpr_cache_t *)safe_malloc(sizeof(tcpr_cache_t));
  204. return (newcache);
  205. }
  206. /**
  207. * adds the cache data for a packet to the given cachedata
  208. */
  209. tcpr_dir_t
  210. add_cache(tcpr_cache_t ** cachedata, const int send, const tcpr_dir_t interface)
  211. {
  212. static tcpr_cache_t *lastcache = NULL;
  213. u_char *byte = NULL;
  214. u_int32_t bit;
  215. tcpr_dir_t result = TCPR_DIR_ERROR;
  216. COUNTER index;
  217. #ifdef DEBUG
  218. char bitstring[9] = EIGHT_ZEROS;
  219. #endif
  220. assert(cachedata);
  221. /* first run? malloc our first entry, set bit count to 0 */
  222. if (*cachedata == NULL) {
  223. *cachedata = new_cache();
  224. lastcache = *cachedata;
  225. }
  226. else {
  227. /* check to see if this is the last bit in this struct */
  228. if ((lastcache->packets + 1) > (CACHEDATASIZE * CACHE_PACKETS_PER_BYTE)) {
  229. /*
  230. * if so, we have to malloc a new one and set bit to 0
  231. */
  232. dbg(1, "Adding to cachedata linked list");
  233. lastcache->next = new_cache();
  234. lastcache = lastcache->next;
  235. }
  236. }
  237. /* always increment our bit count */
  238. lastcache->packets++;
  239. dbgx(1, "Cache array packet %d", lastcache->packets);
  240. /* send packet ? */
  241. if (send == SEND) {
  242. index = (lastcache->packets - 1) / (COUNTER)CACHE_PACKETS_PER_BYTE;
  243. bit = (((lastcache->packets - 1) % (COUNTER)CACHE_PACKETS_PER_BYTE) *
  244. (COUNTER)CACHE_BITS_PER_PACKET) + 1;
  245. dbgx(3, "Bit: %d", bit);
  246. byte = (u_char *) & lastcache->data[index];
  247. *byte += (u_char) (1 << bit);
  248. dbgx(2, "set send bit: byte " COUNTER_SPEC " = 0x%x", index, *byte);
  249. /* if true, set low order bit. else, do squat */
  250. if (interface == TCPR_DIR_C2S) {
  251. *byte += (u_char)(1 << (bit - 1));
  252. dbgx(2, "set interface bit: byte " COUNTER_SPEC " = 0x%x", index, *byte);
  253. result = TCPR_DIR_C2S;
  254. }
  255. else {
  256. dbgx(2, "don't set interface bit: byte " COUNTER_SPEC " = 0x%x", index, *byte);
  257. result = TCPR_DIR_S2C;
  258. }
  259. #ifdef DEBUG
  260. /*
  261. * only build the byte string when not in debug mode since
  262. * the calculation is a bit expensive
  263. */
  264. dbgx(3, "Current cache byte: %c%c%c%c%c%c%c%c",
  265. BIT_STR(byte2bits(*byte, bitstring)));
  266. #endif
  267. }
  268. else {
  269. dbg(1, "not setting send bit");
  270. result = TCPR_DIR_NOSEND;
  271. }
  272. return result;
  273. }
  274. /**
  275. * returns the action for a given packet based on the CACHE
  276. */
  277. tcpr_dir_t
  278. check_cache(char *cachedata, COUNTER packetid)
  279. {
  280. COUNTER index = 0;
  281. u_int32_t bit;
  282. assert(cachedata);
  283. if (packetid == 0)
  284. err(1, "packetid must be > 0");
  285. index = (packetid - 1) / (COUNTER)CACHE_PACKETS_PER_BYTE;
  286. bit = (u_int32_t)(((packetid - 1) % (COUNTER)CACHE_PACKETS_PER_BYTE) *
  287. (COUNTER)CACHE_BITS_PER_PACKET) + 1;
  288. #ifdef DEBUG
  289. dbgx(3, "Index: " COUNTER_SPEC "\tBit: %d\tByte: %hhu\tMask: %hhu", index, bit,
  290. cachedata[index], (cachedata[index] & (char)(1 << bit)));
  291. #endif
  292. if (!(cachedata[index] & (char)(1 << bit))) {
  293. return TCPR_DIR_NOSEND;
  294. }
  295. /* go back a bit to get the interface */
  296. bit--;
  297. if (cachedata[index] & (char)(1 << bit)) {
  298. return TCPR_DIR_C2S;
  299. }
  300. else {
  301. return TCPR_DIR_S2C;
  302. }
  303. return TCPR_DIR_ERROR;
  304. }
  305. /*
  306. Local Variables:
  307. mode:c
  308. indent-tabs-mode:nil
  309. c-basic-offset:4
  310. End:
  311. */