32 #include <boost/range/adaptor/map.hpp> 33 #include <pcap/pcap.h> 41 time::nanoseconds idleTimeout)
42 : m_localEndpoint(std::move(localEndpoint))
43 , m_isListening(false)
45 , m_pcap(m_localEndpoint->getName())
46 , m_idleFaceTimeout(idleTimeout)
61 shared_ptr<Face> face;
63 face = createFace(remoteEndpoint, params).second;
65 catch (
const boost::system::system_error& e) {
68 onConnectFailed(504,
"Face creation failed: "s + e.what());
89 m_socket.assign(m_pcap.
getFd());
92 BOOST_THROW_EXCEPTION(
Error(e.what()));
96 asyncRead(onFaceCreated, onFaceCreationFailed);
104 m_socket.async_read_some(boost::asio::null_buffers(),
105 [=] (
const auto& e,
auto) { this->handleRead(e, onFaceCreated, onReceiveFailed); });
109 EthernetChannel::handleRead(
const boost::system::error_code& error,
114 if (error != boost::asio::error::operation_aborted) {
117 onReceiveFailed(500,
"Receive failed: " + error.message());
127 if (pkt ==
nullptr) {
131 const ether_header* eh;
133 m_localEndpoint->getEthernetAddress());
138 ethernet::Address sender(eh->ether_shost);
141 processIncomingPacket(pkt, len, sender, onFaceCreated, onReceiveFailed);
147 if (nDropped - m_nDropped > 0)
149 m_nDropped = nDropped;
152 asyncRead(onFaceCreated, onReceiveFailed);
156 EthernetChannel::processIncomingPacket(
const uint8_t* packet,
size_t length,
157 const ethernet::Address& sender,
163 bool isCreated =
false;
164 shared_ptr<Face> face;
168 std::tie(isCreated, face) = createFace(sender, params);
170 catch (
const EthernetTransport::Error& e) {
173 onReceiveFailed(504,
"Face creation failed: "s + e.what());
183 auto* transport = static_cast<UnicastEthernetTransport*>(face->getTransport());
184 transport->receivePayload(packet, length, sender);
187 std::pair<bool, shared_ptr<Face>>
188 EthernetChannel::createFace(
const ethernet::Address& remoteEndpoint,
189 const FaceParams& params)
191 auto it = m_channelFaces.find(remoteEndpoint);
192 if (it != m_channelFaces.end()) {
195 return {
false, it->second};
199 GenericLinkService::Options options;
200 options.allowFragmentation =
true;
201 options.allowReassembly =
true;
202 options.reliabilityOptions.isEnabled = params.wantLpReliability;
204 auto linkService = make_unique<GenericLinkService>(options);
205 auto transport = make_unique<UnicastEthernetTransport>(*m_localEndpoint, remoteEndpoint,
206 params.persistency, m_idleFaceTimeout,
208 auto face = make_shared<Face>(std::move(linkService), std::move(transport));
210 m_channelFaces[remoteEndpoint] = face;
212 m_channelFaces.erase(remoteEndpoint);
221 EthernetChannel::updateFilter()
227 ") && (ether dst " + m_localEndpoint->getEthernetAddress().toString() +
")";
228 for (
const auto& addr : m_channelFaces | boost::adaptors::map_keys) {
229 filter +=
" && (not ether src " + addr.toString() +
")";
233 filter +=
" && (not vlan)";
void connect(const ethernet::Address &remoteEndpoint, const FaceParams ¶ms, const FaceCreatedCallback &onFaceCreated, const FaceCreationFailedCallback &onConnectFailed)
Create a unicast Ethernet face toward remoteEndpoint.
void setUri(const FaceUri &uri)
void activate(int dlt)
Start capturing packets.
detail::SimulatorIo & getGlobalIoService()
bool isListening() const override
Returns whether the channel is listening.
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 > &face)> FaceCreatedCallback
Prototype for the callback that is invoked when a face is created (in response to an incoming connect...
void setPacketFilter(const char *filter) const
Install a BPF filter on the receiving socket.
void connectFaceClosedSignal(Face &face, const std::function< void()> &f)
invokes a callback when the face is closed
Class implementing Ethernet-based channel to create faces.
#define NFD_LOG_CHAN_DEBUG(msg)
Log a message at DEBUG level.
EthernetChannel-related error.
Copyright (c) 2011-2015 Regents of the University of California.
#define NFD_LOG_CHAN_INFO(msg)
Log a message at INFO level.
#define NFD_LOG_CHAN_TRACE(msg)
Log a message at TRACE level.
#define NFD_LOG_CHAN_WARN(msg)
Log a message at WARN level.
const size_t HDR_LEN
Total octets in Ethernet header (without 802.1Q tag)
void listen(const FaceCreatedCallback &onFaceCreated, const FaceCreationFailedCallback &onFaceCreationFailed)
Start listening.
const uint16_t ETHERTYPE_NDN
std::pair< const ether_header *, std::string > checkFrameHeader(const uint8_t *packet, size_t length, const Address &localAddr, const Address &destAddr)
represents an Ethernet hardware address
EthernetChannel(shared_ptr< const ndn::net::NetworkInterface > localEndpoint, time::nanoseconds idleTimeout)
Create an Ethernet channel on the given localEndpoint (network interface)
int getFd() const
Obtain a file descriptor that can be used in calls such as select(2) and poll(2).
size_t getNDropped() const
Get the number of packets dropped by the kernel, as reported by libpcap.
std::string to_string(const V &v)
std::tuple< const uint8_t *, size_t, std::string > readNextPacket() const
Read the next packet captured on the interface.
#define NFD_LOG_INIT(name)
static FaceUri fromDev(const std::string &ifname)
create dev FaceUri from network device name
Parameters used to set Transport properties or LinkService options on a newly created face.