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