blob: 1d7d93250a596289d70930eee40defaa36671902 [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/*
3 * Copyright (c) 2014-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 */
Davide Pesavento78de7352018-07-22 00:35:45 -040019/*
Junxiao Shi2222a612015-06-06 08:01:38 -070020 * Copyright (c) 2011-2014, Regents of the University of California,
21 *
22 * This file is part of ndndump, the packet capture and analysis tool for Named Data
23 * Networking (NDN).
24 *
25 * ndndump is free software: you can redistribute it and/or modify it under the terms
26 * of the GNU General Public License as published by the Free Software Foundation,
27 * either version 3 of the License, or (at your option) any later version.
28 *
29 * ndndump is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
30 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
31 * PURPOSE. See the GNU General Public License for more details.
32 *
33 * You should have received a copy of the GNU General Public License along with
34 * ndndump, e.g., in COPYING file. If not, see <http://www.gnu.org/licenses/>.
35 **/
36
Junxiao Shi3cd47df2015-06-07 20:58:14 -070037#ifndef NDN_TOOLS_DUMP_NDNDUMP_HPP
38#define NDN_TOOLS_DUMP_NDNDUMP_HPP
Junxiao Shi2222a612015-06-06 08:01:38 -070039
Vince Lehman277ecf02016-02-10 16:37:48 -060040#include "core/common.hpp"
41
Junxiao Shi2222a612015-06-06 08:01:38 -070042#include <pcap.h>
Davide Pesavento78de7352018-07-22 00:35:45 -040043#include <regex>
Junxiao Shi2222a612015-06-06 08:01:38 -070044
45namespace ndn {
Junxiao Shi3cd47df2015-06-07 20:58:14 -070046namespace dump {
Junxiao Shi2222a612015-06-06 08:01:38 -070047
Davide Pesaventoecd44802018-07-23 23:48:10 -040048class NdnDump : noncopyable
Junxiao Shi2222a612015-06-06 08:01:38 -070049{
50public:
51 class Error : public std::runtime_error
52 {
53 public:
Davide Pesaventoecd44802018-07-23 23:48:10 -040054 using std::runtime_error::runtime_error;
Junxiao Shi2222a612015-06-06 08:01:38 -070055 };
56
Davide Pesaventoecd44802018-07-23 23:48:10 -040057 ~NdnDump();
Junxiao Shi2222a612015-06-06 08:01:38 -070058
59 void
60 run();
61
Vince Lehman277ecf02016-02-10 16:37:48 -060062 void
Davide Pesaventoecd44802018-07-23 23:48:10 -040063 printPacket(const pcap_pkthdr* pkthdr, const uint8_t* payload) const;
Vince Lehman277ecf02016-02-10 16:37:48 -060064
Davide Pesaventoecd44802018-07-23 23:48:10 -040065 static constexpr const char*
66 getDefaultPcapFilter() noexcept
Junxiao Shi2222a612015-06-06 08:01:38 -070067 {
Davide Pesavento4a1b21d2018-07-26 20:54:11 -040068 return "(ether proto 0x8624) or (tcp port 6363) or (udp port 6363) or (udp port 56363)";
Junxiao Shi2222a612015-06-06 08:01:38 -070069 }
70
Davide Pesaventoecd44802018-07-23 23:48:10 -040071private:
Junxiao Shi2222a612015-06-06 08:01:38 -070072 void
Davide Pesaventoecd44802018-07-23 23:48:10 -040073 printTimestamp(std::ostream& os, const timeval& tv) const;
Junxiao Shi2222a612015-06-06 08:01:38 -070074
75 int
Junxiao Shic1c2b832016-07-24 20:45:36 +000076 skipDataLinkHeaderAndGetFrameType(const uint8_t*& payload, ssize_t& payloadSize) const;
Junxiao Shi2222a612015-06-06 08:01:38 -070077
78 int
Davide Pesaventoecd44802018-07-23 23:48:10 -040079 skipAndProcessFrameHeader(int frameType, const uint8_t*& payload, ssize_t& payloadSize,
Junxiao Shic1c2b832016-07-24 20:45:36 +000080 std::ostream& os) const;
Junxiao Shi2222a612015-06-06 08:01:38 -070081
82 bool
Junxiao Shic1c2b832016-07-24 20:45:36 +000083 matchesFilter(const Name& name) const;
Junxiao Shi2222a612015-06-06 08:01:38 -070084
Davide Pesaventoecd44802018-07-23 23:48:10 -040085public: // options
Junxiao Shi2222a612015-06-06 08:01:38 -070086 std::string interface;
Junxiao Shi2222a612015-06-06 08:01:38 -070087 std::string inputFile;
Davide Pesaventoecd44802018-07-23 23:48:10 -040088 std::string pcapFilter = getDefaultPcapFilter();
89 optional<std::regex> nameFilter;
Davide Pesavento24c08612018-07-26 13:33:24 -040090 bool wantPromisc = true;
Davide Pesaventob5b8f952018-07-26 14:19:16 -040091 bool wantTimestamp = true;
Davide Pesavento24c08612018-07-26 13:33:24 -040092 bool wantVerbose = false;
Junxiao Shi2222a612015-06-06 08:01:38 -070093
94private:
Davide Pesaventoecd44802018-07-23 23:48:10 -040095 pcap_t* m_pcap = nullptr;
Vince Lehman277ecf02016-02-10 16:37:48 -060096
97PUBLIC_WITH_TESTS_ELSE_PRIVATE:
Davide Pesaventoecd44802018-07-23 23:48:10 -040098 int m_dataLinkType = -1;
Junxiao Shi2222a612015-06-06 08:01:38 -070099};
100
Junxiao Shi3cd47df2015-06-07 20:58:14 -0700101} // namespace dump
Junxiao Shi2222a612015-06-06 08:01:38 -0700102} // namespace ndn
103
Junxiao Shi3cd47df2015-06-07 20:58:14 -0700104#endif // NDN_TOOLS_DUMP_NDNDUMP_HPP