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 | #ifndef NDNDUMP_NDNDUMP_HPP |
| 21 | #define NDNDUMP_NDNDUMP_HPP |
| 22 | |
| 23 | #include <pcap.h> |
| 24 | |
| 25 | #include <ndn-cxx/name.hpp> |
| 26 | #include <boost/regex.hpp> |
| 27 | |
| 28 | namespace ndn { |
| 29 | namespace tools { |
| 30 | |
| 31 | class Ndndump : noncopyable |
| 32 | { |
| 33 | public: |
| 34 | class Error : public std::runtime_error |
| 35 | { |
| 36 | public: |
| 37 | explicit |
| 38 | Error(const std::string& what) |
| 39 | : std::runtime_error(what) |
| 40 | { |
| 41 | } |
| 42 | }; |
| 43 | |
| 44 | Ndndump() |
| 45 | : isVerbose(false) |
| 46 | , pcapProgram("(ether proto 0x8624) || (tcp port 6363) || (udp port 6363)") |
| 47 | // , isSuccinct(false) |
| 48 | // , isMatchInverted(false) |
| 49 | // , shouldPrintStructure(false) |
| 50 | // , isTcpOnly(false) |
| 51 | // , isUdpOnly(false) |
| 52 | { |
| 53 | } |
| 54 | |
| 55 | void |
| 56 | run(); |
| 57 | |
| 58 | private: |
| 59 | static void |
| 60 | onCapturedPacket(uint8_t* userData, const struct pcap_pkthdr* header, const uint8_t* packet) |
| 61 | { |
| 62 | reinterpret_cast<Ndndump*>(userData)->onCapturedPacket(header, packet); |
| 63 | } |
| 64 | |
| 65 | void |
| 66 | onCapturedPacket(const struct pcap_pkthdr* header, const uint8_t* packet); |
| 67 | |
| 68 | void |
| 69 | printInterceptTime(std::ostream& os, const struct pcap_pkthdr* header); |
| 70 | |
| 71 | int |
| 72 | skipDataLinkHeaderAndGetFrameType(const uint8_t*& payload, ssize_t& payloadSize); |
| 73 | |
| 74 | int |
| 75 | skipAndProcessFrameHeader(int frameType, |
| 76 | const uint8_t*& payload, ssize_t& payloadSize, |
| 77 | std::ostream& os); |
| 78 | |
| 79 | bool |
| 80 | matchesFilter(const Name& name) |
| 81 | { |
| 82 | if (nameFilter.empty()) |
| 83 | return true; |
| 84 | |
| 85 | /// \todo Switch to NDN regular expressions |
| 86 | |
| 87 | return boost::regex_match(name.toUri(), nameFilter); |
| 88 | } |
| 89 | |
| 90 | public: |
| 91 | bool isVerbose; |
| 92 | // bool isSuccinct; |
| 93 | // bool isMatchInverted; |
| 94 | // bool shouldPrintStructure; |
| 95 | // bool isTcpOnly; |
| 96 | // bool isUdpOnly; |
| 97 | |
| 98 | std::string pcapProgram; |
| 99 | std::string interface; |
| 100 | boost::regex nameFilter; |
| 101 | std::string inputFile; |
| 102 | // std::string outputFile; |
| 103 | |
| 104 | private: |
| 105 | pcap_t* m_pcap; |
| 106 | int m_dataLinkType; |
| 107 | }; |
| 108 | |
| 109 | |
| 110 | } // namespace tools |
| 111 | } // namespace ndn |
| 112 | |
| 113 | #endif // NDNDUMP_NDNDUMP_HPP |