33 #include <boost/range/adaptor/map.hpp>
34 #include <pcap/pcap.h>
41 time::nanoseconds idleTimeout)
42 : m_localEndpoint(std::move(localEndpoint))
44 , m_pcap(m_localEndpoint->getName())
45 , m_idleFaceTimeout(idleTimeout)
47 setUri(FaceUri::fromDev(m_localEndpoint->getName()));
57 shared_ptr<Face> face;
59 face = createFace(remoteEndpoint, params).second;
61 catch (
const boost::system::system_error& e) {
64 onConnectFailed(504,
"Face creation failed: "s + e.what());
85 m_socket.assign(m_pcap.
getFd());
88 NDN_THROW_NESTED(
Error(e.what()));
92 asyncRead(onFaceCreated, onFaceCreationFailed);
100 m_socket.async_wait(boost::asio::posix::stream_descriptor::wait_read, [=] (
const auto& e) {
101 handleRead(e, onFaceCreated, onReceiveFailed);
106 EthernetChannel::handleRead(
const boost::system::error_code& error,
111 if (error != boost::asio::error::operation_aborted) {
114 onReceiveFailed(500,
"Receive failed: " + error.message());
125 m_localEndpoint->getEthernetAddress());
130 ethernet::Address sender(eh->ether_shost);
131 pkt = pkt.subspan(ethernet::HDR_LEN);
132 processIncomingPacket(pkt, sender, onFaceCreated, onReceiveFailed);
138 if (nDropped - m_nDropped > 0)
140 m_nDropped = nDropped;
143 asyncRead(onFaceCreated, onReceiveFailed);
147 EthernetChannel::processIncomingPacket(span<const uint8_t> packet,
148 const ethernet::Address& sender,
154 bool isCreated =
false;
155 shared_ptr<Face> face;
158 params.persistency = ndn::nfd::FACE_PERSISTENCY_ON_DEMAND;
159 std::tie(isCreated, face) = createFace(sender, params);
161 catch (
const EthernetTransport::Error& e) {
164 onReceiveFailed(504,
"Face creation failed: "s + e.what());
174 auto* transport =
static_cast<UnicastEthernetTransport*
>(face->getTransport());
175 transport->receivePayload(packet, sender);
178 std::pair<bool, shared_ptr<Face>>
179 EthernetChannel::createFace(
const ethernet::Address& remoteEndpoint,
180 const FaceParams& params)
182 auto it = m_channelFaces.find(remoteEndpoint);
183 if (it != m_channelFaces.end()) {
186 return {
false, it->second};
192 options.allowReassembly =
true;
193 options.reliabilityOptions.isEnabled = params.wantLpReliability;
195 options.overrideMtu = *params.mtu;
198 auto linkService = make_unique<GenericLinkService>(options);
199 auto transport = make_unique<UnicastEthernetTransport>(*m_localEndpoint, remoteEndpoint,
200 params.persistency, m_idleFaceTimeout);
201 auto face = make_shared<Face>(std::move(linkService), std::move(transport));
202 face->setChannel(weak_from_this());
204 m_channelFaces[remoteEndpoint] = face;
206 m_channelFaces.erase(remoteEndpoint);
215 EthernetChannel::updateFilter()
220 std::string filter =
"(ether proto " + std::to_string(ethernet::ETHERTYPE_NDN) +
221 ") && (ether dst " + m_localEndpoint->getEthernetAddress().toString() +
")";
222 for (
const auto& addr : m_channelFaces | boost::adaptors::map_keys) {
223 filter +=
" && (not ether src " + addr.toString() +
")";
227 filter +=
" && (not vlan)";
void setUri(const FaceUri &uri) noexcept
EthernetChannel-related error.
void listen(const FaceCreatedCallback &onFaceCreated, const FaceCreationFailedCallback &onFaceCreationFailed)
Start listening.
bool isListening() const final
Returns whether the channel is listening.
void connect(const ethernet::Address &remoteEndpoint, const FaceParams ¶ms, const FaceCreatedCallback &onFaceCreated, const FaceCreationFailedCallback &onConnectFailed)
Create a unicast Ethernet face toward remoteEndpoint.
EthernetChannel(shared_ptr< const ndn::net::NetworkInterface > localEndpoint, time::nanoseconds idleTimeout)
Create an Ethernet channel on the given localEndpoint (network interface).
GenericLinkServiceOptions Options
Options for GenericLinkService.
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.
size_t getNDropped() const
Get the number of packets dropped by the kernel, as reported by libpcap.
void setPacketFilter(const char *filter) const
Install a BPF filter on the receiving socket.
std::tuple< span< const uint8_t >, std::string > readNextPacket() const noexcept
Read the next packet captured on the interface.
#define NFD_LOG_CHAN_DEBUG(msg)
Log a message at DEBUG level.
#define NFD_LOG_CHAN_INFO(msg)
Log a message at INFO level.
#define NFD_LOG_CHAN_WARN(msg)
Log a message at WARN level.
#define NFD_LOG_CHAN_TRACE(msg)
Log a message at TRACE level.
#define NFD_LOG_INIT(name)
std::tuple< const ether_header *, std::string > checkFrameHeader(span< const uint8_t > packet, const Address &localAddr, const Address &destAddr)
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.
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...
void connectFaceClosedSignal(Face &face, std::function< void()> f)
Invokes a callback when a face is closed.
boost::asio::io_context & getGlobalIoService()
Returns the global io_context instance for the calling thread.
Parameters used to set Transport properties or LinkService options on a newly created face.
bool allowFragmentation
Enables fragmentation.