blob: 7e23ef986bc9959d47b1d4c0bf3c08a4173e4ee5 [file] [log] [blame]
Junxiao Shi2222a612015-06-06 08:01:38 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Davide Pesaventoc0702702017-08-24 22:04:00 -04003 * Copyright (c) 2014-2017, 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
Davide Pesaventoc0702702017-08-24 22:04:00 -040044#include <sstream>
45
Junxiao Shi2222a612015-06-06 08:01:38 -070046namespace po = boost::program_options;
47
48namespace boost {
49
50void
51validate(boost::any& v,
52 const std::vector<std::string>& values,
53 boost::regex*, int)
54{
55 po::validators::check_first_occurrence(v);
56 const std::string& str = po::validators::get_single_string(values);
57 v = boost::any(boost::regex(str));
58}
59
60} // namespace boost
61
Junxiao Shi3cd47df2015-06-07 20:58:14 -070062namespace ndn {
63namespace dump {
64
Junxiao Shi2222a612015-06-06 08:01:38 -070065void
66usage(std::ostream& os, const std::string& appName, const po::options_description& options)
67{
68 os << "Usage:\n"
69 << " " << appName << " [-i interface] [-f name-filter] [tcpdump-expression] \n"
70 << "\n"
71 << "Default tcpdump-expression:\n"
72 << " '(ether proto 0x8624) || (tcp port 6363) || (udp port 6363)'\n"
73 << "\n";
74 os << options;
75}
76
77int
78main(int argc, char* argv[])
79{
Junxiao Shi3cd47df2015-06-07 20:58:14 -070080 Ndndump instance;
Junxiao Shi2222a612015-06-06 08:01:38 -070081
82 po::options_description visibleOptions;
83 visibleOptions.add_options()
84 ("help,h", "Produce this help message")
Junxiao Shi3cd47df2015-06-07 20:58:14 -070085 ("version,V", "display version and exit")
Junxiao Shi2222a612015-06-06 08:01:38 -070086 ("interface,i", po::value<std::string>(&instance.interface),
87 "Interface from which to dump packets")
88 ("read,r", po::value<std::string>(&instance.inputFile),
89 "Read packets from file")
90 ("verbose,v",
91 "When parsing and printing, produce verbose output")
92 // ("write,w", po::value<std::string>(&instance.outputFile),
93 // "Write the raw packets to file rather than parsing and printing them out")
94 ("filter,f", po::value<boost::regex>(&instance.nameFilter),
95 "Regular expression to filter out Interest and Data packets")
96 ;
97
98 po::options_description hiddenOptions;
99 hiddenOptions.add_options()
Junxiao Shic1c2b832016-07-24 20:45:36 +0000100 ("pcap-program", po::value<std::vector<std::string>>());
Junxiao Shi2222a612015-06-06 08:01:38 -0700101
102 po::positional_options_description positionalArguments;
Junxiao Shic1c2b832016-07-24 20:45:36 +0000103 positionalArguments.add("pcap-program", -1);
Junxiao Shi2222a612015-06-06 08:01:38 -0700104
105 po::options_description allOptions;
Junxiao Shic1c2b832016-07-24 20:45:36 +0000106 allOptions.add(visibleOptions)
107 .add(hiddenOptions);
Junxiao Shi2222a612015-06-06 08:01:38 -0700108
109 po::variables_map vm;
110
111 try {
112 po::store(po::command_line_parser(argc, argv)
113 .options(allOptions)
114 .positional(positionalArguments)
115 .run(),
116 vm);
117 po::notify(vm);
118 }
Junxiao Shic1c2b832016-07-24 20:45:36 +0000119 catch (const po::error& e) {
120 std::cerr << "ERROR: " << e.what() << "\n\n";
Junxiao Shi2222a612015-06-06 08:01:38 -0700121 usage(std::cerr, argv[0], visibleOptions);
122 return 1;
123 }
124
125 if (vm.count("help") > 0) {
126 usage(std::cout, argv[0], visibleOptions);
127 return 0;
128 }
129
Junxiao Shi3cd47df2015-06-07 20:58:14 -0700130 if (vm.count("version") > 0) {
Junxiao Shic1c2b832016-07-24 20:45:36 +0000131 std::cout << "ndndump " << tools::VERSION << '\n';
Junxiao Shi3cd47df2015-06-07 20:58:14 -0700132 return 0;
133 }
134
Junxiao Shi2222a612015-06-06 08:01:38 -0700135 if (vm.count("verbose") > 0) {
136 instance.isVerbose = true;
137 }
138
139 if (vm.count("pcap-program") > 0) {
Junxiao Shic1c2b832016-07-24 20:45:36 +0000140 const auto& items = vm["pcap-program"].as<std::vector<std::string>>();
Junxiao Shi2222a612015-06-06 08:01:38 -0700141
142 std::ostringstream os;
143 std::copy(items.begin(), items.end(), std::ostream_iterator<std::string>(os, " "));
144 instance.pcapProgram = os.str();
145 }
146
147 if (vm.count("read") > 0 && vm.count("interface") > 0) {
Junxiao Shic1c2b832016-07-24 20:45:36 +0000148 std::cerr << "ERROR: Conflicting -r and -i options\n";
Junxiao Shi2222a612015-06-06 08:01:38 -0700149 usage(std::cerr, argv[0], visibleOptions);
150 return 2;
151 }
152
Junxiao Shic7599632016-07-24 20:46:24 +0000153 try {
154 instance.run();
155 }
156 catch (const std::exception& e) {
157 std::cerr << "ERROR: " << e.what() << "\n\n";
158 }
Junxiao Shi2222a612015-06-06 08:01:38 -0700159
160 return 0;
161}
Junxiao Shi3cd47df2015-06-07 20:58:14 -0700162
163} // namespace dump
164} // namespace ndn
165
166int
167main(int argc, char** argv)
168{
169 return ndn::dump::main(argc, argv);
170}