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