Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 3 | * Copyright (c) 2014-2016, Regents of the University of California. |
Junxiao Shi | 3cd47df | 2015-06-07 20:58:14 -0700 | [diff] [blame] | 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 Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 20 | * 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 | |
| 37 | #include "ndndump.hpp" |
| 38 | |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 39 | #include "tcpdump/tcpdump-stdinc.h" |
| 40 | |
| 41 | namespace ndn { |
Junxiao Shi | 3cd47df | 2015-06-07 20:58:14 -0700 | [diff] [blame] | 42 | namespace dump { |
| 43 | // namespace is necessary to prevent clashing with system includes |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 44 | |
| 45 | #include "tcpdump/ether.h" |
| 46 | #include "tcpdump/ip.h" |
| 47 | #include "tcpdump/udp.h" |
| 48 | #include "tcpdump/tcp.h" |
| 49 | |
Junxiao Shi | 3cd47df | 2015-06-07 20:58:14 -0700 | [diff] [blame] | 50 | } // namespace dump |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 51 | } // namespace ndn |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 52 | |
| 53 | #include <boost/lexical_cast.hpp> |
| 54 | |
| 55 | #include <iomanip> |
| 56 | |
| 57 | #include <ndn-cxx/interest.hpp> |
| 58 | #include <ndn-cxx/data.hpp> |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 59 | #include <ndn-cxx/lp/nack.hpp> |
| 60 | #include <ndn-cxx/lp/packet.hpp> |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 61 | |
| 62 | namespace ndn { |
Junxiao Shi | 3cd47df | 2015-06-07 20:58:14 -0700 | [diff] [blame] | 63 | namespace dump { |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 64 | |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 65 | const size_t MAX_SNAPLEN = 65535; |
| 66 | |
Junxiao Shi | c1c2b83 | 2016-07-24 20:45:36 +0000 | [diff] [blame] | 67 | Ndndump::Ndndump() |
| 68 | : isVerbose(false) |
| 69 | , pcapProgram("(ether proto 0x8624) || (tcp port 6363) || (udp port 6363)") |
| 70 | // , isSuccinct(false) |
| 71 | // , isMatchInverted(false) |
| 72 | // , shouldPrintStructure(false) |
| 73 | // , isTcpOnly(false) |
| 74 | // , isUdpOnly(false) |
| 75 | { |
| 76 | } |
| 77 | |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 78 | void |
| 79 | Ndndump::run() |
| 80 | { |
| 81 | if (inputFile.empty() && interface.empty()) { |
| 82 | char errbuf[PCAP_ERRBUF_SIZE]; |
| 83 | const char* pcapDevice = pcap_lookupdev(errbuf); |
| 84 | |
Junxiao Shi | c1c2b83 | 2016-07-24 20:45:36 +0000 | [diff] [blame] | 85 | if (pcapDevice == nullptr) { |
Junxiao Shi | c759963 | 2016-07-24 20:46:24 +0000 | [diff] [blame] | 86 | BOOST_THROW_EXCEPTION(Error(errbuf)); |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | interface = pcapDevice; |
| 90 | } |
| 91 | |
| 92 | if (isVerbose) { |
| 93 | if (!interface.empty()) { |
| 94 | std::cerr << "ndndump: listening on " << interface << std::endl; |
| 95 | } |
| 96 | else { |
| 97 | std::cerr << "ndndump: reading from " << inputFile << std::endl; |
| 98 | } |
| 99 | |
| 100 | if (!nameFilter.empty()) { |
| 101 | std::cerr << "ndndump: using name filter " << nameFilter << std::endl; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | if (!interface.empty()) { |
| 106 | char errbuf[PCAP_ERRBUF_SIZE]; |
| 107 | m_pcap = pcap_open_live(interface.c_str(), MAX_SNAPLEN, 0, 1000, errbuf); |
Junxiao Shi | c1c2b83 | 2016-07-24 20:45:36 +0000 | [diff] [blame] | 108 | if (m_pcap == nullptr) { |
Junxiao Shi | c759963 | 2016-07-24 20:46:24 +0000 | [diff] [blame] | 109 | BOOST_THROW_EXCEPTION(Error("Cannot open interface " + interface + " (" + errbuf + ")")); |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 110 | } |
| 111 | } |
| 112 | else { |
| 113 | char errbuf[PCAP_ERRBUF_SIZE]; |
| 114 | m_pcap = pcap_open_offline(inputFile.c_str(), errbuf); |
Junxiao Shi | c1c2b83 | 2016-07-24 20:45:36 +0000 | [diff] [blame] | 115 | if (m_pcap == nullptr) { |
Junxiao Shi | c759963 | 2016-07-24 20:46:24 +0000 | [diff] [blame] | 116 | BOOST_THROW_EXCEPTION(Error("Cannot open file " + inputFile + " for reading (" + errbuf + ")")); |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | |
| 120 | if (!pcapProgram.empty()) { |
| 121 | if (isVerbose) { |
| 122 | std::cerr << "ndndump: pcap_filter = " << pcapProgram << std::endl; |
| 123 | } |
| 124 | |
| 125 | bpf_program program; |
Junxiao Shi | c1c2b83 | 2016-07-24 20:45:36 +0000 | [diff] [blame] | 126 | int res = pcap_compile(m_pcap, &program, pcapProgram.c_str(), 0, PCAP_NETMASK_UNKNOWN); |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 127 | |
Junxiao Shi | c1c2b83 | 2016-07-24 20:45:36 +0000 | [diff] [blame] | 128 | if (res < 0) { |
Junxiao Shi | c759963 | 2016-07-24 20:46:24 +0000 | [diff] [blame] | 129 | BOOST_THROW_EXCEPTION(Error("Cannot parse tcpdump expression '" + pcapProgram + |
| 130 | "' (" + pcap_geterr(m_pcap) + ")")); |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Junxiao Shi | c1c2b83 | 2016-07-24 20:45:36 +0000 | [diff] [blame] | 133 | res = pcap_setfilter(m_pcap, &program); |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 134 | pcap_freecode(&program); |
| 135 | |
Junxiao Shi | c1c2b83 | 2016-07-24 20:45:36 +0000 | [diff] [blame] | 136 | if (res < 0) { |
Junxiao Shi | c759963 | 2016-07-24 20:46:24 +0000 | [diff] [blame] | 137 | BOOST_THROW_EXCEPTION(Error(std::string("pcap_setfilter failed (") + pcap_geterr(m_pcap) + ")")); |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | |
| 141 | m_dataLinkType = pcap_datalink(m_pcap); |
Junxiao Shi | c1c2b83 | 2016-07-24 20:45:36 +0000 | [diff] [blame] | 142 | if (m_dataLinkType != DLT_EN10MB && m_dataLinkType != DLT_PPP) { |
Junxiao Shi | 1c71bcd | 2016-11-24 04:00:42 +0000 | [diff] [blame^] | 143 | BOOST_THROW_EXCEPTION(Error("Unsupported pcap format (" + to_string(m_dataLinkType) + ")")); |
Junxiao Shi | c1c2b83 | 2016-07-24 20:45:36 +0000 | [diff] [blame] | 144 | } |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 145 | |
| 146 | pcap_loop(m_pcap, -1, &Ndndump::onCapturedPacket, reinterpret_cast<uint8_t*>(this)); |
| 147 | } |
| 148 | |
| 149 | |
| 150 | void |
Junxiao Shi | c1c2b83 | 2016-07-24 20:45:36 +0000 | [diff] [blame] | 151 | Ndndump::onCapturedPacket(const pcap_pkthdr* header, const uint8_t* packet) const |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 152 | { |
| 153 | std::ostringstream os; |
| 154 | printInterceptTime(os, header); |
| 155 | |
| 156 | const uint8_t* payload = packet; |
| 157 | ssize_t payloadSize = header->len; |
| 158 | |
| 159 | int frameType = skipDataLinkHeaderAndGetFrameType(payload, payloadSize); |
| 160 | if (frameType < 0) { |
| 161 | std::cerr << "Unknown frame type" << std::endl; |
| 162 | return; |
| 163 | } |
| 164 | |
Junxiao Shi | c1c2b83 | 2016-07-24 20:45:36 +0000 | [diff] [blame] | 165 | int res = skipAndProcessFrameHeader(frameType, payload, payloadSize, os); |
| 166 | if (res < 0) { |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 167 | return; |
| 168 | } |
| 169 | |
| 170 | bool isOk = false; |
| 171 | Block block; |
| 172 | std::tie(isOk, block) = Block::fromBuffer(payload, payloadSize); |
| 173 | if (!isOk) { |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 174 | // if packet is incomplete, we will not be able to process it |
| 175 | if (payloadSize > 0) { |
| 176 | std::cout << os.str() << ", " << "INCOMPLETE-PACKET" << ", size: " << payloadSize << std::endl; |
| 177 | } |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 178 | return; |
| 179 | } |
| 180 | |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 181 | lp::Packet lpPacket; |
| 182 | Block netPacket; |
| 183 | |
| 184 | if (block.type() == lp::tlv::LpPacket) { |
| 185 | lpPacket = lp::Packet(block); |
| 186 | |
| 187 | Buffer::const_iterator begin, end; |
| 188 | |
| 189 | if (lpPacket.has<lp::FragmentField>()) { |
| 190 | std::tie(begin, end) = lpPacket.get<lp::FragmentField>(); |
| 191 | } |
| 192 | else { |
| 193 | std::cout << os.str() << ", " << "NDNLPv2-IDLE" << std::endl; |
| 194 | return; |
| 195 | } |
| 196 | |
| 197 | bool isOk = false; |
| 198 | std::tie(isOk, netPacket) = Block::fromBuffer(&*begin, std::distance(begin, end)); |
| 199 | if (!isOk) { |
| 200 | // if network packet is fragmented, we will not be able to process it |
| 201 | std::cout << os.str() << ", " << "NDNLPv2-FRAGMENT" << std::endl; |
| 202 | return; |
| 203 | } |
| 204 | } |
| 205 | else { |
| 206 | netPacket = block; |
| 207 | } |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 208 | |
| 209 | try { |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 210 | if (netPacket.type() == tlv::Interest) { |
| 211 | Interest interest(netPacket); |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 212 | if (matchesFilter(interest.getName())) { |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 213 | |
| 214 | if (lpPacket.has<lp::NackField>()) { |
| 215 | lp::Nack nack(interest); |
| 216 | nack.setHeader(lpPacket.get<lp::NackField>()); |
| 217 | |
| 218 | std::cout << os.str() << ", " << "NACK: " << nack.getReason() << ", " << interest << std::endl; |
| 219 | } |
| 220 | else { |
| 221 | std::cout << os.str() << ", " << "INTEREST: " << interest << std::endl; |
| 222 | } |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 223 | } |
| 224 | } |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 225 | else if (netPacket.type() == tlv::Data) { |
| 226 | Data data(netPacket); |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 227 | if (matchesFilter(data.getName())) { |
| 228 | std::cout << os.str() << ", " << "DATA: " << data.getName() << std::endl; |
| 229 | } |
| 230 | } |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 231 | else { |
| 232 | std::cout << os.str() << ", " << "UNKNOWN-NETWORK-PACKET" << std::endl; |
| 233 | } |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 234 | } |
Junxiao Shi | c1c2b83 | 2016-07-24 20:45:36 +0000 | [diff] [blame] | 235 | catch (const tlv::Error& e) { |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 236 | std::cerr << e.what() << std::endl; |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | void |
Junxiao Shi | c1c2b83 | 2016-07-24 20:45:36 +0000 | [diff] [blame] | 241 | Ndndump::printInterceptTime(std::ostream& os, const pcap_pkthdr* header) const |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 242 | { |
| 243 | os << header->ts.tv_sec |
| 244 | << "." |
| 245 | << std::setfill('0') << std::setw(6) << header->ts.tv_usec; |
| 246 | |
| 247 | // struct tm* tm; |
| 248 | // if (flags.unit_time) { |
| 249 | // os << (int) header->ts.tv_sec |
| 250 | // << "." |
| 251 | // << setfill('0') << setw(6) << (int)header->ts.tv_usec; |
| 252 | // } else { |
| 253 | // tm = localtime(&(header->ts.tv_sec)); |
| 254 | // os << (int)tm->tm_hour << ":" |
| 255 | // << setfill('0') << setw(2) << (int)tm->tm_min<< ":" |
| 256 | // << setfill('0') << setw(2) << (int)tm->tm_sec<< "." |
| 257 | // << setfill('0') << setw(6) << (int)header->ts.tv_usec; |
| 258 | // } |
| 259 | os << " "; |
| 260 | } |
| 261 | |
| 262 | int |
Junxiao Shi | c1c2b83 | 2016-07-24 20:45:36 +0000 | [diff] [blame] | 263 | Ndndump::skipDataLinkHeaderAndGetFrameType(const uint8_t*& payload, ssize_t& payloadSize) const |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 264 | { |
| 265 | int frameType = 0; |
| 266 | |
| 267 | switch (m_dataLinkType) { |
Junxiao Shi | c1c2b83 | 2016-07-24 20:45:36 +0000 | [diff] [blame] | 268 | case DLT_EN10MB: { // Ethernet frames can have Ethernet or 802.3 encapsulation |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 269 | const ether_header* etherHeader = reinterpret_cast<const ether_header*>(payload); |
| 270 | |
| 271 | if (payloadSize < 0) { |
| 272 | std::cerr << "Invalid pcap Ethernet frame" << std::endl; |
| 273 | return -1; |
| 274 | } |
| 275 | |
| 276 | frameType = ntohs(etherHeader->ether_type); |
| 277 | payloadSize -= ETHER_HDRLEN; |
| 278 | payload += ETHER_HDRLEN; |
| 279 | |
| 280 | break; |
| 281 | } |
Junxiao Shi | c1c2b83 | 2016-07-24 20:45:36 +0000 | [diff] [blame] | 282 | case DLT_PPP: { |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 283 | frameType = *payload; |
Junxiao Shi | c1c2b83 | 2016-07-24 20:45:36 +0000 | [diff] [blame] | 284 | --payloadSize; |
| 285 | ++payload; |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 286 | |
| 287 | if (!(frameType & 1)) { |
| 288 | frameType = (frameType << 8) | *payload; |
Junxiao Shi | c1c2b83 | 2016-07-24 20:45:36 +0000 | [diff] [blame] | 289 | --payloadSize; |
| 290 | ++payload; |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | if (payloadSize < 0) { |
| 294 | std::cerr << "Invalid PPP frame" << std::endl; |
| 295 | return -1; |
| 296 | } |
| 297 | |
| 298 | break; |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | return frameType; |
| 303 | } |
| 304 | |
| 305 | int |
| 306 | Ndndump::skipAndProcessFrameHeader(int frameType, |
| 307 | const uint8_t*& payload, ssize_t& payloadSize, |
Junxiao Shi | c1c2b83 | 2016-07-24 20:45:36 +0000 | [diff] [blame] | 308 | std::ostream& os) const |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 309 | { |
Junxiao Shi | c1c2b83 | 2016-07-24 20:45:36 +0000 | [diff] [blame] | 310 | switch (frameType) { |
| 311 | case 0x0800: // ETHERTYPE_IP |
| 312 | case DLT_EN10MB: { // pcap encapsulation |
| 313 | const ip* ipHeader = reinterpret_cast<const ip*>(payload); |
| 314 | size_t ipHeaderSize = IP_HL(ipHeader) * 4; |
| 315 | if (ipHeaderSize < 20) { |
| 316 | std::cerr << "invalid IP header len " << ipHeaderSize << " bytes" << std::endl; |
| 317 | return -1; |
| 318 | } |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 319 | |
Junxiao Shi | c1c2b83 | 2016-07-24 20:45:36 +0000 | [diff] [blame] | 320 | os << "From: " << inet_ntoa(ipHeader->ip_src) << ", "; |
| 321 | os << "To: " << inet_ntoa(ipHeader->ip_dst); |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 322 | |
Junxiao Shi | c1c2b83 | 2016-07-24 20:45:36 +0000 | [diff] [blame] | 323 | payloadSize -= ipHeaderSize; |
| 324 | payload += ipHeaderSize; |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 325 | |
Junxiao Shi | c1c2b83 | 2016-07-24 20:45:36 +0000 | [diff] [blame] | 326 | if (payloadSize < 0) { |
| 327 | std::cerr << "Invalid pcap IP packet" << std::endl; |
| 328 | return -1; |
| 329 | } |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 330 | |
Junxiao Shi | c1c2b83 | 2016-07-24 20:45:36 +0000 | [diff] [blame] | 331 | switch (ipHeader->ip_p) { |
| 332 | case IPPROTO_UDP: { |
| 333 | // if (!flags.udp) |
| 334 | // return -1; |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 335 | |
Junxiao Shi | c1c2b83 | 2016-07-24 20:45:36 +0000 | [diff] [blame] | 336 | payloadSize -= sizeof(udphdr); |
| 337 | payload += sizeof(udphdr); |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 338 | |
Junxiao Shi | c1c2b83 | 2016-07-24 20:45:36 +0000 | [diff] [blame] | 339 | if (payloadSize < 0) { |
| 340 | std::cerr << "Invalid pcap UDP/IP packet" << std::endl; |
| 341 | return -1; |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 342 | } |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 343 | |
Junxiao Shi | c1c2b83 | 2016-07-24 20:45:36 +0000 | [diff] [blame] | 344 | os << ", Tunnel Type: UDP"; |
| 345 | break; |
| 346 | } |
| 347 | case IPPROTO_TCP: { |
| 348 | // if (!flags.tcp) |
| 349 | // return -1; |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 350 | |
Junxiao Shi | c1c2b83 | 2016-07-24 20:45:36 +0000 | [diff] [blame] | 351 | const tcphdr* tcpHeader = reinterpret_cast<const tcphdr*>(payload); |
| 352 | size_t tcpHeaderSize = TH_OFF(tcpHeader) * 4; |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 353 | |
Junxiao Shi | c1c2b83 | 2016-07-24 20:45:36 +0000 | [diff] [blame] | 354 | if (tcpHeaderSize < 20) { |
| 355 | std::cerr << "Invalid TCP Header len: " << tcpHeaderSize <<" bytes" << std::endl; |
| 356 | return -1; |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 357 | } |
Junxiao Shi | c1c2b83 | 2016-07-24 20:45:36 +0000 | [diff] [blame] | 358 | |
| 359 | payloadSize -= tcpHeaderSize; |
| 360 | payload += tcpHeaderSize; |
| 361 | |
| 362 | if (payloadSize < 0) { |
| 363 | std::cerr << "Invalid pcap TCP/IP packet" << std::endl; |
| 364 | return -1; |
| 365 | } |
| 366 | |
| 367 | os << ", Tunnel Type: TCP"; |
| 368 | break; |
| 369 | } |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 370 | default: |
| 371 | return -1; |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 372 | } |
Junxiao Shi | c1c2b83 | 2016-07-24 20:45:36 +0000 | [diff] [blame] | 373 | |
| 374 | break; |
| 375 | } |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 376 | case /*ETHERTYPE_NDN*/0x7777: |
| 377 | os << "Tunnel Type: EthernetFrame"; |
| 378 | break; |
| 379 | case /*ETHERTYPE_NDNLP*/0x8624: |
| 380 | os << "Tunnel Type: EthernetFrame"; |
| 381 | break; |
| 382 | case 0x0077: // pcap |
| 383 | os << "Tunnel Type: PPP"; |
| 384 | payloadSize -= 2; |
| 385 | payload += 2; |
| 386 | break; |
Junxiao Shi | c1c2b83 | 2016-07-24 20:45:36 +0000 | [diff] [blame] | 387 | default: // do nothing if it is not a recognized type of a packet |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 388 | return -1; |
Junxiao Shi | c1c2b83 | 2016-07-24 20:45:36 +0000 | [diff] [blame] | 389 | } |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 390 | |
| 391 | return 0; |
| 392 | } |
| 393 | |
Junxiao Shi | c1c2b83 | 2016-07-24 20:45:36 +0000 | [diff] [blame] | 394 | bool |
| 395 | Ndndump::matchesFilter(const Name& name) const |
| 396 | { |
| 397 | if (nameFilter.empty()) |
| 398 | return true; |
| 399 | |
| 400 | /// \todo Switch to NDN regular expressions |
| 401 | |
| 402 | return boost::regex_match(name.toUri(), nameFilter); |
| 403 | } |
| 404 | |
Junxiao Shi | 3cd47df | 2015-06-07 20:58:14 -0700 | [diff] [blame] | 405 | } // namespace dump |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 406 | } // namespace ndn |