blob: 065919e25f4c4e136925e2b9500b4ee885a84390 [file] [log] [blame]
Davide Pesavento44deacc2014-02-19 10:48:07 +01001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi0d82d042017-07-07 06:15:27 +00002/*
Davide Pesavento91c15c82024-01-15 17:14:23 -05003 * Copyright (c) 2014-2024, Regents of the University of California,
Junxiao Shi1e46be32015-01-08 20:18:05 -07004 * 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 Afanasyev9bcbc7c2014-04-06 19:37:37 -070010 *
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 DiBenedettoef04f272014-06-04 14:28:31 -060024 */
Davide Pesavento44deacc2014-02-19 10:48:07 +010025
Davide Pesavento35120ea2015-11-17 21:13:18 +010026#ifndef NFD_DAEMON_FACE_ETHERNET_TRANSPORT_HPP
27#define NFD_DAEMON_FACE_ETHERNET_TRANSPORT_HPP
Davide Pesavento44deacc2014-02-19 10:48:07 +010028
Junxiao Shi0d82d042017-07-07 06:15:27 +000029#include "ethernet-protocol.hpp"
Davide Pesaventofe0580c2017-05-12 02:02:10 -040030#include "pcap-helper.hpp"
Davide Pesavento35120ea2015-11-17 21:13:18 +010031#include "transport.hpp"
Davide Pesaventob3a23ca2019-05-04 20:40:21 -040032
Davide Pesaventoa9b09b62022-06-04 14:07:25 -040033#include <boost/asio/posix/stream_descriptor.hpp>
Junxiao Shi0d82d042017-07-07 06:15:27 +000034#include <ndn-cxx/net/network-interface.hpp>
Davide Pesavento44deacc2014-02-19 10:48:07 +010035
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040036namespace nfd::face {
Davide Pesavento44deacc2014-02-19 10:48:07 +010037
38/**
Davide Pesaventoc0df94e2023-10-08 19:26:55 -040039 * @brief Base class for Ethernet-based Transports.
Davide Pesavento44deacc2014-02-19 10:48:07 +010040 */
Davide Pesavento6bd6d0b2017-03-25 15:16:40 -040041class EthernetTransport : public Transport
Davide Pesavento44deacc2014-02-19 10:48:07 +010042{
43public:
Davide Pesavento35120ea2015-11-17 21:13:18 +010044 class Error : public std::runtime_error
Davide Pesavento44deacc2014-02-19 10:48:07 +010045 {
Davide Pesavento35120ea2015-11-17 21:13:18 +010046 public:
Davide Pesaventob3a23ca2019-05-04 20:40:21 -040047 using std::runtime_error::runtime_error;
Davide Pesavento44deacc2014-02-19 10:48:07 +010048 };
49
Davide Pesavento43ff2a92017-05-18 19:50:57 -040050 /**
Davide Pesaventoc0df94e2023-10-08 19:26:55 -040051 * @brief Processes the payload of an incoming frame.
Davide Pesaventoa599d2a2022-02-16 18:52:43 -050052 * @param payload Payload bytes, starting from the first byte after the Ethernet header
Davide Pesavento43ff2a92017-05-18 19:50:57 -040053 * @param sender Sender address
54 */
55 void
Davide Pesaventoa599d2a2022-02-16 18:52:43 -050056 receivePayload(span<const uint8_t> payload, const ethernet::Address& sender);
Davide Pesavento43ff2a92017-05-18 19:50:57 -040057
Davide Pesavento35120ea2015-11-17 21:13:18 +010058protected:
Junxiao Shi0d82d042017-07-07 06:15:27 +000059 EthernetTransport(const ndn::net::NetworkInterface& localEndpoint,
Davide Pesavento6bd6d0b2017-03-25 15:16:40 -040060 const ethernet::Address& remoteEndpoint);
61
Yanbiao Li32dab972016-11-27 12:26:09 +080062 void
Davide Pesaventob84bd3a2016-04-22 02:21:45 +020063 doClose() final;
Davide Pesavento44deacc2014-02-19 10:48:07 +010064
Davide Pesavento1816d4b2017-07-02 12:20:48 -040065 bool
Davide Pesavento0b0a71d2017-07-02 13:57:36 -040066 hasRecentlyReceived() const
Davide Pesavento1816d4b2017-07-02 12:20:48 -040067 {
Davide Pesavento0b0a71d2017-07-02 13:57:36 -040068 return m_hasRecentlyReceived;
Davide Pesavento1816d4b2017-07-02 12:20:48 -040069 }
70
71 void
Davide Pesavento0b0a71d2017-07-02 13:57:36 -040072 resetRecentlyReceived()
Davide Pesavento1816d4b2017-07-02 12:20:48 -040073 {
Davide Pesavento0b0a71d2017-07-02 13:57:36 -040074 m_hasRecentlyReceived = false;
Davide Pesavento1816d4b2017-07-02 12:20:48 -040075 }
76
Davide Pesavento6bd6d0b2017-03-25 15:16:40 -040077private:
Davide Pesavento44deacc2014-02-19 10:48:07 +010078 void
Junxiao Shifbdd5ce2018-11-26 15:10:51 +000079 handleNetifStateChange(ndn::net::InterfaceState netifState);
80
81 void
Teng Liang13d582a2020-07-21 20:23:11 -070082 doSend(const Block& packet) final;
Davide Pesavento10783f22014-03-15 04:40:01 +010083
Davide Pesavento7726ae52014-11-23 21:01:05 +010084 /**
Davide Pesaventoc0df94e2023-10-08 19:26:55 -040085 * @brief Sends the specified TLV block on the network wrapped in an Ethernet frame.
Davide Pesavento7726ae52014-11-23 21:01:05 +010086 */
Davide Pesavento10783f22014-03-15 04:40:01 +010087 void
Davide Pesavento44deacc2014-02-19 10:48:07 +010088 sendPacket(const ndn::Block& block);
89
90 void
Davide Pesavento43ff2a92017-05-18 19:50:57 -040091 asyncRead();
Davide Pesavento44deacc2014-02-19 10:48:07 +010092
Davide Pesavento7726ae52014-11-23 21:01:05 +010093 void
Davide Pesavento43ff2a92017-05-18 19:50:57 -040094 handleRead(const boost::system::error_code& error);
Davide Pesavento7726ae52014-11-23 21:01:05 +010095
Davide Pesavento44deacc2014-02-19 10:48:07 +010096 void
Davide Pesavento1816d4b2017-07-02 12:20:48 -040097 handleError(const std::string& errorMessage);
Davide Pesavento44deacc2014-02-19 10:48:07 +010098
Davide Pesavento6bd6d0b2017-03-25 15:16:40 -040099protected:
Davide Pesavento292e5e12015-03-13 02:08:33 +0100100 boost::asio::posix::stream_descriptor m_socket;
Davide Pesaventofe0580c2017-05-12 02:02:10 -0400101 PcapHelper m_pcap;
Davide Pesavento35120ea2015-11-17 21:13:18 +0100102 ethernet::Address m_srcAddress;
103 ethernet::Address m_destAddress;
104 std::string m_interfaceName;
Davide Pesavento9a090a02015-01-29 18:15:26 +0100105
Davide Pesavento6bd6d0b2017-03-25 15:16:40 -0400106private:
Eric Newberryf3ee8082020-01-28 13:44:18 -0800107 signal::ScopedConnection m_netifStateChangedConn;
108 signal::ScopedConnection m_netifMtuChangedConn;
Davide Pesavento91c15c82024-01-15 17:14:23 -0500109 bool m_hasRecentlyReceived = false;
110#ifndef NDEBUG
111 /// Number of frames dropped by the kernel, as reported by libpcap
112 size_t m_nDropped = 0;
Davide Pesavento9a090a02015-01-29 18:15:26 +0100113#endif
Davide Pesavento44deacc2014-02-19 10:48:07 +0100114};
115
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400116} // namespace nfd::face
Davide Pesavento44deacc2014-02-19 10:48:07 +0100117
Davide Pesavento35120ea2015-11-17 21:13:18 +0100118#endif // NFD_DAEMON_FACE_ETHERNET_TRANSPORT_HPP