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 | |
| 40 | #include <boost/program_options/options_description.hpp> |
| 41 | #include <boost/program_options/variables_map.hpp> |
| 42 | #include <boost/program_options/parsers.hpp> |
| 43 | |
Davide Pesavento | c070270 | 2017-08-24 22:04:00 -0400 | [diff] [blame] | 44 | #include <sstream> |
| 45 | |
Junxiao Shi | 3cd47df | 2015-06-07 20:58:14 -0700 | [diff] [blame] | 46 | namespace ndn { |
| 47 | namespace dump { |
| 48 | |
Davide Pesavento | 78de735 | 2018-07-22 00:35:45 -0400 | [diff] [blame] | 49 | namespace po = boost::program_options; |
| 50 | |
| 51 | static void |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 52 | usage(std::ostream& os, const std::string& appName, const po::options_description& options) |
| 53 | { |
| 54 | os << "Usage:\n" |
| 55 | << " " << appName << " [-i interface] [-f name-filter] [tcpdump-expression] \n" |
| 56 | << "\n" |
| 57 | << "Default tcpdump-expression:\n" |
| 58 | << " '(ether proto 0x8624) || (tcp port 6363) || (udp port 6363)'\n" |
| 59 | << "\n"; |
| 60 | os << options; |
| 61 | } |
| 62 | |
Davide Pesavento | 78de735 | 2018-07-22 00:35:45 -0400 | [diff] [blame] | 63 | static int |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 64 | main(int argc, char* argv[]) |
| 65 | { |
Junxiao Shi | 3cd47df | 2015-06-07 20:58:14 -0700 | [diff] [blame] | 66 | Ndndump instance; |
Davide Pesavento | 78de735 | 2018-07-22 00:35:45 -0400 | [diff] [blame] | 67 | std::string filter; |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 68 | |
| 69 | po::options_description visibleOptions; |
| 70 | visibleOptions.add_options() |
| 71 | ("help,h", "Produce this help message") |
Davide Pesavento | 78de735 | 2018-07-22 00:35:45 -0400 | [diff] [blame] | 72 | ("version,V", "Display version and exit") |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 73 | ("interface,i", po::value<std::string>(&instance.interface), |
Davide Pesavento | 78de735 | 2018-07-22 00:35:45 -0400 | [diff] [blame] | 74 | "Interface from which to dump packets") |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 75 | ("read,r", po::value<std::string>(&instance.inputFile), |
Davide Pesavento | 78de735 | 2018-07-22 00:35:45 -0400 | [diff] [blame] | 76 | "Read packets from file") |
| 77 | ("verbose,v", "When parsing and printing, produce verbose output") |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 78 | // ("write,w", po::value<std::string>(&instance.outputFile), |
| 79 | // "Write the raw packets to file rather than parsing and printing them out") |
Davide Pesavento | 78de735 | 2018-07-22 00:35:45 -0400 | [diff] [blame] | 80 | ("filter,f", po::value<std::string>(&filter), |
| 81 | "Regular expression to filter out Interest and Data packets") |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 82 | ; |
| 83 | |
| 84 | po::options_description hiddenOptions; |
| 85 | hiddenOptions.add_options() |
Junxiao Shi | c1c2b83 | 2016-07-24 20:45:36 +0000 | [diff] [blame] | 86 | ("pcap-program", po::value<std::vector<std::string>>()); |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 87 | |
| 88 | po::positional_options_description positionalArguments; |
Junxiao Shi | c1c2b83 | 2016-07-24 20:45:36 +0000 | [diff] [blame] | 89 | positionalArguments.add("pcap-program", -1); |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 90 | |
| 91 | po::options_description allOptions; |
Junxiao Shi | c1c2b83 | 2016-07-24 20:45:36 +0000 | [diff] [blame] | 92 | allOptions.add(visibleOptions) |
| 93 | .add(hiddenOptions); |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 94 | |
| 95 | po::variables_map vm; |
| 96 | |
| 97 | try { |
| 98 | po::store(po::command_line_parser(argc, argv) |
| 99 | .options(allOptions) |
| 100 | .positional(positionalArguments) |
| 101 | .run(), |
| 102 | vm); |
| 103 | po::notify(vm); |
| 104 | } |
Junxiao Shi | c1c2b83 | 2016-07-24 20:45:36 +0000 | [diff] [blame] | 105 | catch (const po::error& e) { |
| 106 | std::cerr << "ERROR: " << e.what() << "\n\n"; |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 107 | usage(std::cerr, argv[0], visibleOptions); |
| 108 | return 1; |
| 109 | } |
| 110 | |
| 111 | if (vm.count("help") > 0) { |
| 112 | usage(std::cout, argv[0], visibleOptions); |
| 113 | return 0; |
| 114 | } |
| 115 | |
Junxiao Shi | 3cd47df | 2015-06-07 20:58:14 -0700 | [diff] [blame] | 116 | if (vm.count("version") > 0) { |
Junxiao Shi | c1c2b83 | 2016-07-24 20:45:36 +0000 | [diff] [blame] | 117 | std::cout << "ndndump " << tools::VERSION << '\n'; |
Junxiao Shi | 3cd47df | 2015-06-07 20:58:14 -0700 | [diff] [blame] | 118 | return 0; |
| 119 | } |
| 120 | |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 121 | if (vm.count("verbose") > 0) { |
| 122 | instance.isVerbose = true; |
| 123 | } |
| 124 | |
Davide Pesavento | 78de735 | 2018-07-22 00:35:45 -0400 | [diff] [blame] | 125 | if (vm.count("filter") > 0) { |
| 126 | try { |
| 127 | instance.nameFilter = std::regex(filter); |
| 128 | } |
| 129 | catch (const std::regex_error& e) { |
| 130 | std::cerr << "ERROR: Invalid filter expression: " << e.what() << std::endl; |
| 131 | return 2; |
| 132 | } |
| 133 | } |
| 134 | |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 135 | if (vm.count("pcap-program") > 0) { |
Junxiao Shi | c1c2b83 | 2016-07-24 20:45:36 +0000 | [diff] [blame] | 136 | const auto& items = vm["pcap-program"].as<std::vector<std::string>>(); |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 137 | |
| 138 | std::ostringstream os; |
| 139 | std::copy(items.begin(), items.end(), std::ostream_iterator<std::string>(os, " ")); |
| 140 | instance.pcapProgram = os.str(); |
| 141 | } |
| 142 | |
| 143 | if (vm.count("read") > 0 && vm.count("interface") > 0) { |
Davide Pesavento | 78de735 | 2018-07-22 00:35:45 -0400 | [diff] [blame] | 144 | std::cerr << "ERROR: Conflicting -r and -i options\n\n"; |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 145 | usage(std::cerr, argv[0], visibleOptions); |
| 146 | return 2; |
| 147 | } |
| 148 | |
Junxiao Shi | c759963 | 2016-07-24 20:46:24 +0000 | [diff] [blame] | 149 | try { |
| 150 | instance.run(); |
| 151 | } |
| 152 | catch (const std::exception& e) { |
Davide Pesavento | 78de735 | 2018-07-22 00:35:45 -0400 | [diff] [blame] | 153 | std::cerr << "ERROR: " << e.what() << "\n"; |
Junxiao Shi | c759963 | 2016-07-24 20:46:24 +0000 | [diff] [blame] | 154 | } |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 155 | |
| 156 | return 0; |
| 157 | } |
Junxiao Shi | 3cd47df | 2015-06-07 20:58:14 -0700 | [diff] [blame] | 158 | |
| 159 | } // namespace dump |
| 160 | } // namespace ndn |
| 161 | |
| 162 | int |
| 163 | main(int argc, char** argv) |
| 164 | { |
| 165 | return ndn::dump::main(argc, argv); |
| 166 | } |