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