blob: 21a12a4ed4994fb6ea7277c4bcb7266af9ad03a5 [file] [log] [blame]
Davide Pesavento44deacc2014-02-19 10:48:07 +01001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi08d07a72014-06-09 23:17:57 -07003 * Copyright (c) 2014, Regents of the University of California,
4 * Arizona Board of Regents,
5 * Colorado State University,
6 * University Pierre & Marie Curie, Sorbonne University,
7 * Washington University in St. Louis,
8 * Beijing Institute of Technology,
9 * The University of Memphis
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070010 *
11 * This file is part of NFD (Named Data Networking Forwarding Daemon).
12 * See AUTHORS.md for complete list of NFD authors and contributors.
13 *
14 * NFD is free software: you can redistribute it and/or modify it under the terms
15 * of the GNU General Public License as published by the Free Software Foundation,
16 * either version 3 of the License, or (at your option) any later version.
17 *
18 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20 * PURPOSE. See the GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along with
23 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
Junxiao Shi3ffe66d2014-11-06 15:37:59 -070024 */
Davide Pesavento44deacc2014-02-19 10:48:07 +010025
26#include "ethernet-face.hpp"
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060027#include "core/logger.hpp"
Davide Pesavento44deacc2014-02-19 10:48:07 +010028
29#include <pcap/pcap.h>
30
Davide Pesavento10783f22014-03-15 04:40:01 +010031#include <cerrno> // for errno
32#include <cstdio> // for std::snprintf()
33#include <cstring> // for std::strerror() and std::strncpy()
Davide Pesavento44deacc2014-02-19 10:48:07 +010034#include <arpa/inet.h> // for htons() and ntohs()
35#include <net/ethernet.h> // for struct ether_header
36#include <net/if.h> // for struct ifreq
Davide Pesavento44deacc2014-02-19 10:48:07 +010037#include <sys/ioctl.h> // for ioctl()
38#include <unistd.h> // for dup()
39
Davide Pesavento10783f22014-03-15 04:40:01 +010040#if defined(__linux__)
41#include <netpacket/packet.h> // for struct packet_mreq
42#include <sys/socket.h> // for setsockopt()
43#endif
44
45#ifdef SIOCADDMULTI
46#if defined(__APPLE__) || defined(__FreeBSD__)
47#include <net/if_dl.h> // for struct sockaddr_dl
48#endif
49#endif
50
Alexander Afanasyevd9479aa2014-06-26 13:52:02 -070051#if !defined(PCAP_NETMASK_UNKNOWN)
52/*
53 * Value to pass to pcap_compile() as the netmask if you don't know what
54 * the netmask is.
55 */
56#define PCAP_NETMASK_UNKNOWN 0xffffffff
57#endif
58
Davide Pesavento44deacc2014-02-19 10:48:07 +010059namespace nfd {
60
Davide Pesavento1bdef282014-04-08 20:59:50 +020061NFD_LOG_INIT("EthernetFace");
Davide Pesavento44deacc2014-02-19 10:48:07 +010062
Matteo Sammarco66df9742014-11-21 18:31:26 +010063const time::nanoseconds EthernetFace::REASSEMBLER_LIFETIME = time::seconds(60);
64
Davide Pesavento44deacc2014-02-19 10:48:07 +010065EthernetFace::EthernetFace(const shared_ptr<boost::asio::posix::stream_descriptor>& socket,
Davide Pesaventob499a602014-11-18 22:36:56 +010066 const NetworkInterfaceInfo& interface,
Davide Pesavento44deacc2014-02-19 10:48:07 +010067 const ethernet::Address& address)
Davide Pesaventob499a602014-11-18 22:36:56 +010068 : Face(FaceUri(address), FaceUri::fromDev(interface.name))
Davide Pesavento7726ae52014-11-23 21:01:05 +010069 , m_pcap(nullptr, pcap_close)
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +000070 , m_socket(socket)
Davide Pesavento10783f22014-03-15 04:40:01 +010071#if defined(__linux__)
72 , m_interfaceIndex(interface.index)
73#endif
Davide Pesaventob499a602014-11-18 22:36:56 +010074 , m_interfaceName(interface.name)
75 , m_srcAddress(interface.etherAddress)
Davide Pesavento44deacc2014-02-19 10:48:07 +010076 , m_destAddress(address)
77{
Davide Pesaventob60cc122014-03-19 19:26:02 +010078 NFD_LOG_INFO("Creating ethernet face on " << m_interfaceName << ": "
79 << m_srcAddress << " <--> " << m_destAddress);
Davide Pesavento44deacc2014-02-19 10:48:07 +010080 pcapInit();
81
Davide Pesavento7726ae52014-11-23 21:01:05 +010082 int fd = pcap_get_selectable_fd(m_pcap.get());
Davide Pesavento44deacc2014-02-19 10:48:07 +010083 if (fd < 0)
84 throw Error("pcap_get_selectable_fd() failed");
85
86 // need to duplicate the fd, otherwise both pcap_close()
87 // and stream_descriptor::close() will try to close the
88 // same fd and one of them will fail
89 m_socket->assign(::dup(fd));
90
Davide Pesavento44deacc2014-02-19 10:48:07 +010091 m_interfaceMtu = getInterfaceMtu();
Davide Pesaventob60cc122014-03-19 19:26:02 +010092 NFD_LOG_DEBUG("[id:" << getId() << ",endpoint:" << m_interfaceName
Davide Pesavento44deacc2014-02-19 10:48:07 +010093 << "] Interface MTU is: " << m_interfaceMtu);
94
Matteo Sammarco66df9742014-11-21 18:31:26 +010095 m_slicer.reset(new ndnlp::Slicer(m_interfaceMtu));
96
Davide Pesavento44deacc2014-02-19 10:48:07 +010097 char filter[100];
Davide Pesavento10783f22014-03-15 04:40:01 +010098 std::snprintf(filter, sizeof(filter),
99 "(ether proto 0x%x) && (ether dst %s) && (not ether src %s)",
100 ethernet::ETHERTYPE_NDN,
101 m_destAddress.toString().c_str(),
102 m_srcAddress.toString().c_str());
Davide Pesavento44deacc2014-02-19 10:48:07 +0100103 setPacketFilter(filter);
104
Davide Pesavento10783f22014-03-15 04:40:01 +0100105 joinMulticastGroup();
106
Davide Pesavento44deacc2014-02-19 10:48:07 +0100107 m_socket->async_read_some(boost::asio::null_buffers(),
108 bind(&EthernetFace::handleRead, this,
109 boost::asio::placeholders::error,
110 boost::asio::placeholders::bytes_transferred));
111}
112
113EthernetFace::~EthernetFace()
114{
Davide Pesavento44deacc2014-02-19 10:48:07 +0100115}
116
117void
118EthernetFace::sendInterest(const Interest& interest)
119{
Alexander Afanasyev7e698e62014-03-07 16:48:35 +0000120 onSendInterest(interest);
Matteo Sammarco66df9742014-11-21 18:31:26 +0100121 ndnlp::PacketArray pa = m_slicer->slice(interest.wireEncode());
122 for (const auto& packet : *pa) {
123 sendPacket(packet);
124 }
Davide Pesavento44deacc2014-02-19 10:48:07 +0100125}
126
127void
128EthernetFace::sendData(const Data& data)
129{
Alexander Afanasyev7e698e62014-03-07 16:48:35 +0000130 onSendData(data);
Matteo Sammarco66df9742014-11-21 18:31:26 +0100131 ndnlp::PacketArray pa = m_slicer->slice(data.wireEncode());
132 for (const auto& packet : *pa) {
133 sendPacket(packet);
134 }
Davide Pesavento44deacc2014-02-19 10:48:07 +0100135}
136
137void
138EthernetFace::close()
139{
Davide Pesavento7726ae52014-11-23 21:01:05 +0100140 if (!m_pcap)
141 return;
Alexander Afanasyev251f3c12014-06-02 18:39:58 +0300142
Davide Pesavento7726ae52014-11-23 21:01:05 +0100143 NFD_LOG_INFO("[id:" << getId() << ",endpoint:" << m_interfaceName
144 << "] Closing face");
145
146 boost::system::error_code error;
147 m_socket->cancel(error); // ignore errors
148 m_socket->close(error); // ignore errors
149 m_pcap.reset(nullptr);
150
151 fail("Face closed");
Davide Pesavento44deacc2014-02-19 10:48:07 +0100152}
153
154void
155EthernetFace::pcapInit()
156{
Davide Pesavento7726ae52014-11-23 21:01:05 +0100157 char errbuf[PCAP_ERRBUF_SIZE] = {};
158 m_pcap.reset(pcap_create(m_interfaceName.c_str(), errbuf));
Davide Pesavento44deacc2014-02-19 10:48:07 +0100159 if (!m_pcap)
160 throw Error("pcap_create(): " + std::string(errbuf));
161
Davide Pesaventof0ae4422014-05-05 16:32:12 +0200162#ifdef HAVE_PCAP_SET_IMMEDIATE_MODE
163 // Enable "immediate mode", effectively disabling any read buffering in the kernel.
164 // This corresponds to the BIOCIMMEDIATE ioctl on BSD-like systems (including OS X)
165 // where libpcap uses a BPF device. On Linux this forces libpcap not to use TPACKET_V3,
166 // even if the kernel supports it, thus preventing bug #1511.
Davide Pesavento7726ae52014-11-23 21:01:05 +0100167 pcap_set_immediate_mode(m_pcap.get(), 1);
Davide Pesaventof0ae4422014-05-05 16:32:12 +0200168#endif
169
Davide Pesavento7726ae52014-11-23 21:01:05 +0100170 if (pcap_activate(m_pcap.get()) < 0)
Davide Pesavento44deacc2014-02-19 10:48:07 +0100171 throw Error("pcap_activate() failed");
172
Davide Pesavento7726ae52014-11-23 21:01:05 +0100173 if (pcap_set_datalink(m_pcap.get(), DLT_EN10MB) < 0)
174 throw Error("pcap_set_datalink(): " + std::string(pcap_geterr(m_pcap.get())));
Davide Pesavento9eaeac72014-03-15 05:03:42 +0100175
Davide Pesavento7726ae52014-11-23 21:01:05 +0100176 if (pcap_setdirection(m_pcap.get(), PCAP_D_IN) < 0)
Davide Pesavento9eaeac72014-03-15 05:03:42 +0100177 // no need to throw on failure, BPF will filter unwanted packets anyway
Davide Pesavento7726ae52014-11-23 21:01:05 +0100178 NFD_LOG_WARN("pcap_setdirection(): " << pcap_geterr(m_pcap.get()));
Davide Pesavento44deacc2014-02-19 10:48:07 +0100179}
180
181void
182EthernetFace::setPacketFilter(const char* filterString)
183{
184 bpf_program filter;
Davide Pesavento7726ae52014-11-23 21:01:05 +0100185 if (pcap_compile(m_pcap.get(), &filter, filterString, 1, PCAP_NETMASK_UNKNOWN) < 0)
186 throw Error("pcap_compile(): " + std::string(pcap_geterr(m_pcap.get())));
Davide Pesavento44deacc2014-02-19 10:48:07 +0100187
Davide Pesavento7726ae52014-11-23 21:01:05 +0100188 int ret = pcap_setfilter(m_pcap.get(), &filter);
Davide Pesavento44deacc2014-02-19 10:48:07 +0100189 pcap_freecode(&filter);
190 if (ret < 0)
Davide Pesavento7726ae52014-11-23 21:01:05 +0100191 throw Error("pcap_setfilter(): " + std::string(pcap_geterr(m_pcap.get())));
Davide Pesavento44deacc2014-02-19 10:48:07 +0100192}
193
194void
Davide Pesavento10783f22014-03-15 04:40:01 +0100195EthernetFace::joinMulticastGroup()
196{
197#if defined(__linux__)
198 packet_mreq mr{};
199 mr.mr_ifindex = m_interfaceIndex;
200 mr.mr_type = PACKET_MR_MULTICAST;
201 mr.mr_alen = m_destAddress.size();
202 std::copy(m_destAddress.begin(), m_destAddress.end(), mr.mr_address);
203
204 if (::setsockopt(m_socket->native_handle(), SOL_PACKET,
205 PACKET_ADD_MEMBERSHIP, &mr, sizeof(mr)) == 0)
206 return; // success
207
208 NFD_LOG_WARN("[id:" << getId() << ",endpoint:" << m_interfaceName
209 << "] setsockopt(PACKET_ADD_MEMBERSHIP) failed: " << std::strerror(errno));
210#endif
211
212#if defined(SIOCADDMULTI)
213 ifreq ifr{};
214 std::strncpy(ifr.ifr_name, m_interfaceName.c_str(), sizeof(ifr.ifr_name) - 1);
215
216#if defined(__APPLE__) || defined(__FreeBSD__)
217 /*
218 * Differences between Linux and the BSDs (including OS X):
219 * o BSD does not have ifr_hwaddr; use ifr_addr instead.
220 * o While OS X seems to accept both AF_LINK and AF_UNSPEC as the address
221 * family, FreeBSD explicitly requires AF_LINK, so we have to use AF_LINK
222 * and sockaddr_dl instead of the generic sockaddr structure.
223 * o BSD's sockaddr (and sockaddr_dl in particular) contains an additional
224 * field, sa_len (sdl_len), which must be set to the total length of the
225 * structure, including the length field itself.
226 * o We do not specify the interface name, thus sdl_nlen is left at 0 and
227 * LLADDR is effectively the same as sdl_data.
228 */
229 sockaddr_dl* sdl = reinterpret_cast<sockaddr_dl*>(&ifr.ifr_addr);
230 sdl->sdl_len = sizeof(ifr.ifr_addr);
231 sdl->sdl_family = AF_LINK;
232 sdl->sdl_alen = m_destAddress.size();
233 std::copy(m_destAddress.begin(), m_destAddress.end(), LLADDR(sdl));
234
235 static_assert(sizeof(ifr.ifr_addr) >= offsetof(sockaddr_dl, sdl_data) + ethernet::ADDR_LEN,
236 "ifr_addr in struct ifreq is too small on this platform");
237#else
238 ifr.ifr_hwaddr.sa_family = AF_UNSPEC;
239 std::copy(m_destAddress.begin(), m_destAddress.end(), ifr.ifr_hwaddr.sa_data);
240
241 static_assert(sizeof(ifr.ifr_hwaddr) >= offsetof(sockaddr, sa_data) + ethernet::ADDR_LEN,
242 "ifr_hwaddr in struct ifreq is too small on this platform");
243#endif
244
245 if (::ioctl(m_socket->native_handle(), SIOCADDMULTI, &ifr) >= 0)
246 return; // success
247
248 NFD_LOG_WARN("[id:" << getId() << ",endpoint:" << m_interfaceName
249 << "] ioctl(SIOCADDMULTI) failed: " << std::strerror(errno));
250#endif
251
252 if (!m_destAddress.isBroadcast())
253 {
254 NFD_LOG_DEBUG("[id:" << getId() << ",endpoint:" << m_interfaceName
255 << "] Falling back to promiscuous mode");
Davide Pesavento7726ae52014-11-23 21:01:05 +0100256 pcap_set_promisc(m_pcap.get(), 1);
Davide Pesavento10783f22014-03-15 04:40:01 +0100257 }
258}
259
260void
Davide Pesavento44deacc2014-02-19 10:48:07 +0100261EthernetFace::sendPacket(const ndn::Block& block)
262{
263 if (!m_pcap)
264 {
Davide Pesaventob60cc122014-03-19 19:26:02 +0100265 NFD_LOG_WARN("[id:" << getId() << ",endpoint:" << m_interfaceName
Davide Pesavento44deacc2014-02-19 10:48:07 +0100266 << "] Trying to send on closed face");
Davide Pesavento7726ae52014-11-23 21:01:05 +0100267 return fail("Face closed");
Davide Pesavento44deacc2014-02-19 10:48:07 +0100268 }
269
Matteo Sammarco66df9742014-11-21 18:31:26 +0100270 BOOST_ASSERT(block.size() <= m_interfaceMtu);
Davide Pesavento44deacc2014-02-19 10:48:07 +0100271
272 /// \todo Right now there is no reserve when packet is received, but
273 /// we should reserve some space at the beginning and at the end
274 ndn::EncodingBuffer buffer(block);
275
Davide Pesaventoe4631002014-03-21 20:55:36 +0100276 // pad with zeroes if the payload is too short
Davide Pesavento44deacc2014-02-19 10:48:07 +0100277 if (block.size() < ethernet::MIN_DATA_LEN)
278 {
Davide Pesaventob60cc122014-03-19 19:26:02 +0100279 static const uint8_t padding[ethernet::MIN_DATA_LEN] = {0};
280 buffer.appendByteArray(padding, ethernet::MIN_DATA_LEN - block.size());
Davide Pesavento44deacc2014-02-19 10:48:07 +0100281 }
282
283 // construct and prepend the ethernet header
Junxiao Shi3ffe66d2014-11-06 15:37:59 -0700284 static uint16_t ethertype = htons(ethernet::ETHERTYPE_NDN);
Davide Pesavento44deacc2014-02-19 10:48:07 +0100285 buffer.prependByteArray(reinterpret_cast<const uint8_t*>(&ethertype), ethernet::TYPE_LEN);
Davide Pesaventob60cc122014-03-19 19:26:02 +0100286 buffer.prependByteArray(m_srcAddress.data(), m_srcAddress.size());
Davide Pesavento44deacc2014-02-19 10:48:07 +0100287 buffer.prependByteArray(m_destAddress.data(), m_destAddress.size());
288
289 // send the packet
Davide Pesavento7726ae52014-11-23 21:01:05 +0100290 int sent = pcap_inject(m_pcap.get(), buffer.buf(), buffer.size());
Davide Pesavento44deacc2014-02-19 10:48:07 +0100291 if (sent < 0)
292 {
Davide Pesavento7726ae52014-11-23 21:01:05 +0100293 return fail("pcap_inject(): " + std::string(pcap_geterr(m_pcap.get())));
Davide Pesavento44deacc2014-02-19 10:48:07 +0100294 }
295 else if (static_cast<size_t>(sent) < buffer.size())
296 {
Davide Pesavento7726ae52014-11-23 21:01:05 +0100297 return fail("Failed to inject frame");
Davide Pesavento44deacc2014-02-19 10:48:07 +0100298 }
299
Davide Pesaventob60cc122014-03-19 19:26:02 +0100300 NFD_LOG_TRACE("[id:" << getId() << ",endpoint:" << m_interfaceName
Davide Pesavento4c1a0782014-08-14 16:13:20 +0200301 << "] Successfully sent: " << block.size() << " bytes");
302 this->getMutableCounters().getNOutBytes() += block.size();
Davide Pesavento44deacc2014-02-19 10:48:07 +0100303}
304
305void
306EthernetFace::handleRead(const boost::system::error_code& error, size_t)
307{
Davide Pesavento7726ae52014-11-23 21:01:05 +0100308 if (!m_pcap)
309 return fail("Face closed");
310
Davide Pesavento44deacc2014-02-19 10:48:07 +0100311 if (error)
312 return processErrorCode(error);
313
Davide Pesavento7726ae52014-11-23 21:01:05 +0100314 pcap_pkthdr* header;
Davide Pesavento44deacc2014-02-19 10:48:07 +0100315 const uint8_t* packet;
Davide Pesavento7726ae52014-11-23 21:01:05 +0100316 int ret = pcap_next_ex(m_pcap.get(), &header, &packet);
Davide Pesavento44deacc2014-02-19 10:48:07 +0100317 if (ret < 0)
318 {
Davide Pesavento7726ae52014-11-23 21:01:05 +0100319 return fail("pcap_next_ex(): " + std::string(pcap_geterr(m_pcap.get())));
Davide Pesavento44deacc2014-02-19 10:48:07 +0100320 }
321 else if (ret == 0)
322 {
Davide Pesavento7726ae52014-11-23 21:01:05 +0100323 NFD_LOG_WARN("[id:" << getId() << ",endpoint:" << m_interfaceName << "] Read timeout");
Davide Pesavento44deacc2014-02-19 10:48:07 +0100324 }
325 else
326 {
Davide Pesavento7726ae52014-11-23 21:01:05 +0100327 processIncomingPacket(header, packet);
Davide Pesavento44deacc2014-02-19 10:48:07 +0100328 }
329
330 m_socket->async_read_some(boost::asio::null_buffers(),
331 bind(&EthernetFace::handleRead, this,
332 boost::asio::placeholders::error,
333 boost::asio::placeholders::bytes_transferred));
334}
335
336void
Davide Pesavento7726ae52014-11-23 21:01:05 +0100337EthernetFace::processIncomingPacket(const pcap_pkthdr* header, const uint8_t* packet)
Davide Pesavento44deacc2014-02-19 10:48:07 +0100338{
Davide Pesavento7726ae52014-11-23 21:01:05 +0100339 size_t length = header->caplen;
340 if (length < ethernet::HDR_LEN + ethernet::MIN_DATA_LEN)
Davide Pesavento44deacc2014-02-19 10:48:07 +0100341 {
Davide Pesavento7726ae52014-11-23 21:01:05 +0100342 NFD_LOG_WARN("[id:" << getId() << ",endpoint:" << m_interfaceName
343 << "] Received frame is too short (" << length << " bytes)");
Davide Pesavento44deacc2014-02-19 10:48:07 +0100344 return;
345 }
346
Davide Pesavento7726ae52014-11-23 21:01:05 +0100347 const ether_header* eh = reinterpret_cast<const ether_header*>(packet);
Matteo Sammarco66df9742014-11-21 18:31:26 +0100348 const ethernet::Address sourceAddress(eh->ether_shost);
Davide Pesavento7726ae52014-11-23 21:01:05 +0100349
350 // assert in case BPF fails to filter unwanted frames
351 BOOST_ASSERT_MSG(ethernet::Address(eh->ether_dhost) == m_destAddress,
352 "Received frame addressed to a different multicast group");
Matteo Sammarco66df9742014-11-21 18:31:26 +0100353 BOOST_ASSERT_MSG(sourceAddress != m_srcAddress,
Davide Pesavento7726ae52014-11-23 21:01:05 +0100354 "Received frame sent by this host");
355 BOOST_ASSERT_MSG(ntohs(eh->ether_type) == ethernet::ETHERTYPE_NDN,
356 "Received frame with unrecognized ethertype");
357
358 packet += ethernet::HDR_LEN;
359 length -= ethernet::HDR_LEN;
360
361 /// \todo Reserve space in front and at the back of the underlying buffer
Matteo Sammarco66df9742014-11-21 18:31:26 +0100362 Block fragment;
363 bool isOk = Block::fromBuffer(packet, length, fragment);
364 if (!isOk)
Davide Pesavento7726ae52014-11-23 21:01:05 +0100365 {
366 NFD_LOG_WARN("[id:" << getId() << ",endpoint:" << m_interfaceName
Matteo Sammarco66df9742014-11-21 18:31:26 +0100367 << "] Block received from " << sourceAddress.toString()
368 << " is invalid or too large to process");
369 return;
Davide Pesavento7726ae52014-11-23 21:01:05 +0100370 }
Matteo Sammarco66df9742014-11-21 18:31:26 +0100371
372 NFD_LOG_TRACE("[id:" << getId() << ",endpoint:" << m_interfaceName
373 << "] Received: " << fragment.size() << " bytes from "
374 << sourceAddress.toString());
375 this->getMutableCounters().getNInBytes() += fragment.size();
376
377 Reassembler& reassembler = m_reassemblers[sourceAddress];
378 if (!reassembler.pms)
379 {
380 // new sender, setup a PartialMessageStore for it
381 reassembler.pms.reset(new ndnlp::PartialMessageStore);
382 reassembler.pms->onReceive +=
383 [this, sourceAddress] (const Block& block) {
384 NFD_LOG_TRACE("[id:" << getId() << ",endpoint:" << m_interfaceName
385 << "] All fragments received from " << sourceAddress.toString());
386 if (!decodeAndDispatchInput(block))
387 NFD_LOG_WARN("[id:" << getId() << ",endpoint:" << m_interfaceName
388 << "] Received unrecognized TLV block of type " << block.type()
389 << " from " << sourceAddress.toString());
390 };
391 }
392
393 scheduler::cancel(reassembler.expireEvent);
394 reassembler.expireEvent = scheduler::schedule(REASSEMBLER_LIFETIME,
395 [this, sourceAddress] {
396 BOOST_VERIFY(m_reassemblers.erase(sourceAddress) == 1);
397 });
398
399 try {
400 reassembler.pms->receiveNdnlpData(fragment);
401 }
402 catch (const ndnlp::ParseError& e) {
403 NFD_LOG_WARN("[id:" << getId() << ",endpoint:" << m_interfaceName
404 << "] Received invalid NDNLP fragment from "
405 << sourceAddress.toString() << " : " << e.what());
406 }
Davide Pesavento7726ae52014-11-23 21:01:05 +0100407}
408
409void
410EthernetFace::processErrorCode(const boost::system::error_code& error)
411{
412 if (error == boost::asio::error::operation_aborted)
413 // cancel() has been called on the socket
414 return;
415
Davide Pesavento44deacc2014-02-19 10:48:07 +0100416 std::string msg;
417 if (error == boost::asio::error::eof)
418 {
419 msg = "Face closed";
Davide Pesaventob60cc122014-03-19 19:26:02 +0100420 NFD_LOG_INFO("[id:" << getId() << ",endpoint:" << m_interfaceName << "] " << msg);
Davide Pesavento44deacc2014-02-19 10:48:07 +0100421 }
422 else
423 {
Davide Pesavento7726ae52014-11-23 21:01:05 +0100424 msg = "Receive operation failed: " + error.message();
Davide Pesaventob60cc122014-03-19 19:26:02 +0100425 NFD_LOG_WARN("[id:" << getId() << ",endpoint:" << m_interfaceName << "] " << msg);
Davide Pesavento44deacc2014-02-19 10:48:07 +0100426 }
Junxiao Shi08d07a72014-06-09 23:17:57 -0700427 fail(msg);
Davide Pesavento44deacc2014-02-19 10:48:07 +0100428}
429
Davide Pesavento44deacc2014-02-19 10:48:07 +0100430size_t
431EthernetFace::getInterfaceMtu() const
432{
433 size_t mtu = ethernet::MAX_DATA_LEN;
434
435#ifdef SIOCGIFMTU
Davide Pesavento10783f22014-03-15 04:40:01 +0100436 ifreq ifr{};
Davide Pesaventob60cc122014-03-19 19:26:02 +0100437 std::strncpy(ifr.ifr_name, m_interfaceName.c_str(), sizeof(ifr.ifr_name) - 1);
Davide Pesavento44deacc2014-02-19 10:48:07 +0100438
439 if (::ioctl(m_socket->native_handle(), SIOCGIFMTU, &ifr) < 0)
440 {
Davide Pesaventob60cc122014-03-19 19:26:02 +0100441 NFD_LOG_WARN("[id:" << getId() << ",endpoint:" << m_interfaceName
Davide Pesavento10783f22014-03-15 04:40:01 +0100442 << "] Failed to get interface MTU: " << std::strerror(errno));
Davide Pesavento44deacc2014-02-19 10:48:07 +0100443 }
444 else
445 {
446 mtu = std::min(mtu, static_cast<size_t>(ifr.ifr_mtu));
447 }
448#endif
449
450 return mtu;
451}
452
453} // namespace nfd