blob: 033ffc45049ede59b909bae4310b1f483a22523b [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/*
Davide Pesaventob3570c62022-02-19 19:19:00 -05003 * Copyright (c) 2011-2022, 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 */
Junxiao Shi2222a612015-06-06 08:01:38 -070019
20#include "ndndump.hpp"
Junxiao Shi3cd47df2015-06-07 20:58:14 -070021#include "core/version.hpp"
Junxiao Shi2222a612015-06-06 08:01:38 -070022
Davide Pesavento11c69912019-09-01 01:30:13 -040023#include <ndn-cxx/util/ostream-joiner.hpp>
24
Davide Pesaventoa0e6b602021-01-21 19:47:04 -050025#include <boost/program_options/options_description.hpp>
26#include <boost/program_options/parsers.hpp>
27#include <boost/program_options/variables_map.hpp>
28
Davide Pesaventoc0702702017-08-24 22:04:00 -040029#include <sstream>
30
Davide Pesaventob3570c62022-02-19 19:19:00 -050031namespace ndn::dump {
Junxiao Shi3cd47df2015-06-07 20:58:14 -070032
Davide Pesavento78de7352018-07-22 00:35:45 -040033namespace po = boost::program_options;
34
35static void
Davide Pesaventob3570c62022-02-19 19:19:00 -050036usage(std::ostream& os, std::string_view progName, const po::options_description& options)
Junxiao Shi2222a612015-06-06 08:01:38 -070037{
Davide Pesaventob3570c62022-02-19 19:19:00 -050038 os << "Usage: " << progName << " [options] [pcap-filter]\n"
Junxiao Shi2222a612015-06-06 08:01:38 -070039 << "\n"
Davide Pesaventoecd44802018-07-23 23:48:10 -040040 << "Default pcap-filter:\n"
Davide Pesavento4a1b21d2018-07-26 20:54:11 -040041 << " '" << NdnDump::getDefaultPcapFilter() << "'\n"
Davide Pesaventoecd44802018-07-23 23:48:10 -040042 << "\n"
43 << options;
Junxiao Shi2222a612015-06-06 08:01:38 -070044}
45
Davide Pesavento78de7352018-07-22 00:35:45 -040046static int
Junxiao Shi2222a612015-06-06 08:01:38 -070047main(int argc, char* argv[])
48{
Davide Pesaventoecd44802018-07-23 23:48:10 -040049 NdnDump instance;
50 std::string nameFilter;
51 std::vector<std::string> pcapFilter;
Junxiao Shi2222a612015-06-06 08:01:38 -070052
Davide Pesaventoecd44802018-07-23 23:48:10 -040053 po::options_description visibleOptions("Options");
Junxiao Shi2222a612015-06-06 08:01:38 -070054 visibleOptions.add_options()
Davide Pesaventoecd44802018-07-23 23:48:10 -040055 ("help,h", "print this help message and exit")
Junxiao Shi2222a612015-06-06 08:01:38 -070056 ("interface,i", po::value<std::string>(&instance.interface),
Davide Pesaventoecd44802018-07-23 23:48:10 -040057 "capture packets from the specified interface; if unspecified, the first "
58 "non-loopback interface will be used; on Linux, the special value \"any\" "
59 "can be used to capture from all interfaces")
60 ("read,r", po::value<std::string>(&instance.inputFile),
61 "read packets from the specified file; use \"-\" to read from standard input")
62 ("filter,f", po::value<std::string>(&nameFilter),
63 "print packet only if name matches this regular expression")
Davide Pesavento24c08612018-07-26 13:33:24 -040064 ("no-promiscuous-mode,p", po::bool_switch(), "do not put the interface into promiscuous mode")
Davide Pesaventob5b8f952018-07-26 14:19:16 -040065 ("no-timestamp,t", po::bool_switch(), "do not print a timestamp for each packet")
Davide Pesavento24c08612018-07-26 13:33:24 -040066 ("verbose,v", po::bool_switch(&instance.wantVerbose),
Davide Pesaventoecd44802018-07-23 23:48:10 -040067 "print more detailed information about each packet")
68 ("version,V", "print program version and exit")
Junxiao Shi2222a612015-06-06 08:01:38 -070069 ;
70
71 po::options_description hiddenOptions;
72 hiddenOptions.add_options()
Davide Pesaventoecd44802018-07-23 23:48:10 -040073 ("pcap-filter", po::value<std::vector<std::string>>(&pcapFilter))
74 ;
Junxiao Shi2222a612015-06-06 08:01:38 -070075
Davide Pesaventoecd44802018-07-23 23:48:10 -040076 po::positional_options_description posOptions;
77 posOptions.add("pcap-filter", -1);
Junxiao Shi2222a612015-06-06 08:01:38 -070078
79 po::options_description allOptions;
Davide Pesaventoecd44802018-07-23 23:48:10 -040080 allOptions.add(visibleOptions).add(hiddenOptions);
Junxiao Shi2222a612015-06-06 08:01:38 -070081
82 po::variables_map vm;
Junxiao Shi2222a612015-06-06 08:01:38 -070083 try {
Davide Pesaventoecd44802018-07-23 23:48:10 -040084 po::store(po::command_line_parser(argc, argv).options(allOptions).positional(posOptions).run(), vm);
Junxiao Shi2222a612015-06-06 08:01:38 -070085 po::notify(vm);
86 }
Junxiao Shic1c2b832016-07-24 20:45:36 +000087 catch (const po::error& e) {
88 std::cerr << "ERROR: " << e.what() << "\n\n";
Junxiao Shi2222a612015-06-06 08:01:38 -070089 usage(std::cerr, argv[0], visibleOptions);
Davide Pesaventoecd44802018-07-23 23:48:10 -040090 return 2;
91 }
92 catch (const boost::bad_any_cast& e) {
93 std::cerr << "ERROR: " << e.what() << "\n\n";
94 usage(std::cerr, argv[0], visibleOptions);
95 return 2;
Junxiao Shi2222a612015-06-06 08:01:38 -070096 }
97
98 if (vm.count("help") > 0) {
99 usage(std::cout, argv[0], visibleOptions);
100 return 0;
101 }
102
Junxiao Shi3cd47df2015-06-07 20:58:14 -0700103 if (vm.count("version") > 0) {
Davide Pesaventoc3d65c92018-07-26 13:01:21 -0400104 std::cout << "ndndump " << tools::VERSION << "\n"
105 << pcap_lib_version() << std::endl;
Junxiao Shi3cd47df2015-06-07 20:58:14 -0700106 return 0;
107 }
108
Davide Pesaventoecd44802018-07-23 23:48:10 -0400109 if (vm.count("interface") > 0 && vm.count("read") > 0) {
110 std::cerr << "ERROR: conflicting '--interface' and '--read' options specified\n\n";
111 usage(std::cerr, argv[0], visibleOptions);
112 return 2;
Junxiao Shi2222a612015-06-06 08:01:38 -0700113 }
114
Davide Pesavento78de7352018-07-22 00:35:45 -0400115 if (vm.count("filter") > 0) {
116 try {
Davide Pesaventoecd44802018-07-23 23:48:10 -0400117 instance.nameFilter = std::regex(nameFilter);
Davide Pesavento78de7352018-07-22 00:35:45 -0400118 }
119 catch (const std::regex_error& e) {
Davide Pesaventoecd44802018-07-23 23:48:10 -0400120 std::cerr << "ERROR: invalid filter regex: " << e.what() << std::endl;
Davide Pesavento78de7352018-07-22 00:35:45 -0400121 return 2;
122 }
123 }
124
Davide Pesaventoecd44802018-07-23 23:48:10 -0400125 if (vm.count("pcap-filter") > 0) {
Junxiao Shi2222a612015-06-06 08:01:38 -0700126 std::ostringstream os;
Davide Pesaventoecd44802018-07-23 23:48:10 -0400127 std::copy(pcapFilter.begin(), pcapFilter.end(), make_ostream_joiner(os, " "));
128 instance.pcapFilter = os.str();
Junxiao Shi2222a612015-06-06 08:01:38 -0700129 }
130
Davide Pesavento24c08612018-07-26 13:33:24 -0400131 instance.wantPromisc = !vm["no-promiscuous-mode"].as<bool>();
Davide Pesaventob5b8f952018-07-26 14:19:16 -0400132 instance.wantTimestamp = !vm["no-timestamp"].as<bool>();
Davide Pesavento24c08612018-07-26 13:33:24 -0400133
Junxiao Shic7599632016-07-24 20:46:24 +0000134 try {
135 instance.run();
136 }
137 catch (const std::exception& e) {
Davide Pesaventoecd44802018-07-23 23:48:10 -0400138 std::cerr << "ERROR: " << e.what() << std::endl;
139 return 1;
Junxiao Shic7599632016-07-24 20:46:24 +0000140 }
Junxiao Shi2222a612015-06-06 08:01:38 -0700141 return 0;
142}
Junxiao Shi3cd47df2015-06-07 20:58:14 -0700143
Davide Pesaventob3570c62022-02-19 19:19:00 -0500144} // namespace ndn::dump
Junxiao Shi3cd47df2015-06-07 20:58:14 -0700145
146int
Davide Pesaventoda85e252019-03-18 11:42:01 -0400147main(int argc, char* argv[])
Junxiao Shi3cd47df2015-06-07 20:58:14 -0700148{
149 return ndn::dump::main(argc, argv);
150}