blob: bf1ff55b356484007ee4d1da214a7a79ec99f899 [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#include <ndn-cxx/name.hpp>
Junxiao Shi2222a612015-06-06 08:01:38 -070045
46namespace ndn {
Junxiao Shi3cd47df2015-06-07 20:58:14 -070047namespace dump {
Junxiao Shi2222a612015-06-06 08:01:38 -070048
49class Ndndump : noncopyable
50{
51public:
52 class Error : public std::runtime_error
53 {
54 public:
55 explicit
56 Error(const std::string& what)
57 : std::runtime_error(what)
58 {
59 }
60 };
61
Junxiao Shic1c2b832016-07-24 20:45:36 +000062 Ndndump();
Junxiao Shi2222a612015-06-06 08:01:38 -070063
64 void
65 run();
66
Vince Lehman277ecf02016-02-10 16:37:48 -060067PUBLIC_WITH_TESTS_ELSE_PRIVATE:
68 void
Junxiao Shic1c2b832016-07-24 20:45:36 +000069 onCapturedPacket(const pcap_pkthdr* header, const uint8_t* packet) const;
Vince Lehman277ecf02016-02-10 16:37:48 -060070
Junxiao Shi2222a612015-06-06 08:01:38 -070071private:
72 static void
Vince Lehman277ecf02016-02-10 16:37:48 -060073 onCapturedPacket(uint8_t* userData, const pcap_pkthdr* header, const uint8_t* packet)
Junxiao Shi2222a612015-06-06 08:01:38 -070074 {
Junxiao Shic1c2b832016-07-24 20:45:36 +000075 reinterpret_cast<const Ndndump*>(userData)->onCapturedPacket(header, packet);
Junxiao Shi2222a612015-06-06 08:01:38 -070076 }
77
78 void
Junxiao Shic1c2b832016-07-24 20:45:36 +000079 printInterceptTime(std::ostream& os, const pcap_pkthdr* header) const;
Junxiao Shi2222a612015-06-06 08:01:38 -070080
81 int
Junxiao Shic1c2b832016-07-24 20:45:36 +000082 skipDataLinkHeaderAndGetFrameType(const uint8_t*& payload, ssize_t& payloadSize) const;
Junxiao Shi2222a612015-06-06 08:01:38 -070083
84 int
85 skipAndProcessFrameHeader(int frameType,
86 const uint8_t*& payload, ssize_t& payloadSize,
Junxiao Shic1c2b832016-07-24 20:45:36 +000087 std::ostream& os) const;
Junxiao Shi2222a612015-06-06 08:01:38 -070088
89 bool
Junxiao Shic1c2b832016-07-24 20:45:36 +000090 matchesFilter(const Name& name) const;
Junxiao Shi2222a612015-06-06 08:01:38 -070091
92public:
93 bool isVerbose;
94 // bool isSuccinct;
95 // bool isMatchInverted;
96 // bool shouldPrintStructure;
97 // bool isTcpOnly;
98 // bool isUdpOnly;
99
100 std::string pcapProgram;
101 std::string interface;
Davide Pesavento78de7352018-07-22 00:35:45 -0400102 optional<std::regex> nameFilter;
Junxiao Shi2222a612015-06-06 08:01:38 -0700103 std::string inputFile;
104 // std::string outputFile;
105
106private:
107 pcap_t* m_pcap;
Vince Lehman277ecf02016-02-10 16:37:48 -0600108
109PUBLIC_WITH_TESTS_ELSE_PRIVATE:
Junxiao Shi2222a612015-06-06 08:01:38 -0700110 int m_dataLinkType;
111};
112
Junxiao Shi3cd47df2015-06-07 20:58:14 -0700113} // namespace dump
Junxiao Shi2222a612015-06-06 08:01:38 -0700114} // namespace ndn
115
Junxiao Shi3cd47df2015-06-07 20:58:14 -0700116#endif // NDN_TOOLS_DUMP_NDNDUMP_HPP