Davide Pesavento | fe0580c | 2017-05-12 02:02:10 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 5f35f64 | 2018-05-10 19:36:03 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2019, Regents of the University of California, |
Davide Pesavento | fe0580c | 2017-05-12 02:02:10 -0400 | [diff] [blame] | 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 | |
| 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) |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 44 | NDN_THROW(Error("pcap_create: " + std::string(errbuf))); |
Davide Pesavento | fe0580c | 2017-05-12 02:02:10 -0400 | [diff] [blame] | 45 | |
Davide Pesavento | fe0580c | 2017-05-12 02:02:10 -0400 | [diff] [blame] | 46 | // Enable "immediate mode", effectively disabling any read buffering in the kernel. |
| 47 | // This corresponds to the BIOCIMMEDIATE ioctl on BSD-like systems (including macOS) |
| 48 | // where libpcap uses a BPF device. On Linux this forces libpcap not to use TPACKET_V3, |
| 49 | // even if the kernel supports it, thus preventing bug #1511. |
| 50 | if (pcap_set_immediate_mode(m_pcap, 1) < 0) |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 51 | NDN_THROW(Error("pcap_set_immediate_mode failed")); |
Davide Pesavento | fe0580c | 2017-05-12 02:02:10 -0400 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | PcapHelper::~PcapHelper() |
| 55 | { |
| 56 | close(); |
| 57 | } |
| 58 | |
| 59 | void |
| 60 | PcapHelper::activate(int dlt) |
| 61 | { |
| 62 | int ret = pcap_activate(m_pcap); |
| 63 | if (ret < 0) |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 64 | NDN_THROW(Error("pcap_activate: " + std::string(pcap_statustostr(ret)))); |
Davide Pesavento | fe0580c | 2017-05-12 02:02:10 -0400 | [diff] [blame] | 65 | |
| 66 | if (pcap_set_datalink(m_pcap, dlt) < 0) |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 67 | NDN_THROW(Error("pcap_set_datalink: " + getLastError())); |
Davide Pesavento | fe0580c | 2017-05-12 02:02:10 -0400 | [diff] [blame] | 68 | |
| 69 | if (pcap_setdirection(m_pcap, PCAP_D_IN) < 0) |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 70 | NDN_THROW(Error("pcap_setdirection: " + getLastError())); |
Davide Pesavento | fe0580c | 2017-05-12 02:02:10 -0400 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | void |
| 74 | PcapHelper::close() |
| 75 | { |
| 76 | if (m_pcap) { |
| 77 | pcap_close(m_pcap); |
| 78 | m_pcap = nullptr; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | int |
| 83 | PcapHelper::getFd() const |
| 84 | { |
| 85 | int fd = pcap_get_selectable_fd(m_pcap); |
| 86 | if (fd < 0) |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 87 | NDN_THROW(Error("pcap_get_selectable_fd failed")); |
Davide Pesavento | fe0580c | 2017-05-12 02:02:10 -0400 | [diff] [blame] | 88 | |
| 89 | // we need to duplicate the fd, otherwise both pcap_close() and the |
| 90 | // caller may attempt to close the same fd and one of them will fail |
| 91 | return ::dup(fd); |
| 92 | } |
| 93 | |
| 94 | std::string |
| 95 | PcapHelper::getLastError() const |
| 96 | { |
| 97 | return pcap_geterr(m_pcap); |
| 98 | } |
| 99 | |
| 100 | size_t |
| 101 | PcapHelper::getNDropped() const |
| 102 | { |
| 103 | pcap_stat ps{}; |
| 104 | if (pcap_stats(m_pcap, &ps) < 0) |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 105 | NDN_THROW(Error("pcap_stats: " + getLastError())); |
Davide Pesavento | fe0580c | 2017-05-12 02:02:10 -0400 | [diff] [blame] | 106 | |
| 107 | return ps.ps_drop; |
| 108 | } |
| 109 | |
| 110 | void |
| 111 | PcapHelper::setPacketFilter(const char* filter) const |
| 112 | { |
| 113 | bpf_program prog; |
| 114 | if (pcap_compile(m_pcap, &prog, filter, 1, PCAP_NETMASK_UNKNOWN) < 0) |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 115 | NDN_THROW(Error("pcap_compile: " + getLastError())); |
Davide Pesavento | fe0580c | 2017-05-12 02:02:10 -0400 | [diff] [blame] | 116 | |
| 117 | int ret = pcap_setfilter(m_pcap, &prog); |
| 118 | pcap_freecode(&prog); |
| 119 | if (ret < 0) |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 120 | NDN_THROW(Error("pcap_setfilter: " + getLastError())); |
Davide Pesavento | fe0580c | 2017-05-12 02:02:10 -0400 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | std::tuple<const uint8_t*, size_t, std::string> |
| 124 | PcapHelper::readNextPacket() const |
| 125 | { |
| 126 | pcap_pkthdr* header; |
| 127 | const uint8_t* packet; |
| 128 | |
| 129 | int ret = pcap_next_ex(m_pcap, &header, &packet); |
| 130 | if (ret < 0) |
| 131 | return std::make_tuple(nullptr, 0, getLastError()); |
| 132 | else if (ret == 0) |
| 133 | return std::make_tuple(nullptr, 0, "timed out"); |
| 134 | else |
| 135 | return std::make_tuple(packet, header->caplen, ""); |
| 136 | } |
| 137 | |
| 138 | } // namespace face |
| 139 | } // namespace nfd |