NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
pcap-helper.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2022, Regents of the University of California,
4  * Arizona Board of Regents,
5  * Colorado State University,
6  * University Pierre & Marie Curie, Sorbonne University,
7  * Washington University in St. Louis,
8  * Beijing Institute of Technology,
9  * The University of Memphis.
10  *
11  * This file is part of NFD (Named Data Networking Forwarding Daemon).
12  * See AUTHORS.md for complete list of NFD authors and contributors.
13  *
14  * NFD is free software: you can redistribute it and/or modify it under the terms
15  * of the GNU General Public License as published by the Free Software Foundation,
16  * either version 3 of the License, or (at your option) any later version.
17  *
18  * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20  * PURPOSE. See the GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License along with
23  * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
24  */
25 
26 #include "pcap-helper.hpp"
27 #include "ethernet-protocol.hpp"
28 
30 
31 #include <pcap/pcap.h>
32 #include <unistd.h>
33 
34 #if !defined(PCAP_NETMASK_UNKNOWN)
35 #define PCAP_NETMASK_UNKNOWN 0xffffffff
36 #endif
37 
38 namespace nfd {
39 namespace face {
40 
41 PcapHelper::PcapHelper(const std::string& interfaceName)
42  : m_pcap(nullptr)
43 {
44  char errbuf[PCAP_ERRBUF_SIZE] = {};
45  m_pcap = pcap_create(interfaceName.data(), errbuf);
46  if (!m_pcap)
47  NDN_THROW(Error("pcap_create: " + std::string(errbuf)));
48 
49  // Enable "immediate mode", effectively disabling any read buffering in the kernel.
50  // This corresponds to the BIOCIMMEDIATE ioctl on BSD-like systems (including macOS)
51  // where libpcap uses a BPF device. On Linux this forces libpcap not to use TPACKET_V3,
52  // even if the kernel supports it, thus preventing bug #1511.
53  if (pcap_set_immediate_mode(m_pcap, 1) < 0)
54  NDN_THROW(Error("pcap_set_immediate_mode failed"));
55 
56  pcap_set_snaplen(m_pcap, ethernet::HDR_LEN + ndn::MAX_NDN_PACKET_SIZE);
57  pcap_set_buffer_size(m_pcap, 4 * 1024 * 1024);
58 }
59 
61 {
62  close();
63 }
64 
65 void
67 {
69  int ret = pcap_activate(m_pcap);
70  if (ret < 0)
71  NDN_THROW(Error("pcap_activate: " + std::string(pcap_statustostr(ret))));
72  });
73 
74  if (pcap_set_datalink(m_pcap, dlt) < 0)
75  NDN_THROW(Error("pcap_set_datalink: " + getLastError()));
76 
77  if (pcap_setdirection(m_pcap, PCAP_D_IN) < 0)
78  NDN_THROW(Error("pcap_setdirection: " + getLastError()));
79 }
80 
81 void
83 {
84  if (m_pcap) {
85  pcap_close(m_pcap);
86  m_pcap = nullptr;
87  }
88 }
89 
90 int
92 {
93  int fd = pcap_get_selectable_fd(m_pcap);
94  if (fd < 0)
95  NDN_THROW(Error("pcap_get_selectable_fd failed"));
96 
97  // we need to duplicate the fd, otherwise both pcap_close() and the
98  // caller may attempt to close the same fd and one of them will fail
99  return ::dup(fd);
100 }
101 
102 std::string
103 PcapHelper::getLastError() const noexcept
104 {
105  return pcap_geterr(m_pcap);
106 }
107 
108 size_t
110 {
111  pcap_stat ps{};
112  if (pcap_stats(m_pcap, &ps) < 0)
113  NDN_THROW(Error("pcap_stats: " + getLastError()));
114 
115  return ps.ps_drop;
116 }
117 
118 void
119 PcapHelper::setPacketFilter(const char* filter) const
120 {
121  bpf_program prog;
122  if (pcap_compile(m_pcap, &prog, filter, 1, PCAP_NETMASK_UNKNOWN) < 0)
123  NDN_THROW(Error("pcap_compile: " + getLastError()));
124 
125  int ret = pcap_setfilter(m_pcap, &prog);
126  pcap_freecode(&prog);
127  if (ret < 0)
128  NDN_THROW(Error("pcap_setfilter: " + getLastError()));
129 }
130 
131 std::tuple<span<const uint8_t>, std::string>
133 {
134  pcap_pkthdr* header;
135  const uint8_t* packet;
136 
137  int ret = pcap_next_ex(m_pcap, &header, &packet);
138  if (ret < 0)
139  return {span<uint8_t>{}, getLastError()};
140  else if (ret == 0)
141  return {span<uint8_t>{}, "timed out"};
142  else
143  return {{packet, header->caplen}, ""};
144 }
145 
146 } // namespace face
147 } // namespace nfd
void activate(int dlt)
Start capturing packets.
Definition: pcap-helper.cpp:66
static void runElevated(F &&f)
#define PCAP_NETMASK_UNKNOWN
Definition: pcap-helper.cpp:35
void setPacketFilter(const char *filter) const
Install a BPF filter on the receiving socket.
#define NDN_THROW(e)
Definition: exception.hpp:61
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:39
const size_t HDR_LEN
Total octets in Ethernet header (without 802.1Q tag)
Definition: ethernet.hpp:43
void close() noexcept
Stop capturing and close the handle.
Definition: pcap-helper.cpp:82
std::string getLastError() const noexcept
Get last error message.
PcapHelper(const std::string &interfaceName)
Create a libpcap context for live packet capture on a network interface.
Definition: pcap-helper.cpp:41
int getFd() const
Obtain a file descriptor that can be used in calls such as select(2) and poll(2). ...
Definition: pcap-helper.cpp:91
size_t getNDropped() const
Get the number of packets dropped by the kernel, as reported by libpcap.
std::tuple< span< const uint8_t >, std::string > readNextPacket() const noexcept
Read the next packet captured on the interface.
const size_t MAX_NDN_PACKET_SIZE
Practical size limit of a network-layer packet.
Definition: tlv.hpp:41