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-2019, 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 
29 #include <pcap/pcap.h>
30 #include <unistd.h>
31 
32 #if !defined(PCAP_NETMASK_UNKNOWN)
33 #define PCAP_NETMASK_UNKNOWN 0xffffffff
34 #endif
35 
36 namespace nfd {
37 namespace face {
38 
39 PcapHelper::PcapHelper(const std::string& interfaceName)
40  : m_pcap(nullptr)
41 {
42  char errbuf[PCAP_ERRBUF_SIZE] = {};
43  m_pcap = pcap_create(interfaceName.data(), errbuf);
44  if (!m_pcap)
45  NDN_THROW(Error("pcap_create: " + std::string(errbuf)));
46 
47  // Enable "immediate mode", effectively disabling any read buffering in the kernel.
48  // This corresponds to the BIOCIMMEDIATE ioctl on BSD-like systems (including macOS)
49  // where libpcap uses a BPF device. On Linux this forces libpcap not to use TPACKET_V3,
50  // even if the kernel supports it, thus preventing bug #1511.
51  if (pcap_set_immediate_mode(m_pcap, 1) < 0)
52  NDN_THROW(Error("pcap_set_immediate_mode failed"));
53 
54  pcap_set_snaplen(m_pcap, ethernet::HDR_LEN + ndn::MAX_NDN_PACKET_SIZE);
55  pcap_set_buffer_size(m_pcap, 4 * 1024 * 1024);
56 }
57 
59 {
60  close();
61 }
62 
63 void
65 {
66  int ret = pcap_activate(m_pcap);
67  if (ret < 0)
68  NDN_THROW(Error("pcap_activate: " + std::string(pcap_statustostr(ret))));
69 
70  if (pcap_set_datalink(m_pcap, dlt) < 0)
71  NDN_THROW(Error("pcap_set_datalink: " + getLastError()));
72 
73  if (pcap_setdirection(m_pcap, PCAP_D_IN) < 0)
74  NDN_THROW(Error("pcap_setdirection: " + getLastError()));
75 }
76 
77 void
79 {
80  if (m_pcap) {
81  pcap_close(m_pcap);
82  m_pcap = nullptr;
83  }
84 }
85 
86 int
88 {
89  int fd = pcap_get_selectable_fd(m_pcap);
90  if (fd < 0)
91  NDN_THROW(Error("pcap_get_selectable_fd failed"));
92 
93  // we need to duplicate the fd, otherwise both pcap_close() and the
94  // caller may attempt to close the same fd and one of them will fail
95  return ::dup(fd);
96 }
97 
98 std::string
100 {
101  return pcap_geterr(m_pcap);
102 }
103 
104 size_t
106 {
107  pcap_stat ps{};
108  if (pcap_stats(m_pcap, &ps) < 0)
109  NDN_THROW(Error("pcap_stats: " + getLastError()));
110 
111  return ps.ps_drop;
112 }
113 
114 void
115 PcapHelper::setPacketFilter(const char* filter) const
116 {
117  bpf_program prog;
118  if (pcap_compile(m_pcap, &prog, filter, 1, PCAP_NETMASK_UNKNOWN) < 0)
119  NDN_THROW(Error("pcap_compile: " + getLastError()));
120 
121  int ret = pcap_setfilter(m_pcap, &prog);
122  pcap_freecode(&prog);
123  if (ret < 0)
124  NDN_THROW(Error("pcap_setfilter: " + getLastError()));
125 }
126 
127 std::tuple<const uint8_t*, size_t, std::string>
129 {
130  pcap_pkthdr* header;
131  const uint8_t* packet;
132 
133  int ret = pcap_next_ex(m_pcap, &header, &packet);
134  if (ret < 0)
135  return std::make_tuple(nullptr, 0, getLastError());
136  else if (ret == 0)
137  return std::make_tuple(nullptr, 0, "timed out");
138  else
139  return std::make_tuple(packet, header->caplen, "");
140 }
141 
142 } // namespace face
143 } // namespace nfd
PcapHelper(const std::string &interfaceName)
Create a libpcap context for live packet capture on a network interface.
Definition: pcap-helper.cpp:39
#define PCAP_NETMASK_UNKNOWN
Definition: pcap-helper.cpp:33
size_t getNDropped() const
Get the number of packets dropped by the kernel, as reported by libpcap.
std::string getLastError() const
Get last error message.
Definition: pcap-helper.cpp:99
Copyright (c) 2014-2015, Regents of the University of California, Arizona Board of Regents...
Definition: algorithm.hpp:32
std::tuple< const uint8_t *, size_t, std::string > readNextPacket() const
Read the next packet captured on the interface.
void activate(int dlt)
Start capturing packets.
Definition: pcap-helper.cpp:64
int getFd() const
Obtain a file descriptor that can be used in calls such as select(2) and poll(2). ...
Definition: pcap-helper.cpp:87
void close()
Stop capturing and close the handle.
Definition: pcap-helper.cpp:78
void setPacketFilter(const char *filter) const
Install a BPF filter on the receiving socket.