blob: 1dd659315e1a8551ae4bf97170d09d3490cbb7fe [file] [log] [blame]
Davide Pesavento44deacc2014-02-19 10:48:07 +01001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Steve DiBenedettoef04f272014-06-04 14:28:31 -06003 * Copyright (c) 2014, Regents of the University of California,
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 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
Alexander Afanasyev613e2a92014-04-15 13:36:58 -070026#ifndef NFD_DAEMON_FACE_ETHERNET_FACE_HPP
27#define NFD_DAEMON_FACE_ETHERNET_FACE_HPP
Davide Pesavento44deacc2014-02-19 10:48:07 +010028
Davide Pesavento7726ae52014-11-23 21:01:05 +010029#include "common.hpp"
Davide Pesavento44deacc2014-02-19 10:48:07 +010030#include "face.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 {
43
44/**
Davide Pesavento7726ae52014-11-23 21:01:05 +010045 * @brief Implementation of Face abstraction that uses raw
Davide Pesavento44deacc2014-02-19 10:48:07 +010046 * Ethernet frames as underlying transport mechanism
47 */
48class EthernetFace : public Face
49{
50public:
51 /**
Davide Pesavento7726ae52014-11-23 21:01:05 +010052 * @brief EthernetFace-related error
Davide Pesavento44deacc2014-02-19 10:48:07 +010053 */
54 struct Error : public Face::Error
55 {
56 Error(const std::string& what) : Face::Error(what) {}
57 };
58
59 EthernetFace(const shared_ptr<boost::asio::posix::stream_descriptor>& socket,
Davide Pesaventob499a602014-11-18 22:36:56 +010060 const NetworkInterfaceInfo& interface,
Davide Pesavento44deacc2014-02-19 10:48:07 +010061 const ethernet::Address& address);
62
63 virtual
64 ~EthernetFace();
65
66 /// send an Interest
67 virtual void
68 sendInterest(const Interest& interest);
69
70 /// send a Data
71 virtual void
72 sendData(const Data& data);
73
74 /**
Davide Pesavento7726ae52014-11-23 21:01:05 +010075 * @brief Closes the face
Davide Pesavento44deacc2014-02-19 10:48:07 +010076 *
Davide Pesavento7726ae52014-11-23 21:01:05 +010077 * This terminates all communication on the face and triggers the onFail() event.
Davide Pesavento44deacc2014-02-19 10:48:07 +010078 */
79 virtual void
80 close();
81
82private:
Davide Pesavento7726ae52014-11-23 21:01:05 +010083 /**
84 * @brief Allocates and initializes a libpcap context for live capture
85 */
Davide Pesavento44deacc2014-02-19 10:48:07 +010086 void
87 pcapInit();
88
Davide Pesavento7726ae52014-11-23 21:01:05 +010089 /**
90 * @brief Installs a BPF filter on the receiving socket
91 *
92 * @param filterString string containing the source BPF program
93 */
Davide Pesavento44deacc2014-02-19 10:48:07 +010094 void
95 setPacketFilter(const char* filterString);
96
Davide Pesavento7726ae52014-11-23 21:01:05 +010097 /**
98 * @brief Enables receiving frames addressed to our MAC multicast group
99 */
Davide Pesavento44deacc2014-02-19 10:48:07 +0100100 void
Davide Pesavento10783f22014-03-15 04:40:01 +0100101 joinMulticastGroup();
102
Davide Pesavento7726ae52014-11-23 21:01:05 +0100103 /**
104 * @brief Sends the specified TLV block on the network wrapped in an Ethernet frame
105 */
Davide Pesavento10783f22014-03-15 04:40:01 +0100106 void
Davide Pesavento44deacc2014-02-19 10:48:07 +0100107 sendPacket(const ndn::Block& block);
108
Davide Pesavento7726ae52014-11-23 21:01:05 +0100109 /**
110 * @brief Receive callback
111 */
Davide Pesavento44deacc2014-02-19 10:48:07 +0100112 void
Davide Pesavento7726ae52014-11-23 21:01:05 +0100113 handleRead(const boost::system::error_code& error, size_t nBytesRead);
Davide Pesavento44deacc2014-02-19 10:48:07 +0100114
Davide Pesavento7726ae52014-11-23 21:01:05 +0100115PUBLIC_WITH_TESTS_ELSE_PRIVATE:
116 /**
117 * @brief Processes an incoming frame as captured by libpcap
118 *
119 * @param header pointer to capture metadata
120 * @param packet pointer to the received frame, including the link-layer header
121 */
122 void
123 processIncomingPacket(const pcap_pkthdr* header, const uint8_t* packet);
124
125private:
126 /**
127 * @brief Handles errors encountered by Boost.Asio on the receive path
128 */
Davide Pesavento44deacc2014-02-19 10:48:07 +0100129 void
130 processErrorCode(const boost::system::error_code& error);
131
Davide Pesavento7726ae52014-11-23 21:01:05 +0100132 /**
133 * @brief Returns the MTU of the underlying network interface
134 */
Davide Pesavento44deacc2014-02-19 10:48:07 +0100135 size_t
136 getInterfaceMtu() const;
137
138private:
Davide Pesavento7726ae52014-11-23 21:01:05 +0100139 unique_ptr<pcap_t, void(*)(pcap_t*)> m_pcap;
Davide Pesavento44deacc2014-02-19 10:48:07 +0100140 shared_ptr<boost::asio::posix::stream_descriptor> m_socket;
Davide Pesavento7726ae52014-11-23 21:01:05 +0100141
Davide Pesavento10783f22014-03-15 04:40:01 +0100142#if defined(__linux__)
143 int m_interfaceIndex;
144#endif
Davide Pesaventob60cc122014-03-19 19:26:02 +0100145 std::string m_interfaceName;
146 ethernet::Address m_srcAddress;
Davide Pesavento44deacc2014-02-19 10:48:07 +0100147 ethernet::Address m_destAddress;
148 size_t m_interfaceMtu;
Davide Pesavento44deacc2014-02-19 10:48:07 +0100149};
150
151} // namespace nfd
152
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700153#endif // NFD_DAEMON_FACE_ETHERNET_FACE_HPP