blob: 23d1821a63e061aed5cf87b0a342aa1efefd00ef [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 Pesavento5748e822024-01-26 18:40:22 -05003 * Copyright (c) 2011-2024, 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 Pesavento5748e822024-01-26 18:40:22 -050029#include <iostream>
Davide Pesaventoc0702702017-08-24 22:04:00 -040030#include <sstream>
31
Davide Pesaventob3570c62022-02-19 19:19:00 -050032namespace ndn::dump {
Junxiao Shi3cd47df2015-06-07 20:58:14 -070033
Davide Pesavento78de7352018-07-22 00:35:45 -040034namespace po = boost::program_options;
35
36static void
Davide Pesaventob3570c62022-02-19 19:19:00 -050037usage(std::ostream& os, std::string_view progName, const po::options_description& options)
Junxiao Shi2222a612015-06-06 08:01:38 -070038{
Davide Pesaventob3570c62022-02-19 19:19:00 -050039 os << "Usage: " << progName << " [options] [pcap-filter]\n"
Junxiao Shi2222a612015-06-06 08:01:38 -070040 << "\n"
Davide Pesaventoecd44802018-07-23 23:48:10 -040041 << "Default pcap-filter:\n"
Davide Pesavento4a1b21d2018-07-26 20:54:11 -040042 << " '" << NdnDump::getDefaultPcapFilter() << "'\n"
Davide Pesaventoecd44802018-07-23 23:48:10 -040043 << "\n"
44 << options;
Junxiao Shi2222a612015-06-06 08:01:38 -070045}
46
Davide Pesavento78de7352018-07-22 00:35:45 -040047static int
Junxiao Shi2222a612015-06-06 08:01:38 -070048main(int argc, char* argv[])
49{
Davide Pesaventoecd44802018-07-23 23:48:10 -040050 NdnDump instance;
51 std::string nameFilter;
52 std::vector<std::string> pcapFilter;
Junxiao Shi2222a612015-06-06 08:01:38 -070053
Davide Pesaventoecd44802018-07-23 23:48:10 -040054 po::options_description visibleOptions("Options");
Junxiao Shi2222a612015-06-06 08:01:38 -070055 visibleOptions.add_options()
Davide Pesaventoecd44802018-07-23 23:48:10 -040056 ("help,h", "print this help message and exit")
Junxiao Shi2222a612015-06-06 08:01:38 -070057 ("interface,i", po::value<std::string>(&instance.interface),
Davide Pesaventoecd44802018-07-23 23:48:10 -040058 "capture packets from the specified interface; if unspecified, the first "
59 "non-loopback interface will be used; on Linux, the special value \"any\" "
60 "can be used to capture from all interfaces")
61 ("read,r", po::value<std::string>(&instance.inputFile),
62 "read packets from the specified file; use \"-\" to read from standard input")
63 ("filter,f", po::value<std::string>(&nameFilter),
64 "print packet only if name matches this regular expression")
Davide Pesavento24c08612018-07-26 13:33:24 -040065 ("no-promiscuous-mode,p", po::bool_switch(), "do not put the interface into promiscuous mode")
Davide Pesaventob5b8f952018-07-26 14:19:16 -040066 ("no-timestamp,t", po::bool_switch(), "do not print a timestamp for each packet")
Davide Pesavento24c08612018-07-26 13:33:24 -040067 ("verbose,v", po::bool_switch(&instance.wantVerbose),
Davide Pesaventoecd44802018-07-23 23:48:10 -040068 "print more detailed information about each packet")
69 ("version,V", "print program version and exit")
Junxiao Shi2222a612015-06-06 08:01:38 -070070 ;
71
72 po::options_description hiddenOptions;
73 hiddenOptions.add_options()
Davide Pesaventoecd44802018-07-23 23:48:10 -040074 ("pcap-filter", po::value<std::vector<std::string>>(&pcapFilter))
75 ;
Junxiao Shi2222a612015-06-06 08:01:38 -070076
Davide Pesaventoecd44802018-07-23 23:48:10 -040077 po::positional_options_description posOptions;
78 posOptions.add("pcap-filter", -1);
Junxiao Shi2222a612015-06-06 08:01:38 -070079
80 po::options_description allOptions;
Davide Pesaventoecd44802018-07-23 23:48:10 -040081 allOptions.add(visibleOptions).add(hiddenOptions);
Junxiao Shi2222a612015-06-06 08:01:38 -070082
83 po::variables_map vm;
Junxiao Shi2222a612015-06-06 08:01:38 -070084 try {
Davide Pesaventoecd44802018-07-23 23:48:10 -040085 po::store(po::command_line_parser(argc, argv).options(allOptions).positional(posOptions).run(), vm);
Junxiao Shi2222a612015-06-06 08:01:38 -070086 po::notify(vm);
87 }
Junxiao Shic1c2b832016-07-24 20:45:36 +000088 catch (const po::error& e) {
89 std::cerr << "ERROR: " << e.what() << "\n\n";
Junxiao Shi2222a612015-06-06 08:01:38 -070090 usage(std::cerr, argv[0], visibleOptions);
Davide Pesaventoecd44802018-07-23 23:48:10 -040091 return 2;
92 }
93 catch (const boost::bad_any_cast& e) {
94 std::cerr << "ERROR: " << e.what() << "\n\n";
95 usage(std::cerr, argv[0], visibleOptions);
96 return 2;
Junxiao Shi2222a612015-06-06 08:01:38 -070097 }
98
99 if (vm.count("help") > 0) {
100 usage(std::cout, argv[0], visibleOptions);
101 return 0;
102 }
103
Junxiao Shi3cd47df2015-06-07 20:58:14 -0700104 if (vm.count("version") > 0) {
Davide Pesaventoc3d65c92018-07-26 13:01:21 -0400105 std::cout << "ndndump " << tools::VERSION << "\n"
106 << pcap_lib_version() << std::endl;
Junxiao Shi3cd47df2015-06-07 20:58:14 -0700107 return 0;
108 }
109
Davide Pesaventoecd44802018-07-23 23:48:10 -0400110 if (vm.count("interface") > 0 && vm.count("read") > 0) {
111 std::cerr << "ERROR: conflicting '--interface' and '--read' options specified\n\n";
112 usage(std::cerr, argv[0], visibleOptions);
113 return 2;
Junxiao Shi2222a612015-06-06 08:01:38 -0700114 }
115
Davide Pesavento78de7352018-07-22 00:35:45 -0400116 if (vm.count("filter") > 0) {
117 try {
Davide Pesaventoecd44802018-07-23 23:48:10 -0400118 instance.nameFilter = std::regex(nameFilter);
Davide Pesavento78de7352018-07-22 00:35:45 -0400119 }
120 catch (const std::regex_error& e) {
Davide Pesaventoecd44802018-07-23 23:48:10 -0400121 std::cerr << "ERROR: invalid filter regex: " << e.what() << std::endl;
Davide Pesavento78de7352018-07-22 00:35:45 -0400122 return 2;
123 }
124 }
125
Davide Pesaventoecd44802018-07-23 23:48:10 -0400126 if (vm.count("pcap-filter") > 0) {
Junxiao Shi2222a612015-06-06 08:01:38 -0700127 std::ostringstream os;
Davide Pesaventoecd44802018-07-23 23:48:10 -0400128 std::copy(pcapFilter.begin(), pcapFilter.end(), make_ostream_joiner(os, " "));
129 instance.pcapFilter = os.str();
Junxiao Shi2222a612015-06-06 08:01:38 -0700130 }
131
Davide Pesavento24c08612018-07-26 13:33:24 -0400132 instance.wantPromisc = !vm["no-promiscuous-mode"].as<bool>();
Davide Pesaventob5b8f952018-07-26 14:19:16 -0400133 instance.wantTimestamp = !vm["no-timestamp"].as<bool>();
Davide Pesavento24c08612018-07-26 13:33:24 -0400134
Junxiao Shic7599632016-07-24 20:46:24 +0000135 try {
136 instance.run();
137 }
138 catch (const std::exception& e) {
Davide Pesaventoecd44802018-07-23 23:48:10 -0400139 std::cerr << "ERROR: " << e.what() << std::endl;
140 return 1;
Junxiao Shic7599632016-07-24 20:46:24 +0000141 }
Junxiao Shi2222a612015-06-06 08:01:38 -0700142 return 0;
143}
Junxiao Shi3cd47df2015-06-07 20:58:14 -0700144
Davide Pesaventob3570c62022-02-19 19:19:00 -0500145} // namespace ndn::dump
Junxiao Shi3cd47df2015-06-07 20:58:14 -0700146
147int
Davide Pesaventoda85e252019-03-18 11:42:01 -0400148main(int argc, char* argv[])
Junxiao Shi3cd47df2015-06-07 20:58:14 -0700149{
150 return ndn::dump::main(argc, argv);
151}