blob: 0777d74e54c88f3652447db06b611c96eb1fb6a3 [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
Junxiao Shi9f5b01d2016-08-05 03:54:28 +000029#include "core/common.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
Alexander Afanasyev885a85b2014-04-12 21:01:13 -070033#ifndef HAVE_LIBPCAP
34#error "Cannot include this file when libpcap is not available"
Davide Pesavento44deacc2014-02-19 10:48:07 +010035#endif
36
37// forward declarations
38struct pcap;
39typedef pcap pcap_t;
Davide Pesavento7726ae52014-11-23 21:01:05 +010040struct pcap_pkthdr;
Davide Pesavento44deacc2014-02-19 10:48:07 +010041
42namespace nfd {
Davide Pesavento35120ea2015-11-17 21:13:18 +010043namespace face {
Davide Pesavento44deacc2014-02-19 10:48:07 +010044
45/**
Davide Pesavento35120ea2015-11-17 21:13:18 +010046 * \brief A multicast Transport that uses raw Ethernet II frames
Davide Pesavento44deacc2014-02-19 10:48:07 +010047 */
Davide Pesaventob84bd3a2016-04-22 02:21:45 +020048class EthernetTransport final : public Transport
Davide Pesavento44deacc2014-02-19 10:48:07 +010049{
50public:
Davide Pesavento35120ea2015-11-17 21:13:18 +010051 class Error : public std::runtime_error
Davide Pesavento44deacc2014-02-19 10:48:07 +010052 {
Davide Pesavento35120ea2015-11-17 21:13:18 +010053 public:
54 explicit
55 Error(const std::string& what)
56 : std::runtime_error(what)
57 {
58 }
Davide Pesavento44deacc2014-02-19 10:48:07 +010059 };
60
Davide Pesavento44deacc2014-02-19 10:48:07 +010061 /**
Davide Pesavento35120ea2015-11-17 21:13:18 +010062 * @brief Creates an Ethernet-based transport for multicast communication
Davide Pesavento44deacc2014-02-19 10:48:07 +010063 */
Davide Pesavento35120ea2015-11-17 21:13:18 +010064 EthernetTransport(const NetworkInterfaceInfo& interface,
Teng Liangbfea5752017-03-29 04:51:10 +000065 const ethernet::Address& mcastAddress,
66 ndn::nfd::LinkType linkType);
Davide Pesavento35120ea2015-11-17 21:13:18 +010067
68protected:
Yanbiao Li32dab972016-11-27 12:26:09 +080069 void
Davide Pesaventob84bd3a2016-04-22 02:21:45 +020070 doClose() final;
Davide Pesavento44deacc2014-02-19 10:48:07 +010071
72private:
Yanbiao Li32dab972016-11-27 12:26:09 +080073 void
Davide Pesaventob84bd3a2016-04-22 02:21:45 +020074 doSend(Transport::Packet&& packet) final;
Davide Pesavento35120ea2015-11-17 21:13:18 +010075
Davide Pesavento7726ae52014-11-23 21:01:05 +010076 /**
77 * @brief Allocates and initializes a libpcap context for live capture
78 */
Davide Pesavento44deacc2014-02-19 10:48:07 +010079 void
80 pcapInit();
81
Davide Pesavento7726ae52014-11-23 21:01:05 +010082 /**
83 * @brief Installs a BPF filter on the receiving socket
84 *
85 * @param filterString string containing the source BPF program
86 */
Davide Pesavento44deacc2014-02-19 10:48:07 +010087 void
88 setPacketFilter(const char* filterString);
89
Davide Pesavento7726ae52014-11-23 21:01:05 +010090 /**
91 * @brief Enables receiving frames addressed to our MAC multicast group
Davide Pesaventof8b41eb2014-12-26 19:14:06 +010092 *
93 * @return true if successful, false otherwise
Davide Pesavento7726ae52014-11-23 21:01:05 +010094 */
Davide Pesaventof8b41eb2014-12-26 19:14:06 +010095 bool
Davide Pesavento10783f22014-03-15 04:40:01 +010096 joinMulticastGroup();
97
Davide Pesavento7726ae52014-11-23 21:01:05 +010098 /**
99 * @brief Sends the specified TLV block on the network wrapped in an Ethernet frame
100 */
Davide Pesavento10783f22014-03-15 04:40:01 +0100101 void
Davide Pesavento44deacc2014-02-19 10:48:07 +0100102 sendPacket(const ndn::Block& block);
103
Davide Pesavento7726ae52014-11-23 21:01:05 +0100104 /**
105 * @brief Receive callback
106 */
Davide Pesavento44deacc2014-02-19 10:48:07 +0100107 void
Davide Pesavento7726ae52014-11-23 21:01:05 +0100108 handleRead(const boost::system::error_code& error, size_t nBytesRead);
Davide Pesavento44deacc2014-02-19 10:48:07 +0100109
Davide Pesavento7726ae52014-11-23 21:01:05 +0100110PUBLIC_WITH_TESTS_ELSE_PRIVATE:
111 /**
112 * @brief Processes an incoming frame as captured by libpcap
113 *
114 * @param header pointer to capture metadata
115 * @param packet pointer to the received frame, including the link-layer header
116 */
117 void
118 processIncomingPacket(const pcap_pkthdr* header, const uint8_t* packet);
119
120private:
121 /**
122 * @brief Handles errors encountered by Boost.Asio on the receive path
123 */
Davide Pesavento44deacc2014-02-19 10:48:07 +0100124 void
125 processErrorCode(const boost::system::error_code& error);
126
Davide Pesavento7726ae52014-11-23 21:01:05 +0100127 /**
128 * @brief Returns the MTU of the underlying network interface
129 */
Davide Pesavento44deacc2014-02-19 10:48:07 +0100130 size_t
Davide Pesavento292e5e12015-03-13 02:08:33 +0100131 getInterfaceMtu();
Davide Pesavento44deacc2014-02-19 10:48:07 +0100132
133private:
Davide Pesavento7726ae52014-11-23 21:01:05 +0100134 unique_ptr<pcap_t, void(*)(pcap_t*)> m_pcap;
Davide Pesavento292e5e12015-03-13 02:08:33 +0100135 boost::asio::posix::stream_descriptor m_socket;
Davide Pesavento7726ae52014-11-23 21:01:05 +0100136
Davide Pesavento35120ea2015-11-17 21:13:18 +0100137 ethernet::Address m_srcAddress;
138 ethernet::Address m_destAddress;
139 std::string m_interfaceName;
Davide Pesavento10783f22014-03-15 04:40:01 +0100140#if defined(__linux__)
141 int m_interfaceIndex;
142#endif
Davide Pesavento9a090a02015-01-29 18:15:26 +0100143
144#ifdef _DEBUG
145 /// number of packets dropped by the kernel, as reported by libpcap
146 unsigned int m_nDropped;
147#endif
Davide Pesavento44deacc2014-02-19 10:48:07 +0100148};
149
Davide Pesavento35120ea2015-11-17 21:13:18 +0100150} // namespace face
Davide Pesavento44deacc2014-02-19 10:48:07 +0100151} // namespace nfd
152
Davide Pesavento35120ea2015-11-17 21:13:18 +0100153#endif // NFD_DAEMON_FACE_ETHERNET_TRANSPORT_HPP