30 #include <pcap/pcap.h>
34 #include <boost/asio/defer.hpp>
35 #include <boost/endian/conversion.hpp>
42 const ethernet::Address& remoteEndpoint)
44 , m_pcap(localEndpoint.getName())
45 , m_srcAddress(localEndpoint.getEthernetAddress())
46 , m_destAddress(remoteEndpoint)
47 , m_interfaceName(localEndpoint.getName())
54 NDN_THROW_NESTED(
Error(e.what()));
58 handleNetifStateChange(localEndpoint.getState());
60 m_netifStateChangedConn = localEndpoint.onStateChanged.connect(
61 [
this] (ndn::net::InterfaceState, ndn::net::InterfaceState newState) {
62 handleNetifStateChange(newState);
65 m_netifMtuChangedConn = localEndpoint.onMtuChanged.connect(
66 [
this] (uint32_t, uint32_t mtu) {
81 boost::system::error_code error;
95 EthernetTransport::handleNetifStateChange(ndn::net::InterfaceState netifState)
98 if (netifState == ndn::net::InterfaceState::RUNNING) {
109 EthernetTransport::doSend(
const Block& packet)
117 EthernetTransport::sendPacket(
const ndn::Block& block)
119 ndn::EncodingBuffer buffer(block);
122 if (block.size() < ethernet::MIN_DATA_LEN) {
123 static const std::array<uint8_t, ethernet::MIN_DATA_LEN> padding{};
124 buffer.appendBytes(ndn::span(padding).subspan(block.size()));
128 #if BOOST_VERSION >= 107200
131 uint16_t ethertype = boost::endian::native_to_big(ethernet::ETHERTYPE_NDN);
132 buffer.prependBytes({
reinterpret_cast<const uint8_t*
>(ðertype), ethernet::TYPE_LEN});
137 int sent = pcap_inject(
m_pcap, buffer.data(), buffer.size());
140 else if (
static_cast<size_t>(sent) < buffer.size())
141 handleError(
"Failed to send the full frame: size=" + std::to_string(buffer.size()) +
142 " sent=" + std::to_string(sent));
149 EthernetTransport::asyncRead()
151 m_socket.async_wait(boost::asio::posix::stream_descriptor::wait_read,
152 [
this] (
const auto& e) { this->handleRead(e); });
156 EthernetTransport::handleRead(
const boost::system::error_code& error)
161 if (error != boost::asio::error::operation_aborted &&
165 handleError(
"Receive operation failed: " + error.message());
181 ethernet::Address sender(eh->ether_shost);
182 pkt = pkt.subspan(ethernet::HDR_LEN);
189 if (nDropped - m_nDropped > 0)
191 m_nDropped = nDropped;
202 auto [isOk, element] = Block::fromBuffer(payload);
208 m_hasRecentlyReceived =
true;
214 EthernetTransport::handleError(
const std::string& errorMessage)
void receivePayload(span< const uint8_t > payload, const ethernet::Address &sender)
Processes the payload of an incoming frame.
void doClose() final
Performs Transport specific operations to close the transport.
boost::asio::posix::stream_descriptor m_socket
ethernet::Address m_srcAddress
ethernet::Address m_destAddress
EthernetTransport(const ndn::net::NetworkInterface &localEndpoint, const ethernet::Address &remoteEndpoint)
int getFd() const
Obtain a file descriptor that can be used in calls such as select(2) and poll(2).
std::string getLastError() const noexcept
Get last error message.
void activate(int dlt)
Start capturing packets.
size_t getNDropped() const
Get the number of packets dropped by the kernel, as reported by libpcap.
std::tuple< span< const uint8_t >, std::string > readNextPacket() const noexcept
Read the next packet captured on the interface.
void close() noexcept
Stop capturing and close the handle.
void receive(const Block &packet, const EndpointId &endpoint={})
Pass a received link-layer packet to the upper layer for further processing.
ndn::nfd::FacePersistency getPersistency() const noexcept
Returns the current persistency setting of the transport.
void setMtu(ssize_t mtu) noexcept
TransportState getState() const noexcept
Returns the current transport state.
void setState(TransportState newState)
Set transport state.
#define NFD_LOG_FACE_ERROR(msg)
Log a message at ERROR level.
#define NFD_LOG_FACE_DEBUG(msg)
Log a message at DEBUG level.
#define NFD_LOG_FACE_WARN(msg)
Log a message at WARN level.
#define NFD_LOG_FACE_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)
@ CLOSED
the transport is closed, and can be safely deallocated
@ CLOSING
the transport is being closed gracefully, either by the peer or by a call to close()
@ FAILED
the transport is being closed due to a failure
@ DOWN
the transport is temporarily down, and is being recovered
@ UP
the transport is up and can transmit packets
std::variant< std::monostate, ethernet::Address, udp::Endpoint > EndpointId
Identifies a remote endpoint on the link.
boost::asio::io_context & getGlobalIoService()
Returns the global io_context instance for the calling thread.