blob: 06e9a0e45fd229c646757e5bada4811591b7f920 [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"
Davide Pesaventoecd44802018-07-23 23:48:10 -040038#include "core/common.hpp"
Junxiao Shi3cd47df2015-06-07 20:58:14 -070039#include "core/version.hpp"
Junxiao Shi2222a612015-06-06 08:01:38 -070040
Davide Pesaventoc0702702017-08-24 22:04:00 -040041#include <sstream>
42
Junxiao Shi3cd47df2015-06-07 20:58:14 -070043namespace ndn {
44namespace dump {
45
Davide Pesavento78de7352018-07-22 00:35:45 -040046namespace po = boost::program_options;
47
48static void
Junxiao Shi2222a612015-06-06 08:01:38 -070049usage(std::ostream& os, const std::string& appName, const po::options_description& options)
50{
Davide Pesaventoecd44802018-07-23 23:48:10 -040051 os << "Usage: " << appName << " [options] [pcap-filter]\n"
Junxiao Shi2222a612015-06-06 08:01:38 -070052 << "\n"
Davide Pesaventoecd44802018-07-23 23:48:10 -040053 << "Default pcap-filter:\n"
54 << " '" << NdnDump::getDefaultPcapFilter() << "'\n"
55 << "\n"
56 << options;
Junxiao Shi2222a612015-06-06 08:01:38 -070057}
58
Davide Pesavento78de7352018-07-22 00:35:45 -040059static int
Junxiao Shi2222a612015-06-06 08:01:38 -070060main(int argc, char* argv[])
61{
Davide Pesaventoecd44802018-07-23 23:48:10 -040062 NdnDump instance;
63 std::string nameFilter;
64 std::vector<std::string> pcapFilter;
Junxiao Shi2222a612015-06-06 08:01:38 -070065
Davide Pesaventoecd44802018-07-23 23:48:10 -040066 po::options_description visibleOptions("Options");
Junxiao Shi2222a612015-06-06 08:01:38 -070067 visibleOptions.add_options()
Davide Pesaventoecd44802018-07-23 23:48:10 -040068 ("help,h", "print this help message and exit")
Junxiao Shi2222a612015-06-06 08:01:38 -070069 ("interface,i", po::value<std::string>(&instance.interface),
Davide Pesaventoecd44802018-07-23 23:48:10 -040070 "capture packets from the specified interface; if unspecified, the first "
71 "non-loopback interface will be used; on Linux, the special value \"any\" "
72 "can be used to capture from all interfaces")
73 ("read,r", po::value<std::string>(&instance.inputFile),
74 "read packets from the specified file; use \"-\" to read from standard input")
75 ("filter,f", po::value<std::string>(&nameFilter),
76 "print packet only if name matches this regular expression")
77 ("verbose,v", po::bool_switch(&instance.isVerbose),
78 "print more detailed information about each packet")
79 ("version,V", "print program version and exit")
Junxiao Shi2222a612015-06-06 08:01:38 -070080 ;
81
82 po::options_description hiddenOptions;
83 hiddenOptions.add_options()
Davide Pesaventoecd44802018-07-23 23:48:10 -040084 ("pcap-filter", po::value<std::vector<std::string>>(&pcapFilter))
85 ;
Junxiao Shi2222a612015-06-06 08:01:38 -070086
Davide Pesaventoecd44802018-07-23 23:48:10 -040087 po::positional_options_description posOptions;
88 posOptions.add("pcap-filter", -1);
Junxiao Shi2222a612015-06-06 08:01:38 -070089
90 po::options_description allOptions;
Davide Pesaventoecd44802018-07-23 23:48:10 -040091 allOptions.add(visibleOptions).add(hiddenOptions);
Junxiao Shi2222a612015-06-06 08:01:38 -070092
93 po::variables_map vm;
Junxiao Shi2222a612015-06-06 08:01:38 -070094 try {
Davide Pesaventoecd44802018-07-23 23:48:10 -040095 po::store(po::command_line_parser(argc, argv).options(allOptions).positional(posOptions).run(), vm);
Junxiao Shi2222a612015-06-06 08:01:38 -070096 po::notify(vm);
97 }
Junxiao Shic1c2b832016-07-24 20:45:36 +000098 catch (const po::error& e) {
99 std::cerr << "ERROR: " << e.what() << "\n\n";
Junxiao Shi2222a612015-06-06 08:01:38 -0700100 usage(std::cerr, argv[0], visibleOptions);
Davide Pesaventoecd44802018-07-23 23:48:10 -0400101 return 2;
102 }
103 catch (const boost::bad_any_cast& e) {
104 std::cerr << "ERROR: " << e.what() << "\n\n";
105 usage(std::cerr, argv[0], visibleOptions);
106 return 2;
Junxiao Shi2222a612015-06-06 08:01:38 -0700107 }
108
109 if (vm.count("help") > 0) {
110 usage(std::cout, argv[0], visibleOptions);
111 return 0;
112 }
113
Junxiao Shi3cd47df2015-06-07 20:58:14 -0700114 if (vm.count("version") > 0) {
Davide Pesaventoecd44802018-07-23 23:48:10 -0400115 std::cout << "ndndump " << tools::VERSION << std::endl;
Junxiao Shi3cd47df2015-06-07 20:58:14 -0700116 return 0;
117 }
118
Davide Pesaventoecd44802018-07-23 23:48:10 -0400119 if (vm.count("interface") > 0 && vm.count("read") > 0) {
120 std::cerr << "ERROR: conflicting '--interface' and '--read' options specified\n\n";
121 usage(std::cerr, argv[0], visibleOptions);
122 return 2;
Junxiao Shi2222a612015-06-06 08:01:38 -0700123 }
124
Davide Pesavento78de7352018-07-22 00:35:45 -0400125 if (vm.count("filter") > 0) {
126 try {
Davide Pesaventoecd44802018-07-23 23:48:10 -0400127 instance.nameFilter = std::regex(nameFilter);
Davide Pesavento78de7352018-07-22 00:35:45 -0400128 }
129 catch (const std::regex_error& e) {
Davide Pesaventoecd44802018-07-23 23:48:10 -0400130 std::cerr << "ERROR: invalid filter regex: " << e.what() << std::endl;
Davide Pesavento78de7352018-07-22 00:35:45 -0400131 return 2;
132 }
133 }
134
Davide Pesaventoecd44802018-07-23 23:48:10 -0400135 if (vm.count("pcap-filter") > 0) {
Junxiao Shi2222a612015-06-06 08:01:38 -0700136 std::ostringstream os;
Davide Pesaventoecd44802018-07-23 23:48:10 -0400137 std::copy(pcapFilter.begin(), pcapFilter.end(), make_ostream_joiner(os, " "));
138 instance.pcapFilter = os.str();
Junxiao Shi2222a612015-06-06 08:01:38 -0700139 }
140
Junxiao Shic7599632016-07-24 20:46:24 +0000141 try {
142 instance.run();
143 }
144 catch (const std::exception& e) {
Davide Pesaventoecd44802018-07-23 23:48:10 -0400145 std::cerr << "ERROR: " << e.what() << std::endl;
146 return 1;
Junxiao Shic7599632016-07-24 20:46:24 +0000147 }
Junxiao Shi2222a612015-06-06 08:01:38 -0700148 return 0;
149}
Junxiao Shi3cd47df2015-06-07 20:58:14 -0700150
151} // namespace dump
152} // namespace ndn
153
154int
155main(int argc, char** argv)
156{
157 return ndn::dump::main(argc, argv);
158}