cache.c 11 KB

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