blob: 9d786e73308b15c74fff78d3346239e3bef18de9 [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 Pesavento59984282022-02-16 22:41:03 -05003 * Copyright (c) 2011-2022, 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 Pesavento78de7352018-07-22 00:35:45 -040026#include <regex>
Junxiao Shi2222a612015-06-06 08:01:38 -070027
Davide Pesavento7b9837b2019-02-23 19:07:50 -050028#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 Pesaventob3570c62022-02-19 19:19:00 -050040namespace ndn::dump {
Junxiao Shi2222a612015-06-06 08:01:38 -070041
Davide Pesaventob6b176c2018-07-28 22:05:16 -040042class OutputFormatter;
43
Davide Pesaventoecd44802018-07-23 23:48:10 -040044class NdnDump : noncopyable
Junxiao Shi2222a612015-06-06 08:01:38 -070045{
46public:
47 class Error : public std::runtime_error
48 {
49 public:
Davide Pesaventoecd44802018-07-23 23:48:10 -040050 using std::runtime_error::runtime_error;
Junxiao Shi2222a612015-06-06 08:01:38 -070051 };
52
Davide Pesaventoecd44802018-07-23 23:48:10 -040053 ~NdnDump();
Junxiao Shi2222a612015-06-06 08:01:38 -070054
55 void
56 run();
57
Vince Lehman277ecf02016-02-10 16:37:48 -060058 void
Davide Pesaventoecd44802018-07-23 23:48:10 -040059 printPacket(const pcap_pkthdr* pkthdr, const uint8_t* payload) const;
Vince Lehman277ecf02016-02-10 16:37:48 -060060
Davide Pesaventob3570c62022-02-19 19:19:00 -050061 static constexpr std::string_view
Davide Pesaventoecd44802018-07-23 23:48:10 -040062 getDefaultPcapFilter() noexcept
Junxiao Shi2222a612015-06-06 08:01:38 -070063 {
Davide Pesavento4a1b21d2018-07-26 20:54:11 -040064 return "(ether proto 0x8624) or (tcp port 6363) or (udp port 6363) or (udp port 56363)";
Junxiao Shi2222a612015-06-06 08:01:38 -070065 }
66
Davide Pesaventoecd44802018-07-23 23:48:10 -040067private:
Davide Pesavento59984282022-02-16 22:41:03 -050068 static void
69 printTimestamp(std::ostream& os, const timeval& tv);
Junxiao Shi2222a612015-06-06 08:01:38 -070070
Davide Pesaventob6b176c2018-07-28 22:05:16 -040071 bool
72 dispatchByEtherType(OutputFormatter& out, const uint8_t* pkt, size_t len, uint16_t etherType) const;
Junxiao Shi2222a612015-06-06 08:01:38 -070073
Davide Pesaventob6b176c2018-07-28 22:05:16 -040074 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 Pesavento0ca7e692018-08-05 02:21:06 -040090 printIp6(OutputFormatter& out, const uint8_t* pkt, size_t len) const;
91
92 bool
Davide Pesaventob6b176c2018-07-28 22:05:16 -040093 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 Shi2222a612015-06-06 08:01:38 -0700100
Davide Pesaventob3570c62022-02-19 19:19:00 -0500101 [[nodiscard]] bool
Junxiao Shic1c2b832016-07-24 20:45:36 +0000102 matchesFilter(const Name& name) const;
Junxiao Shi2222a612015-06-06 08:01:38 -0700103
Davide Pesaventoecd44802018-07-23 23:48:10 -0400104public: // options
Junxiao Shi2222a612015-06-06 08:01:38 -0700105 std::string interface;
Junxiao Shi2222a612015-06-06 08:01:38 -0700106 std::string inputFile;
Davide Pesaventob3570c62022-02-19 19:19:00 -0500107 std::string pcapFilter{getDefaultPcapFilter()};
Davide Pesaventoecd44802018-07-23 23:48:10 -0400108 optional<std::regex> nameFilter;
Davide Pesavento24c08612018-07-26 13:33:24 -0400109 bool wantPromisc = true;
Davide Pesaventob5b8f952018-07-26 14:19:16 -0400110 bool wantTimestamp = true;
Davide Pesavento24c08612018-07-26 13:33:24 -0400111 bool wantVerbose = false;
Junxiao Shi2222a612015-06-06 08:01:38 -0700112
113private:
Davide Pesaventoecd44802018-07-23 23:48:10 -0400114 pcap_t* m_pcap = nullptr;
Vince Lehman277ecf02016-02-10 16:37:48 -0600115
116PUBLIC_WITH_TESTS_ELSE_PRIVATE:
Davide Pesaventoecd44802018-07-23 23:48:10 -0400117 int m_dataLinkType = -1;
Junxiao Shi2222a612015-06-06 08:01:38 -0700118};
119
Davide Pesaventob3570c62022-02-19 19:19:00 -0500120} // namespace ndn::dump
Junxiao Shi2222a612015-06-06 08:01:38 -0700121
Junxiao Shi3cd47df2015-06-07 20:58:14 -0700122#endif // NDN_TOOLS_DUMP_NDNDUMP_HPP