Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame^] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2011-2014, Regents of the University of California, |
| 4 | * |
| 5 | * This file is part of ndndump, the packet capture and analysis tool for Named Data |
| 6 | * Networking (NDN). |
| 7 | * |
| 8 | * ndndump 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 | * ndndump 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 | * ndndump, e.g., in COPYING file. If not, see <http://www.gnu.org/licenses/>. |
| 18 | **/ |
| 19 | |
| 20 | #include "ndndump.hpp" |
| 21 | |
| 22 | #include <boost/program_options/options_description.hpp> |
| 23 | #include <boost/program_options/variables_map.hpp> |
| 24 | #include <boost/program_options/parsers.hpp> |
| 25 | |
| 26 | namespace po = boost::program_options; |
| 27 | |
| 28 | namespace boost { |
| 29 | |
| 30 | void |
| 31 | validate(boost::any& v, |
| 32 | const std::vector<std::string>& values, |
| 33 | boost::regex*, int) |
| 34 | { |
| 35 | po::validators::check_first_occurrence(v); |
| 36 | const std::string& str = po::validators::get_single_string(values); |
| 37 | v = boost::any(boost::regex(str)); |
| 38 | } |
| 39 | |
| 40 | } // namespace boost |
| 41 | |
| 42 | void |
| 43 | usage(std::ostream& os, const std::string& appName, const po::options_description& options) |
| 44 | { |
| 45 | os << "Usage:\n" |
| 46 | << " " << appName << " [-i interface] [-f name-filter] [tcpdump-expression] \n" |
| 47 | << "\n" |
| 48 | << "Default tcpdump-expression:\n" |
| 49 | << " '(ether proto 0x8624) || (tcp port 6363) || (udp port 6363)'\n" |
| 50 | << "\n"; |
| 51 | os << options; |
| 52 | } |
| 53 | |
| 54 | int |
| 55 | main(int argc, char* argv[]) |
| 56 | { |
| 57 | ndn::tools::Ndndump instance; |
| 58 | |
| 59 | po::options_description visibleOptions; |
| 60 | visibleOptions.add_options() |
| 61 | ("help,h", "Produce this help message") |
| 62 | ("interface,i", po::value<std::string>(&instance.interface), |
| 63 | "Interface from which to dump packets") |
| 64 | ("read,r", po::value<std::string>(&instance.inputFile), |
| 65 | "Read packets from file") |
| 66 | ("verbose,v", |
| 67 | "When parsing and printing, produce verbose output") |
| 68 | // ("write,w", po::value<std::string>(&instance.outputFile), |
| 69 | // "Write the raw packets to file rather than parsing and printing them out") |
| 70 | ("filter,f", po::value<boost::regex>(&instance.nameFilter), |
| 71 | "Regular expression to filter out Interest and Data packets") |
| 72 | ; |
| 73 | |
| 74 | po::options_description hiddenOptions; |
| 75 | hiddenOptions.add_options() |
| 76 | ("pcap-program", po::value<std::vector<std::string> >()); |
| 77 | ; |
| 78 | |
| 79 | po::positional_options_description positionalArguments; |
| 80 | positionalArguments |
| 81 | .add("pcap-program", -1); |
| 82 | |
| 83 | po::options_description allOptions; |
| 84 | allOptions |
| 85 | .add(visibleOptions) |
| 86 | .add(hiddenOptions) |
| 87 | ; |
| 88 | |
| 89 | po::variables_map vm; |
| 90 | |
| 91 | try { |
| 92 | po::store(po::command_line_parser(argc, argv) |
| 93 | .options(allOptions) |
| 94 | .positional(positionalArguments) |
| 95 | .run(), |
| 96 | vm); |
| 97 | po::notify(vm); |
| 98 | } |
| 99 | catch (std::exception& e) { |
| 100 | std::cerr << "ERROR: " << e.what() << std::endl << std::endl; |
| 101 | usage(std::cerr, argv[0], visibleOptions); |
| 102 | return 1; |
| 103 | } |
| 104 | |
| 105 | if (vm.count("help") > 0) { |
| 106 | usage(std::cout, argv[0], visibleOptions); |
| 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | if (vm.count("verbose") > 0) { |
| 111 | instance.isVerbose = true; |
| 112 | } |
| 113 | |
| 114 | if (vm.count("pcap-program") > 0) { |
| 115 | typedef std::vector<std::string> Strings; |
| 116 | const Strings& items = vm["pcap-program"].as<Strings>(); |
| 117 | |
| 118 | std::ostringstream os; |
| 119 | std::copy(items.begin(), items.end(), std::ostream_iterator<std::string>(os, " ")); |
| 120 | instance.pcapProgram = os.str(); |
| 121 | } |
| 122 | |
| 123 | if (vm.count("read") > 0 && vm.count("interface") > 0) { |
| 124 | std::cerr << "ERROR: Conflicting -r and -i options" << std::endl; |
| 125 | usage(std::cerr, argv[0], visibleOptions); |
| 126 | return 2; |
| 127 | } |
| 128 | |
| 129 | instance.run(); |
| 130 | |
| 131 | return 0; |
| 132 | } |