Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Yanbiao Li | 32dab97 | 2016-11-27 12:26:09 +0800 | [diff] [blame^] | 3 | * Copyright (c) 2014-2017, Regents of the University of California, |
Junxiao Shi | 1e46be3 | 2015-01-08 20:18:05 -0700 | [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. |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 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/>. |
Steve DiBenedetto | ef04f27 | 2014-06-04 14:28:31 -0600 | [diff] [blame] | 24 | */ |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 25 | |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 26 | #ifndef NFD_DAEMON_FACE_ETHERNET_TRANSPORT_HPP |
| 27 | #define NFD_DAEMON_FACE_ETHERNET_TRANSPORT_HPP |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 28 | |
Junxiao Shi | 9f5b01d | 2016-08-05 03:54:28 +0000 | [diff] [blame] | 29 | #include "core/common.hpp" |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 30 | #include "transport.hpp" |
Junxiao Shi | a1937bf | 2014-11-06 11:43:40 -0700 | [diff] [blame] | 31 | #include "core/network-interface.hpp" |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 32 | |
Alexander Afanasyev | 885a85b | 2014-04-12 21:01:13 -0700 | [diff] [blame] | 33 | #ifndef HAVE_LIBPCAP |
| 34 | #error "Cannot include this file when libpcap is not available" |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 35 | #endif |
| 36 | |
| 37 | // forward declarations |
| 38 | struct pcap; |
| 39 | typedef pcap pcap_t; |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 40 | struct pcap_pkthdr; |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 41 | |
| 42 | namespace nfd { |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 43 | namespace face { |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 44 | |
| 45 | /** |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 46 | * \brief A multicast Transport that uses raw Ethernet II frames |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 47 | */ |
Davide Pesavento | b84bd3a | 2016-04-22 02:21:45 +0200 | [diff] [blame] | 48 | class EthernetTransport final : public Transport |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 49 | { |
| 50 | public: |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 51 | class Error : public std::runtime_error |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 52 | { |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 53 | public: |
| 54 | explicit |
| 55 | Error(const std::string& what) |
| 56 | : std::runtime_error(what) |
| 57 | { |
| 58 | } |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 59 | }; |
| 60 | |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 61 | /** |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 62 | * @brief Creates an Ethernet-based transport for multicast communication |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 63 | */ |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 64 | EthernetTransport(const NetworkInterfaceInfo& interface, |
| 65 | const ethernet::Address& mcastAddress); |
| 66 | |
| 67 | protected: |
Yanbiao Li | 32dab97 | 2016-11-27 12:26:09 +0800 | [diff] [blame^] | 68 | void |
Davide Pesavento | b84bd3a | 2016-04-22 02:21:45 +0200 | [diff] [blame] | 69 | doClose() final; |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 70 | |
| 71 | private: |
Yanbiao Li | 32dab97 | 2016-11-27 12:26:09 +0800 | [diff] [blame^] | 72 | void |
Davide Pesavento | b84bd3a | 2016-04-22 02:21:45 +0200 | [diff] [blame] | 73 | doSend(Transport::Packet&& packet) final; |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 74 | |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 75 | /** |
| 76 | * @brief Allocates and initializes a libpcap context for live capture |
| 77 | */ |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 78 | void |
| 79 | pcapInit(); |
| 80 | |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 81 | /** |
| 82 | * @brief Installs a BPF filter on the receiving socket |
| 83 | * |
| 84 | * @param filterString string containing the source BPF program |
| 85 | */ |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 86 | void |
| 87 | setPacketFilter(const char* filterString); |
| 88 | |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 89 | /** |
| 90 | * @brief Enables receiving frames addressed to our MAC multicast group |
Davide Pesavento | f8b41eb | 2014-12-26 19:14:06 +0100 | [diff] [blame] | 91 | * |
| 92 | * @return true if successful, false otherwise |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 93 | */ |
Davide Pesavento | f8b41eb | 2014-12-26 19:14:06 +0100 | [diff] [blame] | 94 | bool |
Davide Pesavento | 10783f2 | 2014-03-15 04:40:01 +0100 | [diff] [blame] | 95 | joinMulticastGroup(); |
| 96 | |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 97 | /** |
| 98 | * @brief Sends the specified TLV block on the network wrapped in an Ethernet frame |
| 99 | */ |
Davide Pesavento | 10783f2 | 2014-03-15 04:40:01 +0100 | [diff] [blame] | 100 | void |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 101 | sendPacket(const ndn::Block& block); |
| 102 | |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 103 | /** |
| 104 | * @brief Receive callback |
| 105 | */ |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 106 | void |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 107 | handleRead(const boost::system::error_code& error, size_t nBytesRead); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 108 | |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 109 | PUBLIC_WITH_TESTS_ELSE_PRIVATE: |
| 110 | /** |
| 111 | * @brief Processes an incoming frame as captured by libpcap |
| 112 | * |
| 113 | * @param header pointer to capture metadata |
| 114 | * @param packet pointer to the received frame, including the link-layer header |
| 115 | */ |
| 116 | void |
| 117 | processIncomingPacket(const pcap_pkthdr* header, const uint8_t* packet); |
| 118 | |
| 119 | private: |
| 120 | /** |
| 121 | * @brief Handles errors encountered by Boost.Asio on the receive path |
| 122 | */ |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 123 | void |
| 124 | processErrorCode(const boost::system::error_code& error); |
| 125 | |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 126 | /** |
| 127 | * @brief Returns the MTU of the underlying network interface |
| 128 | */ |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 129 | size_t |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 130 | getInterfaceMtu(); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 131 | |
| 132 | private: |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 133 | unique_ptr<pcap_t, void(*)(pcap_t*)> m_pcap; |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 134 | boost::asio::posix::stream_descriptor m_socket; |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 135 | |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 136 | ethernet::Address m_srcAddress; |
| 137 | ethernet::Address m_destAddress; |
| 138 | std::string m_interfaceName; |
Davide Pesavento | 10783f2 | 2014-03-15 04:40:01 +0100 | [diff] [blame] | 139 | #if defined(__linux__) |
| 140 | int m_interfaceIndex; |
| 141 | #endif |
Davide Pesavento | 9a090a0 | 2015-01-29 18:15:26 +0100 | [diff] [blame] | 142 | |
| 143 | #ifdef _DEBUG |
| 144 | /// number of packets dropped by the kernel, as reported by libpcap |
| 145 | unsigned int m_nDropped; |
| 146 | #endif |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 147 | }; |
| 148 | |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 149 | } // namespace face |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 150 | } // namespace nfd |
| 151 | |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 152 | #endif // NFD_DAEMON_FACE_ETHERNET_TRANSPORT_HPP |