tcpprep.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664
  1. /* $Id$ */
  2. /*
  3. * Copyright (c) 2001-2010 Aaron Turner <aturner at synfin dot net>
  4. * Copyright (c) 2013-2018 Fred Klassen <tcpreplay at appneta dot com> - AppNeta
  5. *
  6. * The Tcpreplay Suite of tools is free software: you can redistribute it
  7. * and/or modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation, either version 3 of the
  9. * License, or with the authors permission any later version.
  10. *
  11. * The Tcpreplay Suite is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with the Tcpreplay Suite. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. /*
  20. * Purpose:
  21. * 1) Remove the performance bottleneck in tcpreplay for choosing an NIC
  22. * 2) Separate code to make it more manageable
  23. * 3) Add additional features which require multiple passes of a pcap
  24. *
  25. * Support:
  26. * Right now we support matching source IP based upon on of the following:
  27. * - Regular expression
  28. * - IP address is contained in one of a list of CIDR blocks
  29. * - Auto learning of CIDR block for servers (clients all other)
  30. */
  31. #include "config.h"
  32. #include "defines.h"
  33. #include "common.h"
  34. #include <stdio.h>
  35. #include <stdlib.h>
  36. #include <string.h>
  37. #include <regex.h>
  38. #include <string.h>
  39. #include <unistd.h>
  40. #include <errno.h>
  41. #include "tcpprep.h"
  42. #include "tcpprep_api.h"
  43. #include "tcpprep_opts.h"
  44. #include "lib/tree.h"
  45. #include "tree.h"
  46. #include "lib/sll.h"
  47. #ifndef HAVE_STRLCPY
  48. #include "lib/strlcpy.h"
  49. #endif
  50. /*
  51. * global variables
  52. */
  53. #ifdef DEBUG
  54. int debug = 0;
  55. #endif
  56. tcpprep_t *tcpprep;
  57. int info = 0;
  58. char *ourregex = NULL;
  59. char *cidr = NULL;
  60. tcpr_data_tree_t treeroot;
  61. void print_comment(const char *);
  62. void print_info(const char *);
  63. void print_stats(const char *);
  64. static int check_ipv4_regex(const unsigned long ip);
  65. static int check_ipv6_regex(const struct tcpr_in6_addr *addr);
  66. static COUNTER process_raw_packets(pcap_t * pcap);
  67. static int check_dst_port(ipv4_hdr_t *ip_hdr, ipv6_hdr_t *ip6_hdr, int len);
  68. /*
  69. * main()
  70. */
  71. int
  72. main(int argc, char *argv[])
  73. {
  74. int out_file;
  75. COUNTER totpackets = 0;
  76. char errbuf[PCAP_ERRBUF_SIZE];
  77. tcpprep_opt_t *options;
  78. tcpprep = tcpprep_init();
  79. options = tcpprep->options;
  80. optionProcess(&tcpprepOptions, argc, argv);
  81. tcpprep_post_args(tcpprep, argc, argv);
  82. /* open the cache file */
  83. if ((out_file = open(OPT_ARG(CACHEFILE), O_WRONLY | O_CREAT | O_TRUNC,
  84. S_IREAD | S_IWRITE | S_IRGRP | S_IWGRP | S_IROTH)) == -1)
  85. errx(-1, "Unable to open cache file %s for writing: %s",
  86. OPT_ARG(CACHEFILE), strerror(errno));
  87. readpcap:
  88. /* open the pcap file */
  89. if ((options->pcap = pcap_open_offline(OPT_ARG(PCAP), errbuf)) == NULL)
  90. errx(-1, "Error opening file: %s", errbuf);
  91. #ifdef HAVE_PCAP_SNAPSHOT
  92. if (pcap_snapshot(options->pcap) < 65535)
  93. warnx("%s was captured using a snaplen of %d bytes. This may mean you have truncated packets.",
  94. OPT_ARG(PCAP), pcap_snapshot(options->pcap));
  95. #endif
  96. /* make sure we support the DLT type */
  97. switch(pcap_datalink(options->pcap)) {
  98. case DLT_EN10MB:
  99. case DLT_LINUX_SLL:
  100. case DLT_RAW:
  101. case DLT_C_HDLC:
  102. case DLT_JUNIPER_ETHER:
  103. case DLT_PPP_SERIAL:
  104. break; /* do nothing because all is good */
  105. default:
  106. errx(-1, "Unsupported pcap DLT type: 0x%x", pcap_datalink(options->pcap));
  107. }
  108. /* Can only split based on MAC address for ethernet */
  109. if ((pcap_datalink(options->pcap) != DLT_EN10MB) &&
  110. (options->mode == MAC_MODE)) {
  111. err(-1, "MAC mode splitting is only supported by DLT_EN10MB packet captures.");
  112. }
  113. #ifdef ENABLE_VERBOSE
  114. if (HAVE_OPT(VERBOSE)) {
  115. tcpdump_open(&tcpprep->tcpdump, options->pcap);
  116. }
  117. #endif
  118. /* do we apply a bpf filter? */
  119. if (options->bpf.filter != NULL) {
  120. if (pcap_compile(options->pcap, &options->bpf.program, options->bpf.filter,
  121. options->bpf.optimize, 0) != 0) {
  122. errx(-1, "Error compiling BPF filter: %s", pcap_geterr(options->pcap));
  123. }
  124. pcap_setfilter(options->pcap, &options->bpf.program);
  125. pcap_freecode(&options->bpf.program);
  126. }
  127. if ((totpackets = process_raw_packets(options->pcap)) == 0) {
  128. pcap_close(options->pcap);
  129. err(-1, "No packets were processed. Filter too limiting?");
  130. }
  131. pcap_close(options->pcap);
  132. #ifdef ENABLE_VERBOSE
  133. tcpdump_close(&tcpprep->tcpdump);
  134. #endif
  135. /* we need to process the pcap file twice in HASH/AUTO mode */
  136. if (options->mode == AUTO_MODE) {
  137. options->mode = options->automode;
  138. if (options->mode == ROUTER_MODE) { /* do we need to convert TREE->CIDR? */
  139. if (info)
  140. notice("Building network list from pre-cache...\n");
  141. if (!process_tree()) {
  142. err(-1, "Error: unable to build a valid list of servers. Aborting.");
  143. }
  144. }
  145. else {
  146. /*
  147. * in bridge mode we need to calculate client/sever
  148. * manually since this is done automatically in
  149. * process_tree()
  150. */
  151. tree_calculate(&treeroot);
  152. }
  153. if (info)
  154. notice("Building cache file...\n");
  155. /*
  156. * re-process files, but this time generate
  157. * cache
  158. */
  159. goto readpcap;
  160. }
  161. #ifdef DEBUG
  162. if (debug && (options->cidrdata != NULL))
  163. print_cidr(options->cidrdata);
  164. #endif
  165. /* write cache data */
  166. totpackets = write_cache(options->cachedata, out_file, totpackets,
  167. options->comment);
  168. if (info)
  169. notice("Done.\nCached " COUNTER_SPEC " packets.\n", totpackets);
  170. /* close cache file */
  171. close(out_file);
  172. restore_stdin();
  173. return 0;
  174. }
  175. /**
  176. * checks the dst port to see if this is destined for a server port.
  177. * returns 1 for true, 0 for false
  178. */
  179. static int
  180. check_dst_port(ipv4_hdr_t *ip_hdr, ipv6_hdr_t *ip6_hdr, int len)
  181. {
  182. tcp_hdr_t *tcp_hdr = NULL;
  183. udp_hdr_t *udp_hdr = NULL;
  184. tcpprep_opt_t *options = tcpprep->options;
  185. uint8_t proto;
  186. u_char *l4;
  187. if (ip_hdr) {
  188. if (len < ((ip_hdr->ip_hl * 4) + 4))
  189. return 0; /* not enough data in the packet to know */
  190. proto = ip_hdr->ip_p;
  191. l4 = get_layer4_v4(ip_hdr, len);
  192. } else if (ip6_hdr) {
  193. if (len < (TCPR_IPV6_H + 4))
  194. return 0; /* not enough data in the packet to know */
  195. proto = get_ipv6_l4proto(ip6_hdr, len);
  196. dbgx(3, "Our layer4 proto is 0x%hhu", proto);
  197. if ((l4 = get_layer4_v6(ip6_hdr, len)) == NULL)
  198. return 0;
  199. dbgx(3, "Found proto %u at offset %p. base %p (%p)", proto, (void *)l4, (void *)ip6_hdr, (void*)(l4 - (u_char *)ip6_hdr));
  200. } else {
  201. assert(0);
  202. }
  203. dbg(3, "Checking the destination port...");
  204. switch(proto) {
  205. case IPPROTO_TCP:
  206. tcp_hdr = (tcp_hdr_t *)l4;
  207. /* is a service? */
  208. if (options->services.tcp[ntohs(tcp_hdr->th_dport)]) {
  209. dbgx(1, "TCP packet is destined for a server port: %d", ntohs(tcp_hdr->th_dport));
  210. return 1;
  211. }
  212. /* nope */
  213. dbgx(1, "TCP packet is NOT destined for a server port: %d", ntohs(tcp_hdr->th_dport));
  214. return 0;
  215. break;
  216. case IPPROTO_UDP:
  217. udp_hdr = (udp_hdr_t *)l4;
  218. /* is a service? */
  219. if (options->services.udp[ntohs(udp_hdr->uh_dport)]) {
  220. dbgx(1, "UDP packet is destined for a server port: %d", ntohs(udp_hdr->uh_dport));
  221. return 1;
  222. }
  223. /* nope */
  224. dbgx(1, "UDP packet is NOT destined for a server port: %d", ntohs(udp_hdr->uh_dport));
  225. return 0;
  226. break;
  227. default:
  228. /* not a TCP or UDP packet... return as non_ip */
  229. dbg(1, "Packet isn't a UDP or TCP packet... no port to process.");
  230. return options->nonip;
  231. }
  232. }
  233. /**
  234. * checks to see if an ip address matches a regex. Returns 1 for true
  235. * 0 for false
  236. */
  237. static int
  238. check_ipv4_regex(const unsigned long ip)
  239. {
  240. int eflags = 0;
  241. u_char src_ip[16];
  242. size_t nmatch = 0;
  243. tcpprep_opt_t *options = tcpprep->options;
  244. memset(src_ip, '\0', sizeof(src_ip));
  245. strlcpy((char *)src_ip, (char *)get_addr2name4(ip, RESOLVE),
  246. sizeof(src_ip));
  247. if (regexec(&options->preg, (char *)src_ip, nmatch, NULL, eflags) == 0) {
  248. return 1;
  249. } else {
  250. return 0;
  251. }
  252. }
  253. static int
  254. check_ipv6_regex(const struct tcpr_in6_addr *addr)
  255. {
  256. int eflags = 0;
  257. u_char src_ip[INET6_ADDRSTRLEN];
  258. size_t nmatch = 0;
  259. tcpprep_opt_t *options = tcpprep->options;
  260. memset(src_ip, '\0', sizeof(src_ip));
  261. strlcpy((char *)src_ip, (char *)get_addr2name6(addr, RESOLVE),
  262. sizeof(src_ip));
  263. if (regexec(&options->preg, (char *)src_ip, nmatch, NULL, eflags) == 0) {
  264. return 1;
  265. } else {
  266. return 0;
  267. }
  268. }
  269. /**
  270. * uses libpcap library to parse the packets and build
  271. * the cache file.
  272. */
  273. static COUNTER
  274. process_raw_packets(pcap_t * pcap)
  275. {
  276. ipv4_hdr_t *ip_hdr = NULL;
  277. ipv6_hdr_t *ip6_hdr = NULL;
  278. eth_hdr_t *eth_hdr = NULL;
  279. struct pcap_pkthdr pkthdr;
  280. const u_char *pktdata = NULL;
  281. COUNTER packetnum = 0;
  282. int l2len;
  283. u_char *ipbuff, *buffptr;
  284. tcpr_dir_t direction = TCPR_DIR_ERROR;
  285. tcpprep_opt_t *options = tcpprep->options;
  286. assert(pcap);
  287. ipbuff = safe_malloc(MAXPACKET);
  288. while ((pktdata = safe_pcap_next(pcap, &pkthdr)) != NULL) {
  289. packetnum++;
  290. dbgx(1, "Packet " COUNTER_SPEC, packetnum);
  291. /* look for include or exclude LIST match */
  292. if (options->xX.list != NULL) {
  293. if (options->xX.mode < xXExclude) {
  294. /* include list */
  295. if (!check_list(options->xX.list, packetnum)) {
  296. add_cache(&(options->cachedata), DONT_SEND, 0);
  297. continue;
  298. }
  299. }
  300. /* exclude list */
  301. else if (check_list(options->xX.list, packetnum)) {
  302. add_cache(&(options->cachedata), DONT_SEND, 0);
  303. continue;
  304. }
  305. }
  306. /*
  307. * If the packet doesn't include an IPv4 header we should just treat
  308. * it as a non-IP packet, UNLESS we're in MAC mode, in which case
  309. * we should let the MAC matcher below handle it
  310. */
  311. if (options->mode != MAC_MODE) {
  312. dbg(3, "Looking for IPv4/v6 header in non-MAC mode");
  313. /* get the IP header (if any) */
  314. buffptr = ipbuff;
  315. /* first look for IPv4 */
  316. if ((ip_hdr = (ipv4_hdr_t *)get_ipv4(pktdata, pkthdr.caplen,
  317. pcap_datalink(pcap), &buffptr))) {
  318. dbg(2, "Packet is IPv4");
  319. } else if ((ip6_hdr = (ipv6_hdr_t *)get_ipv6(pktdata, pkthdr.caplen,
  320. pcap_datalink(pcap), &buffptr))) {
  321. /* IPv6 */
  322. dbg(2, "Packet is IPv6");
  323. } else {
  324. /* we're something else... */
  325. dbg(2, "Packet isn't IPv4/v6");
  326. /* we don't want to cache these packets twice */
  327. if (options->mode != AUTO_MODE) {
  328. dbg(3, "Adding to cache using options for Non-IP packets");
  329. add_cache(&options->cachedata, SEND, options->nonip);
  330. }
  331. /* go to next packet */
  332. continue;
  333. }
  334. l2len = get_l2len(pktdata, pkthdr.caplen, pcap_datalink(pcap));
  335. if (l2len < 0) {
  336. /* go to next packet */
  337. continue;
  338. }
  339. /* look for include or exclude CIDR match */
  340. if (options->xX.cidr != NULL) {
  341. if (ip_hdr) {
  342. if (!process_xX_by_cidr_ipv4(options->xX.mode, options->xX.cidr, ip_hdr)) {
  343. add_cache(&options->cachedata, DONT_SEND, 0);
  344. continue;
  345. }
  346. } else if (ip6_hdr) {
  347. if (!process_xX_by_cidr_ipv6(options->xX.mode, options->xX.cidr, ip6_hdr)) {
  348. add_cache(&options->cachedata, DONT_SEND, 0);
  349. continue;
  350. }
  351. }
  352. }
  353. }
  354. switch (options->mode) {
  355. case REGEX_MODE:
  356. dbg(2, "processing regex mode...");
  357. if (ip_hdr) {
  358. direction = check_ipv4_regex(ip_hdr->ip_src.s_addr);
  359. } else if (ip6_hdr) {
  360. direction = check_ipv6_regex(&ip6_hdr->ip_src);
  361. }
  362. /* reverse direction? */
  363. if (HAVE_OPT(REVERSE) && (direction == TCPR_DIR_C2S || direction == TCPR_DIR_S2C))
  364. direction = direction == TCPR_DIR_C2S ? TCPR_DIR_S2C : TCPR_DIR_C2S;
  365. add_cache(&options->cachedata, SEND, direction);
  366. break;
  367. case CIDR_MODE:
  368. dbg(2, "processing cidr mode...");
  369. if (ip_hdr) {
  370. direction = check_ip_cidr(options->cidrdata, ip_hdr->ip_src.s_addr) ? TCPR_DIR_C2S : TCPR_DIR_S2C;
  371. } else if (ip6_hdr) {
  372. direction = check_ip6_cidr(options->cidrdata, &ip6_hdr->ip_src) ? TCPR_DIR_C2S : TCPR_DIR_S2C;
  373. }
  374. /* reverse direction? */
  375. if (HAVE_OPT(REVERSE) && (direction == TCPR_DIR_C2S || direction == TCPR_DIR_S2C))
  376. direction = direction == TCPR_DIR_C2S ? TCPR_DIR_S2C : TCPR_DIR_C2S;
  377. add_cache(&options->cachedata, SEND, direction);
  378. break;
  379. case MAC_MODE:
  380. dbg(2, "processing mac mode...");
  381. if (pkthdr.caplen < sizeof(*eth_hdr)) {
  382. dbg(2, "capture length too short for mac mode processing");
  383. break;
  384. }
  385. eth_hdr = (eth_hdr_t *)pktdata;
  386. direction = macinstring(options->maclist, (u_char *)eth_hdr->ether_shost);
  387. /* reverse direction? */
  388. if (HAVE_OPT(REVERSE) && (direction == TCPR_DIR_C2S || direction == TCPR_DIR_S2C))
  389. direction = direction == TCPR_DIR_C2S ? TCPR_DIR_S2C : TCPR_DIR_C2S;
  390. add_cache(&options->cachedata, SEND, direction);
  391. break;
  392. case AUTO_MODE:
  393. dbg(2, "processing first pass of auto mode...");
  394. /* first run through in auto mode: create tree */
  395. if (options->automode != FIRST_MODE) {
  396. if (ip_hdr) {
  397. add_tree_ipv4(ip_hdr->ip_src.s_addr, pktdata, pkthdr.caplen);
  398. } else if (ip6_hdr) {
  399. add_tree_ipv6(&ip6_hdr->ip_src, pktdata, pkthdr.caplen);
  400. }
  401. } else {
  402. if (ip_hdr) {
  403. add_tree_first_ipv4(pktdata, pkthdr.caplen);
  404. } else if (ip6_hdr) {
  405. add_tree_first_ipv6(pktdata, pkthdr.caplen);
  406. }
  407. }
  408. break;
  409. case ROUTER_MODE:
  410. /*
  411. * second run through in auto mode: create route
  412. * based cache
  413. */
  414. dbg(2, "processing second pass of auto: router mode...");
  415. if (ip_hdr) {
  416. add_cache(&options->cachedata, SEND,
  417. check_ip_tree(options->nonip, ip_hdr->ip_src.s_addr));
  418. } else {
  419. add_cache(&options->cachedata, SEND,
  420. check_ip6_tree(options->nonip, &ip6_hdr->ip_src));
  421. }
  422. break;
  423. case BRIDGE_MODE:
  424. /*
  425. * second run through in auto mode: create bridge
  426. * based cache
  427. */
  428. dbg(2, "processing second pass of auto: bridge mode...");
  429. if (ip_hdr) {
  430. add_cache(&options->cachedata, SEND,
  431. check_ip_tree(DIR_UNKNOWN, ip_hdr->ip_src.s_addr));
  432. } else {
  433. add_cache(&options->cachedata, SEND,
  434. check_ip6_tree(DIR_UNKNOWN, &ip6_hdr->ip_src));
  435. }
  436. break;
  437. case SERVER_MODE:
  438. /*
  439. * second run through in auto mode: create bridge
  440. * where unknowns are servers
  441. */
  442. dbg(2, "processing second pass of auto: server mode...");
  443. if (ip_hdr) {
  444. add_cache(&options->cachedata, SEND,
  445. check_ip_tree(DIR_SERVER, ip_hdr->ip_src.s_addr));
  446. } else {
  447. add_cache(&options->cachedata, SEND,
  448. check_ip6_tree(DIR_SERVER, &ip6_hdr->ip_src));
  449. }
  450. break;
  451. case CLIENT_MODE:
  452. /*
  453. * second run through in auto mode: create bridge
  454. * where unknowns are clients
  455. */
  456. dbg(2, "processing second pass of auto: client mode...");
  457. if (ip_hdr) {
  458. add_cache(&options->cachedata, SEND,
  459. check_ip_tree(DIR_CLIENT, ip_hdr->ip_src.s_addr));
  460. } else {
  461. add_cache(&options->cachedata, SEND,
  462. check_ip6_tree(DIR_CLIENT, &ip6_hdr->ip_src));
  463. }
  464. break;
  465. case PORT_MODE:
  466. /*
  467. * process ports based on their destination port
  468. */
  469. dbg(2, "processing port mode...");
  470. add_cache(&options->cachedata, SEND,
  471. check_dst_port(ip_hdr, ip6_hdr, (pkthdr.caplen - l2len)));
  472. break;
  473. case FIRST_MODE:
  474. /*
  475. * First packet mode, looks at each host and picks clients
  476. * by the ones which send the first packet in a session
  477. */
  478. dbg(2, "processing second pass of auto: first packet mode...");
  479. if (ip_hdr) {
  480. add_cache(&options->cachedata, SEND,
  481. check_ip_tree(DIR_UNKNOWN, ip_hdr->ip_src.s_addr));
  482. } else {
  483. add_cache(&options->cachedata, SEND,
  484. check_ip6_tree(DIR_UNKNOWN, &ip6_hdr->ip_src));
  485. }
  486. break;
  487. default:
  488. errx(-1, "Whoops! What mode are we in anyways? %d", options->mode);
  489. }
  490. #ifdef ENABLE_VERBOSE
  491. if (options->verbose)
  492. tcpdump_print(&tcpprep->tcpdump, &pkthdr, pktdata);
  493. #endif
  494. }
  495. safe_free(ipbuff);
  496. return packetnum;
  497. }
  498. /**
  499. * print the tcpprep cache file comment
  500. */
  501. void
  502. print_comment(const char *file)
  503. {
  504. char *cachedata = NULL;
  505. char *comment = NULL;
  506. COUNTER count = 0;
  507. count = read_cache(&cachedata, file, &comment);
  508. printf("tcpprep args: %s\n", comment);
  509. printf("Cache contains data for " COUNTER_SPEC " packets\n", count);
  510. exit(0);
  511. }
  512. /**
  513. * prints out the cache file details
  514. */
  515. void
  516. print_info(const char *file)
  517. {
  518. char *cachedata = NULL;
  519. char *comment = NULL;
  520. COUNTER count = 0, i;
  521. count = read_cache(&cachedata, file, &comment);
  522. if (count > 65535)
  523. exit(-1);
  524. for (i = 1; i <= count; i ++) {
  525. switch (check_cache(cachedata, i)) {
  526. case TCPR_DIR_C2S:
  527. printf("Packet " COUNTER_SPEC " -> Primary\n", i);
  528. break;
  529. case TCPR_DIR_S2C:
  530. printf("Packet " COUNTER_SPEC " -> Secondary\n", i);
  531. break;
  532. case TCPR_DIR_NOSEND:
  533. printf("Packet " COUNTER_SPEC " -> Don't Send\n", i);
  534. break;
  535. default:
  536. err(-1, "Invalid cachedata value!");
  537. break;
  538. }
  539. }
  540. exit(0);
  541. }
  542. /**
  543. * Print the per-packet statistics
  544. */
  545. void
  546. print_stats(const char *file)
  547. {
  548. char *cachedata = NULL;
  549. char *comment = NULL;
  550. COUNTER i, count = 0;
  551. COUNTER pri = 0, sec = 0, nosend = 0;
  552. count = read_cache(&cachedata, file, &comment);
  553. for (i = 1; i <= count; i ++) {
  554. int cacheval = check_cache(cachedata, i);
  555. switch (cacheval) {
  556. case TCPR_DIR_C2S:
  557. pri ++;
  558. break;
  559. case TCPR_DIR_S2C:
  560. sec ++;
  561. break;
  562. case TCPR_DIR_NOSEND:
  563. nosend ++;
  564. break;
  565. default:
  566. errx(-1, "Unknown cache value: %d", cacheval);
  567. }
  568. }
  569. printf("Primary packets:\t" COUNTER_SPEC "\n", pri);
  570. printf("Secondary packets:\t" COUNTER_SPEC "\n", sec);
  571. printf("Skipped packets:\t" COUNTER_SPEC "\n", nosend);
  572. printf("------------------------------\n");
  573. printf("Total packets:\t\t" COUNTER_SPEC "\n", count);
  574. exit(0);
  575. }