tcpprep.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  1. /* $Id: tcpprep.c 2006 2008-05-01 04:46:55Z aturner $ */
  2. /*
  3. * Copyright (c) 2001-2007 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. /*
  32. * Purpose:
  33. * 1) Remove the performance bottleneck in tcpreplay for choosing an NIC
  34. * 2) Seperate code to make it more manageable
  35. * 3) Add addtional features which require multiple passes of a pcap
  36. *
  37. * Support:
  38. * Right now we support matching source IP based upon on of the following:
  39. * - Regular expression
  40. * - IP address is contained in one of a list of CIDR blocks
  41. * - Auto learning of CIDR block for servers (clients all other)
  42. */
  43. #include "config.h"
  44. #include "defines.h"
  45. #include "common.h"
  46. #include <stdio.h>
  47. #include <stdlib.h>
  48. #include <string.h>
  49. #include <regex.h>
  50. #include <string.h>
  51. #include <unistd.h>
  52. #include <errno.h>
  53. #include "tcpprep.h"
  54. #include "tcpedit/tcpedit.h"
  55. #include "tcpprep_opts.h"
  56. #include "lib/tree.h"
  57. #include "tree.h"
  58. #include "lib/sll.h"
  59. #include "lib/strlcpy.h"
  60. /*
  61. * global variables
  62. */
  63. #ifdef DEBUG
  64. int debug = 0;
  65. #endif
  66. #ifdef ENABLE_VERBOSE
  67. tcpdump_t tcpdump;
  68. #endif
  69. tcpprep_opt_t options;
  70. int info = 0;
  71. char *ourregex = NULL;
  72. char *cidr = NULL;
  73. tcpr_data_tree_t treeroot;
  74. static void init(void);
  75. static void post_args(int, char *[]);
  76. static void print_comment(const char *);
  77. static void print_info(const char *);
  78. static void print_stats(const char *);
  79. static int check_ip_regex(const unsigned long ip);
  80. static COUNTER process_raw_packets(pcap_t * pcap);
  81. static int check_dst_port(ipv4_hdr_t *ip_hdr, int len);
  82. /*
  83. * main()
  84. */
  85. int
  86. main(int argc, char *argv[])
  87. {
  88. int out_file;
  89. COUNTER totpackets = 0;
  90. char errbuf[PCAP_ERRBUF_SIZE];
  91. int optct = 0;
  92. init(); /* init our globals */
  93. optct = optionProcess(&tcpprepOptions, argc, argv);
  94. post_args(argc, argv);
  95. argc -= optct;
  96. argv += optct;
  97. /* open the cache file */
  98. if ((out_file = open(OPT_ARG(CACHEFILE), O_WRONLY | O_CREAT | O_TRUNC,
  99. S_IREAD | S_IWRITE | S_IRGRP | S_IWGRP | S_IROTH)) == -1)
  100. errx(1, "Unable to open cache file %s for writing: %s",
  101. OPT_ARG(CACHEFILE), strerror(errno));
  102. readpcap:
  103. /* open the pcap file */
  104. if ((options.pcap = pcap_open_offline(OPT_ARG(PCAP), errbuf)) == NULL)
  105. errx(1, "Error opening file: %s", errbuf);
  106. /* make sure we support the DLT type */
  107. switch(pcap_datalink(options.pcap)) {
  108. case DLT_EN10MB:
  109. case DLT_LINUX_SLL:
  110. case DLT_RAW:
  111. case DLT_C_HDLC:
  112. break; /* do nothing because all is good */
  113. default:
  114. errx(1, "Unsupported pcap DLT type: 0x%x", pcap_datalink(options.pcap));
  115. }
  116. /* Can only split based on MAC address for ethernet */
  117. if ((pcap_datalink(options.pcap) != DLT_EN10MB) &&
  118. (options.mode == MAC_MODE)) {
  119. err(1, "MAC mode splitting is only supported by DLT_EN10MB packet captures.");
  120. }
  121. #ifdef ENABLE_VERBOSE
  122. if (HAVE_OPT(VERBOSE)) {
  123. tcpdump_open(&tcpdump, options.pcap);
  124. }
  125. #endif
  126. /* do we apply a bpf filter? */
  127. if (options.bpf.filter != NULL) {
  128. if (pcap_compile(options.pcap, &options.bpf.program, options.bpf.filter,
  129. options.bpf.optimize, 0) != 0) {
  130. errx(1, "Error compiling BPF filter: %s", pcap_geterr(options.pcap));
  131. }
  132. pcap_setfilter(options.pcap, &options.bpf.program);
  133. }
  134. if ((totpackets = process_raw_packets(options.pcap)) == 0) {
  135. pcap_close(options.pcap);
  136. err(1, "No packets were processed. Filter too limiting?");
  137. }
  138. pcap_close(options.pcap);
  139. #ifdef ENABLE_VERBOSE
  140. tcpdump_close(&tcpdump);
  141. #endif
  142. /* we need to process the pcap file twice in HASH/AUTO mode */
  143. if (options.mode == AUTO_MODE) {
  144. options.mode = options.automode;
  145. if (options.mode == ROUTER_MODE) { /* do we need to convert TREE->CIDR? */
  146. if (info)
  147. notice("Building network list from pre-cache...\n");
  148. if (!process_tree()) {
  149. err(1, "Error: unable to build a valid list of servers. Aborting.");
  150. }
  151. }
  152. else {
  153. /*
  154. * in bridge mode we need to calculate client/sever
  155. * manually since this is done automatically in
  156. * process_tree()
  157. */
  158. tree_calculate(&treeroot);
  159. }
  160. if (info)
  161. notice("Buliding cache file...\n");
  162. /*
  163. * re-process files, but this time generate
  164. * cache
  165. */
  166. goto readpcap;
  167. }
  168. #ifdef DEBUG
  169. if (debug && (options.cidrdata != NULL))
  170. print_cidr(options.cidrdata);
  171. #endif
  172. /* write cache data */
  173. totpackets = write_cache(options.cachedata, out_file, totpackets,
  174. options.comment);
  175. if (info)
  176. notice("Done.\nCached " COUNTER_SPEC " packets.\n", totpackets);
  177. /* close cache file */
  178. close(out_file);
  179. return 0;
  180. }
  181. /**
  182. * checks the dst port to see if this is destined for a server port.
  183. * returns 1 for true, 0 for false
  184. */
  185. static int
  186. check_dst_port(ipv4_hdr_t *ip_hdr, int len)
  187. {
  188. tcp_hdr_t *tcp_hdr = NULL;
  189. udp_hdr_t *udp_hdr = NULL;
  190. assert(ip_hdr);
  191. if (len < ((ip_hdr->ip_hl * 4) + 4))
  192. return 0; /* not enough data in the packet to know */
  193. dbg(3, "Checking the destination port...");
  194. if (ip_hdr->ip_p == IPPROTO_TCP) {
  195. tcp_hdr = (tcp_hdr_t *)get_layer4(ip_hdr);
  196. /* is a service? */
  197. if (options.services.tcp[ntohs(tcp_hdr->th_dport)]) {
  198. dbgx(1, "TCP packet is destined for a server port: %d", ntohs(tcp_hdr->th_dport));
  199. return 1;
  200. }
  201. /* nope */
  202. dbgx(1, "TCP packet is NOT destined for a server port: %d", ntohs(tcp_hdr->th_dport));
  203. return 0;
  204. } else if (ip_hdr->ip_p == IPPROTO_UDP) {
  205. udp_hdr = (udp_hdr_t *)get_layer4(ip_hdr);
  206. /* is a service? */
  207. if (options.services.udp[ntohs(udp_hdr->uh_dport)]) {
  208. dbgx(1, "UDP packet is destined for a server port: %d", ntohs(udp_hdr->uh_dport));
  209. return 1;
  210. }
  211. /* nope */
  212. dbgx(1, "UDP packet is NOT destined for a server port: %d", ntohs(udp_hdr->uh_dport));
  213. return 0;
  214. }
  215. /* not a TCP or UDP packet... return as non_ip */
  216. dbg(1, "Packet isn't a UDP or TCP packet... no port to process.");
  217. return options.nonip;
  218. }
  219. /**
  220. * checks to see if an ip address matches a regex. Returns 1 for true
  221. * 0 for false
  222. */
  223. static int
  224. check_ip_regex(const unsigned long ip)
  225. {
  226. int eflags = 0;
  227. u_char src_ip[16];
  228. size_t nmatch = 0;
  229. regmatch_t *pmatch = NULL;
  230. memset(src_ip, '\0', 16);
  231. strlcpy((char *)src_ip, (char *)get_addr2name4(ip, RESOLVE),
  232. sizeof(src_ip));
  233. if (regexec(&options.preg, (char *)src_ip, nmatch, pmatch, eflags) == 0) {
  234. return (1);
  235. }
  236. else {
  237. return (0);
  238. }
  239. }
  240. /**
  241. * uses libpcap library to parse the packets and build
  242. * the cache file.
  243. */
  244. static COUNTER
  245. process_raw_packets(pcap_t * pcap)
  246. {
  247. ipv4_hdr_t *ip_hdr = NULL;
  248. eth_hdr_t *eth_hdr = NULL;
  249. struct pcap_pkthdr pkthdr;
  250. const u_char *pktdata = NULL;
  251. COUNTER packetnum = 0;
  252. int l2len, cache_result = 0;
  253. u_char ipbuff[MAXPACKET], *buffptr;
  254. tcpr_dir_t direction;
  255. #ifdef ENABLE_VERBOSE
  256. struct pollfd poller[1];
  257. poller[0].fd = tcpdump.outfd;
  258. poller[0].events = POLLIN;
  259. poller[0].revents = 0;
  260. #endif
  261. assert(pcap);
  262. while ((pktdata = pcap_next(pcap, &pkthdr)) != NULL) {
  263. packetnum++;
  264. dbgx(1, "Packet " COUNTER_SPEC, packetnum);
  265. /* look for include or exclude LIST match */
  266. if (options.xX.list != NULL) {
  267. if (options.xX.mode < xXExclude) {
  268. if (!check_list(options.xX.list, packetnum)) {
  269. add_cache(&(options.cachedata), DONT_SEND, 0);
  270. continue;
  271. }
  272. }
  273. else if (check_list(options.xX.list, packetnum)) {
  274. add_cache(&(options.cachedata), DONT_SEND, 0);
  275. continue;
  276. }
  277. }
  278. eth_hdr = (eth_hdr_t *)pktdata;
  279. /* get the IP header (if any) */
  280. buffptr = ipbuff;
  281. ip_hdr = (ipv4_hdr_t *)get_ipv4(pktdata, pkthdr.caplen,
  282. pcap_datalink(pcap), &buffptr);
  283. if (ip_hdr == NULL) {
  284. dbg(2, "Packet isn't IP");
  285. /* we don't want to cache these packets twice */
  286. if (options.mode != AUTO_MODE) {
  287. dbg(3, "Adding to cache using options for Non-IP packets");
  288. add_cache(&options.cachedata, SEND, options.nonip);
  289. }
  290. continue;
  291. }
  292. l2len = get_l2len(pktdata, pkthdr.caplen, pcap_datalink(pcap));
  293. /* look for include or exclude CIDR match */
  294. if (options.xX.cidr != NULL) {
  295. if (!process_xX_by_cidr(options.xX.mode, options.xX.cidr, ip_hdr)) {
  296. add_cache(&options.cachedata, DONT_SEND, 0);
  297. continue;
  298. }
  299. }
  300. switch (options.mode) {
  301. case REGEX_MODE:
  302. dbg(2, "processing regex mode...");
  303. direction = check_ip_regex(ip_hdr->ip_src.s_addr);
  304. /* reverse direction? */
  305. if (HAVE_OPT(REVERSE) && (direction == TCPR_DIR_C2S || direction == TCPR_DIR_S2C))
  306. direction = direction == TCPR_DIR_C2S ? TCPR_DIR_S2C : TCPR_DIR_C2S;
  307. cache_result = add_cache(&options.cachedata, SEND, direction);
  308. break;
  309. case CIDR_MODE:
  310. dbg(2, "processing cidr mode...");
  311. direction = check_ip_cidr(options.cidrdata, ip_hdr->ip_src.s_addr) ? TCPR_DIR_C2S : TCPR_DIR_S2C;
  312. /* reverse direction? */
  313. if (HAVE_OPT(REVERSE) && (direction == TCPR_DIR_C2S || direction == TCPR_DIR_S2C))
  314. direction = direction == TCPR_DIR_C2S ? TCPR_DIR_S2C : TCPR_DIR_C2S;
  315. cache_result = add_cache(&options.cachedata, SEND, direction);
  316. break;
  317. case MAC_MODE:
  318. dbg(2, "processing mac mode...");
  319. direction = macinstring(options.maclist, (u_char *)eth_hdr->ether_shost);
  320. /* reverse direction? */
  321. if (HAVE_OPT(REVERSE) && (direction == TCPR_DIR_C2S || direction == TCPR_DIR_S2C))
  322. direction = direction == TCPR_DIR_C2S ? TCPR_DIR_S2C : TCPR_DIR_C2S;
  323. cache_result = add_cache(&options.cachedata, SEND, direction);
  324. break;
  325. case AUTO_MODE:
  326. dbg(2, "processing first pass of auto mode...");
  327. /* first run through in auto mode: create tree */
  328. if (options.automode != FIRST_MODE) {
  329. add_tree(ip_hdr->ip_src.s_addr, pktdata);
  330. } else {
  331. add_tree_first(pktdata);
  332. }
  333. break;
  334. case ROUTER_MODE:
  335. /*
  336. * second run through in auto mode: create route
  337. * based cache
  338. */
  339. dbg(2, "processing second pass of auto: router mode...");
  340. cache_result = add_cache(&options.cachedata, SEND,
  341. check_ip_tree(options.nonip, ip_hdr->ip_src.s_addr));
  342. break;
  343. case BRIDGE_MODE:
  344. /*
  345. * second run through in auto mode: create bridge
  346. * based cache
  347. */
  348. dbg(2, "processing second pass of auto: bridge mode...");
  349. cache_result = add_cache(&options.cachedata, SEND,
  350. check_ip_tree(DIR_UNKNOWN, ip_hdr->ip_src.s_addr));
  351. break;
  352. case SERVER_MODE:
  353. /*
  354. * second run through in auto mode: create bridge
  355. * where unknowns are servers
  356. */
  357. dbg(2, "processing second pass of auto: server mode...");
  358. cache_result = add_cache(&options.cachedata, SEND,
  359. check_ip_tree(DIR_SERVER, ip_hdr->ip_src.s_addr));
  360. break;
  361. case CLIENT_MODE:
  362. /*
  363. * second run through in auto mode: create bridge
  364. * where unknowns are clients
  365. */
  366. dbg(2, "processing second pass of auto: client mode...");
  367. cache_result = add_cache(&options.cachedata, SEND,
  368. check_ip_tree(DIR_CLIENT, ip_hdr->ip_src.s_addr));
  369. break;
  370. case PORT_MODE:
  371. /*
  372. * process ports based on their destination port
  373. */
  374. dbg(2, "processing port mode...");
  375. cache_result = add_cache(&options.cachedata, SEND,
  376. check_dst_port(ip_hdr, (pkthdr.caplen - l2len)));
  377. break;
  378. case FIRST_MODE:
  379. /*
  380. * First packet mode, looks at each host and picks clients
  381. * by the ones which send the first packet in a session
  382. */
  383. dbg(2, "processing second pass of auto: first packet mode...");
  384. cache_result = add_cache(&options.cachedata, SEND,
  385. check_ip_tree(DIR_UNKNOWN, ip_hdr->ip_src.s_addr));
  386. break;
  387. default:
  388. errx(1, "Whops! What mode are we in anyways? %d", options.mode);
  389. }
  390. #ifdef ENABLE_VERBOSE
  391. if (options.verbose)
  392. tcpdump_print(&tcpdump, &pkthdr, pktdata);
  393. #endif
  394. }
  395. return packetnum;
  396. }
  397. /**
  398. * init our options
  399. */
  400. void
  401. init(void)
  402. {
  403. int i;
  404. memset(&options, '\0', sizeof(options));
  405. options.bpf.optimize = BPF_OPTIMIZE;
  406. for (i = DEFAULT_LOW_SERVER_PORT; i <= DEFAULT_HIGH_SERVER_PORT; i++) {
  407. options.services.tcp[i] = 1;
  408. options.services.udp[i] = 1;
  409. }
  410. }
  411. /**
  412. * post process args
  413. */
  414. static void
  415. post_args(int argc, char *argv[])
  416. {
  417. char myargs[MYARGS_LEN];
  418. int i, bufsize;
  419. char *tempstr;
  420. memset(myargs, 0, MYARGS_LEN);
  421. /* print_comment and print_info don't return */
  422. if (HAVE_OPT(PRINT_COMMENT))
  423. print_comment(OPT_ARG(PRINT_COMMENT));
  424. if (HAVE_OPT(PRINT_INFO))
  425. print_info(OPT_ARG(PRINT_INFO));
  426. if (HAVE_OPT(PRINT_STATS))
  427. print_stats(OPT_ARG(PRINT_STATS));
  428. if (! HAVE_OPT(CACHEFILE) && ! HAVE_OPT(PCAP))
  429. err(1, "Must specify an output cachefile (-o) and input pcap (-i)");
  430. if (! options.mode)
  431. err(1, "Must specify a processing mode: -a, -c, -r, -p");
  432. #ifdef DEBUG
  433. if (HAVE_OPT(DBUG))
  434. debug = OPT_VALUE_DBUG;
  435. #endif
  436. #ifdef ENABLE_VERBOSE
  437. if (HAVE_OPT(VERBOSE)) {
  438. options.verbose = 1;
  439. }
  440. if (HAVE_OPT(DECODE))
  441. tcpdump.args = safe_strdup(OPT_ARG(DECODE));
  442. /*
  443. * put the open after decode options so they are passed to tcpdump
  444. */
  445. #endif
  446. /*
  447. * if we are to include the cli args, then prep it for the
  448. * cache file header
  449. */
  450. if (! options.nocomment) {
  451. /* copy all of our args to myargs */
  452. for (i = 1; i < argc; i ++) {
  453. /* skip the -C <comment> */
  454. if (strcmp(argv[i], "-C") == 0) {
  455. i += 2;
  456. continue;
  457. }
  458. strlcat(myargs, argv[i], MYARGS_LEN);
  459. strlcat(myargs, " ", MYARGS_LEN);
  460. }
  461. /* remove trailing space */
  462. myargs[strlen(myargs) - 1] = 0;
  463. dbgx(1, "Comment args length: %d", strlen(myargs));
  464. }
  465. /* setup or options.comment buffer so that that we get args\ncomment */
  466. if (options.comment != NULL) {
  467. strlcat(myargs, "\n", MYARGS_LEN);
  468. bufsize = strlen(options.comment) + strlen(myargs) + 1;
  469. options.comment = (char *)safe_realloc(options.comment,
  470. bufsize);
  471. tempstr = strdup(options.comment);
  472. strlcpy(options.comment, myargs, bufsize);
  473. strlcat(options.comment, tempstr, bufsize);
  474. } else {
  475. bufsize = strlen(myargs) + 1;
  476. options.comment = (char *)safe_malloc(bufsize);
  477. strlcpy(options.comment, myargs, bufsize);
  478. }
  479. dbgx(1, "Final comment length: %d", strlen(options.comment));
  480. /* copy over our min/max mask */
  481. options.min_mask = OPT_VALUE_MINMASK;
  482. options.max_mask = OPT_VALUE_MAXMASK;
  483. if (! options.min_mask > options.max_mask)
  484. errx(1, "Min network mask len (%d) must be less then max network mask len (%d)",
  485. options.min_mask, options.max_mask);
  486. options.ratio = atof(OPT_ARG(RATIO));
  487. if (options.ratio < 0)
  488. err(1, "Ratio must be a non-negative number.");
  489. }
  490. /**
  491. * print the tcpprep cache file comment
  492. */
  493. static void
  494. print_comment(const char *file)
  495. {
  496. char *cachedata = NULL;
  497. char *comment = NULL;
  498. COUNTER count = 0;
  499. count = read_cache(&cachedata, file, &comment);
  500. printf("tcpprep args: %s\n", comment);
  501. printf("Cache contains data for " COUNTER_SPEC " packets\n", count);
  502. exit(0);
  503. }
  504. /**
  505. * prints out the cache file details
  506. */
  507. static void
  508. print_info(const char *file)
  509. {
  510. char *cachedata = NULL;
  511. char *comment = NULL;
  512. COUNTER count = 0, i;
  513. count = read_cache(&cachedata, file, &comment);
  514. for (i = 1; i <= count; i ++) {
  515. switch (check_cache(cachedata, i)) {
  516. case TCPR_DIR_C2S:
  517. printf("Packet " COUNTER_SPEC " -> Primary\n", i);
  518. break;
  519. case TCPR_DIR_S2C:
  520. printf("Packet " COUNTER_SPEC " -> Secondary\n", i);
  521. break;
  522. case TCPR_DIR_NOSEND:
  523. printf("Packet " COUNTER_SPEC " -> Don't Send\n", i);
  524. break;
  525. default:
  526. err(1, "Invalid cachedata value!");
  527. break;
  528. }
  529. }
  530. exit(0);
  531. }
  532. /**
  533. * Print the per-packet statistics
  534. */
  535. static void
  536. print_stats(const char *file)
  537. {
  538. char *cachedata = NULL;
  539. char *comment = NULL;
  540. COUNTER count = 0;
  541. COUNTER pri = 0, sec = 0, nosend = 0;
  542. count = read_cache(&cachedata, file, &comment);
  543. for (COUNTER i = 1; i <= count; i ++) {
  544. int cacheval = check_cache(cachedata, i);
  545. switch (cacheval) {
  546. case TCPR_DIR_C2S:
  547. pri ++;
  548. break;
  549. case TCPR_DIR_S2C:
  550. sec ++;
  551. break;
  552. case TCPR_DIR_NOSEND:
  553. nosend ++;
  554. break;
  555. default:
  556. errx(1, "Unknown cache value: %d", cacheval);
  557. }
  558. }
  559. printf("Primary packets:\t" COUNTER_SPEC "\n", pri);
  560. printf("Secondary packets:\t" COUNTER_SPEC "\n", sec);
  561. printf("Skipped packets:\t" COUNTER_SPEC "\n", nosend);
  562. printf("------------------------------\n");
  563. printf("Total packets:\t\t" COUNTER_SPEC "\n", count);
  564. exit(0);
  565. }
  566. /*
  567. Local Variables:
  568. mode:c
  569. indent-tabs-mode:nil
  570. c-basic-offset:4
  571. End:
  572. */