Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 78de735 | 2018-07-22 00:35:45 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame^] | 3 | * Copyright (c) 2011-2022, Regents of the University of California. |
Junxiao Shi | 3cd47df | 2015-06-07 20:58:14 -0700 | [diff] [blame] | 4 | * |
| 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 Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 19 | |
| 20 | #include "ndndump.hpp" |
Junxiao Shi | 3cd47df | 2015-06-07 20:58:14 -0700 | [diff] [blame] | 21 | #include "core/version.hpp" |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 22 | |
Davide Pesavento | 11c6991 | 2019-09-01 01:30:13 -0400 | [diff] [blame] | 23 | #include <ndn-cxx/util/ostream-joiner.hpp> |
| 24 | |
Davide Pesavento | a0e6b60 | 2021-01-21 19:47:04 -0500 | [diff] [blame] | 25 | #include <boost/program_options/options_description.hpp> |
| 26 | #include <boost/program_options/parsers.hpp> |
| 27 | #include <boost/program_options/variables_map.hpp> |
| 28 | |
Davide Pesavento | c070270 | 2017-08-24 22:04:00 -0400 | [diff] [blame] | 29 | #include <sstream> |
| 30 | |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame^] | 31 | namespace ndn::dump { |
Junxiao Shi | 3cd47df | 2015-06-07 20:58:14 -0700 | [diff] [blame] | 32 | |
Davide Pesavento | 78de735 | 2018-07-22 00:35:45 -0400 | [diff] [blame] | 33 | namespace po = boost::program_options; |
| 34 | |
| 35 | static void |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame^] | 36 | usage(std::ostream& os, std::string_view progName, const po::options_description& options) |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 37 | { |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame^] | 38 | os << "Usage: " << progName << " [options] [pcap-filter]\n" |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 39 | << "\n" |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 40 | << "Default pcap-filter:\n" |
Davide Pesavento | 4a1b21d | 2018-07-26 20:54:11 -0400 | [diff] [blame] | 41 | << " '" << NdnDump::getDefaultPcapFilter() << "'\n" |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 42 | << "\n" |
| 43 | << options; |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 44 | } |
| 45 | |
Davide Pesavento | 78de735 | 2018-07-22 00:35:45 -0400 | [diff] [blame] | 46 | static int |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 47 | main(int argc, char* argv[]) |
| 48 | { |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 49 | NdnDump instance; |
| 50 | std::string nameFilter; |
| 51 | std::vector<std::string> pcapFilter; |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 52 | |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 53 | po::options_description visibleOptions("Options"); |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 54 | visibleOptions.add_options() |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 55 | ("help,h", "print this help message and exit") |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 56 | ("interface,i", po::value<std::string>(&instance.interface), |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 57 | "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 Pesavento | 24c0861 | 2018-07-26 13:33:24 -0400 | [diff] [blame] | 64 | ("no-promiscuous-mode,p", po::bool_switch(), "do not put the interface into promiscuous mode") |
Davide Pesavento | b5b8f95 | 2018-07-26 14:19:16 -0400 | [diff] [blame] | 65 | ("no-timestamp,t", po::bool_switch(), "do not print a timestamp for each packet") |
Davide Pesavento | 24c0861 | 2018-07-26 13:33:24 -0400 | [diff] [blame] | 66 | ("verbose,v", po::bool_switch(&instance.wantVerbose), |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 67 | "print more detailed information about each packet") |
| 68 | ("version,V", "print program version and exit") |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 69 | ; |
| 70 | |
| 71 | po::options_description hiddenOptions; |
| 72 | hiddenOptions.add_options() |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 73 | ("pcap-filter", po::value<std::vector<std::string>>(&pcapFilter)) |
| 74 | ; |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 75 | |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 76 | po::positional_options_description posOptions; |
| 77 | posOptions.add("pcap-filter", -1); |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 78 | |
| 79 | po::options_description allOptions; |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 80 | allOptions.add(visibleOptions).add(hiddenOptions); |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 81 | |
| 82 | po::variables_map vm; |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 83 | try { |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 84 | po::store(po::command_line_parser(argc, argv).options(allOptions).positional(posOptions).run(), vm); |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 85 | po::notify(vm); |
| 86 | } |
Junxiao Shi | c1c2b83 | 2016-07-24 20:45:36 +0000 | [diff] [blame] | 87 | catch (const po::error& e) { |
| 88 | std::cerr << "ERROR: " << e.what() << "\n\n"; |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 89 | usage(std::cerr, argv[0], visibleOptions); |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 90 | 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 Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | if (vm.count("help") > 0) { |
| 99 | usage(std::cout, argv[0], visibleOptions); |
| 100 | return 0; |
| 101 | } |
| 102 | |
Junxiao Shi | 3cd47df | 2015-06-07 20:58:14 -0700 | [diff] [blame] | 103 | if (vm.count("version") > 0) { |
Davide Pesavento | c3d65c9 | 2018-07-26 13:01:21 -0400 | [diff] [blame] | 104 | std::cout << "ndndump " << tools::VERSION << "\n" |
| 105 | << pcap_lib_version() << std::endl; |
Junxiao Shi | 3cd47df | 2015-06-07 20:58:14 -0700 | [diff] [blame] | 106 | return 0; |
| 107 | } |
| 108 | |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 109 | 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 Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 113 | } |
| 114 | |
Davide Pesavento | 78de735 | 2018-07-22 00:35:45 -0400 | [diff] [blame] | 115 | if (vm.count("filter") > 0) { |
| 116 | try { |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 117 | instance.nameFilter = std::regex(nameFilter); |
Davide Pesavento | 78de735 | 2018-07-22 00:35:45 -0400 | [diff] [blame] | 118 | } |
| 119 | catch (const std::regex_error& e) { |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 120 | std::cerr << "ERROR: invalid filter regex: " << e.what() << std::endl; |
Davide Pesavento | 78de735 | 2018-07-22 00:35:45 -0400 | [diff] [blame] | 121 | return 2; |
| 122 | } |
| 123 | } |
| 124 | |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 125 | if (vm.count("pcap-filter") > 0) { |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 126 | std::ostringstream os; |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 127 | std::copy(pcapFilter.begin(), pcapFilter.end(), make_ostream_joiner(os, " ")); |
| 128 | instance.pcapFilter = os.str(); |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 129 | } |
| 130 | |
Davide Pesavento | 24c0861 | 2018-07-26 13:33:24 -0400 | [diff] [blame] | 131 | instance.wantPromisc = !vm["no-promiscuous-mode"].as<bool>(); |
Davide Pesavento | b5b8f95 | 2018-07-26 14:19:16 -0400 | [diff] [blame] | 132 | instance.wantTimestamp = !vm["no-timestamp"].as<bool>(); |
Davide Pesavento | 24c0861 | 2018-07-26 13:33:24 -0400 | [diff] [blame] | 133 | |
Junxiao Shi | c759963 | 2016-07-24 20:46:24 +0000 | [diff] [blame] | 134 | try { |
| 135 | instance.run(); |
| 136 | } |
| 137 | catch (const std::exception& e) { |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 138 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 139 | return 1; |
Junxiao Shi | c759963 | 2016-07-24 20:46:24 +0000 | [diff] [blame] | 140 | } |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 141 | return 0; |
| 142 | } |
Junxiao Shi | 3cd47df | 2015-06-07 20:58:14 -0700 | [diff] [blame] | 143 | |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame^] | 144 | } // namespace ndn::dump |
Junxiao Shi | 3cd47df | 2015-06-07 20:58:14 -0700 | [diff] [blame] | 145 | |
| 146 | int |
Davide Pesavento | da85e25 | 2019-03-18 11:42:01 -0400 | [diff] [blame] | 147 | main(int argc, char* argv[]) |
Junxiao Shi | 3cd47df | 2015-06-07 20:58:14 -0700 | [diff] [blame] | 148 | { |
| 149 | return ndn::dump::main(argc, argv); |
| 150 | } |