blob: 8a10defc9d30998fd8d3335a9e936b38b1c71d44 [file] [log] [blame]
Junxiao Shi2222a612015-06-06 08:01:38 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento78de7352018-07-22 00:35:45 -04002/*
Davide Pesavento5748e822024-01-26 18:40:22 -05003 * Copyright (c) 2011-2024, Regents of the University of California.
Junxiao Shi3cd47df2015-06-07 20:58:14 -07004 *
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 Shi2222a612015-06-06 08:01:38 -070019
Junxiao Shi3cd47df2015-06-07 20:58:14 -070020#ifndef NDN_TOOLS_DUMP_NDNDUMP_HPP
21#define NDN_TOOLS_DUMP_NDNDUMP_HPP
Junxiao Shi2222a612015-06-06 08:01:38 -070022
Vince Lehman277ecf02016-02-10 16:37:48 -060023#include "core/common.hpp"
24
Junxiao Shi2222a612015-06-06 08:01:38 -070025#include <pcap.h>
Davide Pesavento5748e822024-01-26 18:40:22 -050026
27#include <optional>
Davide Pesavento78de7352018-07-22 00:35:45 -040028#include <regex>
Junxiao Shi2222a612015-06-06 08:01:38 -070029
Davide Pesavento7b9837b2019-02-23 19:07:50 -050030#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 Pesaventob3570c62022-02-19 19:19:00 -050042namespace ndn::dump {
Junxiao Shi2222a612015-06-06 08:01:38 -070043
Davide Pesaventob6b176c2018-07-28 22:05:16 -040044class OutputFormatter;
45
Davide Pesaventoecd44802018-07-23 23:48:10 -040046class NdnDump : noncopyable
Junxiao Shi2222a612015-06-06 08:01:38 -070047{
48public:
49 class Error : public std::runtime_error
50 {
51 public:
Davide Pesaventoecd44802018-07-23 23:48:10 -040052 using std::runtime_error::runtime_error;
Junxiao Shi2222a612015-06-06 08:01:38 -070053 };
54
Davide Pesaventoecd44802018-07-23 23:48:10 -040055 ~NdnDump();
Junxiao Shi2222a612015-06-06 08:01:38 -070056
57 void
58 run();
59
Vince Lehman277ecf02016-02-10 16:37:48 -060060 void
Davide Pesaventoecd44802018-07-23 23:48:10 -040061 printPacket(const pcap_pkthdr* pkthdr, const uint8_t* payload) const;
Vince Lehman277ecf02016-02-10 16:37:48 -060062
Davide Pesaventob3570c62022-02-19 19:19:00 -050063 static constexpr std::string_view
Davide Pesaventoecd44802018-07-23 23:48:10 -040064 getDefaultPcapFilter() noexcept
Junxiao Shi2222a612015-06-06 08:01:38 -070065 {
Davide Pesavento4a1b21d2018-07-26 20:54:11 -040066 return "(ether proto 0x8624) or (tcp port 6363) or (udp port 6363) or (udp port 56363)";
Junxiao Shi2222a612015-06-06 08:01:38 -070067 }
68
Davide Pesaventoecd44802018-07-23 23:48:10 -040069private:
Davide Pesavento59984282022-02-16 22:41:03 -050070 static void
71 printTimestamp(std::ostream& os, const timeval& tv);
Junxiao Shi2222a612015-06-06 08:01:38 -070072
Davide Pesaventob6b176c2018-07-28 22:05:16 -040073 bool
74 dispatchByEtherType(OutputFormatter& out, const uint8_t* pkt, size_t len, uint16_t etherType) const;
Junxiao Shi2222a612015-06-06 08:01:38 -070075
Davide Pesaventob6b176c2018-07-28 22:05:16 -040076 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 Pesavento0ca7e692018-08-05 02:21:06 -040092 printIp6(OutputFormatter& out, const uint8_t* pkt, size_t len) const;
93
94 bool
Davide Pesaventob6b176c2018-07-28 22:05:16 -040095 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 Shi2222a612015-06-06 08:01:38 -0700102
Davide Pesaventob3570c62022-02-19 19:19:00 -0500103 [[nodiscard]] bool
Junxiao Shic1c2b832016-07-24 20:45:36 +0000104 matchesFilter(const Name& name) const;
Junxiao Shi2222a612015-06-06 08:01:38 -0700105
Davide Pesaventoecd44802018-07-23 23:48:10 -0400106public: // options
Junxiao Shi2222a612015-06-06 08:01:38 -0700107 std::string interface;
Junxiao Shi2222a612015-06-06 08:01:38 -0700108 std::string inputFile;
Davide Pesaventob3570c62022-02-19 19:19:00 -0500109 std::string pcapFilter{getDefaultPcapFilter()};
Davide Pesavento242d5062022-03-11 16:34:23 -0500110 std::optional<std::regex> nameFilter;
Davide Pesavento24c08612018-07-26 13:33:24 -0400111 bool wantPromisc = true;
Davide Pesaventob5b8f952018-07-26 14:19:16 -0400112 bool wantTimestamp = true;
Davide Pesavento24c08612018-07-26 13:33:24 -0400113 bool wantVerbose = false;
Junxiao Shi2222a612015-06-06 08:01:38 -0700114
115private:
Davide Pesaventoecd44802018-07-23 23:48:10 -0400116 pcap_t* m_pcap = nullptr;
Vince Lehman277ecf02016-02-10 16:37:48 -0600117
118PUBLIC_WITH_TESTS_ELSE_PRIVATE:
Davide Pesaventoecd44802018-07-23 23:48:10 -0400119 int m_dataLinkType = -1;
Junxiao Shi2222a612015-06-06 08:01:38 -0700120};
121
Davide Pesaventob3570c62022-02-19 19:19:00 -0500122} // namespace ndn::dump
Junxiao Shi2222a612015-06-06 08:01:38 -0700123
Junxiao Shi3cd47df2015-06-07 20:58:14 -0700124#endif // NDN_TOOLS_DUMP_NDNDUMP_HPP