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 | |
Davide Pesavento | 051db00 | 2018-07-22 18:56:16 -0400 | [diff] [blame] | 39 | #include <net/ethernet.h> |
| 40 | #include <netinet/ip.h> |
| 41 | #include <netinet/tcp.h> |
| 42 | #include <netinet/udp.h> |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 43 | |
Junxiao Shi | 022bddf | 2016-11-24 23:15:20 +0000 | [diff] [blame] | 44 | #include <pcap/sll.h> |
| 45 | |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 46 | #include <iomanip> |
Davide Pesavento | c070270 | 2017-08-24 22:04:00 -0400 | [diff] [blame] | 47 | #include <sstream> |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 48 | |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 49 | #include <ndn-cxx/lp/nack.hpp> |
| 50 | #include <ndn-cxx/lp/packet.hpp> |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 51 | #include <ndn-cxx/net/ethernet.hpp> |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 52 | |
| 53 | namespace ndn { |
Junxiao Shi | 3cd47df | 2015-06-07 20:58:14 -0700 | [diff] [blame] | 54 | namespace dump { |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 55 | |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 56 | NdnDump::~NdnDump() |
Junxiao Shi | c1c2b83 | 2016-07-24 20:45:36 +0000 | [diff] [blame] | 57 | { |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 58 | if (m_pcap) |
| 59 | pcap_close(m_pcap); |
| 60 | } |
| 61 | |
| 62 | static void |
| 63 | pcapCallback(uint8_t* user, const pcap_pkthdr* pkthdr, const uint8_t* payload) |
| 64 | { |
| 65 | reinterpret_cast<const NdnDump*>(user)->printPacket(pkthdr, payload); |
Junxiao Shi | c1c2b83 | 2016-07-24 20:45:36 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 68 | void |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 69 | NdnDump::run() |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 70 | { |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 71 | char errbuf[PCAP_ERRBUF_SIZE]; |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 72 | |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 73 | if (inputFile.empty() && interface.empty()) { |
| 74 | const char* defaultDevice = pcap_lookupdev(errbuf); |
| 75 | |
| 76 | if (defaultDevice == nullptr) { |
Junxiao Shi | c759963 | 2016-07-24 20:46:24 +0000 | [diff] [blame] | 77 | BOOST_THROW_EXCEPTION(Error(errbuf)); |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 80 | interface = defaultDevice; |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 81 | } |
| 82 | |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 83 | std::string action; |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 84 | if (!interface.empty()) { |
Davide Pesavento | 24c0861 | 2018-07-26 13:33:24 -0400 | [diff] [blame] | 85 | m_pcap = pcap_open_live(interface.data(), 65535, wantPromisc, 1000, errbuf); |
Junxiao Shi | c1c2b83 | 2016-07-24 20:45:36 +0000 | [diff] [blame] | 86 | if (m_pcap == nullptr) { |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 87 | BOOST_THROW_EXCEPTION(Error("Cannot open interface " + interface + ": " + errbuf)); |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 88 | } |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 89 | action = "listening on " + interface; |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 90 | } |
| 91 | else { |
Davide Pesavento | 78de735 | 2018-07-22 00:35:45 -0400 | [diff] [blame] | 92 | m_pcap = pcap_open_offline(inputFile.data(), errbuf); |
Junxiao Shi | c1c2b83 | 2016-07-24 20:45:36 +0000 | [diff] [blame] | 93 | if (m_pcap == nullptr) { |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 94 | BOOST_THROW_EXCEPTION(Error("Cannot open file '" + inputFile + "' for reading: " + errbuf)); |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 95 | } |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 96 | action = "reading from file " + inputFile; |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 99 | m_dataLinkType = pcap_datalink(m_pcap); |
| 100 | const char* dltName = pcap_datalink_val_to_name(m_dataLinkType); |
| 101 | const char* dltDesc = pcap_datalink_val_to_description(m_dataLinkType); |
| 102 | std::string formattedDlt = dltName ? dltName : to_string(m_dataLinkType); |
| 103 | if (dltDesc) { |
| 104 | formattedDlt += " ("s + dltDesc + ")"; |
| 105 | } |
| 106 | |
| 107 | std::cerr << "ndndump: " << action << ", link-type " << formattedDlt << std::endl; |
| 108 | |
| 109 | switch (m_dataLinkType) { |
| 110 | case DLT_EN10MB: |
| 111 | case DLT_LINUX_SLL: |
| 112 | case DLT_PPP: |
| 113 | // we know how to handle these |
| 114 | break; |
| 115 | default: |
| 116 | BOOST_THROW_EXCEPTION(Error("Unsupported link-layer header type " + formattedDlt)); |
| 117 | } |
| 118 | |
| 119 | if (!pcapFilter.empty()) { |
Davide Pesavento | 24c0861 | 2018-07-26 13:33:24 -0400 | [diff] [blame] | 120 | if (wantVerbose) { |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 121 | std::cerr << "ndndump: using pcap filter: " << pcapFilter << std::endl; |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | bpf_program program; |
Davide Pesavento | fb42a3d | 2018-07-26 12:33:14 -0400 | [diff] [blame] | 125 | int res = pcap_compile(m_pcap, &program, pcapFilter.data(), 1, PCAP_NETMASK_UNKNOWN); |
Junxiao Shi | c1c2b83 | 2016-07-24 20:45:36 +0000 | [diff] [blame] | 126 | if (res < 0) { |
Davide Pesavento | fb42a3d | 2018-07-26 12:33:14 -0400 | [diff] [blame] | 127 | BOOST_THROW_EXCEPTION(Error("Cannot compile pcap filter '" + pcapFilter + "': " + pcap_geterr(m_pcap))); |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Junxiao Shi | c1c2b83 | 2016-07-24 20:45:36 +0000 | [diff] [blame] | 130 | res = pcap_setfilter(m_pcap, &program); |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 131 | pcap_freecode(&program); |
Junxiao Shi | c1c2b83 | 2016-07-24 20:45:36 +0000 | [diff] [blame] | 132 | if (res < 0) { |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 133 | BOOST_THROW_EXCEPTION(Error("Cannot set pcap filter: "s + pcap_geterr(m_pcap))); |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 134 | } |
| 135 | } |
| 136 | |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 137 | if (pcap_loop(m_pcap, -1, &pcapCallback, reinterpret_cast<uint8_t*>(this)) < 0) { |
| 138 | BOOST_THROW_EXCEPTION(Error("pcap_loop: "s + pcap_geterr(m_pcap))); |
Junxiao Shi | c1c2b83 | 2016-07-24 20:45:36 +0000 | [diff] [blame] | 139 | } |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 140 | } |
| 141 | |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 142 | void |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 143 | NdnDump::printPacket(const pcap_pkthdr* pkthdr, const uint8_t* payload) const |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 144 | { |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 145 | // sanity checks |
| 146 | if (pkthdr->caplen == 0) { |
| 147 | std::cout << "[Invalid header: caplen=0]" << std::endl; |
| 148 | return; |
| 149 | } |
| 150 | if (pkthdr->len == 0) { |
| 151 | std::cout << "[Invalid header: len=0]" << std::endl; |
| 152 | return; |
| 153 | } |
| 154 | else if (pkthdr->len < pkthdr->caplen) { |
| 155 | std::cout << "[Invalid header: len(" << pkthdr->len |
| 156 | << ") < caplen(" << pkthdr->caplen << ")]" << std::endl; |
| 157 | return; |
| 158 | } |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 159 | |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 160 | std::ostringstream os; |
Davide Pesavento | b5b8f95 | 2018-07-26 14:19:16 -0400 | [diff] [blame] | 161 | if (wantTimestamp) { |
| 162 | printTimestamp(os, pkthdr->ts); |
| 163 | } |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 164 | |
| 165 | ssize_t payloadSize = pkthdr->len; |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 166 | |
| 167 | int frameType = skipDataLinkHeaderAndGetFrameType(payload, payloadSize); |
| 168 | if (frameType < 0) { |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 169 | return; |
| 170 | } |
| 171 | |
Junxiao Shi | c1c2b83 | 2016-07-24 20:45:36 +0000 | [diff] [blame] | 172 | int res = skipAndProcessFrameHeader(frameType, payload, payloadSize, os); |
| 173 | if (res < 0) { |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 174 | return; |
| 175 | } |
| 176 | |
| 177 | bool isOk = false; |
| 178 | Block block; |
| 179 | std::tie(isOk, block) = Block::fromBuffer(payload, payloadSize); |
| 180 | if (!isOk) { |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 181 | // if packet is incomplete, we will not be able to process it |
| 182 | if (payloadSize > 0) { |
| 183 | std::cout << os.str() << ", " << "INCOMPLETE-PACKET" << ", size: " << payloadSize << std::endl; |
| 184 | } |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 185 | return; |
| 186 | } |
| 187 | |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 188 | lp::Packet lpPacket; |
| 189 | Block netPacket; |
| 190 | |
| 191 | if (block.type() == lp::tlv::LpPacket) { |
Davide Pesavento | d1b1bf6 | 2018-07-26 18:05:08 -0400 | [diff] [blame] | 192 | try { |
| 193 | lpPacket.wireDecode(block); |
| 194 | } |
| 195 | catch (const tlv::Error& e) { |
| 196 | std::cout << os.str() << ", " << "INVALID-NDNLPv2-PACKET: " << e.what() << std::endl; |
| 197 | return; |
| 198 | } |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 199 | |
| 200 | Buffer::const_iterator begin, end; |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 201 | if (lpPacket.has<lp::FragmentField>()) { |
| 202 | std::tie(begin, end) = lpPacket.get<lp::FragmentField>(); |
| 203 | } |
| 204 | else { |
| 205 | std::cout << os.str() << ", " << "NDNLPv2-IDLE" << std::endl; |
| 206 | return; |
| 207 | } |
| 208 | |
| 209 | bool isOk = false; |
| 210 | std::tie(isOk, netPacket) = Block::fromBuffer(&*begin, std::distance(begin, end)); |
| 211 | if (!isOk) { |
| 212 | // if network packet is fragmented, we will not be able to process it |
| 213 | std::cout << os.str() << ", " << "NDNLPv2-FRAGMENT" << std::endl; |
| 214 | return; |
| 215 | } |
| 216 | } |
| 217 | else { |
| 218 | netPacket = block; |
| 219 | } |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 220 | |
| 221 | try { |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 222 | if (netPacket.type() == tlv::Interest) { |
| 223 | Interest interest(netPacket); |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 224 | if (matchesFilter(interest.getName())) { |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 225 | if (lpPacket.has<lp::NackField>()) { |
| 226 | lp::Nack nack(interest); |
| 227 | nack.setHeader(lpPacket.get<lp::NackField>()); |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 228 | std::cout << os.str() << ", " << "NACK: " << nack.getReason() << ", " << interest << std::endl; |
| 229 | } |
| 230 | else { |
| 231 | std::cout << os.str() << ", " << "INTEREST: " << interest << std::endl; |
| 232 | } |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 233 | } |
| 234 | } |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 235 | else if (netPacket.type() == tlv::Data) { |
| 236 | Data data(netPacket); |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 237 | if (matchesFilter(data.getName())) { |
| 238 | std::cout << os.str() << ", " << "DATA: " << data.getName() << std::endl; |
| 239 | } |
| 240 | } |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 241 | else { |
| 242 | std::cout << os.str() << ", " << "UNKNOWN-NETWORK-PACKET" << std::endl; |
| 243 | } |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 244 | } |
Junxiao Shi | c1c2b83 | 2016-07-24 20:45:36 +0000 | [diff] [blame] | 245 | catch (const tlv::Error& e) { |
Davide Pesavento | d1b1bf6 | 2018-07-26 18:05:08 -0400 | [diff] [blame] | 246 | std::cout << os.str() << ", " << "INVALID-PACKET: " << e.what() << std::endl; |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 247 | } |
| 248 | } |
| 249 | |
| 250 | void |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 251 | NdnDump::printTimestamp(std::ostream& os, const timeval& tv) const |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 252 | { |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 253 | /// \todo Add more timestamp formats (time since previous packet, time since first packet, ...) |
| 254 | os << tv.tv_sec |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 255 | << "." |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 256 | << std::setfill('0') << std::setw(6) << tv.tv_usec |
| 257 | << " "; |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | int |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 261 | NdnDump::skipDataLinkHeaderAndGetFrameType(const uint8_t*& payload, ssize_t& payloadSize) const |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 262 | { |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 263 | int frameType = -1; |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 264 | |
| 265 | switch (m_dataLinkType) { |
Junxiao Shi | c1c2b83 | 2016-07-24 20:45:36 +0000 | [diff] [blame] | 266 | case DLT_EN10MB: { // Ethernet frames can have Ethernet or 802.3 encapsulation |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 267 | const ether_header* eh = reinterpret_cast<const ether_header*>(payload); |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 268 | |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 269 | if (payloadSize < ETHER_HDR_LEN) { |
| 270 | std::cerr << "Invalid Ethernet frame" << std::endl; |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 271 | return -1; |
| 272 | } |
| 273 | |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 274 | frameType = ntohs(eh->ether_type); |
Davide Pesavento | 051db00 | 2018-07-22 18:56:16 -0400 | [diff] [blame] | 275 | payloadSize -= ETHER_HDR_LEN; |
| 276 | payload += ETHER_HDR_LEN; |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 277 | |
| 278 | break; |
| 279 | } |
Junxiao Shi | c1c2b83 | 2016-07-24 20:45:36 +0000 | [diff] [blame] | 280 | case DLT_PPP: { |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 281 | frameType = *payload; |
Junxiao Shi | c1c2b83 | 2016-07-24 20:45:36 +0000 | [diff] [blame] | 282 | --payloadSize; |
| 283 | ++payload; |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 284 | |
| 285 | if (!(frameType & 1)) { |
| 286 | frameType = (frameType << 8) | *payload; |
Junxiao Shi | c1c2b83 | 2016-07-24 20:45:36 +0000 | [diff] [blame] | 287 | --payloadSize; |
| 288 | ++payload; |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 289 | } |
| 290 | |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 291 | if (payloadSize < 4) { // PPP_HDRLEN in linux/ppp_defs.h |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 292 | std::cerr << "Invalid PPP frame" << std::endl; |
| 293 | return -1; |
| 294 | } |
| 295 | |
| 296 | break; |
| 297 | } |
Junxiao Shi | 022bddf | 2016-11-24 23:15:20 +0000 | [diff] [blame] | 298 | case DLT_LINUX_SLL: { |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 299 | const sll_header* sll = reinterpret_cast<const sll_header*>(payload); |
Junxiao Shi | 022bddf | 2016-11-24 23:15:20 +0000 | [diff] [blame] | 300 | |
| 301 | if (payloadSize < SLL_HDR_LEN) { |
| 302 | std::cerr << "Invalid LINUX_SLL frame" << std::endl; |
| 303 | return -1; |
| 304 | } |
| 305 | |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 306 | frameType = ntohs(sll->sll_protocol); |
Junxiao Shi | 022bddf | 2016-11-24 23:15:20 +0000 | [diff] [blame] | 307 | payloadSize -= SLL_HDR_LEN; |
| 308 | payload += SLL_HDR_LEN; |
| 309 | |
| 310 | break; |
| 311 | } |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 312 | default: |
| 313 | std::cerr << "Unknown frame type" << std::endl; |
| 314 | break; |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | return frameType; |
| 318 | } |
| 319 | |
| 320 | int |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 321 | NdnDump::skipAndProcessFrameHeader(int frameType, 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) { |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 325 | case ETHERTYPE_IP: |
Junxiao Shi | c1c2b83 | 2016-07-24 20:45:36 +0000 | [diff] [blame] | 326 | case DLT_EN10MB: { // pcap encapsulation |
| 327 | const ip* ipHeader = reinterpret_cast<const ip*>(payload); |
Davide Pesavento | 051db00 | 2018-07-22 18:56:16 -0400 | [diff] [blame] | 328 | size_t ipHeaderSize = ipHeader->ip_hl * 4; |
Junxiao Shi | c1c2b83 | 2016-07-24 20:45:36 +0000 | [diff] [blame] | 329 | if (ipHeaderSize < 20) { |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 330 | std::cerr << "Invalid IPv4 header len: " << ipHeaderSize << " bytes" << std::endl; |
Junxiao Shi | c1c2b83 | 2016-07-24 20:45:36 +0000 | [diff] [blame] | 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) { |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 341 | std::cerr << "Invalid IPv4 packet" << std::endl; |
Junxiao Shi | c1c2b83 | 2016-07-24 20:45:36 +0000 | [diff] [blame] | 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: { |
Junxiao Shi | c1c2b83 | 2016-07-24 20:45:36 +0000 | [diff] [blame] | 347 | payloadSize -= sizeof(udphdr); |
| 348 | payload += sizeof(udphdr); |
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 | if (payloadSize < 0) { |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 351 | std::cerr << "Invalid UDP/IP packet" << std::endl; |
Junxiao Shi | c1c2b83 | 2016-07-24 20:45:36 +0000 | [diff] [blame] | 352 | return -1; |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 353 | } |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 354 | |
Junxiao Shi | c1c2b83 | 2016-07-24 20:45:36 +0000 | [diff] [blame] | 355 | os << ", Tunnel Type: UDP"; |
| 356 | break; |
| 357 | } |
| 358 | case IPPROTO_TCP: { |
Junxiao Shi | c1c2b83 | 2016-07-24 20:45:36 +0000 | [diff] [blame] | 359 | const tcphdr* tcpHeader = reinterpret_cast<const tcphdr*>(payload); |
Davide Pesavento | 051db00 | 2018-07-22 18:56:16 -0400 | [diff] [blame] | 360 | size_t tcpHeaderSize = tcpHeader->th_off * 4; |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 361 | |
Junxiao Shi | c1c2b83 | 2016-07-24 20:45:36 +0000 | [diff] [blame] | 362 | if (tcpHeaderSize < 20) { |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 363 | std::cerr << "Invalid TCP header len: " << tcpHeaderSize << " bytes" << std::endl; |
Junxiao Shi | c1c2b83 | 2016-07-24 20:45:36 +0000 | [diff] [blame] | 364 | return -1; |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 365 | } |
Junxiao Shi | c1c2b83 | 2016-07-24 20:45:36 +0000 | [diff] [blame] | 366 | |
| 367 | payloadSize -= tcpHeaderSize; |
| 368 | payload += tcpHeaderSize; |
| 369 | |
| 370 | if (payloadSize < 0) { |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 371 | std::cerr << "Invalid TCP/IP packet" << std::endl; |
Junxiao Shi | c1c2b83 | 2016-07-24 20:45:36 +0000 | [diff] [blame] | 372 | return -1; |
| 373 | } |
| 374 | |
| 375 | os << ", Tunnel Type: TCP"; |
| 376 | break; |
| 377 | } |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 378 | default: |
| 379 | return -1; |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 380 | } |
Junxiao Shi | c1c2b83 | 2016-07-24 20:45:36 +0000 | [diff] [blame] | 381 | break; |
| 382 | } |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 383 | case ethernet::ETHERTYPE_NDN: |
| 384 | case 0x7777: // NDN ethertype used in ndnSIM |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 385 | os << "Tunnel Type: EthernetFrame"; |
| 386 | break; |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 387 | case 0x0077: // protocol field in PPP header used in ndnSIM |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 388 | os << "Tunnel Type: PPP"; |
| 389 | payloadSize -= 2; |
| 390 | payload += 2; |
| 391 | break; |
Junxiao Shi | c1c2b83 | 2016-07-24 20:45:36 +0000 | [diff] [blame] | 392 | 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] | 393 | return -1; |
Junxiao Shi | c1c2b83 | 2016-07-24 20:45:36 +0000 | [diff] [blame] | 394 | } |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 395 | |
| 396 | return 0; |
| 397 | } |
| 398 | |
Junxiao Shi | c1c2b83 | 2016-07-24 20:45:36 +0000 | [diff] [blame] | 399 | bool |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 400 | NdnDump::matchesFilter(const Name& name) const |
Junxiao Shi | c1c2b83 | 2016-07-24 20:45:36 +0000 | [diff] [blame] | 401 | { |
Davide Pesavento | 78de735 | 2018-07-22 00:35:45 -0400 | [diff] [blame] | 402 | if (!nameFilter) |
Junxiao Shi | c1c2b83 | 2016-07-24 20:45:36 +0000 | [diff] [blame] | 403 | return true; |
| 404 | |
| 405 | /// \todo Switch to NDN regular expressions |
Davide Pesavento | 78de735 | 2018-07-22 00:35:45 -0400 | [diff] [blame] | 406 | return std::regex_match(name.toUri(), *nameFilter); |
Junxiao Shi | c1c2b83 | 2016-07-24 20:45:36 +0000 | [diff] [blame] | 407 | } |
| 408 | |
Junxiao Shi | 3cd47df | 2015-06-07 20:58:14 -0700 | [diff] [blame] | 409 | } // namespace dump |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 410 | } // namespace ndn |