blob: db12128847054a6e9a0cba1bf6d460fc271cea06 [file] [log] [blame]
Yukai Tu0a49d342015-09-13 12:54:22 +08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento8215a3a2017-12-25 19:14:33 -05002/*
Eric Newberryb49313d2017-12-24 20:22:27 -07003 * Copyright (c) 2014-2018, Regents of the University of California,
Yukai Tu0a49d342015-09-13 12:54:22 +08004 * 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
26#include "multicast-udp-transport.hpp"
Eric Newberryb49313d2017-12-24 20:22:27 -070027#include "socket-utils.hpp"
Junxiao Shi13546112015-10-14 19:33:07 -070028#include "udp-protocol.hpp"
Yukai Tu0a49d342015-09-13 12:54:22 +080029
Davide Pesaventod7b44c82018-02-15 17:39:31 -050030#include "core/privilege-helper.hpp"
31
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -050032#include <boost/functional/hash.hpp>
33
Davide Pesavento8215a3a2017-12-25 19:14:33 -050034#ifdef __linux__
35#include <cerrno> // for errno
36#include <cstring> // for std::strerror()
37#include <sys/socket.h> // for setsockopt()
38#endif // __linux__
39
Yukai Tu0a49d342015-09-13 12:54:22 +080040namespace nfd {
41namespace face {
42
Davide Pesaventoa3148082018-04-12 18:21:54 -040043NFD_LOG_MEMBER_INIT_SPECIALIZED((DatagramTransport<boost::asio::ip::udp, Multicast>), MulticastUdpTransport);
Yukai Tu0a49d342015-09-13 12:54:22 +080044
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -050045MulticastUdpTransport::MulticastUdpTransport(const protocol::endpoint& multicastGroup,
Yukai Tu0a49d342015-09-13 12:54:22 +080046 protocol::socket&& recvSocket,
Teng Liangfe4fce32017-03-29 04:49:38 +000047 protocol::socket&& sendSocket,
48 ndn::nfd::LinkType linkType)
Yukai Tu0a49d342015-09-13 12:54:22 +080049 : DatagramTransport(std::move(recvSocket))
50 , m_multicastGroup(multicastGroup)
51 , m_sendSocket(std::move(sendSocket))
52{
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -050053 this->setLocalUri(FaceUri(m_sendSocket.local_endpoint()));
Yukai Tu0a49d342015-09-13 12:54:22 +080054 this->setRemoteUri(FaceUri(multicastGroup));
Junxiao Shi13546112015-10-14 19:33:07 -070055 this->setScope(ndn::nfd::FACE_SCOPE_NON_LOCAL);
Yukai Tu0a49d342015-09-13 12:54:22 +080056 this->setPersistency(ndn::nfd::FACE_PERSISTENCY_PERMANENT);
Teng Liangfe4fce32017-03-29 04:49:38 +000057 this->setLinkType(linkType);
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -050058 this->setMtu(udp::computeMtu(m_sendSocket.local_endpoint()));
Yukai Tu0a49d342015-09-13 12:54:22 +080059
Eric Newberryb49313d2017-12-24 20:22:27 -070060 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());
65 this->setSendQueueCapacity(QUEUE_ERROR);
66 }
67 else {
68 this->setSendQueueCapacity(sendBufferSizeOption.value());
69 }
70
Yukai Tu0a49d342015-09-13 12:54:22 +080071 NFD_LOG_FACE_INFO("Creating transport");
72}
73
Eric Newberryb49313d2017-12-24 20:22:27 -070074ssize_t
75MulticastUdpTransport::getSendQueueLength()
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
Yukai Tu0a49d342015-09-13 12:54:22 +080084void
Yukai Tu0a49d342015-09-13 12:54:22 +080085MulticastUdpTransport::doSend(Transport::Packet&& packet)
86{
87 NFD_LOG_FACE_TRACE(__func__);
88
89 m_sendSocket.async_send_to(boost::asio::buffer(packet.packet), m_multicastGroup,
Davide Pesaventoe4b22382018-06-10 14:37:24 -040090 // packet.packet is copied into the lambda to retain the underlying Buffer
91 [this, p = packet.packet] (auto&&... args) {
92 this->handleSend(std::forward<decltype(args)>(args)...);
93 });
Yukai Tu0a49d342015-09-13 12:54:22 +080094}
95
96void
97MulticastUdpTransport::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
109 DatagramTransport::doClose();
110}
111
Davide Pesaventod7b44c82018-02-15 17:39:31 -0500112static void
113bindToDevice(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.
Ju Panbc59dd02018-11-18 14:03:47 -0700121 // 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.
Davide Pesaventod7b44c82018-02-15 17:39:31 -0500123
Ju Panbc59dd02018-11-18 14:03:47 -0700124#if defined(__linux__) && !defined(__ANDROID__)
Davide Pesaventod7b44c82018-02-15 17:39:31 -0500125 PrivilegeHelper::runElevated([=] {
126 if (::setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, ifname.data(), ifname.size() + 1) < 0) {
127 BOOST_THROW_EXCEPTION(MulticastUdpTransport::Error("Cannot bind multicast rx socket to " +
128 ifname + ": " + std::strerror(errno)));
129 }
130 });
Ju Panbc59dd02018-11-18 14:03:47 -0700131#endif
Davide Pesaventod7b44c82018-02-15 17:39:31 -0500132}
133
Davide Pesavento8215a3a2017-12-25 19:14:33 -0500134void
135MulticastUdpTransport::openRxSocket(protocol::socket& sock,
136 const protocol::endpoint& multicastGroup,
137 const boost::asio::ip::address& localAddress,
138 const shared_ptr<const ndn::net::NetworkInterface>& netif)
139{
140 BOOST_ASSERT(!sock.is_open());
141
142 sock.open(multicastGroup.protocol());
143 sock.set_option(protocol::socket::reuse_address(true));
Davide Pesavento8215a3a2017-12-25 19:14:33 -0500144
145 if (multicastGroup.address().is_v4()) {
146 BOOST_ASSERT(localAddress.is_v4());
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500147 sock.bind(multicastGroup);
Davide Pesavento8215a3a2017-12-25 19:14:33 -0500148 sock.set_option(boost::asio::ip::multicast::join_group(multicastGroup.address().to_v4(),
149 localAddress.to_v4()));
150 }
151 else {
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500152 BOOST_ASSERT(localAddress.is_v6());
153 sock.set_option(boost::asio::ip::v6_only(true));
154#ifdef WITH_TESTS
155 // To simplify unit tests, we bind to the "any" IPv6 address if the supplied multicast
156 // address lacks a scope id. Calling bind() without a scope id would otherwise fail.
157 if (multicastGroup.address().to_v6().scope_id() == 0)
158 sock.bind(protocol::endpoint(boost::asio::ip::address_v6::any(), multicastGroup.port()));
159 else
160#endif
161 sock.bind(multicastGroup);
162 sock.set_option(boost::asio::ip::multicast::join_group(multicastGroup.address().to_v6()));
Davide Pesavento8215a3a2017-12-25 19:14:33 -0500163 }
164
Davide Pesaventod7b44c82018-02-15 17:39:31 -0500165 if (netif)
166 bindToDevice(sock.native_handle(), netif->getName());
Davide Pesavento8215a3a2017-12-25 19:14:33 -0500167}
168
169void
170MulticastUdpTransport::openTxSocket(protocol::socket& sock,
171 const protocol::endpoint& localEndpoint,
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500172 const shared_ptr<const ndn::net::NetworkInterface>& netif,
Davide Pesavento8215a3a2017-12-25 19:14:33 -0500173 bool enableLoopback)
174{
175 BOOST_ASSERT(!sock.is_open());
176
177 sock.open(localEndpoint.protocol());
178 sock.set_option(protocol::socket::reuse_address(true));
179 sock.set_option(boost::asio::ip::multicast::enable_loopback(enableLoopback));
Davide Pesavento8215a3a2017-12-25 19:14:33 -0500180
181 if (localEndpoint.address().is_v4()) {
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500182 sock.bind(localEndpoint);
Davide Pesavento8215a3a2017-12-25 19:14:33 -0500183 if (!localEndpoint.address().is_unspecified())
184 sock.set_option(boost::asio::ip::multicast::outbound_interface(localEndpoint.address().to_v4()));
185 }
186 else {
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500187 sock.set_option(boost::asio::ip::v6_only(true));
188 sock.bind(localEndpoint);
189 if (netif)
190 sock.set_option(boost::asio::ip::multicast::outbound_interface(netif->getIndex()));
Davide Pesavento8215a3a2017-12-25 19:14:33 -0500191 }
192}
193
Davide Pesaventoeee7b972015-11-18 21:34:46 +0100194template<>
195Transport::EndpointId
196DatagramTransport<boost::asio::ip::udp, Multicast>::makeEndpointId(const protocol::endpoint& ep)
197{
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500198 if (ep.address().is_v4()) {
199 return (static_cast<uint64_t>(ep.port()) << 32) |
200 static_cast<uint64_t>(ep.address().to_v4().to_ulong());
201 }
202 else {
203 size_t seed = 0;
204 const auto& addrBytes = ep.address().to_v6().to_bytes();
205 boost::hash_range(seed, addrBytes.begin(), addrBytes.end());
206 boost::hash_combine(seed, ep.port());
207 return seed;
208 }
Davide Pesaventoeee7b972015-11-18 21:34:46 +0100209}
210
Yukai Tu0a49d342015-09-13 12:54:22 +0800211} // namespace face
212} // namespace nfd