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 | /* |
| 3 | * Copyright (c) 2014-2018, 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 | */ |
Davide Pesavento | 78de735 | 2018-07-22 00:35:45 -0400 | [diff] [blame] | 19 | /* |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 20 | * 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" |
Junxiao Shi | 3cd47df | 2015-06-07 20:58:14 -0700 | [diff] [blame] | 38 | #include "core/version.hpp" |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 39 | |
Davide Pesavento | c070270 | 2017-08-24 22:04:00 -0400 | [diff] [blame] | 40 | #include <sstream> |
| 41 | |
Junxiao Shi | 3cd47df | 2015-06-07 20:58:14 -0700 | [diff] [blame] | 42 | namespace ndn { |
| 43 | namespace dump { |
| 44 | |
Davide Pesavento | 78de735 | 2018-07-22 00:35:45 -0400 | [diff] [blame] | 45 | namespace po = boost::program_options; |
| 46 | |
| 47 | static void |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 48 | usage(std::ostream& os, const std::string& appName, const po::options_description& options) |
| 49 | { |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 50 | os << "Usage: " << appName << " [options] [pcap-filter]\n" |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 51 | << "\n" |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 52 | << "Default pcap-filter:\n" |
Davide Pesavento | 4a1b21d | 2018-07-26 20:54:11 -0400 | [diff] [blame] | 53 | << " '" << NdnDump::getDefaultPcapFilter() << "'\n" |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 54 | << "\n" |
| 55 | << options; |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 56 | } |
| 57 | |
Davide Pesavento | 78de735 | 2018-07-22 00:35:45 -0400 | [diff] [blame] | 58 | static int |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 59 | main(int argc, char* argv[]) |
| 60 | { |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 61 | NdnDump instance; |
| 62 | std::string nameFilter; |
| 63 | std::vector<std::string> pcapFilter; |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 64 | |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 65 | po::options_description visibleOptions("Options"); |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 66 | visibleOptions.add_options() |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 67 | ("help,h", "print this help message and exit") |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 68 | ("interface,i", po::value<std::string>(&instance.interface), |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 69 | "capture packets from the specified interface; if unspecified, the first " |
| 70 | "non-loopback interface will be used; on Linux, the special value \"any\" " |
| 71 | "can be used to capture from all interfaces") |
| 72 | ("read,r", po::value<std::string>(&instance.inputFile), |
| 73 | "read packets from the specified file; use \"-\" to read from standard input") |
| 74 | ("filter,f", po::value<std::string>(&nameFilter), |
| 75 | "print packet only if name matches this regular expression") |
Davide Pesavento | 24c0861 | 2018-07-26 13:33:24 -0400 | [diff] [blame] | 76 | ("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] | 77 | ("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] | 78 | ("verbose,v", po::bool_switch(&instance.wantVerbose), |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 79 | "print more detailed information about each packet") |
| 80 | ("version,V", "print program version and exit") |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 81 | ; |
| 82 | |
| 83 | po::options_description hiddenOptions; |
| 84 | hiddenOptions.add_options() |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 85 | ("pcap-filter", po::value<std::vector<std::string>>(&pcapFilter)) |
| 86 | ; |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 87 | |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 88 | po::positional_options_description posOptions; |
| 89 | posOptions.add("pcap-filter", -1); |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 90 | |
| 91 | po::options_description allOptions; |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 92 | allOptions.add(visibleOptions).add(hiddenOptions); |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 93 | |
| 94 | po::variables_map vm; |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 95 | try { |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 96 | 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] | 97 | po::notify(vm); |
| 98 | } |
Junxiao Shi | c1c2b83 | 2016-07-24 20:45:36 +0000 | [diff] [blame] | 99 | catch (const po::error& e) { |
| 100 | std::cerr << "ERROR: " << e.what() << "\n\n"; |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 101 | usage(std::cerr, argv[0], visibleOptions); |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 102 | return 2; |
| 103 | } |
| 104 | catch (const boost::bad_any_cast& e) { |
| 105 | std::cerr << "ERROR: " << e.what() << "\n\n"; |
| 106 | usage(std::cerr, argv[0], visibleOptions); |
| 107 | return 2; |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | if (vm.count("help") > 0) { |
| 111 | usage(std::cout, argv[0], visibleOptions); |
| 112 | return 0; |
| 113 | } |
| 114 | |
Junxiao Shi | 3cd47df | 2015-06-07 20:58:14 -0700 | [diff] [blame] | 115 | if (vm.count("version") > 0) { |
Davide Pesavento | c3d65c9 | 2018-07-26 13:01:21 -0400 | [diff] [blame] | 116 | std::cout << "ndndump " << tools::VERSION << "\n" |
| 117 | << pcap_lib_version() << std::endl; |
Junxiao Shi | 3cd47df | 2015-06-07 20:58:14 -0700 | [diff] [blame] | 118 | return 0; |
| 119 | } |
| 120 | |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 121 | if (vm.count("interface") > 0 && vm.count("read") > 0) { |
| 122 | std::cerr << "ERROR: conflicting '--interface' and '--read' options specified\n\n"; |
| 123 | usage(std::cerr, argv[0], visibleOptions); |
| 124 | return 2; |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 125 | } |
| 126 | |
Davide Pesavento | 78de735 | 2018-07-22 00:35:45 -0400 | [diff] [blame] | 127 | if (vm.count("filter") > 0) { |
| 128 | try { |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 129 | instance.nameFilter = std::regex(nameFilter); |
Davide Pesavento | 78de735 | 2018-07-22 00:35:45 -0400 | [diff] [blame] | 130 | } |
| 131 | catch (const std::regex_error& e) { |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 132 | std::cerr << "ERROR: invalid filter regex: " << e.what() << std::endl; |
Davide Pesavento | 78de735 | 2018-07-22 00:35:45 -0400 | [diff] [blame] | 133 | return 2; |
| 134 | } |
| 135 | } |
| 136 | |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 137 | if (vm.count("pcap-filter") > 0) { |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 138 | std::ostringstream os; |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 139 | std::copy(pcapFilter.begin(), pcapFilter.end(), make_ostream_joiner(os, " ")); |
| 140 | instance.pcapFilter = os.str(); |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 141 | } |
| 142 | |
Davide Pesavento | 24c0861 | 2018-07-26 13:33:24 -0400 | [diff] [blame] | 143 | instance.wantPromisc = !vm["no-promiscuous-mode"].as<bool>(); |
Davide Pesavento | b5b8f95 | 2018-07-26 14:19:16 -0400 | [diff] [blame] | 144 | instance.wantTimestamp = !vm["no-timestamp"].as<bool>(); |
Davide Pesavento | 24c0861 | 2018-07-26 13:33:24 -0400 | [diff] [blame] | 145 | |
Junxiao Shi | c759963 | 2016-07-24 20:46:24 +0000 | [diff] [blame] | 146 | try { |
| 147 | instance.run(); |
| 148 | } |
| 149 | catch (const std::exception& e) { |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 150 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 151 | return 1; |
Junxiao Shi | c759963 | 2016-07-24 20:46:24 +0000 | [diff] [blame] | 152 | } |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 153 | return 0; |
| 154 | } |
Junxiao Shi | 3cd47df | 2015-06-07 20:58:14 -0700 | [diff] [blame] | 155 | |
| 156 | } // namespace dump |
| 157 | } // namespace ndn |
| 158 | |
| 159 | int |
| 160 | main(int argc, char** argv) |
| 161 | { |
| 162 | return ndn::dump::main(argc, argv); |
| 163 | } |