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 | /* |
Davide Pesavento | 5998428 | 2022-02-16 22:41:03 -0500 | [diff] [blame] | 3 | * Copyright (c) 2011-2022, 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 | */ |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 19 | |
Junxiao Shi | 3cd47df | 2015-06-07 20:58:14 -0700 | [diff] [blame] | 20 | #ifndef NDN_TOOLS_DUMP_NDNDUMP_HPP |
| 21 | #define NDN_TOOLS_DUMP_NDNDUMP_HPP |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 22 | |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 23 | #include "core/common.hpp" |
| 24 | |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 25 | #include <pcap.h> |
Davide Pesavento | 78de735 | 2018-07-22 00:35:45 -0400 | [diff] [blame] | 26 | #include <regex> |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 27 | |
Davide Pesavento | 7b9837b | 2019-02-23 19:07:50 -0500 | [diff] [blame] | 28 | #ifdef HAVE_BSD_TCPHDR |
| 29 | #define TH_OFF th_off |
| 30 | #else |
| 31 | #define TH_OFF doff |
| 32 | #endif |
| 33 | |
| 34 | #ifdef HAVE_BSD_UDPHDR |
| 35 | #define UH_LEN uh_ulen |
| 36 | #else |
| 37 | #define UH_LEN len |
| 38 | #endif |
| 39 | |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame^] | 40 | namespace ndn::dump { |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 41 | |
Davide Pesavento | b6b176c | 2018-07-28 22:05:16 -0400 | [diff] [blame] | 42 | class OutputFormatter; |
| 43 | |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 44 | class NdnDump : noncopyable |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 45 | { |
| 46 | public: |
| 47 | class Error : public std::runtime_error |
| 48 | { |
| 49 | public: |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 50 | using std::runtime_error::runtime_error; |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 51 | }; |
| 52 | |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 53 | ~NdnDump(); |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 54 | |
| 55 | void |
| 56 | run(); |
| 57 | |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 58 | void |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 59 | printPacket(const pcap_pkthdr* pkthdr, const uint8_t* payload) const; |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 60 | |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame^] | 61 | static constexpr std::string_view |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 62 | getDefaultPcapFilter() noexcept |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 63 | { |
Davide Pesavento | 4a1b21d | 2018-07-26 20:54:11 -0400 | [diff] [blame] | 64 | return "(ether proto 0x8624) or (tcp port 6363) or (udp port 6363) or (udp port 56363)"; |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 65 | } |
| 66 | |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 67 | private: |
Davide Pesavento | 5998428 | 2022-02-16 22:41:03 -0500 | [diff] [blame] | 68 | static void |
| 69 | printTimestamp(std::ostream& os, const timeval& tv); |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 70 | |
Davide Pesavento | b6b176c | 2018-07-28 22:05:16 -0400 | [diff] [blame] | 71 | bool |
| 72 | dispatchByEtherType(OutputFormatter& out, const uint8_t* pkt, size_t len, uint16_t etherType) const; |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 73 | |
Davide Pesavento | b6b176c | 2018-07-28 22:05:16 -0400 | [diff] [blame] | 74 | bool |
| 75 | dispatchByIpProto(OutputFormatter& out, const uint8_t* pkt, size_t len, uint8_t ipProto) const; |
| 76 | |
| 77 | bool |
| 78 | printEther(OutputFormatter& out, const uint8_t* pkt, size_t len) const; |
| 79 | |
| 80 | bool |
| 81 | printLinuxSll(OutputFormatter& out, const uint8_t* pkt, size_t len) const; |
| 82 | |
| 83 | bool |
| 84 | printPpp(OutputFormatter& out, const uint8_t* pkt, size_t len) const; |
| 85 | |
| 86 | bool |
| 87 | printIp4(OutputFormatter& out, const uint8_t* pkt, size_t len) const; |
| 88 | |
| 89 | bool |
Davide Pesavento | 0ca7e69 | 2018-08-05 02:21:06 -0400 | [diff] [blame] | 90 | printIp6(OutputFormatter& out, const uint8_t* pkt, size_t len) const; |
| 91 | |
| 92 | bool |
Davide Pesavento | b6b176c | 2018-07-28 22:05:16 -0400 | [diff] [blame] | 93 | printTcp(OutputFormatter& out, const uint8_t* pkt, size_t len) const; |
| 94 | |
| 95 | bool |
| 96 | printUdp(OutputFormatter& out, const uint8_t* pkt, size_t len) const; |
| 97 | |
| 98 | bool |
| 99 | printNdn(OutputFormatter& out, const uint8_t* pkt, size_t len) const; |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 100 | |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame^] | 101 | [[nodiscard]] bool |
Junxiao Shi | c1c2b83 | 2016-07-24 20:45:36 +0000 | [diff] [blame] | 102 | matchesFilter(const Name& name) const; |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 103 | |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 104 | public: // options |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 105 | std::string interface; |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 106 | std::string inputFile; |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame^] | 107 | std::string pcapFilter{getDefaultPcapFilter()}; |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 108 | optional<std::regex> nameFilter; |
Davide Pesavento | 24c0861 | 2018-07-26 13:33:24 -0400 | [diff] [blame] | 109 | bool wantPromisc = true; |
Davide Pesavento | b5b8f95 | 2018-07-26 14:19:16 -0400 | [diff] [blame] | 110 | bool wantTimestamp = true; |
Davide Pesavento | 24c0861 | 2018-07-26 13:33:24 -0400 | [diff] [blame] | 111 | bool wantVerbose = false; |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 112 | |
| 113 | private: |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 114 | pcap_t* m_pcap = nullptr; |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 115 | |
| 116 | PUBLIC_WITH_TESTS_ELSE_PRIVATE: |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 117 | int m_dataLinkType = -1; |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 118 | }; |
| 119 | |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame^] | 120 | } // namespace ndn::dump |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 121 | |
Junxiao Shi | 3cd47df | 2015-06-07 20:58:14 -0700 | [diff] [blame] | 122 | #endif // NDN_TOOLS_DUMP_NDNDUMP_HPP |