blob: c63e38e8a260d36f6d772639f44fe5f53b9b5fde [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"
Davide Pesaventof8b41eb2014-12-26 19:14:06 +010027#include "core/global-io.hpp"
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060028#include "core/logger.hpp"
Davide Pesavento44deacc2014-02-19 10:48:07 +010029
30#include <pcap/pcap.h>
31
Davide Pesavento10783f22014-03-15 04:40:01 +010032#include <cerrno> // for errno
33#include <cstdio> // for std::snprintf()
34#include <cstring> // for std::strerror() and std::strncpy()
Davide Pesavento44deacc2014-02-19 10:48:07 +010035#include <arpa/inet.h> // for htons() and ntohs()
36#include <net/ethernet.h> // for struct ether_header
37#include <net/if.h> // for struct ifreq
Davide Pesavento44deacc2014-02-19 10:48:07 +010038#include <sys/ioctl.h> // for ioctl()
39#include <unistd.h> // for dup()
40
Davide Pesavento10783f22014-03-15 04:40:01 +010041#if defined(__linux__)
42#include <netpacket/packet.h> // for struct packet_mreq
43#include <sys/socket.h> // for setsockopt()
44#endif
45
46#ifdef SIOCADDMULTI
47#if defined(__APPLE__) || defined(__FreeBSD__)
48#include <net/if_dl.h> // for struct sockaddr_dl
49#endif
50#endif
51
Alexander Afanasyevd9479aa2014-06-26 13:52:02 -070052#if !defined(PCAP_NETMASK_UNKNOWN)
53/*
54 * Value to pass to pcap_compile() as the netmask if you don't know what
55 * the netmask is.
56 */
57#define PCAP_NETMASK_UNKNOWN 0xffffffff
58#endif
59
Davide Pesavento44deacc2014-02-19 10:48:07 +010060namespace nfd {
61
Davide Pesavento1bdef282014-04-08 20:59:50 +020062NFD_LOG_INIT("EthernetFace");
Davide Pesavento44deacc2014-02-19 10:48:07 +010063
Matteo Sammarco66df9742014-11-21 18:31:26 +010064const time::nanoseconds EthernetFace::REASSEMBLER_LIFETIME = time::seconds(60);
65
Davide Pesavento44deacc2014-02-19 10:48:07 +010066EthernetFace::EthernetFace(const shared_ptr<boost::asio::posix::stream_descriptor>& socket,
Davide Pesaventob499a602014-11-18 22:36:56 +010067 const NetworkInterfaceInfo& interface,
Davide Pesavento44deacc2014-02-19 10:48:07 +010068 const ethernet::Address& address)
Davide Pesaventob499a602014-11-18 22:36:56 +010069 : Face(FaceUri(address), FaceUri::fromDev(interface.name))
Davide Pesavento7726ae52014-11-23 21:01:05 +010070 , m_pcap(nullptr, pcap_close)
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +000071 , m_socket(socket)
Davide Pesavento10783f22014-03-15 04:40:01 +010072#if defined(__linux__)
73 , m_interfaceIndex(interface.index)
74#endif
Davide Pesaventob499a602014-11-18 22:36:56 +010075 , m_interfaceName(interface.name)
76 , m_srcAddress(interface.etherAddress)
Davide Pesavento44deacc2014-02-19 10:48:07 +010077 , m_destAddress(address)
78{
Davide Pesaventob60cc122014-03-19 19:26:02 +010079 NFD_LOG_INFO("Creating ethernet face on " << m_interfaceName << ": "
80 << m_srcAddress << " <--> " << m_destAddress);
Davide Pesavento44deacc2014-02-19 10:48:07 +010081 pcapInit();
82
Davide Pesavento7726ae52014-11-23 21:01:05 +010083 int fd = pcap_get_selectable_fd(m_pcap.get());
Davide Pesavento44deacc2014-02-19 10:48:07 +010084 if (fd < 0)
Davide Pesaventof8b41eb2014-12-26 19:14:06 +010085 throw Error("pcap_get_selectable_fd failed");
Davide Pesavento44deacc2014-02-19 10:48:07 +010086
87 // need to duplicate the fd, otherwise both pcap_close()
88 // and stream_descriptor::close() will try to close the
89 // same fd and one of them will fail
90 m_socket->assign(::dup(fd));
91
Davide Pesavento44deacc2014-02-19 10:48:07 +010092 m_interfaceMtu = getInterfaceMtu();
Davide Pesaventob60cc122014-03-19 19:26:02 +010093 NFD_LOG_DEBUG("[id:" << getId() << ",endpoint:" << m_interfaceName
Davide Pesavento44deacc2014-02-19 10:48:07 +010094 << "] Interface MTU is: " << m_interfaceMtu);
95
Matteo Sammarco66df9742014-11-21 18:31:26 +010096 m_slicer.reset(new ndnlp::Slicer(m_interfaceMtu));
97
Davide Pesavento44deacc2014-02-19 10:48:07 +010098 char filter[100];
Davide Pesavento10783f22014-03-15 04:40:01 +010099 std::snprintf(filter, sizeof(filter),
100 "(ether proto 0x%x) && (ether dst %s) && (not ether src %s)",
101 ethernet::ETHERTYPE_NDN,
102 m_destAddress.toString().c_str(),
103 m_srcAddress.toString().c_str());
Davide Pesavento44deacc2014-02-19 10:48:07 +0100104 setPacketFilter(filter);
105
Davide Pesaventof8b41eb2014-12-26 19:14:06 +0100106 if (!m_destAddress.isBroadcast() && !joinMulticastGroup())
107 {
108 NFD_LOG_WARN("[id:" << getId() << ",endpoint:" << m_interfaceName
109 << "] Falling back to promiscuous mode");
110 pcap_set_promisc(m_pcap.get(), 1);
111 }
Davide Pesavento10783f22014-03-15 04:40:01 +0100112
Davide Pesavento44deacc2014-02-19 10:48:07 +0100113 m_socket->async_read_some(boost::asio::null_buffers(),
114 bind(&EthernetFace::handleRead, this,
115 boost::asio::placeholders::error,
116 boost::asio::placeholders::bytes_transferred));
117}
118
119EthernetFace::~EthernetFace()
120{
Davide Pesavento44deacc2014-02-19 10:48:07 +0100121}
122
123void
124EthernetFace::sendInterest(const Interest& interest)
125{
Alexander Afanasyev7e698e62014-03-07 16:48:35 +0000126 onSendInterest(interest);
Matteo Sammarco66df9742014-11-21 18:31:26 +0100127 ndnlp::PacketArray pa = m_slicer->slice(interest.wireEncode());
128 for (const auto& packet : *pa) {
129 sendPacket(packet);
130 }
Davide Pesavento44deacc2014-02-19 10:48:07 +0100131}
132
133void
134EthernetFace::sendData(const Data& data)
135{
Alexander Afanasyev7e698e62014-03-07 16:48:35 +0000136 onSendData(data);
Matteo Sammarco66df9742014-11-21 18:31:26 +0100137 ndnlp::PacketArray pa = m_slicer->slice(data.wireEncode());
138 for (const auto& packet : *pa) {
139 sendPacket(packet);
140 }
Davide Pesavento44deacc2014-02-19 10:48:07 +0100141}
142
143void
144EthernetFace::close()
145{
Davide Pesavento7726ae52014-11-23 21:01:05 +0100146 if (!m_pcap)
147 return;
Alexander Afanasyev251f3c12014-06-02 18:39:58 +0300148
Davide Pesavento7726ae52014-11-23 21:01:05 +0100149 NFD_LOG_INFO("[id:" << getId() << ",endpoint:" << m_interfaceName
150 << "] Closing face");
151
152 boost::system::error_code error;
153 m_socket->cancel(error); // ignore errors
154 m_socket->close(error); // ignore errors
155 m_pcap.reset(nullptr);
156
157 fail("Face closed");
Davide Pesavento44deacc2014-02-19 10:48:07 +0100158}
159
160void
161EthernetFace::pcapInit()
162{
Davide Pesavento7726ae52014-11-23 21:01:05 +0100163 char errbuf[PCAP_ERRBUF_SIZE] = {};
164 m_pcap.reset(pcap_create(m_interfaceName.c_str(), errbuf));
Davide Pesavento44deacc2014-02-19 10:48:07 +0100165 if (!m_pcap)
Davide Pesaventof8b41eb2014-12-26 19:14:06 +0100166 throw Error("pcap_create: " + std::string(errbuf));
Davide Pesavento44deacc2014-02-19 10:48:07 +0100167
Davide Pesaventof0ae4422014-05-05 16:32:12 +0200168#ifdef HAVE_PCAP_SET_IMMEDIATE_MODE
169 // Enable "immediate mode", effectively disabling any read buffering in the kernel.
170 // This corresponds to the BIOCIMMEDIATE ioctl on BSD-like systems (including OS X)
171 // where libpcap uses a BPF device. On Linux this forces libpcap not to use TPACKET_V3,
172 // even if the kernel supports it, thus preventing bug #1511.
Davide Pesavento7726ae52014-11-23 21:01:05 +0100173 pcap_set_immediate_mode(m_pcap.get(), 1);
Davide Pesaventof0ae4422014-05-05 16:32:12 +0200174#endif
175
Davide Pesavento7726ae52014-11-23 21:01:05 +0100176 if (pcap_activate(m_pcap.get()) < 0)
Davide Pesaventof8b41eb2014-12-26 19:14:06 +0100177 throw Error("pcap_activate failed");
Davide Pesavento44deacc2014-02-19 10:48:07 +0100178
Davide Pesavento7726ae52014-11-23 21:01:05 +0100179 if (pcap_set_datalink(m_pcap.get(), DLT_EN10MB) < 0)
Davide Pesaventof8b41eb2014-12-26 19:14:06 +0100180 throw Error("pcap_set_datalink: " + std::string(pcap_geterr(m_pcap.get())));
Davide Pesavento9eaeac72014-03-15 05:03:42 +0100181
Davide Pesavento7726ae52014-11-23 21:01:05 +0100182 if (pcap_setdirection(m_pcap.get(), PCAP_D_IN) < 0)
Davide Pesavento9eaeac72014-03-15 05:03:42 +0100183 // no need to throw on failure, BPF will filter unwanted packets anyway
Davide Pesaventof8b41eb2014-12-26 19:14:06 +0100184 NFD_LOG_WARN("pcap_setdirection: " << pcap_geterr(m_pcap.get()));
Davide Pesavento44deacc2014-02-19 10:48:07 +0100185}
186
187void
188EthernetFace::setPacketFilter(const char* filterString)
189{
190 bpf_program filter;
Davide Pesavento7726ae52014-11-23 21:01:05 +0100191 if (pcap_compile(m_pcap.get(), &filter, filterString, 1, PCAP_NETMASK_UNKNOWN) < 0)
Davide Pesaventof8b41eb2014-12-26 19:14:06 +0100192 throw Error("pcap_compile: " + std::string(pcap_geterr(m_pcap.get())));
Davide Pesavento44deacc2014-02-19 10:48:07 +0100193
Davide Pesavento7726ae52014-11-23 21:01:05 +0100194 int ret = pcap_setfilter(m_pcap.get(), &filter);
Davide Pesavento44deacc2014-02-19 10:48:07 +0100195 pcap_freecode(&filter);
196 if (ret < 0)
Davide Pesaventof8b41eb2014-12-26 19:14:06 +0100197 throw Error("pcap_setfilter: " + std::string(pcap_geterr(m_pcap.get())));
Davide Pesavento44deacc2014-02-19 10:48:07 +0100198}
199
Davide Pesaventof8b41eb2014-12-26 19:14:06 +0100200bool
Davide Pesavento10783f22014-03-15 04:40:01 +0100201EthernetFace::joinMulticastGroup()
202{
203#if defined(__linux__)
204 packet_mreq mr{};
205 mr.mr_ifindex = m_interfaceIndex;
206 mr.mr_type = PACKET_MR_MULTICAST;
207 mr.mr_alen = m_destAddress.size();
208 std::copy(m_destAddress.begin(), m_destAddress.end(), mr.mr_address);
209
210 if (::setsockopt(m_socket->native_handle(), SOL_PACKET,
211 PACKET_ADD_MEMBERSHIP, &mr, sizeof(mr)) == 0)
Davide Pesaventof8b41eb2014-12-26 19:14:06 +0100212 return true; // success
Davide Pesavento10783f22014-03-15 04:40:01 +0100213
214 NFD_LOG_WARN("[id:" << getId() << ",endpoint:" << m_interfaceName
215 << "] setsockopt(PACKET_ADD_MEMBERSHIP) failed: " << std::strerror(errno));
216#endif
217
218#if defined(SIOCADDMULTI)
219 ifreq ifr{};
220 std::strncpy(ifr.ifr_name, m_interfaceName.c_str(), sizeof(ifr.ifr_name) - 1);
221
222#if defined(__APPLE__) || defined(__FreeBSD__)
Davide Pesaventof8b41eb2014-12-26 19:14:06 +0100223 // see bug #2327
224 using boost::asio::ip::udp;
225 udp::socket sock(ref(getGlobalIoService()), udp::v4());
226 int fd = sock.native_handle();
227
Davide Pesavento10783f22014-03-15 04:40:01 +0100228 /*
229 * Differences between Linux and the BSDs (including OS X):
230 * o BSD does not have ifr_hwaddr; use ifr_addr instead.
231 * o While OS X seems to accept both AF_LINK and AF_UNSPEC as the address
232 * family, FreeBSD explicitly requires AF_LINK, so we have to use AF_LINK
233 * and sockaddr_dl instead of the generic sockaddr structure.
234 * o BSD's sockaddr (and sockaddr_dl in particular) contains an additional
235 * field, sa_len (sdl_len), which must be set to the total length of the
236 * structure, including the length field itself.
237 * o We do not specify the interface name, thus sdl_nlen is left at 0 and
238 * LLADDR is effectively the same as sdl_data.
239 */
240 sockaddr_dl* sdl = reinterpret_cast<sockaddr_dl*>(&ifr.ifr_addr);
241 sdl->sdl_len = sizeof(ifr.ifr_addr);
242 sdl->sdl_family = AF_LINK;
243 sdl->sdl_alen = m_destAddress.size();
244 std::copy(m_destAddress.begin(), m_destAddress.end(), LLADDR(sdl));
245
246 static_assert(sizeof(ifr.ifr_addr) >= offsetof(sockaddr_dl, sdl_data) + ethernet::ADDR_LEN,
247 "ifr_addr in struct ifreq is too small on this platform");
248#else
Davide Pesaventof8b41eb2014-12-26 19:14:06 +0100249 int fd = m_socket->native_handle();
250
Davide Pesavento10783f22014-03-15 04:40:01 +0100251 ifr.ifr_hwaddr.sa_family = AF_UNSPEC;
252 std::copy(m_destAddress.begin(), m_destAddress.end(), ifr.ifr_hwaddr.sa_data);
253
254 static_assert(sizeof(ifr.ifr_hwaddr) >= offsetof(sockaddr, sa_data) + ethernet::ADDR_LEN,
255 "ifr_hwaddr in struct ifreq is too small on this platform");
256#endif
257
Davide Pesaventof8b41eb2014-12-26 19:14:06 +0100258 if (::ioctl(fd, SIOCADDMULTI, &ifr) == 0)
259 return true; // success
Davide Pesavento10783f22014-03-15 04:40:01 +0100260
261 NFD_LOG_WARN("[id:" << getId() << ",endpoint:" << m_interfaceName
262 << "] ioctl(SIOCADDMULTI) failed: " << std::strerror(errno));
263#endif
264
Davide Pesaventof8b41eb2014-12-26 19:14:06 +0100265 return false;
Davide Pesavento10783f22014-03-15 04:40:01 +0100266}
267
268void
Davide Pesavento44deacc2014-02-19 10:48:07 +0100269EthernetFace::sendPacket(const ndn::Block& block)
270{
271 if (!m_pcap)
272 {
Davide Pesaventob60cc122014-03-19 19:26:02 +0100273 NFD_LOG_WARN("[id:" << getId() << ",endpoint:" << m_interfaceName
Davide Pesavento44deacc2014-02-19 10:48:07 +0100274 << "] Trying to send on closed face");
Davide Pesavento7726ae52014-11-23 21:01:05 +0100275 return fail("Face closed");
Davide Pesavento44deacc2014-02-19 10:48:07 +0100276 }
277
Matteo Sammarco66df9742014-11-21 18:31:26 +0100278 BOOST_ASSERT(block.size() <= m_interfaceMtu);
Davide Pesavento44deacc2014-02-19 10:48:07 +0100279
280 /// \todo Right now there is no reserve when packet is received, but
281 /// we should reserve some space at the beginning and at the end
282 ndn::EncodingBuffer buffer(block);
283
Davide Pesaventoe4631002014-03-21 20:55:36 +0100284 // pad with zeroes if the payload is too short
Davide Pesavento44deacc2014-02-19 10:48:07 +0100285 if (block.size() < ethernet::MIN_DATA_LEN)
286 {
Davide Pesavento8eb99572014-12-19 19:12:15 +0100287 static const uint8_t padding[ethernet::MIN_DATA_LEN] = {};
Davide Pesaventob60cc122014-03-19 19:26:02 +0100288 buffer.appendByteArray(padding, ethernet::MIN_DATA_LEN - block.size());
Davide Pesavento44deacc2014-02-19 10:48:07 +0100289 }
290
291 // construct and prepend the ethernet header
Junxiao Shi3ffe66d2014-11-06 15:37:59 -0700292 static uint16_t ethertype = htons(ethernet::ETHERTYPE_NDN);
Davide Pesavento44deacc2014-02-19 10:48:07 +0100293 buffer.prependByteArray(reinterpret_cast<const uint8_t*>(&ethertype), ethernet::TYPE_LEN);
Davide Pesaventob60cc122014-03-19 19:26:02 +0100294 buffer.prependByteArray(m_srcAddress.data(), m_srcAddress.size());
Davide Pesavento44deacc2014-02-19 10:48:07 +0100295 buffer.prependByteArray(m_destAddress.data(), m_destAddress.size());
296
297 // send the packet
Davide Pesavento7726ae52014-11-23 21:01:05 +0100298 int sent = pcap_inject(m_pcap.get(), buffer.buf(), buffer.size());
Davide Pesavento44deacc2014-02-19 10:48:07 +0100299 if (sent < 0)
300 {
Davide Pesaventof8b41eb2014-12-26 19:14:06 +0100301 return fail("pcap_inject: " + std::string(pcap_geterr(m_pcap.get())));
Davide Pesavento44deacc2014-02-19 10:48:07 +0100302 }
303 else if (static_cast<size_t>(sent) < buffer.size())
304 {
Davide Pesavento7726ae52014-11-23 21:01:05 +0100305 return fail("Failed to inject frame");
Davide Pesavento44deacc2014-02-19 10:48:07 +0100306 }
307
Davide Pesaventob60cc122014-03-19 19:26:02 +0100308 NFD_LOG_TRACE("[id:" << getId() << ",endpoint:" << m_interfaceName
Davide Pesavento4c1a0782014-08-14 16:13:20 +0200309 << "] Successfully sent: " << block.size() << " bytes");
310 this->getMutableCounters().getNOutBytes() += block.size();
Davide Pesavento44deacc2014-02-19 10:48:07 +0100311}
312
313void
314EthernetFace::handleRead(const boost::system::error_code& error, size_t)
315{
Davide Pesavento7726ae52014-11-23 21:01:05 +0100316 if (!m_pcap)
317 return fail("Face closed");
318
Davide Pesavento44deacc2014-02-19 10:48:07 +0100319 if (error)
320 return processErrorCode(error);
321
Davide Pesavento7726ae52014-11-23 21:01:05 +0100322 pcap_pkthdr* header;
Davide Pesavento44deacc2014-02-19 10:48:07 +0100323 const uint8_t* packet;
Davide Pesavento7726ae52014-11-23 21:01:05 +0100324 int ret = pcap_next_ex(m_pcap.get(), &header, &packet);
Davide Pesavento44deacc2014-02-19 10:48:07 +0100325 if (ret < 0)
326 {
Davide Pesaventof8b41eb2014-12-26 19:14:06 +0100327 return fail("pcap_next_ex: " + std::string(pcap_geterr(m_pcap.get())));
Davide Pesavento44deacc2014-02-19 10:48:07 +0100328 }
329 else if (ret == 0)
330 {
Davide Pesavento7726ae52014-11-23 21:01:05 +0100331 NFD_LOG_WARN("[id:" << getId() << ",endpoint:" << m_interfaceName << "] Read timeout");
Davide Pesavento44deacc2014-02-19 10:48:07 +0100332 }
333 else
334 {
Davide Pesavento7726ae52014-11-23 21:01:05 +0100335 processIncomingPacket(header, packet);
Davide Pesavento44deacc2014-02-19 10:48:07 +0100336 }
337
338 m_socket->async_read_some(boost::asio::null_buffers(),
339 bind(&EthernetFace::handleRead, this,
340 boost::asio::placeholders::error,
341 boost::asio::placeholders::bytes_transferred));
342}
343
344void
Davide Pesavento7726ae52014-11-23 21:01:05 +0100345EthernetFace::processIncomingPacket(const pcap_pkthdr* header, const uint8_t* packet)
Davide Pesavento44deacc2014-02-19 10:48:07 +0100346{
Davide Pesavento7726ae52014-11-23 21:01:05 +0100347 size_t length = header->caplen;
348 if (length < ethernet::HDR_LEN + ethernet::MIN_DATA_LEN)
Davide Pesavento44deacc2014-02-19 10:48:07 +0100349 {
Davide Pesavento7726ae52014-11-23 21:01:05 +0100350 NFD_LOG_WARN("[id:" << getId() << ",endpoint:" << m_interfaceName
351 << "] Received frame is too short (" << length << " bytes)");
Davide Pesavento44deacc2014-02-19 10:48:07 +0100352 return;
353 }
354
Davide Pesavento7726ae52014-11-23 21:01:05 +0100355 const ether_header* eh = reinterpret_cast<const ether_header*>(packet);
Matteo Sammarco66df9742014-11-21 18:31:26 +0100356 const ethernet::Address sourceAddress(eh->ether_shost);
Davide Pesavento7726ae52014-11-23 21:01:05 +0100357
358 // assert in case BPF fails to filter unwanted frames
359 BOOST_ASSERT_MSG(ethernet::Address(eh->ether_dhost) == m_destAddress,
360 "Received frame addressed to a different multicast group");
Matteo Sammarco66df9742014-11-21 18:31:26 +0100361 BOOST_ASSERT_MSG(sourceAddress != m_srcAddress,
Davide Pesavento7726ae52014-11-23 21:01:05 +0100362 "Received frame sent by this host");
363 BOOST_ASSERT_MSG(ntohs(eh->ether_type) == ethernet::ETHERTYPE_NDN,
364 "Received frame with unrecognized ethertype");
365
366 packet += ethernet::HDR_LEN;
367 length -= ethernet::HDR_LEN;
368
369 /// \todo Reserve space in front and at the back of the underlying buffer
Matteo Sammarco66df9742014-11-21 18:31:26 +0100370 Block fragment;
371 bool isOk = Block::fromBuffer(packet, length, fragment);
372 if (!isOk)
Davide Pesavento7726ae52014-11-23 21:01:05 +0100373 {
374 NFD_LOG_WARN("[id:" << getId() << ",endpoint:" << m_interfaceName
Matteo Sammarco66df9742014-11-21 18:31:26 +0100375 << "] Block received from " << sourceAddress.toString()
376 << " is invalid or too large to process");
377 return;
Davide Pesavento7726ae52014-11-23 21:01:05 +0100378 }
Matteo Sammarco66df9742014-11-21 18:31:26 +0100379
380 NFD_LOG_TRACE("[id:" << getId() << ",endpoint:" << m_interfaceName
381 << "] Received: " << fragment.size() << " bytes from "
382 << sourceAddress.toString());
383 this->getMutableCounters().getNInBytes() += fragment.size();
384
385 Reassembler& reassembler = m_reassemblers[sourceAddress];
386 if (!reassembler.pms)
387 {
388 // new sender, setup a PartialMessageStore for it
389 reassembler.pms.reset(new ndnlp::PartialMessageStore);
390 reassembler.pms->onReceive +=
391 [this, sourceAddress] (const Block& block) {
392 NFD_LOG_TRACE("[id:" << getId() << ",endpoint:" << m_interfaceName
393 << "] All fragments received from " << sourceAddress.toString());
394 if (!decodeAndDispatchInput(block))
395 NFD_LOG_WARN("[id:" << getId() << ",endpoint:" << m_interfaceName
396 << "] Received unrecognized TLV block of type " << block.type()
397 << " from " << sourceAddress.toString());
398 };
399 }
400
401 scheduler::cancel(reassembler.expireEvent);
402 reassembler.expireEvent = scheduler::schedule(REASSEMBLER_LIFETIME,
403 [this, sourceAddress] {
404 BOOST_VERIFY(m_reassemblers.erase(sourceAddress) == 1);
405 });
406
407 try {
408 reassembler.pms->receiveNdnlpData(fragment);
409 }
410 catch (const ndnlp::ParseError& e) {
411 NFD_LOG_WARN("[id:" << getId() << ",endpoint:" << m_interfaceName
412 << "] Received invalid NDNLP fragment from "
413 << sourceAddress.toString() << " : " << e.what());
414 }
Davide Pesavento7726ae52014-11-23 21:01:05 +0100415}
416
417void
418EthernetFace::processErrorCode(const boost::system::error_code& error)
419{
420 if (error == boost::asio::error::operation_aborted)
421 // cancel() has been called on the socket
422 return;
423
Davide Pesavento44deacc2014-02-19 10:48:07 +0100424 std::string msg;
425 if (error == boost::asio::error::eof)
426 {
427 msg = "Face closed";
Davide Pesaventof8b41eb2014-12-26 19:14:06 +0100428 NFD_LOG_DEBUG("[id:" << getId() << ",endpoint:" << m_interfaceName << "] " << msg);
Davide Pesavento44deacc2014-02-19 10:48:07 +0100429 }
430 else
431 {
Davide Pesavento7726ae52014-11-23 21:01:05 +0100432 msg = "Receive operation failed: " + error.message();
Davide Pesaventob60cc122014-03-19 19:26:02 +0100433 NFD_LOG_WARN("[id:" << getId() << ",endpoint:" << m_interfaceName << "] " << msg);
Davide Pesavento44deacc2014-02-19 10:48:07 +0100434 }
Junxiao Shi08d07a72014-06-09 23:17:57 -0700435 fail(msg);
Davide Pesavento44deacc2014-02-19 10:48:07 +0100436}
437
Davide Pesavento44deacc2014-02-19 10:48:07 +0100438size_t
439EthernetFace::getInterfaceMtu() const
440{
Davide Pesavento44deacc2014-02-19 10:48:07 +0100441#ifdef SIOCGIFMTU
Davide Pesaventof8b41eb2014-12-26 19:14:06 +0100442#if defined(__APPLE__) || defined(__FreeBSD__)
443 // see bug #2328
444 using boost::asio::ip::udp;
445 udp::socket sock(ref(getGlobalIoService()), udp::v4());
446 int fd = sock.native_handle();
447#else
448 int fd = m_socket->native_handle();
449#endif
450
Davide Pesavento10783f22014-03-15 04:40:01 +0100451 ifreq ifr{};
Davide Pesaventob60cc122014-03-19 19:26:02 +0100452 std::strncpy(ifr.ifr_name, m_interfaceName.c_str(), sizeof(ifr.ifr_name) - 1);
Davide Pesavento44deacc2014-02-19 10:48:07 +0100453
Davide Pesaventof8b41eb2014-12-26 19:14:06 +0100454 if (::ioctl(fd, SIOCGIFMTU, &ifr) == 0)
Davide Pesavento8eb99572014-12-19 19:12:15 +0100455 return static_cast<size_t>(ifr.ifr_mtu);
456
457 NFD_LOG_WARN("[id:" << getId() << ",endpoint:" << m_interfaceName
458 << "] Failed to get interface MTU: " << std::strerror(errno));
Davide Pesavento44deacc2014-02-19 10:48:07 +0100459#endif
460
Davide Pesavento8eb99572014-12-19 19:12:15 +0100461 return ethernet::MAX_DATA_LEN;
Davide Pesavento44deacc2014-02-19 10:48:07 +0100462}
463
464} // namespace nfd