blob: d89e280e86394b4c478447c9e03d91a4acc7c639 [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,
65 const ethernet::Address& mcastAddress);
66
67protected:
Yanbiao Li32dab972016-11-27 12:26:09 +080068 void
Davide Pesaventob84bd3a2016-04-22 02:21:45 +020069 doClose() final;
Davide Pesavento44deacc2014-02-19 10:48:07 +010070
71private:
Yanbiao Li32dab972016-11-27 12:26:09 +080072 void
Davide Pesaventob84bd3a2016-04-22 02:21:45 +020073 doSend(Transport::Packet&& packet) final;
Davide Pesavento35120ea2015-11-17 21:13:18 +010074
Davide Pesavento7726ae52014-11-23 21:01:05 +010075 /**
76 * @brief Allocates and initializes a libpcap context for live capture
77 */
Davide Pesavento44deacc2014-02-19 10:48:07 +010078 void
79 pcapInit();
80
Davide Pesavento7726ae52014-11-23 21:01:05 +010081 /**
82 * @brief Installs a BPF filter on the receiving socket
83 *
84 * @param filterString string containing the source BPF program
85 */
Davide Pesavento44deacc2014-02-19 10:48:07 +010086 void
87 setPacketFilter(const char* filterString);
88
Davide Pesavento7726ae52014-11-23 21:01:05 +010089 /**
90 * @brief Enables receiving frames addressed to our MAC multicast group
Davide Pesaventof8b41eb2014-12-26 19:14:06 +010091 *
92 * @return true if successful, false otherwise
Davide Pesavento7726ae52014-11-23 21:01:05 +010093 */
Davide Pesaventof8b41eb2014-12-26 19:14:06 +010094 bool
Davide Pesavento10783f22014-03-15 04:40:01 +010095 joinMulticastGroup();
96
Davide Pesavento7726ae52014-11-23 21:01:05 +010097 /**
98 * @brief Sends the specified TLV block on the network wrapped in an Ethernet frame
99 */
Davide Pesavento10783f22014-03-15 04:40:01 +0100100 void
Davide Pesavento44deacc2014-02-19 10:48:07 +0100101 sendPacket(const ndn::Block& block);
102
Davide Pesavento7726ae52014-11-23 21:01:05 +0100103 /**
104 * @brief Receive callback
105 */
Davide Pesavento44deacc2014-02-19 10:48:07 +0100106 void
Davide Pesavento7726ae52014-11-23 21:01:05 +0100107 handleRead(const boost::system::error_code& error, size_t nBytesRead);
Davide Pesavento44deacc2014-02-19 10:48:07 +0100108
Davide Pesavento7726ae52014-11-23 21:01:05 +0100109PUBLIC_WITH_TESTS_ELSE_PRIVATE:
110 /**
111 * @brief Processes an incoming frame as captured by libpcap
112 *
113 * @param header pointer to capture metadata
114 * @param packet pointer to the received frame, including the link-layer header
115 */
116 void
117 processIncomingPacket(const pcap_pkthdr* header, const uint8_t* packet);
118
119private:
120 /**
121 * @brief Handles errors encountered by Boost.Asio on the receive path
122 */
Davide Pesavento44deacc2014-02-19 10:48:07 +0100123 void
124 processErrorCode(const boost::system::error_code& error);
125
Davide Pesavento7726ae52014-11-23 21:01:05 +0100126 /**
127 * @brief Returns the MTU of the underlying network interface
128 */
Davide Pesavento44deacc2014-02-19 10:48:07 +0100129 size_t
Davide Pesavento292e5e12015-03-13 02:08:33 +0100130 getInterfaceMtu();
Davide Pesavento44deacc2014-02-19 10:48:07 +0100131
132private:
Davide Pesavento7726ae52014-11-23 21:01:05 +0100133 unique_ptr<pcap_t, void(*)(pcap_t*)> m_pcap;
Davide Pesavento292e5e12015-03-13 02:08:33 +0100134 boost::asio::posix::stream_descriptor m_socket;
Davide Pesavento7726ae52014-11-23 21:01:05 +0100135
Davide Pesavento35120ea2015-11-17 21:13:18 +0100136 ethernet::Address m_srcAddress;
137 ethernet::Address m_destAddress;
138 std::string m_interfaceName;
Davide Pesavento10783f22014-03-15 04:40:01 +0100139#if defined(__linux__)
140 int m_interfaceIndex;
141#endif
Davide Pesavento9a090a02015-01-29 18:15:26 +0100142
143#ifdef _DEBUG
144 /// number of packets dropped by the kernel, as reported by libpcap
145 unsigned int m_nDropped;
146#endif
Davide Pesavento44deacc2014-02-19 10:48:07 +0100147};
148
Davide Pesavento35120ea2015-11-17 21:13:18 +0100149} // namespace face
Davide Pesavento44deacc2014-02-19 10:48:07 +0100150} // namespace nfd
151
Davide Pesavento35120ea2015-11-17 21:13:18 +0100152#endif // NFD_DAEMON_FACE_ETHERNET_TRANSPORT_HPP