Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Steve DiBenedetto | ef04f27 | 2014-06-04 14:28:31 -0600 | [diff] [blame] | 3 | * Copyright (c) 2014, 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 |
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 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 26 | #ifndef NFD_DAEMON_FACE_ETHERNET_FACE_HPP |
| 27 | #define NFD_DAEMON_FACE_ETHERNET_FACE_HPP |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 28 | |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 29 | #include "common.hpp" |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 30 | #include "face.hpp" |
Matteo Sammarco | 66df974 | 2014-11-21 18:31:26 +0100 | [diff] [blame] | 31 | #include "ndnlp-partial-message-store.hpp" |
| 32 | #include "ndnlp-slicer.hpp" |
Junxiao Shi | a1937bf | 2014-11-06 11:43:40 -0700 | [diff] [blame] | 33 | #include "core/network-interface.hpp" |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 34 | |
Matteo Sammarco | 66df974 | 2014-11-21 18:31:26 +0100 | [diff] [blame] | 35 | #include <unordered_map> |
| 36 | |
Alexander Afanasyev | 885a85b | 2014-04-12 21:01:13 -0700 | [diff] [blame] | 37 | #ifndef HAVE_LIBPCAP |
| 38 | #error "Cannot include this file when libpcap is not available" |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 39 | #endif |
| 40 | |
| 41 | // forward declarations |
| 42 | struct pcap; |
| 43 | typedef pcap pcap_t; |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 44 | struct pcap_pkthdr; |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 45 | |
| 46 | namespace nfd { |
| 47 | |
| 48 | /** |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 49 | * @brief Implementation of Face abstraction that uses raw |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 50 | * Ethernet frames as underlying transport mechanism |
| 51 | */ |
| 52 | class EthernetFace : public Face |
| 53 | { |
| 54 | public: |
| 55 | /** |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 56 | * @brief EthernetFace-related error |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 57 | */ |
| 58 | struct Error : public Face::Error |
| 59 | { |
| 60 | Error(const std::string& what) : Face::Error(what) {} |
| 61 | }; |
| 62 | |
| 63 | EthernetFace(const shared_ptr<boost::asio::posix::stream_descriptor>& socket, |
Davide Pesavento | b499a60 | 2014-11-18 22:36:56 +0100 | [diff] [blame] | 64 | const NetworkInterfaceInfo& interface, |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 65 | const ethernet::Address& address); |
| 66 | |
| 67 | virtual |
| 68 | ~EthernetFace(); |
| 69 | |
| 70 | /// send an Interest |
| 71 | virtual void |
| 72 | sendInterest(const Interest& interest); |
| 73 | |
| 74 | /// send a Data |
| 75 | virtual void |
| 76 | sendData(const Data& data); |
| 77 | |
| 78 | /** |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 79 | * @brief Closes the face |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 80 | * |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 81 | * This terminates all communication on the face and triggers the onFail() event. |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 82 | */ |
| 83 | virtual void |
| 84 | close(); |
| 85 | |
| 86 | private: |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 87 | /** |
| 88 | * @brief Allocates and initializes a libpcap context for live capture |
| 89 | */ |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 90 | void |
| 91 | pcapInit(); |
| 92 | |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 93 | /** |
| 94 | * @brief Installs a BPF filter on the receiving socket |
| 95 | * |
| 96 | * @param filterString string containing the source BPF program |
| 97 | */ |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 98 | void |
| 99 | setPacketFilter(const char* filterString); |
| 100 | |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 101 | /** |
| 102 | * @brief Enables receiving frames addressed to our MAC multicast group |
| 103 | */ |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 104 | void |
Davide Pesavento | 10783f2 | 2014-03-15 04:40:01 +0100 | [diff] [blame] | 105 | joinMulticastGroup(); |
| 106 | |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 107 | /** |
| 108 | * @brief Sends the specified TLV block on the network wrapped in an Ethernet frame |
| 109 | */ |
Davide Pesavento | 10783f2 | 2014-03-15 04:40:01 +0100 | [diff] [blame] | 110 | void |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 111 | sendPacket(const ndn::Block& block); |
| 112 | |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 113 | /** |
| 114 | * @brief Receive callback |
| 115 | */ |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 116 | void |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 117 | handleRead(const boost::system::error_code& error, size_t nBytesRead); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 118 | |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 119 | PUBLIC_WITH_TESTS_ELSE_PRIVATE: |
| 120 | /** |
| 121 | * @brief Processes an incoming frame as captured by libpcap |
| 122 | * |
| 123 | * @param header pointer to capture metadata |
| 124 | * @param packet pointer to the received frame, including the link-layer header |
| 125 | */ |
| 126 | void |
| 127 | processIncomingPacket(const pcap_pkthdr* header, const uint8_t* packet); |
| 128 | |
| 129 | private: |
| 130 | /** |
| 131 | * @brief Handles errors encountered by Boost.Asio on the receive path |
| 132 | */ |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 133 | void |
| 134 | processErrorCode(const boost::system::error_code& error); |
| 135 | |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 136 | /** |
| 137 | * @brief Returns the MTU of the underlying network interface |
| 138 | */ |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 139 | size_t |
| 140 | getInterfaceMtu() const; |
| 141 | |
| 142 | private: |
Matteo Sammarco | 66df974 | 2014-11-21 18:31:26 +0100 | [diff] [blame] | 143 | struct Reassembler |
| 144 | { |
| 145 | unique_ptr<ndnlp::PartialMessageStore> pms; |
| 146 | EventId expireEvent; |
| 147 | }; |
| 148 | |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 149 | unique_ptr<pcap_t, void(*)(pcap_t*)> m_pcap; |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 150 | shared_ptr<boost::asio::posix::stream_descriptor> m_socket; |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 151 | |
Davide Pesavento | 10783f2 | 2014-03-15 04:40:01 +0100 | [diff] [blame] | 152 | #if defined(__linux__) |
| 153 | int m_interfaceIndex; |
| 154 | #endif |
Davide Pesavento | b60cc12 | 2014-03-19 19:26:02 +0100 | [diff] [blame] | 155 | std::string m_interfaceName; |
| 156 | ethernet::Address m_srcAddress; |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 157 | ethernet::Address m_destAddress; |
Matteo Sammarco | 66df974 | 2014-11-21 18:31:26 +0100 | [diff] [blame] | 158 | |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 159 | size_t m_interfaceMtu; |
Matteo Sammarco | 66df974 | 2014-11-21 18:31:26 +0100 | [diff] [blame] | 160 | unique_ptr<ndnlp::Slicer> m_slicer; |
| 161 | std::unordered_map<ethernet::Address, Reassembler> m_reassemblers; |
| 162 | static const time::nanoseconds REASSEMBLER_LIFETIME; |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 163 | }; |
| 164 | |
| 165 | } // namespace nfd |
| 166 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 167 | #endif // NFD_DAEMON_FACE_ETHERNET_FACE_HPP |