Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | 0d82d04 | 2017-07-07 06:15:27 +0000 | [diff] [blame] | 2 | /* |
Davide Pesavento | a314808 | 2018-04-12 18:21:54 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2018, Regents of the University of California, |
Alexander Afanasyev | 319f2c8 | 2015-01-07 14:56:53 -0800 | [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/>. |
Junxiao Shi | 3ffe66d | 2014-11-06 15:37:59 -0700 | [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 | #include "ethernet-transport.hpp" |
Davide Pesavento | 43ff2a9 | 2017-05-18 19:50:57 -0400 | [diff] [blame] | 27 | #include "ethernet-protocol.hpp" |
Davide Pesavento | f8b41eb | 2014-12-26 19:14:06 +0100 | [diff] [blame] | 28 | #include "core/global-io.hpp" |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 29 | |
| 30 | #include <pcap/pcap.h> |
| 31 | |
Davide Pesavento | 457f135 | 2018-07-14 17:17:38 -0400 | [diff] [blame^] | 32 | #include <cstring> // for memcpy() |
| 33 | |
| 34 | #include <boost/endian/conversion.hpp> |
Alexander Afanasyev | d9479aa | 2014-06-26 13:52:02 -0700 | [diff] [blame] | 35 | |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 36 | namespace nfd { |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 37 | namespace face { |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 38 | |
Davide Pesavento | a314808 | 2018-04-12 18:21:54 -0400 | [diff] [blame] | 39 | NFD_LOG_INIT(EthernetTransport); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 40 | |
Junxiao Shi | 0d82d04 | 2017-07-07 06:15:27 +0000 | [diff] [blame] | 41 | EthernetTransport::EthernetTransport(const ndn::net::NetworkInterface& localEndpoint, |
Davide Pesavento | 6bd6d0b | 2017-03-25 15:16:40 -0400 | [diff] [blame] | 42 | const ethernet::Address& remoteEndpoint) |
Davide Pesavento | fe0580c | 2017-05-12 02:02:10 -0400 | [diff] [blame] | 43 | : m_socket(getGlobalIoService()) |
Junxiao Shi | 0d82d04 | 2017-07-07 06:15:27 +0000 | [diff] [blame] | 44 | , m_pcap(localEndpoint.getName()) |
| 45 | , m_srcAddress(localEndpoint.getEthernetAddress()) |
Davide Pesavento | 6bd6d0b | 2017-03-25 15:16:40 -0400 | [diff] [blame] | 46 | , m_destAddress(remoteEndpoint) |
Junxiao Shi | 0d82d04 | 2017-07-07 06:15:27 +0000 | [diff] [blame] | 47 | , m_interfaceName(localEndpoint.getName()) |
Davide Pesavento | 0b0a71d | 2017-07-02 13:57:36 -0400 | [diff] [blame] | 48 | , m_hasRecentlyReceived(false) |
Davide Pesavento | 9a090a0 | 2015-01-29 18:15:26 +0100 | [diff] [blame] | 49 | #ifdef _DEBUG |
| 50 | , m_nDropped(0) |
| 51 | #endif |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 52 | { |
Davide Pesavento | fe0580c | 2017-05-12 02:02:10 -0400 | [diff] [blame] | 53 | try { |
| 54 | m_pcap.activate(DLT_EN10MB); |
| 55 | m_socket.assign(m_pcap.getFd()); |
| 56 | } |
| 57 | catch (const PcapHelper::Error& e) { |
| 58 | BOOST_THROW_EXCEPTION(Error(e.what())); |
| 59 | } |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 60 | |
Davide Pesavento | 43ff2a9 | 2017-05-18 19:50:57 -0400 | [diff] [blame] | 61 | asyncRead(); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 62 | } |
| 63 | |
Davide Pesavento | 6bd6d0b | 2017-03-25 15:16:40 -0400 | [diff] [blame] | 64 | void |
Davide Pesavento | 6bd6d0b | 2017-03-25 15:16:40 -0400 | [diff] [blame] | 65 | EthernetTransport::doClose() |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 66 | { |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 67 | NFD_LOG_FACE_TRACE(__func__); |
Alexander Afanasyev | 251f3c1 | 2014-06-02 18:39:58 +0300 | [diff] [blame] | 68 | |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 69 | if (m_socket.is_open()) { |
| 70 | // Cancel all outstanding operations and close the socket. |
| 71 | // Use the non-throwing variants and ignore errors, if any. |
| 72 | boost::system::error_code error; |
| 73 | m_socket.cancel(error); |
| 74 | m_socket.close(error); |
| 75 | } |
Davide Pesavento | fe0580c | 2017-05-12 02:02:10 -0400 | [diff] [blame] | 76 | m_pcap.close(); |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 77 | |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 78 | // Ensure that the Transport stays alive at least |
| 79 | // until all pending handlers are dispatched |
| 80 | getGlobalIoService().post([this] { |
| 81 | this->setState(TransportState::CLOSED); |
| 82 | }); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | void |
Davide Pesavento | fe0580c | 2017-05-12 02:02:10 -0400 | [diff] [blame] | 86 | EthernetTransport::doSend(Transport::Packet&& packet) |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 87 | { |
Davide Pesavento | fe0580c | 2017-05-12 02:02:10 -0400 | [diff] [blame] | 88 | NFD_LOG_FACE_TRACE(__func__); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 89 | |
Davide Pesavento | fe0580c | 2017-05-12 02:02:10 -0400 | [diff] [blame] | 90 | sendPacket(packet.packet); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 91 | } |
| 92 | |
Davide Pesavento | 10783f2 | 2014-03-15 04:40:01 +0100 | [diff] [blame] | 93 | void |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 94 | EthernetTransport::sendPacket(const ndn::Block& block) |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 95 | { |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 96 | ndn::EncodingBuffer buffer(block); |
| 97 | |
Davide Pesavento | e463100 | 2014-03-21 20:55:36 +0100 | [diff] [blame] | 98 | // pad with zeroes if the payload is too short |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 99 | if (block.size() < ethernet::MIN_DATA_LEN) { |
| 100 | static const uint8_t padding[ethernet::MIN_DATA_LEN] = {}; |
| 101 | buffer.appendByteArray(padding, ethernet::MIN_DATA_LEN - block.size()); |
| 102 | } |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 103 | |
| 104 | // construct and prepend the ethernet header |
Davide Pesavento | 457f135 | 2018-07-14 17:17:38 -0400 | [diff] [blame^] | 105 | uint16_t ethertype = boost::endian::native_to_big(ethernet::ETHERTYPE_NDN); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 106 | buffer.prependByteArray(reinterpret_cast<const uint8_t*>(ðertype), ethernet::TYPE_LEN); |
Davide Pesavento | b60cc12 | 2014-03-19 19:26:02 +0100 | [diff] [blame] | 107 | buffer.prependByteArray(m_srcAddress.data(), m_srcAddress.size()); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 108 | buffer.prependByteArray(m_destAddress.data(), m_destAddress.size()); |
| 109 | |
Davide Pesavento | 43ff2a9 | 2017-05-18 19:50:57 -0400 | [diff] [blame] | 110 | // send the frame |
Davide Pesavento | fe0580c | 2017-05-12 02:02:10 -0400 | [diff] [blame] | 111 | int sent = pcap_inject(m_pcap, buffer.buf(), buffer.size()); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 112 | if (sent < 0) |
Davide Pesavento | 1816d4b | 2017-07-02 12:20:48 -0400 | [diff] [blame] | 113 | handleError("Send operation failed: " + m_pcap.getLastError()); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 114 | else if (static_cast<size_t>(sent) < buffer.size()) |
Davide Pesavento | 1816d4b | 2017-07-02 12:20:48 -0400 | [diff] [blame] | 115 | handleError("Failed to send the full frame: size=" + to_string(buffer.size()) + |
| 116 | " sent=" + to_string(sent)); |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 117 | else |
| 118 | // print block size because we don't want to count the padding in buffer |
| 119 | NFD_LOG_FACE_TRACE("Successfully sent: " << block.size() << " bytes"); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | void |
Davide Pesavento | 43ff2a9 | 2017-05-18 19:50:57 -0400 | [diff] [blame] | 123 | EthernetTransport::asyncRead() |
| 124 | { |
| 125 | m_socket.async_read_some(boost::asio::null_buffers(), |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 126 | [this] (const auto& e, auto) { this->handleRead(e); }); |
Davide Pesavento | 43ff2a9 | 2017-05-18 19:50:57 -0400 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | void |
| 130 | EthernetTransport::handleRead(const boost::system::error_code& error) |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 131 | { |
Davide Pesavento | 1816d4b | 2017-07-02 12:20:48 -0400 | [diff] [blame] | 132 | if (error) { |
| 133 | // boost::asio::error::operation_aborted must be checked first: in that case, the Transport |
| 134 | // may already have been destructed, therefore it's unsafe to call getState() or do logging. |
| 135 | if (error != boost::asio::error::operation_aborted && |
| 136 | getState() != TransportState::CLOSING && |
| 137 | getState() != TransportState::FAILED && |
| 138 | getState() != TransportState::CLOSED) { |
| 139 | handleError("Receive operation failed: " + error.message()); |
| 140 | } |
| 141 | return; |
| 142 | } |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 143 | |
Davide Pesavento | fe0580c | 2017-05-12 02:02:10 -0400 | [diff] [blame] | 144 | const uint8_t* pkt; |
| 145 | size_t len; |
| 146 | std::string err; |
| 147 | std::tie(pkt, len, err) = m_pcap.readNextPacket(); |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 148 | |
Davide Pesavento | 43ff2a9 | 2017-05-18 19:50:57 -0400 | [diff] [blame] | 149 | if (pkt == nullptr) { |
Davide Pesavento | 1816d4b | 2017-07-02 12:20:48 -0400 | [diff] [blame] | 150 | NFD_LOG_FACE_WARN("Read error: " << err); |
Davide Pesavento | 43ff2a9 | 2017-05-18 19:50:57 -0400 | [diff] [blame] | 151 | } |
| 152 | else { |
| 153 | const ether_header* eh; |
| 154 | std::tie(eh, err) = ethernet::checkFrameHeader(pkt, len, m_srcAddress, |
| 155 | m_destAddress.isMulticast() ? m_destAddress : m_srcAddress); |
| 156 | if (eh == nullptr) { |
Davide Pesavento | 1816d4b | 2017-07-02 12:20:48 -0400 | [diff] [blame] | 157 | NFD_LOG_FACE_WARN(err); |
Davide Pesavento | 43ff2a9 | 2017-05-18 19:50:57 -0400 | [diff] [blame] | 158 | } |
| 159 | else { |
| 160 | ethernet::Address sender(eh->ether_shost); |
| 161 | pkt += ethernet::HDR_LEN; |
| 162 | len -= ethernet::HDR_LEN; |
| 163 | receivePayload(pkt, len, sender); |
| 164 | } |
| 165 | } |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 166 | |
Davide Pesavento | 9a090a0 | 2015-01-29 18:15:26 +0100 | [diff] [blame] | 167 | #ifdef _DEBUG |
Davide Pesavento | fe0580c | 2017-05-12 02:02:10 -0400 | [diff] [blame] | 168 | size_t nDropped = m_pcap.getNDropped(); |
| 169 | if (nDropped - m_nDropped > 0) |
Davide Pesavento | 43ff2a9 | 2017-05-18 19:50:57 -0400 | [diff] [blame] | 170 | NFD_LOG_FACE_DEBUG("Detected " << nDropped - m_nDropped << " dropped frame(s)"); |
Davide Pesavento | fe0580c | 2017-05-12 02:02:10 -0400 | [diff] [blame] | 171 | m_nDropped = nDropped; |
Davide Pesavento | 9a090a0 | 2015-01-29 18:15:26 +0100 | [diff] [blame] | 172 | #endif |
| 173 | |
Davide Pesavento | 43ff2a9 | 2017-05-18 19:50:57 -0400 | [diff] [blame] | 174 | asyncRead(); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | void |
Davide Pesavento | 43ff2a9 | 2017-05-18 19:50:57 -0400 | [diff] [blame] | 178 | EthernetTransport::receivePayload(const uint8_t* payload, size_t length, |
| 179 | const ethernet::Address& sender) |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 180 | { |
Davide Pesavento | 1816d4b | 2017-07-02 12:20:48 -0400 | [diff] [blame] | 181 | NFD_LOG_FACE_TRACE("Received: " << length << " bytes from " << sender); |
| 182 | |
Junxiao Shi | 78926c9 | 2015-02-28 22:56:06 -0700 | [diff] [blame] | 183 | bool isOk = false; |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 184 | Block element; |
Davide Pesavento | 43ff2a9 | 2017-05-18 19:50:57 -0400 | [diff] [blame] | 185 | std::tie(isOk, element) = Block::fromBuffer(payload, length); |
Junxiao Shi | 083f778 | 2015-02-21 12:06:26 -0700 | [diff] [blame] | 186 | if (!isOk) { |
Davide Pesavento | 1816d4b | 2017-07-02 12:20:48 -0400 | [diff] [blame] | 187 | NFD_LOG_FACE_WARN("Failed to parse incoming packet from " << sender); |
| 188 | // This packet won't extend the face lifetime |
Junxiao Shi | 083f778 | 2015-02-21 12:06:26 -0700 | [diff] [blame] | 189 | return; |
| 190 | } |
Davide Pesavento | 0b0a71d | 2017-07-02 13:57:36 -0400 | [diff] [blame] | 191 | m_hasRecentlyReceived = true; |
Matteo Sammarco | 66df974 | 2014-11-21 18:31:26 +0100 | [diff] [blame] | 192 | |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 193 | Transport::Packet tp(std::move(element)); |
| 194 | static_assert(sizeof(tp.remoteEndpoint) >= ethernet::ADDR_LEN, |
| 195 | "Transport::Packet::remoteEndpoint is too small"); |
Davide Pesavento | 43ff2a9 | 2017-05-18 19:50:57 -0400 | [diff] [blame] | 196 | if (m_destAddress.isMulticast()) { |
| 197 | std::memcpy(&tp.remoteEndpoint, sender.data(), sender.size()); |
| 198 | } |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 199 | this->receive(std::move(tp)); |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | void |
Davide Pesavento | 1816d4b | 2017-07-02 12:20:48 -0400 | [diff] [blame] | 203 | EthernetTransport::handleError(const std::string& errorMessage) |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 204 | { |
Davide Pesavento | 1816d4b | 2017-07-02 12:20:48 -0400 | [diff] [blame] | 205 | if (getPersistency() == ndn::nfd::FACE_PERSISTENCY_PERMANENT) { |
| 206 | NFD_LOG_FACE_DEBUG("Permanent face ignores error: " << errorMessage); |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 207 | return; |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 208 | } |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 209 | |
Davide Pesavento | 1816d4b | 2017-07-02 12:20:48 -0400 | [diff] [blame] | 210 | NFD_LOG_FACE_ERROR(errorMessage); |
| 211 | this->setState(TransportState::FAILED); |
| 212 | doClose(); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 213 | } |
| 214 | |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 215 | } // namespace face |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 216 | } // namespace nfd |