blob: 351d76f7e71bc18f5ec8d55beef97eb19a85f448 [file] [log] [blame]
Junxiao Shi2222a612015-06-06 08:01:38 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento78de7352018-07-22 00:35:45 -04002/*
3 * Copyright (c) 2014-2018, 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 */
Davide Pesavento78de7352018-07-22 00:35:45 -040019/*
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 Shi3cd47df2015-06-07 20:58:14 -070046namespace ndn {
47namespace dump {
48
Davide Pesavento78de7352018-07-22 00:35:45 -040049namespace po = boost::program_options;
50
51static void
Junxiao Shi2222a612015-06-06 08:01:38 -070052usage(std::ostream& os, const std::string& appName, const po::options_description& options)
53{
54 os << "Usage:\n"
55 << " " << appName << " [-i interface] [-f name-filter] [tcpdump-expression] \n"
56 << "\n"
57 << "Default tcpdump-expression:\n"
58 << " '(ether proto 0x8624) || (tcp port 6363) || (udp port 6363)'\n"
59 << "\n";
60 os << options;
61}
62
Davide Pesavento78de7352018-07-22 00:35:45 -040063static int
Junxiao Shi2222a612015-06-06 08:01:38 -070064main(int argc, char* argv[])
65{
Junxiao Shi3cd47df2015-06-07 20:58:14 -070066 Ndndump instance;
Davide Pesavento78de7352018-07-22 00:35:45 -040067 std::string filter;
Junxiao Shi2222a612015-06-06 08:01:38 -070068
69 po::options_description visibleOptions;
70 visibleOptions.add_options()
71 ("help,h", "Produce this help message")
Davide Pesavento78de7352018-07-22 00:35:45 -040072 ("version,V", "Display version and exit")
Junxiao Shi2222a612015-06-06 08:01:38 -070073 ("interface,i", po::value<std::string>(&instance.interface),
Davide Pesavento78de7352018-07-22 00:35:45 -040074 "Interface from which to dump packets")
Junxiao Shi2222a612015-06-06 08:01:38 -070075 ("read,r", po::value<std::string>(&instance.inputFile),
Davide Pesavento78de7352018-07-22 00:35:45 -040076 "Read packets from file")
77 ("verbose,v", "When parsing and printing, produce verbose output")
Junxiao Shi2222a612015-06-06 08:01:38 -070078 // ("write,w", po::value<std::string>(&instance.outputFile),
79 // "Write the raw packets to file rather than parsing and printing them out")
Davide Pesavento78de7352018-07-22 00:35:45 -040080 ("filter,f", po::value<std::string>(&filter),
81 "Regular expression to filter out Interest and Data packets")
Junxiao Shi2222a612015-06-06 08:01:38 -070082 ;
83
84 po::options_description hiddenOptions;
85 hiddenOptions.add_options()
Junxiao Shic1c2b832016-07-24 20:45:36 +000086 ("pcap-program", po::value<std::vector<std::string>>());
Junxiao Shi2222a612015-06-06 08:01:38 -070087
88 po::positional_options_description positionalArguments;
Junxiao Shic1c2b832016-07-24 20:45:36 +000089 positionalArguments.add("pcap-program", -1);
Junxiao Shi2222a612015-06-06 08:01:38 -070090
91 po::options_description allOptions;
Junxiao Shic1c2b832016-07-24 20:45:36 +000092 allOptions.add(visibleOptions)
93 .add(hiddenOptions);
Junxiao Shi2222a612015-06-06 08:01:38 -070094
95 po::variables_map vm;
96
97 try {
98 po::store(po::command_line_parser(argc, argv)
99 .options(allOptions)
100 .positional(positionalArguments)
101 .run(),
102 vm);
103 po::notify(vm);
104 }
Junxiao Shic1c2b832016-07-24 20:45:36 +0000105 catch (const po::error& e) {
106 std::cerr << "ERROR: " << e.what() << "\n\n";
Junxiao Shi2222a612015-06-06 08:01:38 -0700107 usage(std::cerr, argv[0], visibleOptions);
108 return 1;
109 }
110
111 if (vm.count("help") > 0) {
112 usage(std::cout, argv[0], visibleOptions);
113 return 0;
114 }
115
Junxiao Shi3cd47df2015-06-07 20:58:14 -0700116 if (vm.count("version") > 0) {
Junxiao Shic1c2b832016-07-24 20:45:36 +0000117 std::cout << "ndndump " << tools::VERSION << '\n';
Junxiao Shi3cd47df2015-06-07 20:58:14 -0700118 return 0;
119 }
120
Junxiao Shi2222a612015-06-06 08:01:38 -0700121 if (vm.count("verbose") > 0) {
122 instance.isVerbose = true;
123 }
124
Davide Pesavento78de7352018-07-22 00:35:45 -0400125 if (vm.count("filter") > 0) {
126 try {
127 instance.nameFilter = std::regex(filter);
128 }
129 catch (const std::regex_error& e) {
130 std::cerr << "ERROR: Invalid filter expression: " << e.what() << std::endl;
131 return 2;
132 }
133 }
134
Junxiao Shi2222a612015-06-06 08:01:38 -0700135 if (vm.count("pcap-program") > 0) {
Junxiao Shic1c2b832016-07-24 20:45:36 +0000136 const auto& items = vm["pcap-program"].as<std::vector<std::string>>();
Junxiao Shi2222a612015-06-06 08:01:38 -0700137
138 std::ostringstream os;
139 std::copy(items.begin(), items.end(), std::ostream_iterator<std::string>(os, " "));
140 instance.pcapProgram = os.str();
141 }
142
143 if (vm.count("read") > 0 && vm.count("interface") > 0) {
Davide Pesavento78de7352018-07-22 00:35:45 -0400144 std::cerr << "ERROR: Conflicting -r and -i options\n\n";
Junxiao Shi2222a612015-06-06 08:01:38 -0700145 usage(std::cerr, argv[0], visibleOptions);
146 return 2;
147 }
148
Junxiao Shic7599632016-07-24 20:46:24 +0000149 try {
150 instance.run();
151 }
152 catch (const std::exception& e) {
Davide Pesavento78de7352018-07-22 00:35:45 -0400153 std::cerr << "ERROR: " << e.what() << "\n";
Junxiao Shic7599632016-07-24 20:46:24 +0000154 }
Junxiao Shi2222a612015-06-06 08:01:38 -0700155
156 return 0;
157}
Junxiao Shi3cd47df2015-06-07 20:58:14 -0700158
159} // namespace dump
160} // namespace ndn
161
162int
163main(int argc, char** argv)
164{
165 return ndn::dump::main(argc, argv);
166}