tcpprep.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  1. /* $Id: tcpprep.c 2169 2009-01-21 20:07:02Z 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. /*
  279. * If the packet doesn't include an IPv4 header we should just treat
  280. * it as a non-IP packet, UNLESS we're in MAC mode, in which case
  281. * we should let the MAC matcher below handle it
  282. */
  283. eth_hdr = (eth_hdr_t *)pktdata;
  284. if (options.mode != MAC_MODE) {
  285. /* get the IP header (if any) */
  286. buffptr = ipbuff;
  287. ip_hdr = (ipv4_hdr_t *)get_ipv4(pktdata, pkthdr.caplen,
  288. pcap_datalink(pcap), &buffptr);
  289. if (ip_hdr == NULL) {
  290. dbg(2, "Packet isn't IP");
  291. /* we don't want to cache these packets twice */
  292. if (options.mode != AUTO_MODE) {
  293. dbg(3, "Adding to cache using options for Non-IP packets");
  294. add_cache(&options.cachedata, SEND, options.nonip);
  295. }
  296. continue;
  297. }
  298. l2len = get_l2len(pktdata, pkthdr.caplen, pcap_datalink(pcap));
  299. /* look for include or exclude CIDR match */
  300. if (options.xX.cidr != NULL) {
  301. if (!process_xX_by_cidr(options.xX.mode, options.xX.cidr, ip_hdr)) {
  302. add_cache(&options.cachedata, DONT_SEND, 0);
  303. continue;
  304. }
  305. }
  306. }
  307. switch (options.mode) {
  308. case REGEX_MODE:
  309. dbg(2, "processing regex mode...");
  310. direction = check_ip_regex(ip_hdr->ip_src.s_addr);
  311. /* reverse direction? */
  312. if (HAVE_OPT(REVERSE) && (direction == TCPR_DIR_C2S || direction == TCPR_DIR_S2C))
  313. direction = direction == TCPR_DIR_C2S ? TCPR_DIR_S2C : TCPR_DIR_C2S;
  314. cache_result = add_cache(&options.cachedata, SEND, direction);
  315. break;
  316. case CIDR_MODE:
  317. dbg(2, "processing cidr mode...");
  318. direction = check_ip_cidr(options.cidrdata, ip_hdr->ip_src.s_addr) ? TCPR_DIR_C2S : TCPR_DIR_S2C;
  319. /* reverse direction? */
  320. if (HAVE_OPT(REVERSE) && (direction == TCPR_DIR_C2S || direction == TCPR_DIR_S2C))
  321. direction = direction == TCPR_DIR_C2S ? TCPR_DIR_S2C : TCPR_DIR_C2S;
  322. cache_result = add_cache(&options.cachedata, SEND, direction);
  323. break;
  324. case MAC_MODE:
  325. dbg(2, "processing mac mode...");
  326. direction = macinstring(options.maclist, (u_char *)eth_hdr->ether_shost);
  327. /* reverse direction? */
  328. if (HAVE_OPT(REVERSE) && (direction == TCPR_DIR_C2S || direction == TCPR_DIR_S2C))
  329. direction = direction == TCPR_DIR_C2S ? TCPR_DIR_S2C : TCPR_DIR_C2S;
  330. cache_result = add_cache(&options.cachedata, SEND, direction);
  331. break;
  332. case AUTO_MODE:
  333. dbg(2, "processing first pass of auto mode...");
  334. /* first run through in auto mode: create tree */
  335. if (options.automode != FIRST_MODE) {
  336. add_tree(ip_hdr->ip_src.s_addr, pktdata);
  337. } else {
  338. add_tree_first(pktdata);
  339. }
  340. break;
  341. case ROUTER_MODE:
  342. /*
  343. * second run through in auto mode: create route
  344. * based cache
  345. */
  346. dbg(2, "processing second pass of auto: router mode...");
  347. cache_result = add_cache(&options.cachedata, SEND,
  348. check_ip_tree(options.nonip, ip_hdr->ip_src.s_addr));
  349. break;
  350. case BRIDGE_MODE:
  351. /*
  352. * second run through in auto mode: create bridge
  353. * based cache
  354. */
  355. dbg(2, "processing second pass of auto: bridge mode...");
  356. cache_result = add_cache(&options.cachedata, SEND,
  357. check_ip_tree(DIR_UNKNOWN, ip_hdr->ip_src.s_addr));
  358. break;
  359. case SERVER_MODE:
  360. /*
  361. * second run through in auto mode: create bridge
  362. * where unknowns are servers
  363. */
  364. dbg(2, "processing second pass of auto: server mode...");
  365. cache_result = add_cache(&options.cachedata, SEND,
  366. check_ip_tree(DIR_SERVER, ip_hdr->ip_src.s_addr));
  367. break;
  368. case CLIENT_MODE:
  369. /*
  370. * second run through in auto mode: create bridge
  371. * where unknowns are clients
  372. */
  373. dbg(2, "processing second pass of auto: client mode...");
  374. cache_result = add_cache(&options.cachedata, SEND,
  375. check_ip_tree(DIR_CLIENT, ip_hdr->ip_src.s_addr));
  376. break;
  377. case PORT_MODE:
  378. /*
  379. * process ports based on their destination port
  380. */
  381. dbg(2, "processing port mode...");
  382. cache_result = add_cache(&options.cachedata, SEND,
  383. check_dst_port(ip_hdr, (pkthdr.caplen - l2len)));
  384. break;
  385. case FIRST_MODE:
  386. /*
  387. * First packet mode, looks at each host and picks clients
  388. * by the ones which send the first packet in a session
  389. */
  390. dbg(2, "processing second pass of auto: first packet mode...");
  391. cache_result = add_cache(&options.cachedata, SEND,
  392. check_ip_tree(DIR_UNKNOWN, ip_hdr->ip_src.s_addr));
  393. break;
  394. default:
  395. errx(-1, "Whops! What mode are we in anyways? %d", options.mode);
  396. }
  397. #ifdef ENABLE_VERBOSE
  398. if (options.verbose)
  399. tcpdump_print(&tcpdump, &pkthdr, pktdata);
  400. #endif
  401. }
  402. return packetnum;
  403. }
  404. /**
  405. * init our options
  406. */
  407. void
  408. init(void)
  409. {
  410. int i;
  411. memset(&options, '\0', sizeof(options));
  412. options.bpf.optimize = BPF_OPTIMIZE;
  413. for (i = DEFAULT_LOW_SERVER_PORT; i <= DEFAULT_HIGH_SERVER_PORT; i++) {
  414. options.services.tcp[i] = 1;
  415. options.services.udp[i] = 1;
  416. }
  417. }
  418. /**
  419. * post process args
  420. */
  421. static void
  422. post_args(int argc, char *argv[])
  423. {
  424. char myargs[MYARGS_LEN];
  425. int i, bufsize;
  426. char *tempstr;
  427. memset(myargs, 0, MYARGS_LEN);
  428. /* print_comment and print_info don't return */
  429. if (HAVE_OPT(PRINT_COMMENT))
  430. print_comment(OPT_ARG(PRINT_COMMENT));
  431. if (HAVE_OPT(PRINT_INFO))
  432. print_info(OPT_ARG(PRINT_INFO));
  433. if (HAVE_OPT(PRINT_STATS))
  434. print_stats(OPT_ARG(PRINT_STATS));
  435. if (! HAVE_OPT(CACHEFILE) && ! HAVE_OPT(PCAP))
  436. err(-1, "Must specify an output cachefile (-o) and input pcap (-i)");
  437. if (! options.mode)
  438. err(-1, "Must specify a processing mode: -a, -c, -r, -p");
  439. #ifdef DEBUG
  440. if (HAVE_OPT(DBUG))
  441. debug = OPT_VALUE_DBUG;
  442. #endif
  443. #ifdef ENABLE_VERBOSE
  444. if (HAVE_OPT(VERBOSE)) {
  445. options.verbose = 1;
  446. }
  447. if (HAVE_OPT(DECODE))
  448. tcpdump.args = safe_strdup(OPT_ARG(DECODE));
  449. /*
  450. * put the open after decode options so they are passed to tcpdump
  451. */
  452. #endif
  453. /*
  454. * if we are to include the cli args, then prep it for the
  455. * cache file header
  456. */
  457. if (! options.nocomment) {
  458. /* copy all of our args to myargs */
  459. for (i = 1; i < argc; i ++) {
  460. /* skip the -C <comment> */
  461. if (strcmp(argv[i], "-C") == 0) {
  462. i += 2;
  463. continue;
  464. }
  465. strlcat(myargs, argv[i], MYARGS_LEN);
  466. strlcat(myargs, " ", MYARGS_LEN);
  467. }
  468. /* remove trailing space */
  469. myargs[strlen(myargs) - 1] = 0;
  470. dbgx(1, "Comment args length: %zu", strlen(myargs));
  471. }
  472. /* setup or options.comment buffer so that that we get args\ncomment */
  473. if (options.comment != NULL) {
  474. strlcat(myargs, "\n", MYARGS_LEN);
  475. bufsize = strlen(options.comment) + strlen(myargs) + 1;
  476. options.comment = (char *)safe_realloc(options.comment,
  477. bufsize);
  478. tempstr = strdup(options.comment);
  479. strlcpy(options.comment, myargs, bufsize);
  480. strlcat(options.comment, tempstr, bufsize);
  481. } else {
  482. bufsize = strlen(myargs) + 1;
  483. options.comment = (char *)safe_malloc(bufsize);
  484. strlcpy(options.comment, myargs, bufsize);
  485. }
  486. dbgx(1, "Final comment length: %zu", strlen(options.comment));
  487. /* copy over our min/max mask */
  488. options.min_mask = OPT_VALUE_MINMASK;
  489. options.max_mask = OPT_VALUE_MAXMASK;
  490. if (! options.min_mask > options.max_mask)
  491. errx(-1, "Min network mask len (%d) must be less then max network mask len (%d)",
  492. options.min_mask, options.max_mask);
  493. options.ratio = atof(OPT_ARG(RATIO));
  494. if (options.ratio < 0)
  495. err(-1, "Ratio must be a non-negative number.");
  496. }
  497. /**
  498. * print the tcpprep cache file comment
  499. */
  500. static void
  501. print_comment(const char *file)
  502. {
  503. char *cachedata = NULL;
  504. char *comment = NULL;
  505. COUNTER count = 0;
  506. count = read_cache(&cachedata, file, &comment);
  507. printf("tcpprep args: %s\n", comment);
  508. printf("Cache contains data for " COUNTER_SPEC " packets\n", count);
  509. exit(0);
  510. }
  511. /**
  512. * prints out the cache file details
  513. */
  514. static void
  515. print_info(const char *file)
  516. {
  517. char *cachedata = NULL;
  518. char *comment = NULL;
  519. COUNTER count = 0, i;
  520. count = read_cache(&cachedata, file, &comment);
  521. for (i = 1; i <= count; i ++) {
  522. switch (check_cache(cachedata, i)) {
  523. case TCPR_DIR_C2S:
  524. printf("Packet " COUNTER_SPEC " -> Primary\n", i);
  525. break;
  526. case TCPR_DIR_S2C:
  527. printf("Packet " COUNTER_SPEC " -> Secondary\n", i);
  528. break;
  529. case TCPR_DIR_NOSEND:
  530. printf("Packet " COUNTER_SPEC " -> Don't Send\n", i);
  531. break;
  532. default:
  533. err(-1, "Invalid cachedata value!");
  534. break;
  535. }
  536. }
  537. exit(0);
  538. }
  539. /**
  540. * Print the per-packet statistics
  541. */
  542. static void
  543. print_stats(const char *file)
  544. {
  545. char *cachedata = NULL;
  546. char *comment = NULL;
  547. COUNTER count = 0;
  548. COUNTER pri = 0, sec = 0, nosend = 0;
  549. count = read_cache(&cachedata, file, &comment);
  550. for (COUNTER i = 1; i <= count; i ++) {
  551. int cacheval = check_cache(cachedata, i);
  552. switch (cacheval) {
  553. case TCPR_DIR_C2S:
  554. pri ++;
  555. break;
  556. case TCPR_DIR_S2C:
  557. sec ++;
  558. break;
  559. case TCPR_DIR_NOSEND:
  560. nosend ++;
  561. break;
  562. default:
  563. errx(-1, "Unknown cache value: %d", cacheval);
  564. }
  565. }
  566. printf("Primary packets:\t" COUNTER_SPEC "\n", pri);
  567. printf("Secondary packets:\t" COUNTER_SPEC "\n", sec);
  568. printf("Skipped packets:\t" COUNTER_SPEC "\n", nosend);
  569. printf("------------------------------\n");
  570. printf("Total packets:\t\t" COUNTER_SPEC "\n", count);
  571. exit(0);
  572. }
  573. /*
  574. Local Variables:
  575. mode:c
  576. indent-tabs-mode:nil
  577. c-basic-offset:4
  578. End:
  579. */