33 #include <boost/range/adaptor/map.hpp> 34 #include <pcap/pcap.h> 42 time::nanoseconds idleTimeout)
43 : m_localEndpoint(
std::move(localEndpoint))
44 , m_isListening(false)
46 , m_pcap(m_localEndpoint->getName())
47 , m_idleFaceTimeout(idleTimeout)
52 setUri(FaceUri::fromDev(m_localEndpoint->getName()));
62 shared_ptr<Face> face;
64 face = createFace(remoteEndpoint, params).second;
66 catch (
const boost::system::system_error& e) {
69 onConnectFailed(504,
"Face creation failed: "s + e.what());
90 m_socket.assign(m_pcap.
getFd());
93 NDN_THROW_NESTED(
Error(e.what()));
97 asyncRead(onFaceCreated, onFaceCreationFailed);
105 m_socket.async_read_some(boost::asio::null_buffers(),
106 [=] (
const auto& e,
auto) { this->handleRead(e, onFaceCreated, onReceiveFailed); });
110 EthernetChannel::handleRead(
const boost::system::error_code& error,
115 if (error != boost::asio::error::operation_aborted) {
118 onReceiveFailed(500,
"Receive failed: " + error.message());
128 if (pkt ==
nullptr) {
132 const ether_header* eh;
134 m_localEndpoint->getEthernetAddress());
139 ethernet::Address sender(eh->ether_shost);
140 pkt += ethernet::HDR_LEN;
141 len -= ethernet::HDR_LEN;
142 processIncomingPacket(pkt, len, sender, onFaceCreated, onReceiveFailed);
148 if (nDropped - m_nDropped > 0)
150 m_nDropped = nDropped;
153 asyncRead(onFaceCreated, onReceiveFailed);
157 EthernetChannel::processIncomingPacket(
const uint8_t* packet,
size_t length,
158 const ethernet::Address& sender,
164 bool isCreated =
false;
165 shared_ptr<Face> face;
168 params.
persistency = ndn::nfd::FACE_PERSISTENCY_ON_DEMAND;
169 std::tie(isCreated, face) = createFace(sender, params);
174 onReceiveFailed(504,
"Face creation failed: "s + e.what());
188 std::pair<bool, shared_ptr<Face>>
189 EthernetChannel::createFace(
const ethernet::Address& remoteEndpoint,
192 auto it = m_channelFaces.find(remoteEndpoint);
193 if (it != m_channelFaces.end()) {
196 return {
false, it->second};
208 auto linkService = make_unique<GenericLinkService>(options);
209 auto transport = make_unique<UnicastEthernetTransport>(*m_localEndpoint, remoteEndpoint,
211 auto face = make_shared<Face>(std::move(linkService), std::move(transport));
212 face->setChannel(shared_from_this());
214 m_channelFaces[remoteEndpoint] = face;
216 m_channelFaces.erase(remoteEndpoint);
225 EthernetChannel::updateFilter()
230 std::string filter =
"(ether proto " + to_string(ethernet::ETHERTYPE_NDN) +
231 ") && (ether dst " + m_localEndpoint->getEthernetAddress().toString() +
")";
232 for (
const auto& addr : m_channelFaces | boost::adaptors::map_keys) {
233 filter +=
" && (not ether src " + addr.toString() +
")";
237 filter +=
" && (not vlan)";
size_t getNDropped() const
Get the number of packets dropped by the kernel, as reported by libpcap.
void listen(const FaceCreatedCallback &onFaceCreated, const FaceCreationFailedCallback &onFaceCreationFailed)
Start listening.
bool allowFragmentation
enables fragmentation
bool isEnabled
enables link-layer reliability
std::function< void(uint32_t status, const std::string &reason)> FaceCreationFailedCallback
Prototype for the callback that is invoked when a face fails to be created.
void setPacketFilter(const char *filter) const
Install a BPF filter on the receiving socket.
Parameters used to set Transport properties or LinkService options on a newly created face...
void connectFaceClosedSignal(Face &face, std::function< void()> f)
Invokes a callback when a face is closed.
std::tuple< const uint8_t *, size_t, std::string > readNextPacket() const
Read the next packet captured on the interface.
EthernetChannel-related error.
#define NFD_LOG_CHAN_INFO(msg)
Log a message at INFO level.
Copyright (c) 2014-2015, Regents of the University of California, Arizona Board of Regents...
void receivePayload(const uint8_t *payload, size_t length, const ethernet::Address &sender)
Processes the payload of an incoming frame.
EthernetChannel(shared_ptr< const ndn::net::NetworkInterface > localEndpoint, time::nanoseconds idleTimeout)
Create an Ethernet channel on the given localEndpoint (network interface)
ssize_t overrideMtu
overrides MTU provided by Transport
LpReliability::Options reliabilityOptions
options for reliability
std::pair< const ether_header *, std::string > checkFrameHeader(const uint8_t *packet, size_t length, const Address &localAddr, const Address &destAddr)
ndn::nfd::FacePersistency persistency
int getFd() const
Obtain a file descriptor that can be used in calls such as select(2) and poll(2). ...
void activate(int dlt)
Start capturing packets.
void setUri(const FaceUri &uri)
A unicast Transport that uses raw Ethernet II frames.
Options that control the behavior of GenericLinkService.
#define NFD_LOG_INIT(name)
void connect(const ethernet::Address &remoteEndpoint, const FaceParams ¶ms, const FaceCreatedCallback &onFaceCreated, const FaceCreationFailedCallback &onConnectFailed)
Create a unicast Ethernet face toward remoteEndpoint.
std::function< void(const shared_ptr< Face > &)> FaceCreatedCallback
Prototype for the callback that is invoked when a face is created (in response to an incoming connect...
#define NFD_LOG_CHAN_TRACE(msg)
Log a message at TRACE level.
bool isListening() const override
Returns whether the channel is listening.
#define NFD_LOG_CHAN_WARN(msg)
Log a message at WARN level.
bool allowReassembly
enables reassembly
#define NFD_LOG_CHAN_DEBUG(msg)
Log a message at DEBUG level.
boost::asio::io_service & getGlobalIoService()
Returns the global io_service instance for the calling thread.