blob: 57a7edc6d638fd58d0d90bb910048cb4ed1875c9 [file] [log] [blame]
Davide Pesavento44deacc2014-02-19 10:48:07 +01001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Yanbiao Li32dab972016-11-27 12:26:09 +08003 * Copyright (c) 2014-2017, 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
Davide Pesaventofe0580c2017-05-12 02:02:10 -040029#include "pcap-helper.hpp"
Davide Pesavento35120ea2015-11-17 21:13:18 +010030#include "transport.hpp"
Junxiao Shia1937bf2014-11-06 11:43:40 -070031#include "core/network-interface.hpp"
Davide Pesavento44deacc2014-02-19 10:48:07 +010032
Davide Pesavento44deacc2014-02-19 10:48:07 +010033namespace nfd {
Davide Pesavento35120ea2015-11-17 21:13:18 +010034namespace face {
Davide Pesavento44deacc2014-02-19 10:48:07 +010035
36/**
Davide Pesavento6bd6d0b2017-03-25 15:16:40 -040037 * @brief Base class for Ethernet-based Transports
Davide Pesavento44deacc2014-02-19 10:48:07 +010038 */
Davide Pesavento6bd6d0b2017-03-25 15:16:40 -040039class EthernetTransport : public Transport
Davide Pesavento44deacc2014-02-19 10:48:07 +010040{
41public:
Davide Pesavento35120ea2015-11-17 21:13:18 +010042 class Error : public std::runtime_error
Davide Pesavento44deacc2014-02-19 10:48:07 +010043 {
Davide Pesavento35120ea2015-11-17 21:13:18 +010044 public:
45 explicit
46 Error(const std::string& what)
47 : std::runtime_error(what)
48 {
49 }
Davide Pesavento44deacc2014-02-19 10:48:07 +010050 };
51
Davide Pesavento35120ea2015-11-17 21:13:18 +010052protected:
Davide Pesavento6bd6d0b2017-03-25 15:16:40 -040053 EthernetTransport(const NetworkInterfaceInfo& localEndpoint,
54 const ethernet::Address& remoteEndpoint);
55
Yanbiao Li32dab972016-11-27 12:26:09 +080056 void
Davide Pesaventob84bd3a2016-04-22 02:21:45 +020057 doClose() final;
Davide Pesavento44deacc2014-02-19 10:48:07 +010058
Davide Pesavento6bd6d0b2017-03-25 15:16:40 -040059private:
Davide Pesavento44deacc2014-02-19 10:48:07 +010060 void
Davide Pesavento6bd6d0b2017-03-25 15:16:40 -040061 doSend(Transport::Packet&& packet) final;
Davide Pesavento10783f22014-03-15 04:40:01 +010062
Davide Pesavento7726ae52014-11-23 21:01:05 +010063 /**
64 * @brief Sends the specified TLV block on the network wrapped in an Ethernet frame
65 */
Davide Pesavento10783f22014-03-15 04:40:01 +010066 void
Davide Pesavento44deacc2014-02-19 10:48:07 +010067 sendPacket(const ndn::Block& block);
68
Davide Pesavento7726ae52014-11-23 21:01:05 +010069 /**
Davide Pesaventofe0580c2017-05-12 02:02:10 -040070 * @brief async_read_some() callback
Davide Pesavento7726ae52014-11-23 21:01:05 +010071 */
Davide Pesavento44deacc2014-02-19 10:48:07 +010072 void
Davide Pesavento7726ae52014-11-23 21:01:05 +010073 handleRead(const boost::system::error_code& error, size_t nBytesRead);
Davide Pesavento44deacc2014-02-19 10:48:07 +010074
Davide Pesavento7726ae52014-11-23 21:01:05 +010075 /**
Davide Pesaventofe0580c2017-05-12 02:02:10 -040076 * @brief Processes an incoming packet as captured by libpcap
77 * @param packet Pointer to the received packet, including the link-layer header
78 * @param length Packet length
Davide Pesavento7726ae52014-11-23 21:01:05 +010079 */
80 void
Davide Pesaventofe0580c2017-05-12 02:02:10 -040081 processIncomingPacket(const uint8_t* packet, size_t length);
Davide Pesavento7726ae52014-11-23 21:01:05 +010082
Davide Pesavento7726ae52014-11-23 21:01:05 +010083 /**
84 * @brief Handles errors encountered by Boost.Asio on the receive path
85 */
Davide Pesavento44deacc2014-02-19 10:48:07 +010086 void
87 processErrorCode(const boost::system::error_code& error);
88
Davide Pesavento7726ae52014-11-23 21:01:05 +010089 /**
90 * @brief Returns the MTU of the underlying network interface
91 */
Davide Pesaventofe0580c2017-05-12 02:02:10 -040092 int
Davide Pesavento292e5e12015-03-13 02:08:33 +010093 getInterfaceMtu();
Davide Pesavento44deacc2014-02-19 10:48:07 +010094
Davide Pesavento6bd6d0b2017-03-25 15:16:40 -040095protected:
Davide Pesavento292e5e12015-03-13 02:08:33 +010096 boost::asio::posix::stream_descriptor m_socket;
Davide Pesaventofe0580c2017-05-12 02:02:10 -040097 PcapHelper m_pcap;
Davide Pesavento35120ea2015-11-17 21:13:18 +010098 ethernet::Address m_srcAddress;
99 ethernet::Address m_destAddress;
100 std::string m_interfaceName;
Davide Pesavento9a090a02015-01-29 18:15:26 +0100101
Davide Pesavento6bd6d0b2017-03-25 15:16:40 -0400102private:
Davide Pesavento9a090a02015-01-29 18:15:26 +0100103#ifdef _DEBUG
104 /// number of packets dropped by the kernel, as reported by libpcap
Davide Pesaventofe0580c2017-05-12 02:02:10 -0400105 size_t m_nDropped;
Davide Pesavento9a090a02015-01-29 18:15:26 +0100106#endif
Davide Pesavento44deacc2014-02-19 10:48:07 +0100107};
108
Davide Pesavento35120ea2015-11-17 21:13:18 +0100109} // namespace face
Davide Pesavento44deacc2014-02-19 10:48:07 +0100110} // namespace nfd
111
Davide Pesavento35120ea2015-11-17 21:13:18 +0100112#endif // NFD_DAEMON_FACE_ETHERNET_TRANSPORT_HPP