blob: b46c1097e2c20f01bdd6619ca420efe46d8274a0 [file] [log] [blame]
Junxiao Shi2222a612015-06-06 08:01:38 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi3cd47df2015-06-07 20:58:14 -07003 * Copyright (c) 2014-2015, Regents of the University of California.
4 *
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 */
19/**
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
40#include <pcap.h>
41
42#include <ndn-cxx/name.hpp>
43#include <boost/regex.hpp>
44
45namespace ndn {
Junxiao Shi3cd47df2015-06-07 20:58:14 -070046namespace dump {
Junxiao Shi2222a612015-06-06 08:01:38 -070047
48class Ndndump : noncopyable
49{
50public:
51 class Error : public std::runtime_error
52 {
53 public:
54 explicit
55 Error(const std::string& what)
56 : std::runtime_error(what)
57 {
58 }
59 };
60
61 Ndndump()
62 : isVerbose(false)
63 , pcapProgram("(ether proto 0x8624) || (tcp port 6363) || (udp port 6363)")
64 // , isSuccinct(false)
65 // , isMatchInverted(false)
66 // , shouldPrintStructure(false)
67 // , isTcpOnly(false)
68 // , isUdpOnly(false)
69 {
70 }
71
72 void
73 run();
74
75private:
76 static void
77 onCapturedPacket(uint8_t* userData, const struct pcap_pkthdr* header, const uint8_t* packet)
78 {
79 reinterpret_cast<Ndndump*>(userData)->onCapturedPacket(header, packet);
80 }
81
82 void
83 onCapturedPacket(const struct pcap_pkthdr* header, const uint8_t* packet);
84
85 void
86 printInterceptTime(std::ostream& os, const struct pcap_pkthdr* header);
87
88 int
89 skipDataLinkHeaderAndGetFrameType(const uint8_t*& payload, ssize_t& payloadSize);
90
91 int
92 skipAndProcessFrameHeader(int frameType,
93 const uint8_t*& payload, ssize_t& payloadSize,
94 std::ostream& os);
95
96 bool
97 matchesFilter(const Name& name)
98 {
99 if (nameFilter.empty())
100 return true;
101
102 /// \todo Switch to NDN regular expressions
103
104 return boost::regex_match(name.toUri(), nameFilter);
105 }
106
107public:
108 bool isVerbose;
109 // bool isSuccinct;
110 // bool isMatchInverted;
111 // bool shouldPrintStructure;
112 // bool isTcpOnly;
113 // bool isUdpOnly;
114
115 std::string pcapProgram;
116 std::string interface;
117 boost::regex nameFilter;
118 std::string inputFile;
119 // std::string outputFile;
120
121private:
122 pcap_t* m_pcap;
123 int m_dataLinkType;
124};
125
126
Junxiao Shi3cd47df2015-06-07 20:58:14 -0700127} // namespace dump
Junxiao Shi2222a612015-06-06 08:01:38 -0700128} // namespace ndn
129
Junxiao Shi3cd47df2015-06-07 20:58:14 -0700130#endif // NDN_TOOLS_DUMP_NDNDUMP_HPP