pcap-helper.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
26 #include "pcap-helper.hpp"
27 
28 #include <pcap/pcap.h>
29 #include <unistd.h>
30 
31 #if !defined(PCAP_NETMASK_UNKNOWN)
32 #define PCAP_NETMASK_UNKNOWN 0xffffffff
33 #endif
34 
35 namespace nfd {
36 namespace face {
37 
38 PcapHelper::PcapHelper(const std::string& interfaceName)
39  : m_pcap(nullptr)
40 {
41  char errbuf[PCAP_ERRBUF_SIZE] = {};
42  m_pcap = pcap_create(interfaceName.data(), errbuf);
43  if (!m_pcap)
44  BOOST_THROW_EXCEPTION(Error("pcap_create: " + std::string(errbuf)));
45 
46 #ifdef HAVE_PCAP_SET_IMMEDIATE_MODE
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  BOOST_THROW_EXCEPTION(Error("pcap_set_immediate_mode failed"));
53 #endif
54 }
55 
57 {
58  close();
59 }
60 
61 void
63 {
64  int ret = pcap_activate(m_pcap);
65  if (ret < 0)
66  BOOST_THROW_EXCEPTION(Error("pcap_activate: " + std::string(pcap_statustostr(ret))));
67 
68  if (pcap_set_datalink(m_pcap, dlt) < 0)
69  BOOST_THROW_EXCEPTION(Error("pcap_set_datalink: " + getLastError()));
70 
71  if (pcap_setdirection(m_pcap, PCAP_D_IN) < 0)
72  BOOST_THROW_EXCEPTION(Error("pcap_setdirection: " + getLastError()));
73 }
74 
75 void
77 {
78  if (m_pcap) {
79  pcap_close(m_pcap);
80  m_pcap = nullptr;
81  }
82 }
83 
84 int
86 {
87  int fd = pcap_get_selectable_fd(m_pcap);
88  if (fd < 0)
89  BOOST_THROW_EXCEPTION(Error("pcap_get_selectable_fd failed"));
90 
91  // we need to duplicate the fd, otherwise both pcap_close() and the
92  // caller may attempt to close the same fd and one of them will fail
93  return ::dup(fd);
94 }
95 
96 std::string
98 {
99  return pcap_geterr(m_pcap);
100 }
101 
102 size_t
104 {
105  pcap_stat ps{};
106  if (pcap_stats(m_pcap, &ps) < 0)
107  BOOST_THROW_EXCEPTION(Error("pcap_stats: " + getLastError()));
108 
109  return ps.ps_drop;
110 }
111 
112 void
113 PcapHelper::setPacketFilter(const char* filter) const
114 {
115  bpf_program prog;
116  if (pcap_compile(m_pcap, &prog, filter, 1, PCAP_NETMASK_UNKNOWN) < 0)
117  BOOST_THROW_EXCEPTION(Error("pcap_compile: " + getLastError()));
118 
119  int ret = pcap_setfilter(m_pcap, &prog);
120  pcap_freecode(&prog);
121  if (ret < 0)
122  BOOST_THROW_EXCEPTION(Error("pcap_setfilter: " + getLastError()));
123 }
124 
125 std::tuple<const uint8_t*, size_t, std::string>
127 {
128  pcap_pkthdr* header;
129  const uint8_t* packet;
130 
131  int ret = pcap_next_ex(m_pcap, &header, &packet);
132  if (ret < 0)
133  return std::make_tuple(nullptr, 0, getLastError());
134  else if (ret == 0)
135  return std::make_tuple(nullptr, 0, "timed out");
136  else
137  return std::make_tuple(packet, header->caplen, "");
138 }
139 
140 } // namespace face
141 } // namespace nfd
PcapHelper(const std::string &interfaceName)
Create a libpcap context for live packet capture on a network interface.
Definition: pcap-helper.cpp:38
#define PCAP_NETMASK_UNKNOWN
Copyright (c) 2014-2017, Regents of the University of California, Arizona Board of Regents...
Definition: pcap-helper.cpp:32
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:97
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:62
int getFd() const
Obtain a file descriptor that can be used in calls such as select(2) and poll(2). ...
Definition: pcap-helper.cpp:85
void close()
Stop capturing and close the handle.
Definition: pcap-helper.cpp:76
void setPacketFilter(const char *filter) const
Install a BPF filter on the receiving socket.