blob: 2ace7f6c4d79d4901e2e31585b5d20098fc66119 [file] [log] [blame]
Junxiao Shi2222a612015-06-06 08:01:38 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shic1c2b832016-07-24 20:45:36 +00003 * Copyright (c) 2014-2016, Regents of the University of California.
Junxiao Shi3cd47df2015-06-07 20:58:14 -07004 *
5 * This file is part of ndn-tools (Named Data Networking Essential Tools).
6 * See AUTHORS.md for complete list of ndn-tools authors and contributors.
7 *
8 * ndn-tools is free software: you can redistribute it and/or modify it under the terms
9 * of the GNU General Public License as published by the Free Software Foundation,
10 * either version 3 of the License, or (at your option) any later version.
11 *
12 * ndn-tools is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
13 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along with
17 * ndn-tools, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
18 */
19/**
Junxiao Shi2222a612015-06-06 08:01:38 -070020 * Copyright (c) 2011-2014, Regents of the University of California,
21 *
22 * This file is part of ndndump, the packet capture and analysis tool for Named Data
23 * Networking (NDN).
24 *
25 * ndndump is free software: you can redistribute it and/or modify it under the terms
26 * of the GNU General Public License as published by the Free Software Foundation,
27 * either version 3 of the License, or (at your option) any later version.
28 *
29 * ndndump is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
30 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
31 * PURPOSE. See the GNU General Public License for more details.
32 *
33 * You should have received a copy of the GNU General Public License along with
34 * ndndump, e.g., in COPYING file. If not, see <http://www.gnu.org/licenses/>.
35 **/
36
37#include "ndndump.hpp"
Junxiao Shi3cd47df2015-06-07 20:58:14 -070038#include "core/version.hpp"
Junxiao Shi2222a612015-06-06 08:01:38 -070039
40#include <boost/program_options/options_description.hpp>
41#include <boost/program_options/variables_map.hpp>
42#include <boost/program_options/parsers.hpp>
43
44namespace po = boost::program_options;
45
46namespace boost {
47
48void
49validate(boost::any& v,
50 const std::vector<std::string>& values,
51 boost::regex*, int)
52{
53 po::validators::check_first_occurrence(v);
54 const std::string& str = po::validators::get_single_string(values);
55 v = boost::any(boost::regex(str));
56}
57
58} // namespace boost
59
Junxiao Shi3cd47df2015-06-07 20:58:14 -070060namespace ndn {
61namespace dump {
62
Junxiao Shi2222a612015-06-06 08:01:38 -070063void
64usage(std::ostream& os, const std::string& appName, const po::options_description& options)
65{
66 os << "Usage:\n"
67 << " " << appName << " [-i interface] [-f name-filter] [tcpdump-expression] \n"
68 << "\n"
69 << "Default tcpdump-expression:\n"
70 << " '(ether proto 0x8624) || (tcp port 6363) || (udp port 6363)'\n"
71 << "\n";
72 os << options;
73}
74
75int
76main(int argc, char* argv[])
77{
Junxiao Shi3cd47df2015-06-07 20:58:14 -070078 Ndndump instance;
Junxiao Shi2222a612015-06-06 08:01:38 -070079
80 po::options_description visibleOptions;
81 visibleOptions.add_options()
82 ("help,h", "Produce this help message")
Junxiao Shi3cd47df2015-06-07 20:58:14 -070083 ("version,V", "display version and exit")
Junxiao Shi2222a612015-06-06 08:01:38 -070084 ("interface,i", po::value<std::string>(&instance.interface),
85 "Interface from which to dump packets")
86 ("read,r", po::value<std::string>(&instance.inputFile),
87 "Read packets from file")
88 ("verbose,v",
89 "When parsing and printing, produce verbose output")
90 // ("write,w", po::value<std::string>(&instance.outputFile),
91 // "Write the raw packets to file rather than parsing and printing them out")
92 ("filter,f", po::value<boost::regex>(&instance.nameFilter),
93 "Regular expression to filter out Interest and Data packets")
94 ;
95
96 po::options_description hiddenOptions;
97 hiddenOptions.add_options()
Junxiao Shic1c2b832016-07-24 20:45:36 +000098 ("pcap-program", po::value<std::vector<std::string>>());
Junxiao Shi2222a612015-06-06 08:01:38 -070099
100 po::positional_options_description positionalArguments;
Junxiao Shic1c2b832016-07-24 20:45:36 +0000101 positionalArguments.add("pcap-program", -1);
Junxiao Shi2222a612015-06-06 08:01:38 -0700102
103 po::options_description allOptions;
Junxiao Shic1c2b832016-07-24 20:45:36 +0000104 allOptions.add(visibleOptions)
105 .add(hiddenOptions);
Junxiao Shi2222a612015-06-06 08:01:38 -0700106
107 po::variables_map vm;
108
109 try {
110 po::store(po::command_line_parser(argc, argv)
111 .options(allOptions)
112 .positional(positionalArguments)
113 .run(),
114 vm);
115 po::notify(vm);
116 }
Junxiao Shic1c2b832016-07-24 20:45:36 +0000117 catch (const po::error& e) {
118 std::cerr << "ERROR: " << e.what() << "\n\n";
Junxiao Shi2222a612015-06-06 08:01:38 -0700119 usage(std::cerr, argv[0], visibleOptions);
120 return 1;
121 }
122
123 if (vm.count("help") > 0) {
124 usage(std::cout, argv[0], visibleOptions);
125 return 0;
126 }
127
Junxiao Shi3cd47df2015-06-07 20:58:14 -0700128 if (vm.count("version") > 0) {
Junxiao Shic1c2b832016-07-24 20:45:36 +0000129 std::cout << "ndndump " << tools::VERSION << '\n';
Junxiao Shi3cd47df2015-06-07 20:58:14 -0700130 return 0;
131 }
132
Junxiao Shi2222a612015-06-06 08:01:38 -0700133 if (vm.count("verbose") > 0) {
134 instance.isVerbose = true;
135 }
136
137 if (vm.count("pcap-program") > 0) {
Junxiao Shic1c2b832016-07-24 20:45:36 +0000138 const auto& items = vm["pcap-program"].as<std::vector<std::string>>();
Junxiao Shi2222a612015-06-06 08:01:38 -0700139
140 std::ostringstream os;
141 std::copy(items.begin(), items.end(), std::ostream_iterator<std::string>(os, " "));
142 instance.pcapProgram = os.str();
143 }
144
145 if (vm.count("read") > 0 && vm.count("interface") > 0) {
Junxiao Shic1c2b832016-07-24 20:45:36 +0000146 std::cerr << "ERROR: Conflicting -r and -i options\n";
Junxiao Shi2222a612015-06-06 08:01:38 -0700147 usage(std::cerr, argv[0], visibleOptions);
148 return 2;
149 }
150
Junxiao Shic7599632016-07-24 20:46:24 +0000151 try {
152 instance.run();
153 }
154 catch (const std::exception& e) {
155 std::cerr << "ERROR: " << e.what() << "\n\n";
156 }
Junxiao Shi2222a612015-06-06 08:01:38 -0700157
158 return 0;
159}
Junxiao Shi3cd47df2015-06-07 20:58:14 -0700160
161} // namespace dump
162} // namespace ndn
163
164int
165main(int argc, char** argv)
166{
167 return ndn::dump::main(argc, argv);
168}