30 #include <pcap/pcap.h> 34 #include <boost/endian/conversion.hpp> 44 , m_pcap(localEndpoint.getName())
45 , m_srcAddress(localEndpoint.getEthernetAddress())
46 , m_destAddress(remoteEndpoint)
47 , m_interfaceName(localEndpoint.getName())
48 , m_hasRecentlyReceived(false)
62 handleNetifStateChange(localEndpoint.
getState());
66 handleNetifStateChange(newState);
69 m_netifMtuChangedConn = localEndpoint.
onMtuChanged.connect(
70 [
this] (uint32_t, uint32_t mtu) {
85 boost::system::error_code error;
113 EthernetTransport::doSend(
const Block& packet)
121 EthernetTransport::sendPacket(
const ndn::Block& block)
128 buffer.appendBytes(ndn::make_span(padding).subspan(block.
size()));
133 buffer.prependBytes({
reinterpret_cast<const uint8_t*
>(ðertype),
ethernet::TYPE_LEN});
138 int sent = pcap_inject(
m_pcap, buffer.data(), buffer.size());
141 else if (static_cast<size_t>(sent) < buffer.size())
142 handleError(
"Failed to send the full frame: size=" +
to_string(buffer.size()) +
150 EthernetTransport::asyncRead()
152 m_socket.async_read_some(boost::asio::null_buffers(),
153 [
this] (
const auto& e,
auto) { this->handleRead(e); });
157 EthernetTransport::handleRead(
const boost::system::error_code& error)
166 handleError(
"Receive operation failed: " + error.message());
171 span<const uint8_t> pkt;
179 const ether_header* eh;
194 if (nDropped - m_nDropped > 0)
196 m_nDropped = nDropped;
215 m_hasRecentlyReceived =
true;
220 std::memcpy(&endpoint, sender.data(), sender.size());
223 this->
receive(element, endpoint);
227 EthernetTransport::handleError(
const std::string& errorMessage)
#define NDN_THROW_NESTED(e)
static NDN_CXX_NODISCARD std::tuple< bool, Block > fromBuffer(ConstBufferPtr buffer, size_t offset=0)
Try to parse Block from a wire buffer.
InterfaceState getState() const
Returns the current state of the interface.
const size_t MIN_DATA_LEN
Min octets in Ethernet payload (assuming no 802.1Q tag)
interface can be used to send and receive packets
#define NFD_LOG_INIT(name)
std::string to_string(const T &val)
#define NFD_LOG_FACE_TRACE(msg)
Log a message at TRACE level.
void activate(int dlt)
Start capturing packets.
const size_t ADDR_LEN
Octets in one Ethernet address.
#define NFD_LOG_FACE_ERROR(msg)
Log a message at ERROR level.
util::Signal< NetworkInterface, InterfaceState, InterfaceState > onStateChanged
Fires when interface state changes.
EthernetTransport(const ndn::net::NetworkInterface &localEndpoint, const ethernet::Address &remoteEndpoint)
util::Signal< NetworkInterface, uint32_t, uint32_t > onMtuChanged
Fires when interface mtu changes.
InterfaceState
Indicates the state of a network interface.
detail::SimulatorIo & getGlobalIoService()
Returns the global io_service instance for the calling thread.
Represents a TLV element of the NDN packet format.
uint64_t EndpointId
Identifies a remote endpoint on the link.
the transport is being closed due to a failure
void doClose() final
performs Transport specific operations to close the transport
Represents one network interface attached to the host.
#define NFD_LOG_FACE_DEBUG(msg)
Log a message at DEBUG level.
void receive(const Block &packet, const EndpointId &endpoint=0)
Pass a received link-layer packet to the upper layer for further processing.
size_t size() const
Return the size of the encoded wire, i.e., of the whole TLV.
TransportState getState() const
Copyright (c) 2011-2015 Regents of the University of California.
std::tuple< const ether_header *, std::string > checkFrameHeader(span< const uint8_t > packet, const Address &localAddr, const Address &destAddr)
the transport is closed, and can be safely deallocated
void post(const std::function< void()> &callback)
const size_t HDR_LEN
Total octets in Ethernet header (without 802.1Q tag)
boost::asio::posix::stream_descriptor m_socket
void close() noexcept
Stop capturing and close the handle.
std::string getLastError() const noexcept
Get last error message.
ethernet::Address m_destAddress
const size_t TYPE_LEN
Octets in Ethertype field.
const uint16_t ETHERTYPE_NDN
the transport is being closed gracefully, either by the peer or by a call to close() ...
bool isMulticast() const
True if this is a multicast address.
represents an Ethernet hardware address
ndn::nfd::FacePersistency getPersistency() const
int getFd() const
Obtain a file descriptor that can be used in calls such as select(2) and poll(2). ...
Base class for Ethernet-based Transports.
size_t getNDropped() const
Get the number of packets dropped by the kernel, as reported by libpcap.
void setState(TransportState newState)
set transport state
the transport is up and can transmit packets
void receivePayload(span< const uint8_t > payload, const ethernet::Address &sender)
Processes the payload of an incoming frame.
std::tuple< span< const uint8_t >, std::string > readNextPacket() const noexcept
Read the next packet captured on the interface.
EncodingImpl< EncoderTag > EncodingBuffer
ethernet::Address m_srcAddress
#define NFD_LOG_FACE_WARN(msg)
Log a message at WARN level.
the transport is temporarily down, and is being recovered