blob: 0198026ed068256310bf5b9d9caf00a596e6607a [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 Pesaventob6b176c2018-07-28 22:05:16 -04003 * Copyright (c) 2011-2018, 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
28namespace ndn {
Junxiao Shi3cd47df2015-06-07 20:58:14 -070029namespace dump {
Junxiao Shi2222a612015-06-06 08:01:38 -070030
Davide Pesaventob6b176c2018-07-28 22:05:16 -040031class OutputFormatter;
32
Davide Pesaventoecd44802018-07-23 23:48:10 -040033class NdnDump : noncopyable
Junxiao Shi2222a612015-06-06 08:01:38 -070034{
35public:
36 class Error : public std::runtime_error
37 {
38 public:
Davide Pesaventoecd44802018-07-23 23:48:10 -040039 using std::runtime_error::runtime_error;
Junxiao Shi2222a612015-06-06 08:01:38 -070040 };
41
Davide Pesaventoecd44802018-07-23 23:48:10 -040042 ~NdnDump();
Junxiao Shi2222a612015-06-06 08:01:38 -070043
44 void
45 run();
46
Vince Lehman277ecf02016-02-10 16:37:48 -060047 void
Davide Pesaventoecd44802018-07-23 23:48:10 -040048 printPacket(const pcap_pkthdr* pkthdr, const uint8_t* payload) const;
Vince Lehman277ecf02016-02-10 16:37:48 -060049
Davide Pesaventoecd44802018-07-23 23:48:10 -040050 static constexpr const char*
51 getDefaultPcapFilter() noexcept
Junxiao Shi2222a612015-06-06 08:01:38 -070052 {
Davide Pesavento4a1b21d2018-07-26 20:54:11 -040053 return "(ether proto 0x8624) or (tcp port 6363) or (udp port 6363) or (udp port 56363)";
Junxiao Shi2222a612015-06-06 08:01:38 -070054 }
55
Davide Pesaventoecd44802018-07-23 23:48:10 -040056private:
Junxiao Shi2222a612015-06-06 08:01:38 -070057 void
Davide Pesaventoecd44802018-07-23 23:48:10 -040058 printTimestamp(std::ostream& os, const timeval& tv) const;
Junxiao Shi2222a612015-06-06 08:01:38 -070059
Davide Pesaventob6b176c2018-07-28 22:05:16 -040060 bool
61 dispatchByEtherType(OutputFormatter& out, const uint8_t* pkt, size_t len, uint16_t etherType) const;
Junxiao Shi2222a612015-06-06 08:01:38 -070062
Davide Pesaventob6b176c2018-07-28 22:05:16 -040063 bool
64 dispatchByIpProto(OutputFormatter& out, const uint8_t* pkt, size_t len, uint8_t ipProto) const;
65
66 bool
67 printEther(OutputFormatter& out, const uint8_t* pkt, size_t len) const;
68
69 bool
70 printLinuxSll(OutputFormatter& out, const uint8_t* pkt, size_t len) const;
71
72 bool
73 printPpp(OutputFormatter& out, const uint8_t* pkt, size_t len) const;
74
75 bool
76 printIp4(OutputFormatter& out, const uint8_t* pkt, size_t len) const;
77
78 bool
Davide Pesavento0ca7e692018-08-05 02:21:06 -040079 printIp6(OutputFormatter& out, const uint8_t* pkt, size_t len) const;
80
81 bool
Davide Pesaventob6b176c2018-07-28 22:05:16 -040082 printTcp(OutputFormatter& out, const uint8_t* pkt, size_t len) const;
83
84 bool
85 printUdp(OutputFormatter& out, const uint8_t* pkt, size_t len) const;
86
87 bool
88 printNdn(OutputFormatter& out, const uint8_t* pkt, size_t len) const;
Junxiao Shi2222a612015-06-06 08:01:38 -070089
90 bool
Junxiao Shic1c2b832016-07-24 20:45:36 +000091 matchesFilter(const Name& name) const;
Junxiao Shi2222a612015-06-06 08:01:38 -070092
Davide Pesaventoecd44802018-07-23 23:48:10 -040093public: // options
Junxiao Shi2222a612015-06-06 08:01:38 -070094 std::string interface;
Junxiao Shi2222a612015-06-06 08:01:38 -070095 std::string inputFile;
Davide Pesaventoecd44802018-07-23 23:48:10 -040096 std::string pcapFilter = getDefaultPcapFilter();
97 optional<std::regex> nameFilter;
Davide Pesavento24c08612018-07-26 13:33:24 -040098 bool wantPromisc = true;
Davide Pesaventob5b8f952018-07-26 14:19:16 -040099 bool wantTimestamp = true;
Davide Pesavento24c08612018-07-26 13:33:24 -0400100 bool wantVerbose = false;
Junxiao Shi2222a612015-06-06 08:01:38 -0700101
102private:
Davide Pesaventoecd44802018-07-23 23:48:10 -0400103 pcap_t* m_pcap = nullptr;
Vince Lehman277ecf02016-02-10 16:37:48 -0600104
105PUBLIC_WITH_TESTS_ELSE_PRIVATE:
Davide Pesaventoecd44802018-07-23 23:48:10 -0400106 int m_dataLinkType = -1;
Junxiao Shi2222a612015-06-06 08:01:38 -0700107};
108
Junxiao Shi3cd47df2015-06-07 20:58:14 -0700109} // namespace dump
Junxiao Shi2222a612015-06-06 08:01:38 -0700110} // namespace ndn
111
Junxiao Shi3cd47df2015-06-07 20:58:14 -0700112#endif // NDN_TOOLS_DUMP_NDNDUMP_HPP