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