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 | /* |
Davide Pesavento | b6b176c | 2018-07-28 22:05:16 -0400 | [diff] [blame] | 3 | * Copyright (c) 2011-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 | */ |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 19 | |
| 20 | #include "ndndump.hpp" |
| 21 | |
Davide Pesavento | f912653 | 2018-08-04 18:31:06 -0400 | [diff] [blame] | 22 | #include <arpa/inet.h> |
Davide Pesavento | 051db00 | 2018-07-22 18:56:16 -0400 | [diff] [blame] | 23 | #include <net/ethernet.h> |
| 24 | #include <netinet/ip.h> |
Davide Pesavento | 0ca7e69 | 2018-08-05 02:21:06 -0400 | [diff] [blame^] | 25 | #include <netinet/ip6.h> |
Davide Pesavento | 051db00 | 2018-07-22 18:56:16 -0400 | [diff] [blame] | 26 | #include <netinet/tcp.h> |
| 27 | #include <netinet/udp.h> |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 28 | |
Junxiao Shi | 022bddf | 2016-11-24 23:15:20 +0000 | [diff] [blame] | 29 | #include <pcap/sll.h> |
| 30 | |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 31 | #include <iomanip> |
Davide Pesavento | c070270 | 2017-08-24 22:04:00 -0400 | [diff] [blame] | 32 | #include <sstream> |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 33 | |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 34 | #include <ndn-cxx/lp/nack.hpp> |
| 35 | #include <ndn-cxx/lp/packet.hpp> |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 36 | #include <ndn-cxx/net/ethernet.hpp> |
Davide Pesavento | b6b176c | 2018-07-28 22:05:16 -0400 | [diff] [blame] | 37 | #include <ndn-cxx/util/string-helper.hpp> |
| 38 | |
| 39 | #include <boost/endian/conversion.hpp> |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 40 | |
| 41 | namespace ndn { |
Junxiao Shi | 3cd47df | 2015-06-07 20:58:14 -0700 | [diff] [blame] | 42 | namespace dump { |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 43 | |
Davide Pesavento | b6b176c | 2018-07-28 22:05:16 -0400 | [diff] [blame] | 44 | namespace endian = boost::endian; |
| 45 | |
| 46 | class OutputFormatter : noncopyable |
| 47 | { |
| 48 | public: |
| 49 | OutputFormatter(std::ostream& os, std::string d) |
| 50 | : m_os(os) |
| 51 | , m_delim(std::move(d)) |
| 52 | { |
| 53 | } |
| 54 | |
| 55 | OutputFormatter& |
| 56 | addDelimiter() |
| 57 | { |
| 58 | if (!m_isEmpty) { |
| 59 | m_wantDelim = true; |
| 60 | } |
| 61 | return *this; |
| 62 | } |
| 63 | |
| 64 | private: |
| 65 | std::ostream& m_os; |
| 66 | std::string m_delim; |
| 67 | bool m_isEmpty = true; |
| 68 | bool m_wantDelim = false; |
| 69 | |
| 70 | template<typename T> |
| 71 | friend OutputFormatter& operator<<(OutputFormatter&, const T&); |
| 72 | }; |
| 73 | |
| 74 | template<typename T> |
| 75 | OutputFormatter& |
| 76 | operator<<(OutputFormatter& out, const T& val) |
| 77 | { |
| 78 | if (out.m_wantDelim) { |
| 79 | out.m_os << out.m_delim; |
| 80 | out.m_wantDelim = false; |
| 81 | } |
| 82 | out.m_os << val; |
| 83 | out.m_isEmpty = false; |
| 84 | return out; |
| 85 | } |
| 86 | |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 87 | NdnDump::~NdnDump() |
Junxiao Shi | c1c2b83 | 2016-07-24 20:45:36 +0000 | [diff] [blame] | 88 | { |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 89 | if (m_pcap) |
| 90 | pcap_close(m_pcap); |
| 91 | } |
| 92 | |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 93 | void |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 94 | NdnDump::run() |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 95 | { |
Davide Pesavento | f912653 | 2018-08-04 18:31:06 -0400 | [diff] [blame] | 96 | char errbuf[PCAP_ERRBUF_SIZE] = {}; |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 97 | |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 98 | if (inputFile.empty() && interface.empty()) { |
| 99 | const char* defaultDevice = pcap_lookupdev(errbuf); |
| 100 | |
| 101 | if (defaultDevice == nullptr) { |
Junxiao Shi | c759963 | 2016-07-24 20:46:24 +0000 | [diff] [blame] | 102 | BOOST_THROW_EXCEPTION(Error(errbuf)); |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 103 | } |
| 104 | |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 105 | interface = defaultDevice; |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 108 | std::string action; |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 109 | if (!interface.empty()) { |
Davide Pesavento | 24c0861 | 2018-07-26 13:33:24 -0400 | [diff] [blame] | 110 | m_pcap = pcap_open_live(interface.data(), 65535, wantPromisc, 1000, errbuf); |
Junxiao Shi | c1c2b83 | 2016-07-24 20:45:36 +0000 | [diff] [blame] | 111 | if (m_pcap == nullptr) { |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 112 | BOOST_THROW_EXCEPTION(Error("Cannot open interface " + interface + ": " + errbuf)); |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 113 | } |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 114 | action = "listening on " + interface; |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 115 | } |
| 116 | else { |
Davide Pesavento | 78de735 | 2018-07-22 00:35:45 -0400 | [diff] [blame] | 117 | m_pcap = pcap_open_offline(inputFile.data(), errbuf); |
Junxiao Shi | c1c2b83 | 2016-07-24 20:45:36 +0000 | [diff] [blame] | 118 | if (m_pcap == nullptr) { |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 119 | BOOST_THROW_EXCEPTION(Error("Cannot open file '" + inputFile + "' for reading: " + errbuf)); |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 120 | } |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 121 | action = "reading from file " + inputFile; |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 124 | m_dataLinkType = pcap_datalink(m_pcap); |
| 125 | const char* dltName = pcap_datalink_val_to_name(m_dataLinkType); |
| 126 | const char* dltDesc = pcap_datalink_val_to_description(m_dataLinkType); |
| 127 | std::string formattedDlt = dltName ? dltName : to_string(m_dataLinkType); |
| 128 | if (dltDesc) { |
| 129 | formattedDlt += " ("s + dltDesc + ")"; |
| 130 | } |
| 131 | |
| 132 | std::cerr << "ndndump: " << action << ", link-type " << formattedDlt << std::endl; |
| 133 | |
| 134 | switch (m_dataLinkType) { |
| 135 | case DLT_EN10MB: |
| 136 | case DLT_LINUX_SLL: |
| 137 | case DLT_PPP: |
| 138 | // we know how to handle these |
| 139 | break; |
| 140 | default: |
| 141 | BOOST_THROW_EXCEPTION(Error("Unsupported link-layer header type " + formattedDlt)); |
| 142 | } |
| 143 | |
| 144 | if (!pcapFilter.empty()) { |
Davide Pesavento | 24c0861 | 2018-07-26 13:33:24 -0400 | [diff] [blame] | 145 | if (wantVerbose) { |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 146 | std::cerr << "ndndump: using pcap filter: " << pcapFilter << std::endl; |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | bpf_program program; |
Davide Pesavento | fb42a3d | 2018-07-26 12:33:14 -0400 | [diff] [blame] | 150 | 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] | 151 | if (res < 0) { |
Davide Pesavento | fb42a3d | 2018-07-26 12:33:14 -0400 | [diff] [blame] | 152 | 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] | 153 | } |
| 154 | |
Junxiao Shi | c1c2b83 | 2016-07-24 20:45:36 +0000 | [diff] [blame] | 155 | res = pcap_setfilter(m_pcap, &program); |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 156 | pcap_freecode(&program); |
Junxiao Shi | c1c2b83 | 2016-07-24 20:45:36 +0000 | [diff] [blame] | 157 | if (res < 0) { |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 158 | 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] | 159 | } |
| 160 | } |
| 161 | |
Davide Pesavento | b6b176c | 2018-07-28 22:05:16 -0400 | [diff] [blame] | 162 | auto callback = [] (uint8_t* user, const pcap_pkthdr* pkthdr, const uint8_t* payload) { |
| 163 | reinterpret_cast<const NdnDump*>(user)->printPacket(pkthdr, payload); |
| 164 | }; |
| 165 | |
| 166 | if (pcap_loop(m_pcap, -1, callback, reinterpret_cast<uint8_t*>(this)) < 0) { |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 167 | BOOST_THROW_EXCEPTION(Error("pcap_loop: "s + pcap_geterr(m_pcap))); |
Junxiao Shi | c1c2b83 | 2016-07-24 20:45:36 +0000 | [diff] [blame] | 168 | } |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 169 | } |
| 170 | |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 171 | void |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 172 | NdnDump::printPacket(const pcap_pkthdr* pkthdr, const uint8_t* payload) const |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 173 | { |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 174 | // sanity checks |
| 175 | if (pkthdr->caplen == 0) { |
| 176 | std::cout << "[Invalid header: caplen=0]" << std::endl; |
| 177 | return; |
| 178 | } |
| 179 | if (pkthdr->len == 0) { |
| 180 | std::cout << "[Invalid header: len=0]" << std::endl; |
| 181 | return; |
| 182 | } |
| 183 | else if (pkthdr->len < pkthdr->caplen) { |
| 184 | std::cout << "[Invalid header: len(" << pkthdr->len |
| 185 | << ") < caplen(" << pkthdr->caplen << ")]" << std::endl; |
| 186 | return; |
| 187 | } |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 188 | |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 189 | std::ostringstream os; |
Davide Pesavento | b6b176c | 2018-07-28 22:05:16 -0400 | [diff] [blame] | 190 | OutputFormatter out(os, ", "); |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 191 | |
Davide Pesavento | b6b176c | 2018-07-28 22:05:16 -0400 | [diff] [blame] | 192 | bool shouldPrint = false; |
| 193 | switch (m_dataLinkType) { |
| 194 | case DLT_EN10MB: |
| 195 | shouldPrint = printEther(out, payload, pkthdr->len); |
| 196 | break; |
| 197 | case DLT_LINUX_SLL: |
| 198 | shouldPrint = printLinuxSll(out, payload, pkthdr->len); |
| 199 | break; |
| 200 | case DLT_PPP: |
| 201 | shouldPrint = printPpp(out, payload, pkthdr->len); |
| 202 | break; |
| 203 | default: |
| 204 | BOOST_ASSERT(false); |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 205 | return; |
| 206 | } |
| 207 | |
Davide Pesavento | b6b176c | 2018-07-28 22:05:16 -0400 | [diff] [blame] | 208 | if (shouldPrint) { |
| 209 | if (wantTimestamp) { |
| 210 | printTimestamp(std::cout, pkthdr->ts); |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 211 | } |
Davide Pesavento | b6b176c | 2018-07-28 22:05:16 -0400 | [diff] [blame] | 212 | std::cout << os.str() << std::endl; |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 213 | } |
| 214 | } |
| 215 | |
| 216 | void |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 217 | NdnDump::printTimestamp(std::ostream& os, const timeval& tv) const |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 218 | { |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 219 | /// \todo Add more timestamp formats (time since previous packet, time since first packet, ...) |
| 220 | os << tv.tv_sec |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 221 | << "." |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 222 | << std::setfill('0') << std::setw(6) << tv.tv_usec |
| 223 | << " "; |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 224 | } |
| 225 | |
Davide Pesavento | b6b176c | 2018-07-28 22:05:16 -0400 | [diff] [blame] | 226 | bool |
| 227 | NdnDump::dispatchByEtherType(OutputFormatter& out, const uint8_t* pkt, size_t len, uint16_t etherType) const |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 228 | { |
Davide Pesavento | b6b176c | 2018-07-28 22:05:16 -0400 | [diff] [blame] | 229 | out.addDelimiter(); |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 230 | |
Davide Pesavento | b6b176c | 2018-07-28 22:05:16 -0400 | [diff] [blame] | 231 | switch (etherType) { |
| 232 | case ETHERTYPE_IP: |
| 233 | return printIp4(out, pkt, len); |
Davide Pesavento | 0ca7e69 | 2018-08-05 02:21:06 -0400 | [diff] [blame^] | 234 | case ETHERTYPE_IPV6: |
| 235 | return printIp6(out, pkt, len); |
Davide Pesavento | b6b176c | 2018-07-28 22:05:16 -0400 | [diff] [blame] | 236 | case ethernet::ETHERTYPE_NDN: |
| 237 | case 0x7777: // NDN ethertype used in ndnSIM |
Davide Pesavento | f912653 | 2018-08-04 18:31:06 -0400 | [diff] [blame] | 238 | out << "Ethernet"; |
Davide Pesavento | b6b176c | 2018-07-28 22:05:16 -0400 | [diff] [blame] | 239 | return printNdn(out, pkt, len); |
| 240 | default: |
Davide Pesavento | f912653 | 2018-08-04 18:31:06 -0400 | [diff] [blame] | 241 | out << "[Unsupported ethertype " << AsHex{etherType} << "]"; |
Davide Pesavento | b6b176c | 2018-07-28 22:05:16 -0400 | [diff] [blame] | 242 | return true; |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 243 | } |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 244 | } |
| 245 | |
Davide Pesavento | b6b176c | 2018-07-28 22:05:16 -0400 | [diff] [blame] | 246 | bool |
| 247 | NdnDump::dispatchByIpProto(OutputFormatter& out, const uint8_t* pkt, size_t len, uint8_t ipProto) const |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 248 | { |
Davide Pesavento | b6b176c | 2018-07-28 22:05:16 -0400 | [diff] [blame] | 249 | out.addDelimiter(); |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 250 | |
Davide Pesavento | b6b176c | 2018-07-28 22:05:16 -0400 | [diff] [blame] | 251 | switch (ipProto) { |
Davide Pesavento | 0ca7e69 | 2018-08-05 02:21:06 -0400 | [diff] [blame^] | 252 | case IPPROTO_NONE: |
| 253 | out << "[No next header]"; |
| 254 | return true; |
Davide Pesavento | b6b176c | 2018-07-28 22:05:16 -0400 | [diff] [blame] | 255 | case IPPROTO_TCP: |
| 256 | return printTcp(out, pkt, len); |
| 257 | case IPPROTO_UDP: |
| 258 | return printUdp(out, pkt, len); |
| 259 | default: |
Davide Pesavento | f912653 | 2018-08-04 18:31:06 -0400 | [diff] [blame] | 260 | out << "[Unsupported IP proto " << static_cast<int>(ipProto) << "]"; |
Davide Pesavento | b6b176c | 2018-07-28 22:05:16 -0400 | [diff] [blame] | 261 | return true; |
| 262 | } |
| 263 | } |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 264 | |
Davide Pesavento | b6b176c | 2018-07-28 22:05:16 -0400 | [diff] [blame] | 265 | bool |
| 266 | NdnDump::printEther(OutputFormatter& out, const uint8_t* pkt, size_t len) const |
| 267 | { |
| 268 | // IEEE 802.3 Ethernet |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 269 | |
Davide Pesavento | b6b176c | 2018-07-28 22:05:16 -0400 | [diff] [blame] | 270 | if (len < ethernet::HDR_LEN) { |
Davide Pesavento | f912653 | 2018-08-04 18:31:06 -0400 | [diff] [blame] | 271 | out << "Truncated Ethernet frame, length " << len; |
Davide Pesavento | b6b176c | 2018-07-28 22:05:16 -0400 | [diff] [blame] | 272 | return true; |
Junxiao Shi | c1c2b83 | 2016-07-24 20:45:36 +0000 | [diff] [blame] | 273 | } |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 274 | |
Davide Pesavento | b6b176c | 2018-07-28 22:05:16 -0400 | [diff] [blame] | 275 | auto ether = reinterpret_cast<const ether_header*>(pkt); |
| 276 | pkt += ethernet::HDR_LEN; |
| 277 | len -= ethernet::HDR_LEN; |
| 278 | |
| 279 | return dispatchByEtherType(out, pkt, len, endian::big_to_native(ether->ether_type)); |
| 280 | } |
| 281 | |
| 282 | bool |
| 283 | NdnDump::printLinuxSll(OutputFormatter& out, const uint8_t* pkt, size_t len) const |
| 284 | { |
| 285 | // Linux "cooked" capture encapsulation |
| 286 | // https://www.tcpdump.org/linktypes/LINKTYPE_LINUX_SLL.html |
| 287 | |
| 288 | if (len < SLL_HDR_LEN) { |
Davide Pesavento | f912653 | 2018-08-04 18:31:06 -0400 | [diff] [blame] | 289 | out << "Truncated LINUX_SLL frame, length " << len; |
Davide Pesavento | b6b176c | 2018-07-28 22:05:16 -0400 | [diff] [blame] | 290 | return true; |
| 291 | } |
| 292 | |
| 293 | auto sll = reinterpret_cast<const sll_header*>(pkt); |
| 294 | pkt += SLL_HDR_LEN; |
| 295 | len -= SLL_HDR_LEN; |
| 296 | |
| 297 | return dispatchByEtherType(out, pkt, len, endian::big_to_native(sll->sll_protocol)); |
| 298 | } |
| 299 | |
| 300 | bool |
| 301 | NdnDump::printPpp(OutputFormatter& out, const uint8_t* pkt, size_t len) const |
| 302 | { |
| 303 | // PPP, as per RFC 1661 and RFC 1662; if the first 2 bytes are 0xff and 0x03, |
| 304 | // it's PPP in HDLC-like framing, with the PPP header following those two bytes, |
| 305 | // otherwise it's PPP without framing, and the packet begins with the PPP header. |
| 306 | // The data in the frame is not octet-stuffed or bit-stuffed. |
| 307 | |
| 308 | if (len < 2) { |
Davide Pesavento | f912653 | 2018-08-04 18:31:06 -0400 | [diff] [blame] | 309 | out << "Truncated PPP frame, length " << len; |
Davide Pesavento | b6b176c | 2018-07-28 22:05:16 -0400 | [diff] [blame] | 310 | return true; |
| 311 | } |
| 312 | |
| 313 | if (pkt[0] == 0xff && pkt[1] == 0x03) { |
| 314 | // PPP in HDLC-like framing, skip the Address and Control fields |
| 315 | if (len < 4) { |
Davide Pesavento | f912653 | 2018-08-04 18:31:06 -0400 | [diff] [blame] | 316 | out << "Truncated PPP frame, length " << len; |
Davide Pesavento | b6b176c | 2018-07-28 22:05:16 -0400 | [diff] [blame] | 317 | return true; |
| 318 | } |
| 319 | pkt += 2; |
| 320 | len -= 2; |
| 321 | } |
| 322 | |
| 323 | unsigned int proto = pkt[0]; |
| 324 | if (proto & 0x1) { |
| 325 | // Protocol field is compressed in 1 byte |
| 326 | pkt += 1; |
| 327 | len -= 1; |
| 328 | } |
| 329 | else { |
| 330 | // Protocol field is 2 bytes, in network byte order |
| 331 | proto = (proto << 8) | pkt[1]; |
| 332 | pkt += 2; |
| 333 | len -= 2; |
| 334 | } |
| 335 | |
| 336 | switch (proto) { |
| 337 | case 0x0077: // NDN in PPP frame, used by ndnSIM pcap traces |
| 338 | // For some reason, ndnSIM produces PPP frames with 2 extra bytes |
| 339 | // between the protocol field and the beginning of the NDN packet |
| 340 | if (len < 2) { |
Davide Pesavento | f912653 | 2018-08-04 18:31:06 -0400 | [diff] [blame] | 341 | out << "Truncated NDN/PPP frame"; |
Davide Pesavento | b6b176c | 2018-07-28 22:05:16 -0400 | [diff] [blame] | 342 | return true; |
| 343 | } |
| 344 | pkt += 2; |
| 345 | len -= 2; |
Davide Pesavento | f912653 | 2018-08-04 18:31:06 -0400 | [diff] [blame] | 346 | out << "PPP"; |
Davide Pesavento | b6b176c | 2018-07-28 22:05:16 -0400 | [diff] [blame] | 347 | return printNdn(out, pkt, len); |
| 348 | default: |
Davide Pesavento | f912653 | 2018-08-04 18:31:06 -0400 | [diff] [blame] | 349 | out << "[Unsupported PPP proto " << AsHex{proto} << "]"; |
Davide Pesavento | b6b176c | 2018-07-28 22:05:16 -0400 | [diff] [blame] | 350 | return true; |
| 351 | } |
| 352 | } |
| 353 | |
Davide Pesavento | f912653 | 2018-08-04 18:31:06 -0400 | [diff] [blame] | 354 | static void |
| 355 | printIpAddress(OutputFormatter& out, int af, const void* addr) |
| 356 | { |
| 357 | char addrStr[1 + std::max(INET_ADDRSTRLEN, INET6_ADDRSTRLEN)] = {}; |
| 358 | if (inet_ntop(af, addr, addrStr, sizeof(addrStr))) { |
| 359 | out << addrStr; |
| 360 | } |
| 361 | else { |
| 362 | out << "???"; |
| 363 | } |
| 364 | } |
| 365 | |
Davide Pesavento | b6b176c | 2018-07-28 22:05:16 -0400 | [diff] [blame] | 366 | bool |
| 367 | NdnDump::printIp4(OutputFormatter& out, const uint8_t* pkt, size_t len) const |
| 368 | { |
Davide Pesavento | f912653 | 2018-08-04 18:31:06 -0400 | [diff] [blame] | 369 | out.addDelimiter() << "IP "; |
| 370 | |
| 371 | if (len < sizeof(ip)) { |
| 372 | out << "truncated header, length " << len; |
| 373 | return true; |
| 374 | } |
Davide Pesavento | b6b176c | 2018-07-28 22:05:16 -0400 | [diff] [blame] | 375 | |
| 376 | auto ih = reinterpret_cast<const ip*>(pkt); |
Davide Pesavento | f912653 | 2018-08-04 18:31:06 -0400 | [diff] [blame] | 377 | if (ih->ip_v != 4) { |
| 378 | // huh? link layer said this was an IPv4 packet but IP header says otherwise |
| 379 | out << "bad version " << ih->ip_v; |
Davide Pesavento | b6b176c | 2018-07-28 22:05:16 -0400 | [diff] [blame] | 380 | return true; |
| 381 | } |
| 382 | |
Davide Pesavento | f912653 | 2018-08-04 18:31:06 -0400 | [diff] [blame] | 383 | size_t ipHdrLen = ih->ip_hl * 4; |
| 384 | if (ipHdrLen < sizeof(ip)) { |
| 385 | out << "bad header length " << ipHdrLen; |
Davide Pesavento | b6b176c | 2018-07-28 22:05:16 -0400 | [diff] [blame] | 386 | return true; |
| 387 | } |
| 388 | |
Davide Pesavento | f912653 | 2018-08-04 18:31:06 -0400 | [diff] [blame] | 389 | size_t ipLen = endian::big_to_native(ih->ip_len); |
| 390 | if (ipLen < ipHdrLen) { |
| 391 | out << "bad length " << ipLen; |
| 392 | return true; |
| 393 | } |
| 394 | if (ipLen > len) { |
| 395 | out << "truncated packet, " << ipLen - len << " bytes missing"; |
| 396 | return true; |
| 397 | } |
| 398 | // if we reached this point, the following is true: |
| 399 | // sizeof(ip) <= ipHdrLen <= ipLen <= len |
| 400 | |
| 401 | printIpAddress(out, AF_INET, &ih->ip_src); |
| 402 | out << " > "; |
| 403 | printIpAddress(out, AF_INET, &ih->ip_dst); |
| 404 | |
| 405 | pkt += ipHdrLen; |
| 406 | len -= ipHdrLen; |
Davide Pesavento | b6b176c | 2018-07-28 22:05:16 -0400 | [diff] [blame] | 407 | |
| 408 | return dispatchByIpProto(out, pkt, len, ih->ip_p); |
| 409 | } |
| 410 | |
| 411 | bool |
Davide Pesavento | 0ca7e69 | 2018-08-05 02:21:06 -0400 | [diff] [blame^] | 412 | NdnDump::printIp6(OutputFormatter& out, const uint8_t* pkt, size_t len) const |
| 413 | { |
| 414 | out.addDelimiter() << "IP6 "; |
| 415 | |
| 416 | if (len < sizeof(ip6_hdr)) { |
| 417 | out << "truncated header, length " << len; |
| 418 | return true; |
| 419 | } |
| 420 | |
| 421 | auto ip6 = reinterpret_cast<const ip6_hdr*>(pkt); |
| 422 | unsigned int ipVer = (ip6->ip6_vfc & 0xf0) >> 4; |
| 423 | if (ipVer != 6) { |
| 424 | // huh? link layer said this was an IPv6 packet but IP header says otherwise |
| 425 | out << "bad version " << ipVer; |
| 426 | return true; |
| 427 | } |
| 428 | |
| 429 | pkt += sizeof(ip6_hdr); |
| 430 | len -= sizeof(ip6_hdr); |
| 431 | |
| 432 | size_t payloadLen = endian::big_to_native(ip6->ip6_plen); |
| 433 | if (len < payloadLen) { |
| 434 | out << "truncated payload, " << payloadLen - len << " bytes missing"; |
| 435 | return true; |
| 436 | } |
| 437 | |
| 438 | printIpAddress(out, AF_INET6, &ip6->ip6_src); |
| 439 | out << " > "; |
| 440 | printIpAddress(out, AF_INET6, &ip6->ip6_dst); |
| 441 | |
| 442 | // we assume no extension headers are present |
| 443 | return dispatchByIpProto(out, pkt, len, ip6->ip6_nxt); |
| 444 | } |
| 445 | |
| 446 | bool |
Davide Pesavento | b6b176c | 2018-07-28 22:05:16 -0400 | [diff] [blame] | 447 | NdnDump::printTcp(OutputFormatter& out, const uint8_t* pkt, size_t len) const |
| 448 | { |
Davide Pesavento | f912653 | 2018-08-04 18:31:06 -0400 | [diff] [blame] | 449 | out.addDelimiter() << "TCP"; |
| 450 | |
| 451 | if (len < sizeof(tcphdr)) { |
| 452 | out << " truncated header, length " << len; |
| 453 | return true; |
| 454 | } |
Davide Pesavento | b6b176c | 2018-07-28 22:05:16 -0400 | [diff] [blame] | 455 | |
| 456 | auto th = reinterpret_cast<const tcphdr*>(pkt); |
Davide Pesavento | f912653 | 2018-08-04 18:31:06 -0400 | [diff] [blame] | 457 | size_t tcpHdrLen = th->th_off * 4; |
| 458 | if (tcpHdrLen < sizeof(tcphdr)) { |
| 459 | out << " bad header length " << tcpHdrLen; |
| 460 | return true; |
| 461 | } |
| 462 | if (tcpHdrLen > len) { |
| 463 | out << " truncated header, " << tcpHdrLen - len << " bytes missing"; |
Davide Pesavento | b6b176c | 2018-07-28 22:05:16 -0400 | [diff] [blame] | 464 | return true; |
| 465 | } |
| 466 | |
Davide Pesavento | f912653 | 2018-08-04 18:31:06 -0400 | [diff] [blame] | 467 | pkt += tcpHdrLen; |
| 468 | len -= tcpHdrLen; |
Davide Pesavento | b6b176c | 2018-07-28 22:05:16 -0400 | [diff] [blame] | 469 | |
Davide Pesavento | f912653 | 2018-08-04 18:31:06 -0400 | [diff] [blame] | 470 | out.addDelimiter() << "length " << len; |
Davide Pesavento | b6b176c | 2018-07-28 22:05:16 -0400 | [diff] [blame] | 471 | |
Davide Pesavento | b6b176c | 2018-07-28 22:05:16 -0400 | [diff] [blame] | 472 | return printNdn(out, pkt, len); |
| 473 | } |
| 474 | |
| 475 | bool |
| 476 | NdnDump::printUdp(OutputFormatter& out, const uint8_t* pkt, size_t len) const |
| 477 | { |
Davide Pesavento | f912653 | 2018-08-04 18:31:06 -0400 | [diff] [blame] | 478 | out.addDelimiter() << "UDP"; |
Davide Pesavento | b6b176c | 2018-07-28 22:05:16 -0400 | [diff] [blame] | 479 | |
| 480 | if (len < sizeof(udphdr)) { |
Davide Pesavento | f912653 | 2018-08-04 18:31:06 -0400 | [diff] [blame] | 481 | out << " truncated header, length " << len; |
Davide Pesavento | b6b176c | 2018-07-28 22:05:16 -0400 | [diff] [blame] | 482 | return true; |
| 483 | } |
| 484 | |
Davide Pesavento | f912653 | 2018-08-04 18:31:06 -0400 | [diff] [blame] | 485 | auto uh = reinterpret_cast<const udphdr*>(pkt); |
| 486 | size_t udpLen = endian::big_to_native(uh->uh_ulen); |
| 487 | if (udpLen < sizeof(udphdr)) { |
| 488 | out << " bad length " << udpLen; |
| 489 | return true; |
| 490 | } |
| 491 | if (udpLen > len) { |
| 492 | out << " truncated packet, " << udpLen - len << " bytes missing"; |
| 493 | return true; |
| 494 | } |
| 495 | else { |
| 496 | len = udpLen; |
| 497 | } |
| 498 | |
Davide Pesavento | b6b176c | 2018-07-28 22:05:16 -0400 | [diff] [blame] | 499 | pkt += sizeof(udphdr); |
| 500 | len -= sizeof(udphdr); |
| 501 | |
Davide Pesavento | f912653 | 2018-08-04 18:31:06 -0400 | [diff] [blame] | 502 | out.addDelimiter() << "length " << len; |
| 503 | |
Davide Pesavento | b6b176c | 2018-07-28 22:05:16 -0400 | [diff] [blame] | 504 | return printNdn(out, pkt, len); |
| 505 | } |
| 506 | |
| 507 | bool |
| 508 | NdnDump::printNdn(OutputFormatter& out, const uint8_t* pkt, size_t len) const |
| 509 | { |
| 510 | if (len == 0) { |
| 511 | return false; |
| 512 | } |
| 513 | out.addDelimiter(); |
| 514 | |
| 515 | bool isOk = false; |
| 516 | Block block; |
| 517 | std::tie(isOk, block) = Block::fromBuffer(pkt, len); |
| 518 | if (!isOk) { |
| 519 | // if packet is incomplete, we will not be able to process it |
Davide Pesavento | f912653 | 2018-08-04 18:31:06 -0400 | [diff] [blame] | 520 | out << "NDN truncated packet, length " << len; |
Davide Pesavento | b6b176c | 2018-07-28 22:05:16 -0400 | [diff] [blame] | 521 | return true; |
| 522 | } |
| 523 | |
| 524 | lp::Packet lpPacket; |
| 525 | Block netPacket; |
| 526 | |
| 527 | if (block.type() == lp::tlv::LpPacket) { |
Davide Pesavento | f912653 | 2018-08-04 18:31:06 -0400 | [diff] [blame] | 528 | out << "NDNLPv2"; |
Davide Pesavento | b6b176c | 2018-07-28 22:05:16 -0400 | [diff] [blame] | 529 | try { |
| 530 | lpPacket.wireDecode(block); |
| 531 | } |
| 532 | catch (const tlv::Error& e) { |
Davide Pesavento | f912653 | 2018-08-04 18:31:06 -0400 | [diff] [blame] | 533 | out << " invalid packet: " << e.what(); |
Davide Pesavento | b6b176c | 2018-07-28 22:05:16 -0400 | [diff] [blame] | 534 | return true; |
| 535 | } |
| 536 | |
| 537 | Buffer::const_iterator begin, end; |
| 538 | if (lpPacket.has<lp::FragmentField>()) { |
| 539 | std::tie(begin, end) = lpPacket.get<lp::FragmentField>(); |
| 540 | } |
| 541 | else { |
Davide Pesavento | f912653 | 2018-08-04 18:31:06 -0400 | [diff] [blame] | 542 | out << " idle"; |
Davide Pesavento | b6b176c | 2018-07-28 22:05:16 -0400 | [diff] [blame] | 543 | return true; |
| 544 | } |
| 545 | |
| 546 | bool isOk = false; |
| 547 | std::tie(isOk, netPacket) = Block::fromBuffer(&*begin, std::distance(begin, end)); |
| 548 | if (!isOk) { |
| 549 | // if network packet is fragmented, we will not be able to process it |
Davide Pesavento | f912653 | 2018-08-04 18:31:06 -0400 | [diff] [blame] | 550 | out << " fragment"; |
Davide Pesavento | b6b176c | 2018-07-28 22:05:16 -0400 | [diff] [blame] | 551 | return true; |
| 552 | } |
| 553 | } |
| 554 | else { |
| 555 | netPacket = std::move(block); |
| 556 | } |
Davide Pesavento | f912653 | 2018-08-04 18:31:06 -0400 | [diff] [blame] | 557 | out.addDelimiter(); |
Davide Pesavento | b6b176c | 2018-07-28 22:05:16 -0400 | [diff] [blame] | 558 | |
| 559 | try { |
| 560 | switch (netPacket.type()) { |
| 561 | case tlv::Interest: { |
| 562 | Interest interest(netPacket); |
| 563 | if (!matchesFilter(interest.getName())) { |
| 564 | return false; |
| 565 | } |
| 566 | |
| 567 | if (lpPacket.has<lp::NackField>()) { |
| 568 | lp::Nack nack(interest); |
| 569 | nack.setHeader(lpPacket.get<lp::NackField>()); |
Davide Pesavento | f912653 | 2018-08-04 18:31:06 -0400 | [diff] [blame] | 570 | out << "NACK (" << nack.getReason() << "): " << interest; |
Davide Pesavento | b6b176c | 2018-07-28 22:05:16 -0400 | [diff] [blame] | 571 | } |
| 572 | else { |
| 573 | out << "INTEREST: " << interest; |
| 574 | } |
| 575 | break; |
| 576 | } |
| 577 | case tlv::Data: { |
| 578 | Data data(netPacket); |
| 579 | if (!matchesFilter(data.getName())) { |
| 580 | return false; |
| 581 | } |
| 582 | |
| 583 | out << "DATA: " << data.getName(); |
| 584 | break; |
| 585 | } |
| 586 | default: { |
Davide Pesavento | f912653 | 2018-08-04 18:31:06 -0400 | [diff] [blame] | 587 | out << "[Unsupported NDN packet type " << netPacket.type() << "]"; |
Davide Pesavento | b6b176c | 2018-07-28 22:05:16 -0400 | [diff] [blame] | 588 | break; |
| 589 | } |
| 590 | } |
| 591 | } |
| 592 | catch (const tlv::Error& e) { |
Davide Pesavento | f912653 | 2018-08-04 18:31:06 -0400 | [diff] [blame] | 593 | out << "invalid network packet: " << e.what(); |
Davide Pesavento | b6b176c | 2018-07-28 22:05:16 -0400 | [diff] [blame] | 594 | } |
| 595 | |
| 596 | return true; |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 597 | } |
| 598 | |
Junxiao Shi | c1c2b83 | 2016-07-24 20:45:36 +0000 | [diff] [blame] | 599 | bool |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 600 | NdnDump::matchesFilter(const Name& name) const |
Junxiao Shi | c1c2b83 | 2016-07-24 20:45:36 +0000 | [diff] [blame] | 601 | { |
Davide Pesavento | 78de735 | 2018-07-22 00:35:45 -0400 | [diff] [blame] | 602 | if (!nameFilter) |
Junxiao Shi | c1c2b83 | 2016-07-24 20:45:36 +0000 | [diff] [blame] | 603 | return true; |
| 604 | |
| 605 | /// \todo Switch to NDN regular expressions |
Davide Pesavento | 78de735 | 2018-07-22 00:35:45 -0400 | [diff] [blame] | 606 | return std::regex_match(name.toUri(), *nameFilter); |
Junxiao Shi | c1c2b83 | 2016-07-24 20:45:36 +0000 | [diff] [blame] | 607 | } |
| 608 | |
Junxiao Shi | 3cd47df | 2015-06-07 20:58:14 -0700 | [diff] [blame] | 609 | } // namespace dump |
Junxiao Shi | 2222a61 | 2015-06-06 08:01:38 -0700 | [diff] [blame] | 610 | } // namespace ndn |