29 #include <pcap/pcap.h> 33 #include <arpa/inet.h> 34 #include <net/ethernet.h> 37 #include <sys/ioctl.h> 40 #if defined(__linux__) 41 #include <netpacket/packet.h> 42 #include <sys/socket.h> 46 #if defined(__APPLE__) || defined(__FreeBSD__) 47 #include <net/if_dl.h> 51 #if !defined(PCAP_NETMASK_UNKNOWN) 56 #define PCAP_NETMASK_UNKNOWN 0xffffffff 66 : m_pcap(nullptr, pcap_close)
68 , m_srcAddress(interface.etherAddress)
69 , m_destAddress(mcastAddress)
70 , m_interfaceName(interface.
name)
71 #if defined(__linux__)
72 , m_interfaceIndex(interface.index)
79 this->setRemoteUri(
FaceUri(mcastAddress));
88 int fd = pcap_get_selectable_fd(m_pcap.get());
90 BOOST_THROW_EXCEPTION(Error(
"pcap_get_selectable_fd failed"));
95 m_socket.assign(::dup(fd));
98 this->setMtu(getInterfaceMtu());
105 snprintf(filter,
sizeof(filter),
106 "(ether proto 0x%x) && (ether dst %s) && (not ether src %s) && (not vlan)",
108 m_destAddress.toString().c_str(),
109 m_srcAddress.toString().c_str());
110 setPacketFilter(filter);
112 if (!m_destAddress.isBroadcast() && !joinMulticastGroup()) {
114 pcap_set_promisc(m_pcap.get(), 1);
117 m_socket.async_read_some(boost::asio::null_buffers(),
118 bind(&EthernetTransport::handleRead,
this,
119 boost::asio::placeholders::error,
120 boost::asio::placeholders::bytes_transferred));
126 BOOST_THROW_EXCEPTION(
127 std::invalid_argument(
"EthernetTransport supports only FACE_PERSISTENCY_PERMANENT"));
135 sendPacket(packet.packet);
138 void EthernetTransport::doClose()
142 if (m_socket.is_open()) {
145 boost::system::error_code error;
146 m_socket.cancel(error);
147 m_socket.close(error);
159 EthernetTransport::pcapInit()
161 char errbuf[PCAP_ERRBUF_SIZE] = {};
162 m_pcap.reset(pcap_create(m_interfaceName.c_str(), errbuf));
164 BOOST_THROW_EXCEPTION(Error(
"pcap_create: " + std::string(errbuf)));
166 #ifdef HAVE_PCAP_SET_IMMEDIATE_MODE 171 pcap_set_immediate_mode(m_pcap.get(), 1);
174 if (pcap_activate(m_pcap.get()) < 0)
175 BOOST_THROW_EXCEPTION(Error(
"pcap_activate failed"));
177 if (pcap_set_datalink(m_pcap.get(), DLT_EN10MB) < 0)
178 BOOST_THROW_EXCEPTION(Error(
"pcap_set_datalink: " + std::string(pcap_geterr(m_pcap.get()))));
180 if (pcap_setdirection(m_pcap.get(), PCAP_D_IN) < 0)
186 EthernetTransport::setPacketFilter(
const char* filterString)
190 BOOST_THROW_EXCEPTION(Error(
"pcap_compile: " + std::string(pcap_geterr(m_pcap.get()))));
192 int ret = pcap_setfilter(m_pcap.get(), &filter);
193 pcap_freecode(&filter);
195 BOOST_THROW_EXCEPTION(Error(
"pcap_setfilter: " + std::string(pcap_geterr(m_pcap.get()))));
199 EthernetTransport::joinMulticastGroup()
201 #if defined(__linux__) 203 mr.mr_ifindex = m_interfaceIndex;
204 mr.mr_type = PACKET_MR_MULTICAST;
205 mr.mr_alen = m_destAddress.size();
206 std::memcpy(mr.mr_address, m_destAddress.data(), m_destAddress.size());
208 if (::setsockopt(m_socket.native_handle(), SOL_PACKET,
209 PACKET_ADD_MEMBERSHIP, &mr,
sizeof(mr)) == 0)
212 NFD_LOG_FACE_WARN(
"setsockopt(PACKET_ADD_MEMBERSHIP) failed: " << std::strerror(errno));
215 #if defined(SIOCADDMULTI) 217 std::strncpy(ifr.ifr_name, m_interfaceName.c_str(),
sizeof(ifr.ifr_name) - 1);
219 #if defined(__APPLE__) || defined(__FreeBSD__) 221 using boost::asio::ip::udp;
223 int fd = sock.native_handle();
237 sockaddr_dl* sdl =
reinterpret_cast<sockaddr_dl*
>(&ifr.ifr_addr);
238 sdl->sdl_len =
sizeof(ifr.ifr_addr);
239 sdl->sdl_family = AF_LINK;
240 sdl->sdl_alen = m_destAddress.size();
241 std::memcpy(LLADDR(sdl), m_destAddress.data(), m_destAddress.size());
243 static_assert(
sizeof(ifr.ifr_addr) >= offsetof(sockaddr_dl, sdl_data) +
ethernet::ADDR_LEN,
244 "ifr_addr in struct ifreq is too small on this platform");
246 int fd = m_socket.native_handle();
248 ifr.ifr_hwaddr.sa_family = AF_UNSPEC;
249 std::memcpy(ifr.ifr_hwaddr.sa_data, m_destAddress.data(), m_destAddress.size());
252 "ifr_hwaddr in struct ifreq is too small on this platform");
255 if (::ioctl(fd, SIOCADDMULTI, &ifr) == 0)
265 EthernetTransport::sendPacket(
const ndn::Block& block)
279 buffer.prependByteArray(reinterpret_cast<const uint8_t*>(ðertype),
ethernet::TYPE_LEN);
280 buffer.prependByteArray(m_srcAddress.data(), m_srcAddress.size());
281 buffer.prependByteArray(m_destAddress.data(), m_destAddress.size());
284 int sent = pcap_inject(m_pcap.get(), buffer.buf(), buffer.size());
287 else if (static_cast<size_t>(sent) < buffer.size())
288 NFD_LOG_FACE_ERROR(
"Failed to send the full frame: bufsize=" << buffer.size() <<
" sent=" << sent);
295 EthernetTransport::handleRead(
const boost::system::error_code& error,
size_t)
298 return processErrorCode(error);
301 const uint8_t* packet;
304 int ret = pcap_next_ex(m_pcap.get(), &header, &packet);
310 processIncomingPacket(header, packet);
314 ret = pcap_stats(m_pcap.get(), &ps);
319 if (ps.ps_drop - m_nDropped > 0)
321 m_nDropped = ps.ps_drop;
325 m_socket.async_read_some(boost::asio::null_buffers(),
326 bind(&EthernetTransport::handleRead,
this,
327 boost::asio::placeholders::error,
328 boost::asio::placeholders::bytes_transferred));
332 EthernetTransport::processIncomingPacket(
const pcap_pkthdr* header,
const uint8_t* packet)
334 size_t length = header->caplen;
340 const ether_header* eh =
reinterpret_cast<const ether_header*
>(packet);
350 "Received frame addressed to a different multicast group");
351 BOOST_ASSERT_MSG(sourceAddress != m_srcAddress,
352 "Received frame sent by this host");
369 "Transport::Packet::remoteEndpoint is too small");
370 std::memcpy(&tp.remoteEndpoint, sourceAddress.data(), sourceAddress.size());
371 this->receive(std::move(tp));
375 EthernetTransport::processErrorCode(
const boost::system::error_code& error)
382 error == boost::asio::error::operation_aborted)
390 EthernetTransport::getInterfaceMtu()
393 #if defined(__APPLE__) || defined(__FreeBSD__) 395 using boost::asio::ip::udp;
397 int fd = sock.native_handle();
399 int fd = m_socket.native_handle();
403 std::strncpy(ifr.ifr_name, m_interfaceName.c_str(),
sizeof(ifr.ifr_name) - 1);
405 if (::ioctl(fd, SIOCGIFMTU, &ifr) == 0) {
407 return static_cast<size_t>(ifr.ifr_mtu);
const size_t ADDR_LEN
Octets in one Ethernet address.
static std::tuple< bool, Block > fromBuffer(ConstBufferPtr buffer, size_t offset)
Try to construct block from Buffer.
#define NFD_LOG_FACE_TRACE(msg)
Log a message at TRACE level.
represents an Ethernet hardware address
represents the underlying protocol and address used by a Face
#define NFD_LOG_FACE_ERROR(msg)
Log a message at ERROR level.
stores a packet along with the remote endpoint
contains information about a network interface
detail::SimulatorIo & getGlobalIoService()
const size_t TYPE_LEN
Octets in Ethertype field.
Class representing a wire element of NDN-TLV packet format.
the transport is being closed due to a failure
const size_t MIN_DATA_LEN
Min octets in Ethernet payload (assuming no 802.1Q tag)
#define NFD_LOG_FACE_DEBUG(msg)
Log a message at DEBUG level.
EncodingImpl< EncoderTag > EncodingBuffer
#define NFD_LOG_FACE_INFO(msg)
Log a message at INFO level.
Copyright (c) 2011-2015 Regents of the University of California.
the transport is closed, and can be safely deallocated
void post(const std::function< void()> &callback)
the transport is requested to be closed
const size_t MAX_DATA_LEN
Max octets in Ethernet payload.
#define NFD_LOG_INIT(name)
const size_t HDR_LEN
Total octets in Ethernet header (without 802.1Q tag)
static FaceUri fromDev(const std::string &ifname)
create dev FaceUri from network device name
#define PCAP_NETMASK_UNKNOWN
Copyright (c) 2014-2015, Regents of the University of California, Arizona Board of Regents...
const uint16_t ETHERTYPE_NDN
#define NFD_LOG_FACE_WARN(msg)
Log a message at WARN level.