face: introduce PcapHelper class

Change-Id: I26f7d43394e1b17f314c226ca6bce723c2410ae3
Refs: #4011
diff --git a/daemon/face/pcap-helper.cpp b/daemon/face/pcap-helper.cpp
new file mode 100644
index 0000000..b1e57eb
--- /dev/null
+++ b/daemon/face/pcap-helper.cpp
@@ -0,0 +1,141 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2014-2017,  Regents of the University of California,
+ *                           Arizona Board of Regents,
+ *                           Colorado State University,
+ *                           University Pierre & Marie Curie, Sorbonne University,
+ *                           Washington University in St. Louis,
+ *                           Beijing Institute of Technology,
+ *                           The University of Memphis.
+ *
+ * This file is part of NFD (Named Data Networking Forwarding Daemon).
+ * See AUTHORS.md for complete list of NFD authors and contributors.
+ *
+ * NFD is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation,
+ * either version 3 of the License, or (at your option) any later version.
+ *
+ * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
+ * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+ * PURPOSE.  See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * NFD, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "pcap-helper.hpp"
+
+#include <pcap/pcap.h>
+#include <unistd.h>
+
+#if !defined(PCAP_NETMASK_UNKNOWN)
+#define PCAP_NETMASK_UNKNOWN  0xffffffff
+#endif
+
+namespace nfd {
+namespace face {
+
+PcapHelper::PcapHelper(const std::string& interfaceName)
+  : m_pcap(nullptr)
+{
+  char errbuf[PCAP_ERRBUF_SIZE] = {};
+  m_pcap = pcap_create(interfaceName.data(), errbuf);
+  if (!m_pcap)
+    BOOST_THROW_EXCEPTION(Error("pcap_create: " + std::string(errbuf)));
+
+#ifdef HAVE_PCAP_SET_IMMEDIATE_MODE
+  // Enable "immediate mode", effectively disabling any read buffering in the kernel.
+  // This corresponds to the BIOCIMMEDIATE ioctl on BSD-like systems (including macOS)
+  // where libpcap uses a BPF device. On Linux this forces libpcap not to use TPACKET_V3,
+  // even if the kernel supports it, thus preventing bug #1511.
+  if (pcap_set_immediate_mode(m_pcap, 1) < 0)
+    BOOST_THROW_EXCEPTION(Error("pcap_set_immediate_mode failed"));
+#endif
+}
+
+PcapHelper::~PcapHelper()
+{
+  close();
+}
+
+void
+PcapHelper::activate(int dlt)
+{
+  int ret = pcap_activate(m_pcap);
+  if (ret < 0)
+    BOOST_THROW_EXCEPTION(Error("pcap_activate: " + std::string(pcap_statustostr(ret))));
+
+  if (pcap_set_datalink(m_pcap, dlt) < 0)
+    BOOST_THROW_EXCEPTION(Error("pcap_set_datalink: " + getLastError()));
+
+  if (pcap_setdirection(m_pcap, PCAP_D_IN) < 0)
+    BOOST_THROW_EXCEPTION(Error("pcap_setdirection: " + getLastError()));
+}
+
+void
+PcapHelper::close()
+{
+  if (m_pcap) {
+    pcap_close(m_pcap);
+    m_pcap = nullptr;
+  }
+}
+
+int
+PcapHelper::getFd() const
+{
+  int fd = pcap_get_selectable_fd(m_pcap);
+  if (fd < 0)
+    BOOST_THROW_EXCEPTION(Error("pcap_get_selectable_fd failed"));
+
+  // we need to duplicate the fd, otherwise both pcap_close() and the
+  // caller may attempt to close the same fd and one of them will fail
+  return ::dup(fd);
+}
+
+std::string
+PcapHelper::getLastError() const
+{
+  return pcap_geterr(m_pcap);
+}
+
+size_t
+PcapHelper::getNDropped() const
+{
+  pcap_stat ps{};
+  if (pcap_stats(m_pcap, &ps) < 0)
+    BOOST_THROW_EXCEPTION(Error("pcap_stats: " + getLastError()));
+
+  return ps.ps_drop;
+}
+
+void
+PcapHelper::setPacketFilter(const char* filter) const
+{
+  bpf_program prog;
+  if (pcap_compile(m_pcap, &prog, filter, 1, PCAP_NETMASK_UNKNOWN) < 0)
+    BOOST_THROW_EXCEPTION(Error("pcap_compile: " + getLastError()));
+
+  int ret = pcap_setfilter(m_pcap, &prog);
+  pcap_freecode(&prog);
+  if (ret < 0)
+    BOOST_THROW_EXCEPTION(Error("pcap_setfilter: " + getLastError()));
+}
+
+std::tuple<const uint8_t*, size_t, std::string>
+PcapHelper::readNextPacket() const
+{
+  pcap_pkthdr* header;
+  const uint8_t* packet;
+
+  int ret = pcap_next_ex(m_pcap, &header, &packet);
+  if (ret < 0)
+    return std::make_tuple(nullptr, 0, getLastError());
+  else if (ret == 0)
+    return std::make_tuple(nullptr, 0, "timed out");
+  else
+    return std::make_tuple(packet, header->caplen, "");
+}
+
+} // namespace face
+} // namespace nfd