NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
multicast-ethernet-transport.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2019, 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.
10  *
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/>.
24  */
25 
27 #include "common/global.hpp"
28 
29 #include <cerrno> // for errno
30 #include <cstring> // for memcpy(), strerror(), strncpy()
31 #include <net/if.h> // for struct ifreq
32 #include <stdio.h> // for snprintf()
33 #include <sys/ioctl.h> // for ioctl()
34 
35 #if defined(__linux__)
36 #include <netpacket/packet.h> // for struct packet_mreq
37 #include <sys/socket.h> // for setsockopt()
38 #endif
39 
40 #ifdef SIOCADDMULTI
41 #if defined(__APPLE__) || defined(__FreeBSD__)
42 #include <net/if_dl.h> // for struct sockaddr_dl
43 #endif
44 #endif
45 
46 namespace nfd {
47 namespace face {
48 
50 
52  const ethernet::Address& mcastAddress,
53  ndn::nfd::LinkType linkType)
54  : EthernetTransport(localEndpoint, mcastAddress)
55 #if defined(__linux__)
56  , m_interfaceIndex(localEndpoint.getIndex())
57 #endif
58 {
63  this->setLinkType(linkType);
64  this->setMtu(localEndpoint.getMtu());
65 
66  NFD_LOG_FACE_DEBUG("Creating transport");
67 
68  char filter[110];
69  // note #1: we cannot use std::snprintf because it's not available
70  // on some platforms (see #2299)
71  // note #2: "not vlan" must appear last in the filter expression, or the
72  // rest of the filter won't work as intended (see pcap-filter(7))
73  snprintf(filter, sizeof(filter),
74  "(ether proto 0x%x) && (ether dst %s) && (not ether src %s) && (not vlan)",
76  m_destAddress.toString().data(),
77  m_srcAddress.toString().data());
78  m_pcap.setPacketFilter(filter);
79 
80  BOOST_ASSERT(m_destAddress.isMulticast());
82  joinMulticastGroup();
83 }
84 
85 void
86 MulticastEthernetTransport::joinMulticastGroup()
87 {
88 #if defined(__linux__)
89  packet_mreq mr{};
90  mr.mr_ifindex = m_interfaceIndex;
91  mr.mr_type = PACKET_MR_MULTICAST;
92  mr.mr_alen = m_destAddress.size();
93  std::memcpy(mr.mr_address, m_destAddress.data(), m_destAddress.size());
94 
95  if (::setsockopt(m_socket.native_handle(), SOL_PACKET,
96  PACKET_ADD_MEMBERSHIP, &mr, sizeof(mr)) == 0)
97  return; // success
98 
99  NFD_LOG_FACE_WARN("setsockopt(PACKET_ADD_MEMBERSHIP) failed: " << std::strerror(errno));
100 #endif
101 
102 #if defined(SIOCADDMULTI)
103  ifreq ifr{};
104  std::strncpy(ifr.ifr_name, m_interfaceName.data(), sizeof(ifr.ifr_name) - 1);
105 
106 #if defined(__APPLE__) || defined(__FreeBSD__)
107  // see bug #2327
108  using boost::asio::ip::udp;
109  udp::socket sock(getGlobalIoService(), udp::v4());
110  int fd = sock.native_handle();
111 
112  // Differences between Linux and the BSDs (including macOS):
113  // o BSD does not have ifr_hwaddr; use ifr_addr instead.
114  // o While macOS seems to accept both AF_LINK and AF_UNSPEC as the address
115  // family, FreeBSD explicitly requires AF_LINK, so we have to use AF_LINK
116  // and sockaddr_dl instead of the generic sockaddr structure.
117  // o BSD's sockaddr (and sockaddr_dl in particular) contains an additional
118  // field, sa_len (sdl_len), which must be set to the total length of the
119  // structure, including the length field itself.
120  // o We do not specify the interface name, thus sdl_nlen is left at 0 and
121  // LLADDR is effectively the same as sdl_data.
122 
123  sockaddr_dl* sdl = reinterpret_cast<sockaddr_dl*>(&ifr.ifr_addr);
124  sdl->sdl_len = sizeof(ifr.ifr_addr);
125  sdl->sdl_family = AF_LINK;
126  sdl->sdl_alen = m_destAddress.size();
127  std::memcpy(LLADDR(sdl), m_destAddress.data(), m_destAddress.size());
128 
129  static_assert(sizeof(ifr.ifr_addr) >= offsetof(sockaddr_dl, sdl_data) + ethernet::ADDR_LEN,
130  "ifr_addr in struct ifreq is too small on this platform");
131 #else
132  int fd = m_socket.native_handle();
133 
134  ifr.ifr_hwaddr.sa_family = AF_UNSPEC;
135  std::memcpy(ifr.ifr_hwaddr.sa_data, m_destAddress.data(), m_destAddress.size());
136 
137  static_assert(sizeof(ifr.ifr_hwaddr.sa_data) >= ethernet::ADDR_LEN,
138  "ifr_hwaddr in struct ifreq is too small on this platform");
139 #endif
140 
141  if (::ioctl(fd, SIOCADDMULTI, &ifr) == 0)
142  return; // success
143 
144  NFD_LOG_FACE_WARN("ioctl(SIOCADDMULTI) failed: " << std::strerror(errno));
145 #endif
146 
147  NDN_THROW(Error("Failed to join multicast group"));
148 }
149 
150 } // namespace face
151 } // namespace nfd
ndn::ethernet::Address::isBroadcast
bool isBroadcast() const
True if this is a broadcast address (ff:ff:ff:ff:ff:ff)
Definition: ethernet.cpp:60
global.hpp
nfd::face::EthernetTransport::m_socket
boost::asio::posix::stream_descriptor m_socket
Definition: ethernet-transport.hpp:102
ndn::nfd::FACE_SCOPE_NON_LOCAL
@ FACE_SCOPE_NON_LOCAL
face is non-local
Definition: nfd-constants.hpp:36
nfd::face::Transport::setMtu
void setMtu(ssize_t mtu)
Definition: transport.hpp:448
nfd::face::Transport::setRemoteUri
void setRemoteUri(const FaceUri &uri)
Definition: transport.hpp:406
nfd::face::EthernetTransport::m_destAddress
ethernet::Address m_destAddress
Definition: ethernet-transport.hpp:105
ndn::net::NetworkInterface
Represents one network interface attached to the host.
Definition: network-interface.hpp:70
ndn::FaceUri
represents the underlying protocol and address used by a Face
Definition: face-uri.hpp:45
ndn::nfd::FACE_PERSISTENCY_PERMANENT
@ FACE_PERSISTENCY_PERMANENT
face is permanent
Definition: nfd-constants.hpp:49
ndn::ethernet::Address
represents an Ethernet hardware address
Definition: ethernet.hpp:53
multicast-ethernet-transport.hpp
nfd::face::Transport::setScope
void setScope(ndn::nfd::FaceScope scope)
Definition: transport.hpp:418
nfd::face::EthernetTransport::m_pcap
PcapHelper m_pcap
Definition: ethernet-transport.hpp:103
ndn::ethernet::ETHERTYPE_NDN
const uint16_t ETHERTYPE_NDN
Definition: ethernet.hpp:39
nfd::face::EthernetTransport::m_srcAddress
ethernet::Address m_srcAddress
Definition: ethernet-transport.hpp:104
NFD_LOG_FACE_DEBUG
#define NFD_LOG_FACE_DEBUG(msg)
Log a message at DEBUG level.
Definition: face-common.hpp:136
ndn::nfd::LinkType
LinkType
Definition: nfd-constants.hpp:57
nfd::getGlobalIoService
detail::SimulatorIo & getGlobalIoService()
Returns the global io_service instance for the calling thread.
Definition: global.cpp:49
nfd
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:40
NDN_THROW
#define NDN_THROW(e)
Definition: exception.hpp:61
ndn::FaceUri::fromDev
static FaceUri fromDev(const std::string &ifname)
create dev FaceUri from network device name
Definition: face-uri.cpp:169
nfd::face::MulticastEthernetTransport
A multicast Transport that uses raw Ethernet II frames.
Definition: multicast-ethernet-transport.hpp:38
nfd::face::Transport::setLinkType
void setLinkType(ndn::nfd::LinkType linkType)
Definition: transport.hpp:436
ndn::net::NetworkInterface::getMtu
uint32_t getMtu() const
Returns the MTU (maximum transmission unit) of the interface.
Definition: network-interface.hpp:132
ndn::ethernet::Address::toString
std::string toString(char sep=':') const
Converts the address to a human-readable string.
Definition: ethernet.cpp:78
nfd::face::EthernetTransport
Base class for Ethernet-based Transports.
Definition: ethernet-transport.hpp:42
ndn::ethernet::ADDR_LEN
const size_t ADDR_LEN
Octets in one Ethernet address.
Definition: ethernet.hpp:41
nfd::face::EthernetTransport::m_interfaceName
std::string m_interfaceName
Definition: ethernet-transport.hpp:106
nfd::face::Transport::setPersistency
void setPersistency(ndn::nfd::FacePersistency newPersistency)
changes face persistency setting
Definition: transport.cpp:150
nfd::face::MulticastEthernetTransport::MulticastEthernetTransport
MulticastEthernetTransport(const ndn::net::NetworkInterface &localEndpoint, const ethernet::Address &mcastAddress, ndn::nfd::LinkType linkType)
Creates an Ethernet-based transport for multicast communication.
Definition: multicast-ethernet-transport.cpp:51
nfd::face::PcapHelper::setPacketFilter
void setPacketFilter(const char *filter) const
Install a BPF filter on the receiving socket.
Definition: pcap-helper.cpp:115
ndn::ethernet::Address::isMulticast
bool isMulticast() const
True if this is a multicast address.
Definition: ethernet.cpp:66
NFD_LOG_FACE_WARN
#define NFD_LOG_FACE_WARN(msg)
Log a message at WARN level.
Definition: face-common.hpp:142
NFD_LOG_INIT
#define NFD_LOG_INIT(name)
Definition: logger.hpp:31
nfd::face::Transport::setLocalUri
void setLocalUri(const FaceUri &uri)
Definition: transport.hpp:394