blob: 58ba0329e07594bcddbb4ed7041bbea69900445f [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 Pesavento7b9837b2019-02-23 19:07:50 -05003 * Copyright (c) 2011-2019, 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
Junxiao Shi2222a612015-06-06 08:01:38 -070040namespace ndn {
Junxiao Shi3cd47df2015-06-07 20:58:14 -070041namespace dump {
Junxiao Shi2222a612015-06-06 08:01:38 -070042
Davide Pesaventob6b176c2018-07-28 22:05:16 -040043class OutputFormatter;
44
Davide Pesaventoecd44802018-07-23 23:48:10 -040045class NdnDump : noncopyable
Junxiao Shi2222a612015-06-06 08:01:38 -070046{
47public:
48 class Error : public std::runtime_error
49 {
50 public:
Davide Pesaventoecd44802018-07-23 23:48:10 -040051 using std::runtime_error::runtime_error;
Junxiao Shi2222a612015-06-06 08:01:38 -070052 };
53
Davide Pesaventoecd44802018-07-23 23:48:10 -040054 ~NdnDump();
Junxiao Shi2222a612015-06-06 08:01:38 -070055
56 void
57 run();
58
Vince Lehman277ecf02016-02-10 16:37:48 -060059 void
Davide Pesaventoecd44802018-07-23 23:48:10 -040060 printPacket(const pcap_pkthdr* pkthdr, const uint8_t* payload) const;
Vince Lehman277ecf02016-02-10 16:37:48 -060061
Davide Pesaventoecd44802018-07-23 23:48:10 -040062 static constexpr const char*
63 getDefaultPcapFilter() noexcept
Junxiao Shi2222a612015-06-06 08:01:38 -070064 {
Davide Pesavento4a1b21d2018-07-26 20:54:11 -040065 return "(ether proto 0x8624) or (tcp port 6363) or (udp port 6363) or (udp port 56363)";
Junxiao Shi2222a612015-06-06 08:01:38 -070066 }
67
Davide Pesaventoecd44802018-07-23 23:48:10 -040068private:
Junxiao Shi2222a612015-06-06 08:01:38 -070069 void
Davide Pesaventoecd44802018-07-23 23:48:10 -040070 printTimestamp(std::ostream& os, const timeval& tv) const;
Junxiao Shi2222a612015-06-06 08:01:38 -070071
Davide Pesaventob6b176c2018-07-28 22:05:16 -040072 bool
73 dispatchByEtherType(OutputFormatter& out, const uint8_t* pkt, size_t len, uint16_t etherType) const;
Junxiao Shi2222a612015-06-06 08:01:38 -070074
Davide Pesaventob6b176c2018-07-28 22:05:16 -040075 bool
76 dispatchByIpProto(OutputFormatter& out, const uint8_t* pkt, size_t len, uint8_t ipProto) const;
77
78 bool
79 printEther(OutputFormatter& out, const uint8_t* pkt, size_t len) const;
80
81 bool
82 printLinuxSll(OutputFormatter& out, const uint8_t* pkt, size_t len) const;
83
84 bool
85 printPpp(OutputFormatter& out, const uint8_t* pkt, size_t len) const;
86
87 bool
88 printIp4(OutputFormatter& out, const uint8_t* pkt, size_t len) const;
89
90 bool
Davide Pesavento0ca7e692018-08-05 02:21:06 -040091 printIp6(OutputFormatter& out, const uint8_t* pkt, size_t len) const;
92
93 bool
Davide Pesaventob6b176c2018-07-28 22:05:16 -040094 printTcp(OutputFormatter& out, const uint8_t* pkt, size_t len) const;
95
96 bool
97 printUdp(OutputFormatter& out, const uint8_t* pkt, size_t len) const;
98
99 bool
100 printNdn(OutputFormatter& out, const uint8_t* pkt, size_t len) const;
Junxiao Shi2222a612015-06-06 08:01:38 -0700101
102 bool
Junxiao Shic1c2b832016-07-24 20:45:36 +0000103 matchesFilter(const Name& name) const;
Junxiao Shi2222a612015-06-06 08:01:38 -0700104
Davide Pesaventoecd44802018-07-23 23:48:10 -0400105public: // options
Junxiao Shi2222a612015-06-06 08:01:38 -0700106 std::string interface;
Junxiao Shi2222a612015-06-06 08:01:38 -0700107 std::string inputFile;
Davide Pesaventoecd44802018-07-23 23:48:10 -0400108 std::string pcapFilter = getDefaultPcapFilter();
109 optional<std::regex> nameFilter;
Davide Pesavento24c08612018-07-26 13:33:24 -0400110 bool wantPromisc = true;
Davide Pesaventob5b8f952018-07-26 14:19:16 -0400111 bool wantTimestamp = true;
Davide Pesavento24c08612018-07-26 13:33:24 -0400112 bool wantVerbose = false;
Junxiao Shi2222a612015-06-06 08:01:38 -0700113
114private:
Davide Pesaventoecd44802018-07-23 23:48:10 -0400115 pcap_t* m_pcap = nullptr;
Vince Lehman277ecf02016-02-10 16:37:48 -0600116
117PUBLIC_WITH_TESTS_ELSE_PRIVATE:
Davide Pesaventoecd44802018-07-23 23:48:10 -0400118 int m_dataLinkType = -1;
Junxiao Shi2222a612015-06-06 08:01:38 -0700119};
120
Junxiao Shi3cd47df2015-06-07 20:58:14 -0700121} // namespace dump
Junxiao Shi2222a612015-06-06 08:01:38 -0700122} // namespace ndn
123
Junxiao Shi3cd47df2015-06-07 20:58:14 -0700124#endif // NDN_TOOLS_DUMP_NDNDUMP_HPP