blob: 86a9a85cda1051c199ef42cc33d19d2d623387dc [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 Pesaventoda85e252019-03-18 11:42:01 -04003 * Copyright (c) 2011-2019, 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 Pesaventoc0702702017-08-24 22:04:00 -040025#include <sstream>
26
Junxiao Shi3cd47df2015-06-07 20:58:14 -070027namespace ndn {
28namespace dump {
29
Davide Pesavento78de7352018-07-22 00:35:45 -040030namespace po = boost::program_options;
31
32static void
Junxiao Shi2222a612015-06-06 08:01:38 -070033usage(std::ostream& os, const std::string& appName, const po::options_description& options)
34{
Davide Pesaventoecd44802018-07-23 23:48:10 -040035 os << "Usage: " << appName << " [options] [pcap-filter]\n"
Junxiao Shi2222a612015-06-06 08:01:38 -070036 << "\n"
Davide Pesaventoecd44802018-07-23 23:48:10 -040037 << "Default pcap-filter:\n"
Davide Pesavento4a1b21d2018-07-26 20:54:11 -040038 << " '" << NdnDump::getDefaultPcapFilter() << "'\n"
Davide Pesaventoecd44802018-07-23 23:48:10 -040039 << "\n"
40 << options;
Junxiao Shi2222a612015-06-06 08:01:38 -070041}
42
Davide Pesavento78de7352018-07-22 00:35:45 -040043static int
Junxiao Shi2222a612015-06-06 08:01:38 -070044main(int argc, char* argv[])
45{
Davide Pesaventoecd44802018-07-23 23:48:10 -040046 NdnDump instance;
47 std::string nameFilter;
48 std::vector<std::string> pcapFilter;
Junxiao Shi2222a612015-06-06 08:01:38 -070049
Davide Pesaventoecd44802018-07-23 23:48:10 -040050 po::options_description visibleOptions("Options");
Junxiao Shi2222a612015-06-06 08:01:38 -070051 visibleOptions.add_options()
Davide Pesaventoecd44802018-07-23 23:48:10 -040052 ("help,h", "print this help message and exit")
Junxiao Shi2222a612015-06-06 08:01:38 -070053 ("interface,i", po::value<std::string>(&instance.interface),
Davide Pesaventoecd44802018-07-23 23:48:10 -040054 "capture packets from the specified interface; if unspecified, the first "
55 "non-loopback interface will be used; on Linux, the special value \"any\" "
56 "can be used to capture from all interfaces")
57 ("read,r", po::value<std::string>(&instance.inputFile),
58 "read packets from the specified file; use \"-\" to read from standard input")
59 ("filter,f", po::value<std::string>(&nameFilter),
60 "print packet only if name matches this regular expression")
Davide Pesavento24c08612018-07-26 13:33:24 -040061 ("no-promiscuous-mode,p", po::bool_switch(), "do not put the interface into promiscuous mode")
Davide Pesaventob5b8f952018-07-26 14:19:16 -040062 ("no-timestamp,t", po::bool_switch(), "do not print a timestamp for each packet")
Davide Pesavento24c08612018-07-26 13:33:24 -040063 ("verbose,v", po::bool_switch(&instance.wantVerbose),
Davide Pesaventoecd44802018-07-23 23:48:10 -040064 "print more detailed information about each packet")
65 ("version,V", "print program version and exit")
Junxiao Shi2222a612015-06-06 08:01:38 -070066 ;
67
68 po::options_description hiddenOptions;
69 hiddenOptions.add_options()
Davide Pesaventoecd44802018-07-23 23:48:10 -040070 ("pcap-filter", po::value<std::vector<std::string>>(&pcapFilter))
71 ;
Junxiao Shi2222a612015-06-06 08:01:38 -070072
Davide Pesaventoecd44802018-07-23 23:48:10 -040073 po::positional_options_description posOptions;
74 posOptions.add("pcap-filter", -1);
Junxiao Shi2222a612015-06-06 08:01:38 -070075
76 po::options_description allOptions;
Davide Pesaventoecd44802018-07-23 23:48:10 -040077 allOptions.add(visibleOptions).add(hiddenOptions);
Junxiao Shi2222a612015-06-06 08:01:38 -070078
79 po::variables_map vm;
Junxiao Shi2222a612015-06-06 08:01:38 -070080 try {
Davide Pesaventoecd44802018-07-23 23:48:10 -040081 po::store(po::command_line_parser(argc, argv).options(allOptions).positional(posOptions).run(), vm);
Junxiao Shi2222a612015-06-06 08:01:38 -070082 po::notify(vm);
83 }
Junxiao Shic1c2b832016-07-24 20:45:36 +000084 catch (const po::error& e) {
85 std::cerr << "ERROR: " << e.what() << "\n\n";
Junxiao Shi2222a612015-06-06 08:01:38 -070086 usage(std::cerr, argv[0], visibleOptions);
Davide Pesaventoecd44802018-07-23 23:48:10 -040087 return 2;
88 }
89 catch (const boost::bad_any_cast& e) {
90 std::cerr << "ERROR: " << e.what() << "\n\n";
91 usage(std::cerr, argv[0], visibleOptions);
92 return 2;
Junxiao Shi2222a612015-06-06 08:01:38 -070093 }
94
95 if (vm.count("help") > 0) {
96 usage(std::cout, argv[0], visibleOptions);
97 return 0;
98 }
99
Junxiao Shi3cd47df2015-06-07 20:58:14 -0700100 if (vm.count("version") > 0) {
Davide Pesaventoc3d65c92018-07-26 13:01:21 -0400101 std::cout << "ndndump " << tools::VERSION << "\n"
102 << pcap_lib_version() << std::endl;
Junxiao Shi3cd47df2015-06-07 20:58:14 -0700103 return 0;
104 }
105
Davide Pesaventoecd44802018-07-23 23:48:10 -0400106 if (vm.count("interface") > 0 && vm.count("read") > 0) {
107 std::cerr << "ERROR: conflicting '--interface' and '--read' options specified\n\n";
108 usage(std::cerr, argv[0], visibleOptions);
109 return 2;
Junxiao Shi2222a612015-06-06 08:01:38 -0700110 }
111
Davide Pesavento78de7352018-07-22 00:35:45 -0400112 if (vm.count("filter") > 0) {
113 try {
Davide Pesaventoecd44802018-07-23 23:48:10 -0400114 instance.nameFilter = std::regex(nameFilter);
Davide Pesavento78de7352018-07-22 00:35:45 -0400115 }
116 catch (const std::regex_error& e) {
Davide Pesaventoecd44802018-07-23 23:48:10 -0400117 std::cerr << "ERROR: invalid filter regex: " << e.what() << std::endl;
Davide Pesavento78de7352018-07-22 00:35:45 -0400118 return 2;
119 }
120 }
121
Davide Pesaventoecd44802018-07-23 23:48:10 -0400122 if (vm.count("pcap-filter") > 0) {
Junxiao Shi2222a612015-06-06 08:01:38 -0700123 std::ostringstream os;
Davide Pesaventoecd44802018-07-23 23:48:10 -0400124 std::copy(pcapFilter.begin(), pcapFilter.end(), make_ostream_joiner(os, " "));
125 instance.pcapFilter = os.str();
Junxiao Shi2222a612015-06-06 08:01:38 -0700126 }
127
Davide Pesavento24c08612018-07-26 13:33:24 -0400128 instance.wantPromisc = !vm["no-promiscuous-mode"].as<bool>();
Davide Pesaventob5b8f952018-07-26 14:19:16 -0400129 instance.wantTimestamp = !vm["no-timestamp"].as<bool>();
Davide Pesavento24c08612018-07-26 13:33:24 -0400130
Junxiao Shic7599632016-07-24 20:46:24 +0000131 try {
132 instance.run();
133 }
134 catch (const std::exception& e) {
Davide Pesaventoecd44802018-07-23 23:48:10 -0400135 std::cerr << "ERROR: " << e.what() << std::endl;
136 return 1;
Junxiao Shic7599632016-07-24 20:46:24 +0000137 }
Junxiao Shi2222a612015-06-06 08:01:38 -0700138 return 0;
139}
Junxiao Shi3cd47df2015-06-07 20:58:14 -0700140
141} // namespace dump
142} // namespace ndn
143
144int
Davide Pesaventoda85e252019-03-18 11:42:01 -0400145main(int argc, char* argv[])
Junxiao Shi3cd47df2015-06-07 20:58:14 -0700146{
147 return ndn::dump::main(argc, argv);
148}