NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
multicast-udp-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 "socket-utils.hpp"
28 #include "udp-protocol.hpp"
29 
31 
32 #include <boost/functional/hash.hpp>
33 
34 #ifdef __linux__
35 #include <cerrno> // for errno
36 #include <cstring> // for std::strerror()
37 #include <sys/socket.h> // for setsockopt()
38 #endif // __linux__
39 
40 namespace nfd {
41 namespace face {
42 
44 
45 MulticastUdpTransport::MulticastUdpTransport(const protocol::endpoint& multicastGroup,
46  protocol::socket&& recvSocket,
47  protocol::socket&& sendSocket,
48  ndn::nfd::LinkType linkType)
49  : DatagramTransport(std::move(recvSocket))
50  , m_multicastGroup(multicastGroup)
51  , m_sendSocket(std::move(sendSocket))
52 {
53  this->setLocalUri(FaceUri(m_sendSocket.local_endpoint()));
54  this->setRemoteUri(FaceUri(multicastGroup));
57  this->setLinkType(linkType);
58  this->setMtu(udp::computeMtu(m_sendSocket.local_endpoint()));
59 
60  protocol::socket::send_buffer_size sendBufferSizeOption;
61  boost::system::error_code error;
62  m_sendSocket.get_option(sendBufferSizeOption);
63  if (error) {
64  NFD_LOG_FACE_WARN("Failed to obtain send queue capacity from socket: " << error.message());
66  }
67  else {
68  this->setSendQueueCapacity(sendBufferSizeOption.value());
69  }
70 
71  NFD_LOG_FACE_DEBUG("Creating transport");
72 }
73 
74 ssize_t
76 {
77  ssize_t queueLength = getTxQueueLength(m_sendSocket.native_handle());
78  if (queueLength == QUEUE_ERROR) {
79  NFD_LOG_FACE_WARN("Failed to obtain send queue length from socket: " << std::strerror(errno));
80  }
81  return queueLength;
82 }
83 
84 void
85 MulticastUdpTransport::doSend(const Block& packet, const EndpointId&)
86 {
87  NFD_LOG_FACE_TRACE(__func__);
88 
89  m_sendSocket.async_send_to(boost::asio::buffer(packet), m_multicastGroup,
90  // 'packet' is copied into the lambda to retain the underlying Buffer
91  [this, packet] (auto&&... args) {
92  this->handleSend(std::forward<decltype(args)>(args)...);
93  });
94 }
95 
96 void
97 MulticastUdpTransport::doClose()
98 {
99  if (m_sendSocket.is_open()) {
100  NFD_LOG_FACE_TRACE("Closing sending socket");
101 
102  // Cancel all outstanding operations and close the socket.
103  // Use the non-throwing variants and ignore errors, if any.
104  boost::system::error_code error;
105  m_sendSocket.cancel(error);
106  m_sendSocket.close(error);
107  }
108 
110 }
111 
112 static void
113 bindToDevice(int fd, const std::string& ifname)
114 {
115  // On Linux, if there is more than one MulticastUdpTransport for the same multicast
116  // group but they are on different network interfaces, each socket needs to be bound
117  // to the corresponding interface using SO_BINDTODEVICE, otherwise the transport will
118  // receive all packets sent to the other interfaces as well.
119  // This is needed only on Linux. On macOS, the boost::asio::ip::multicast::join_group
120  // option is sufficient to obtain the desired behavior.
121  // We dont't set SO_BINDTODEVICE on Android because this operation requires root privilege
122  // which is not allowed on Android, it will cause "Operation not permitted" error.
123 
124 #if defined(__linux__) && !defined(__ANDROID__)
126  if (::setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, ifname.data(), ifname.size() + 1) < 0) {
127  NDN_THROW_ERRNO(MulticastUdpTransport::Error("Cannot bind multicast rx socket to " + ifname));
128  }
129  });
130 #endif
131 }
132 
133 void
134 MulticastUdpTransport::openRxSocket(protocol::socket& sock,
135  const protocol::endpoint& multicastGroup,
136  const boost::asio::ip::address& localAddress,
137  const shared_ptr<const ndn::net::NetworkInterface>& netif)
138 {
139  BOOST_ASSERT(!sock.is_open());
140 
141  sock.open(multicastGroup.protocol());
142  sock.set_option(protocol::socket::reuse_address(true));
143 
144  if (multicastGroup.address().is_v4()) {
145  BOOST_ASSERT(localAddress.is_v4());
146  sock.bind(multicastGroup);
147  sock.set_option(boost::asio::ip::multicast::join_group(multicastGroup.address().to_v4(),
148  localAddress.to_v4()));
149  }
150  else {
151  BOOST_ASSERT(localAddress.is_v6());
152  sock.set_option(boost::asio::ip::v6_only(true));
153 #ifdef WITH_TESTS
154  // To simplify unit tests, we bind to the "any" IPv6 address if the supplied multicast
155  // address lacks a scope id. Calling bind() without a scope id would otherwise fail.
156  if (multicastGroup.address().to_v6().scope_id() == 0)
157  sock.bind(protocol::endpoint(boost::asio::ip::address_v6::any(), multicastGroup.port()));
158  else
159 #endif
160  sock.bind(multicastGroup);
161  sock.set_option(boost::asio::ip::multicast::join_group(multicastGroup.address().to_v6()));
162  }
163 
164  if (netif)
165  bindToDevice(sock.native_handle(), netif->getName());
166 }
167 
168 void
169 MulticastUdpTransport::openTxSocket(protocol::socket& sock,
170  const protocol::endpoint& localEndpoint,
171  const shared_ptr<const ndn::net::NetworkInterface>& netif,
172  bool enableLoopback)
173 {
174  BOOST_ASSERT(!sock.is_open());
175 
176  sock.open(localEndpoint.protocol());
177  sock.set_option(protocol::socket::reuse_address(true));
178  sock.set_option(boost::asio::ip::multicast::enable_loopback(enableLoopback));
179 
180  if (localEndpoint.address().is_v4()) {
181  sock.bind(localEndpoint);
182  if (!localEndpoint.address().is_unspecified())
183  sock.set_option(boost::asio::ip::multicast::outbound_interface(localEndpoint.address().to_v4()));
184  }
185  else {
186  sock.set_option(boost::asio::ip::v6_only(true));
187  sock.bind(localEndpoint);
188  if (netif)
189  sock.set_option(boost::asio::ip::multicast::outbound_interface(netif->getIndex()));
190  }
191 }
192 
193 template<>
196 {
197  if (ep.address().is_v4()) {
198  return (static_cast<uint64_t>(ep.port()) << 32) |
199  static_cast<uint64_t>(ep.address().to_v4().to_ulong());
200  }
201  else {
202  size_t seed = 0;
203  const auto& addrBytes = ep.address().to_v6().to_bytes();
204  boost::hash_range(seed, addrBytes.begin(), addrBytes.end());
205  boost::hash_combine(seed, ep.port());
206  return seed;
207  }
208 }
209 
210 } // namespace face
211 } // namespace nfd
nfd::face::Transport::setSendQueueCapacity
void setSendQueueCapacity(ssize_t sendQueueCapacity)
Definition: transport.hpp:461
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
nonstd::optional_lite::std11::move
T & move(T &t)
Definition: optional.hpp:421
nfd::face::Transport::setRemoteUri
void setRemoteUri(const FaceUri &uri)
Definition: transport.hpp:406
nfd::udp::computeMtu
ssize_t computeMtu(const Endpoint &localEndpoint)
computes maximum payload size in a UDP packet
Definition: udp-protocol.cpp:32
udp-protocol.hpp
ndn::net::NetworkInterface::getIndex
int getIndex() const
Returns an opaque ID that uniquely identifies the interface on the system.
Definition: network-interface.hpp:92
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
nfd::face::Transport::setScope
void setScope(ndn::nfd::FaceScope scope)
Definition: transport.hpp:418
nfd::face::DatagramTransport::makeEndpointId
static EndpointId makeEndpointId(const typename protocol::endpoint &ep)
Definition: datagram-transport.hpp:262
nfd::face::NFD_LOG_MEMBER_INIT_SPECIALIZED
NFD_LOG_MEMBER_INIT_SPECIALIZED((DatagramTransport< boost::asio::ip::udp, Multicast >), MulticastUdpTransport)
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
socket-utils.hpp
multicast-udp-transport.hpp
nfd::face::DatagramTransport< boost::asio::ip::udp, Multicast >::handleSend
void handleSend(const boost::system::error_code &error, size_t nBytesSent)
Definition: datagram-transport.hpp:214
nfd::face::DatagramTransport::doClose
void doClose() override
performs Transport specific operations to close the transport
Definition: datagram-transport.hpp:139
nfd
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:40
nfd::face::MulticastUdpTransport::openRxSocket
static void openRxSocket(protocol::socket &sock, const protocol::endpoint &multicastGroup, const boost::asio::ip::address &localAddress, const shared_ptr< const ndn::net::NetworkInterface > &netif=nullptr)
Definition: multicast-udp-transport.cpp:134
nfd::face::MulticastUdpTransport::openTxSocket
static void openTxSocket(protocol::socket &sock, const protocol::endpoint &localEndpoint, const shared_ptr< const ndn::net::NetworkInterface > &netif=nullptr, bool enableLoopback=false)
Definition: multicast-udp-transport.cpp:169
NFD_LOG_FACE_TRACE
#define NFD_LOG_FACE_TRACE(msg)
Log a message at TRACE level.
Definition: face-common.hpp:133
nfd::face::QUEUE_ERROR
const ssize_t QUEUE_ERROR
indicates that the transport was unable to retrieve the queue capacity/length
Definition: transport.hpp:103
nfd::face::DatagramTransport< boost::asio::ip::udp, Multicast >
nfd::face::MulticastUdpTransport
A Transport that communicates on a UDP multicast group.
Definition: multicast-udp-transport.hpp:50
nfd::face::MulticastUdpTransport::MulticastUdpTransport
MulticastUdpTransport(const protocol::endpoint &multicastGroup, protocol::socket &&recvSocket, protocol::socket &&sendSocket, ndn::nfd::LinkType linkType)
Creates a UDP-based transport for multicast communication.
Definition: multicast-udp-transport.cpp:45
nfd::face::Transport::setLinkType
void setLinkType(ndn::nfd::LinkType linkType)
Definition: transport.hpp:436
nfd::PrivilegeHelper::runElevated
static void runElevated(F &&f)
Definition: privilege-helper.hpp:70
ndn::Block
Represents a TLV element of NDN packet format.
Definition: block.hpp:43
nfd::face::Transport::setPersistency
void setPersistency(ndn::nfd::FacePersistency newPersistency)
changes face persistency setting
Definition: transport.cpp:150
privilege-helper.hpp
nfd::face::MulticastUdpTransport::getSendQueueLength
ssize_t getSendQueueLength() final
Definition: multicast-udp-transport.cpp:75
ndn::net::NetworkInterface::getName
std::string getName() const
Returns the name of the interface, unique on the system.
Definition: network-interface.hpp:100
nfd::face::EndpointId
uint64_t EndpointId
Identifies a remote endpoint on the link.
Definition: face-common.hpp:65
NFD_LOG_FACE_WARN
#define NFD_LOG_FACE_WARN(msg)
Log a message at WARN level.
Definition: face-common.hpp:142
nfd::face::bindToDevice
static void bindToDevice(int fd, const std::string &ifname)
Definition: multicast-udp-transport.cpp:113
nfd::face::getTxQueueLength
ssize_t getTxQueueLength(int fd)
obtain send queue length from a specified system socket
Definition: socket-utils.cpp:40
nfd::face::Transport::setLocalUri
void setLocalUri(const FaceUri &uri)
Definition: transport.hpp:394