blob: bb373fd0dd4e6a1daf1f95a70c6b6dc338363492 [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/*
Davide Pesaventoe422f9e2022-06-03 01:30:23 -04003 * Copyright (c) 2014-2022, 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 Pesavento2cae8ca2019-04-18 20:48:05 -040030#include "common/privilege-helper.hpp"
Davide Pesaventod7b44c82018-02-15 17:39:31 -050031
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
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040040namespace nfd::face {
Yukai Tu0a49d342015-09-13 12:54:22 +080041
Davide Pesaventoa3148082018-04-12 18:21:54 -040042NFD_LOG_MEMBER_INIT_SPECIALIZED((DatagramTransport<boost::asio::ip::udp, Multicast>), MulticastUdpTransport);
Yukai Tu0a49d342015-09-13 12:54:22 +080043
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -050044MulticastUdpTransport::MulticastUdpTransport(const protocol::endpoint& multicastGroup,
Yukai Tu0a49d342015-09-13 12:54:22 +080045 protocol::socket&& recvSocket,
Teng Liangfe4fce32017-03-29 04:49:38 +000046 protocol::socket&& sendSocket,
47 ndn::nfd::LinkType linkType)
Yukai Tu0a49d342015-09-13 12:54:22 +080048 : DatagramTransport(std::move(recvSocket))
49 , m_multicastGroup(multicastGroup)
50 , m_sendSocket(std::move(sendSocket))
51{
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -050052 this->setLocalUri(FaceUri(m_sendSocket.local_endpoint()));
Yukai Tu0a49d342015-09-13 12:54:22 +080053 this->setRemoteUri(FaceUri(multicastGroup));
Junxiao Shi13546112015-10-14 19:33:07 -070054 this->setScope(ndn::nfd::FACE_SCOPE_NON_LOCAL);
Yukai Tu0a49d342015-09-13 12:54:22 +080055 this->setPersistency(ndn::nfd::FACE_PERSISTENCY_PERMANENT);
Teng Liangfe4fce32017-03-29 04:49:38 +000056 this->setLinkType(linkType);
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -050057 this->setMtu(udp::computeMtu(m_sendSocket.local_endpoint()));
Yukai Tu0a49d342015-09-13 12:54:22 +080058
Eric Newberryb49313d2017-12-24 20:22:27 -070059 protocol::socket::send_buffer_size sendBufferSizeOption;
60 boost::system::error_code error;
61 m_sendSocket.get_option(sendBufferSizeOption);
62 if (error) {
63 NFD_LOG_FACE_WARN("Failed to obtain send queue capacity from socket: " << error.message());
64 this->setSendQueueCapacity(QUEUE_ERROR);
65 }
66 else {
67 this->setSendQueueCapacity(sendBufferSizeOption.value());
68 }
69
Davide Pesaventoa681a242019-03-29 23:48:27 -040070 NFD_LOG_FACE_DEBUG("Creating transport");
Yukai Tu0a49d342015-09-13 12:54:22 +080071}
72
Eric Newberryb49313d2017-12-24 20:22:27 -070073ssize_t
74MulticastUdpTransport::getSendQueueLength()
75{
76 ssize_t queueLength = getTxQueueLength(m_sendSocket.native_handle());
77 if (queueLength == QUEUE_ERROR) {
78 NFD_LOG_FACE_WARN("Failed to obtain send queue length from socket: " << std::strerror(errno));
79 }
80 return queueLength;
81}
82
Yukai Tu0a49d342015-09-13 12:54:22 +080083void
Teng Liang13d582a2020-07-21 20:23:11 -070084MulticastUdpTransport::doSend(const Block& packet)
Yukai Tu0a49d342015-09-13 12:54:22 +080085{
86 NFD_LOG_FACE_TRACE(__func__);
87
Davide Pesaventob3a23ca2019-05-04 20:40:21 -040088 m_sendSocket.async_send_to(boost::asio::buffer(packet), m_multicastGroup,
89 // 'packet' is copied into the lambda to retain the underlying Buffer
90 [this, packet] (auto&&... args) {
Davide Pesaventoe4b22382018-06-10 14:37:24 -040091 this->handleSend(std::forward<decltype(args)>(args)...);
92 });
Yukai Tu0a49d342015-09-13 12:54:22 +080093}
94
95void
96MulticastUdpTransport::doClose()
97{
98 if (m_sendSocket.is_open()) {
99 NFD_LOG_FACE_TRACE("Closing sending socket");
100
101 // Cancel all outstanding operations and close the socket.
102 // Use the non-throwing variants and ignore errors, if any.
103 boost::system::error_code error;
104 m_sendSocket.cancel(error);
105 m_sendSocket.close(error);
106 }
107
108 DatagramTransport::doClose();
109}
110
Davide Pesaventod7b44c82018-02-15 17:39:31 -0500111static void
112bindToDevice(int fd, const std::string& ifname)
113{
114 // On Linux, if there is more than one MulticastUdpTransport for the same multicast
115 // group but they are on different network interfaces, each socket needs to be bound
116 // to the corresponding interface using SO_BINDTODEVICE, otherwise the transport will
117 // receive all packets sent to the other interfaces as well.
118 // This is needed only on Linux. On macOS, the boost::asio::ip::multicast::join_group
119 // option is sufficient to obtain the desired behavior.
Ju Panbc59dd02018-11-18 14:03:47 -0700120 // We dont't set SO_BINDTODEVICE on Android because this operation requires root privilege
121 // which is not allowed on Android, it will cause "Operation not permitted" error.
Davide Pesaventod7b44c82018-02-15 17:39:31 -0500122
Ju Panbc59dd02018-11-18 14:03:47 -0700123#if defined(__linux__) && !defined(__ANDROID__)
Davide Pesaventod7b44c82018-02-15 17:39:31 -0500124 PrivilegeHelper::runElevated([=] {
125 if (::setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, ifname.data(), ifname.size() + 1) < 0) {
Davide Pesavento19779d82019-02-14 13:40:04 -0500126 NDN_THROW_ERRNO(MulticastUdpTransport::Error("Cannot bind multicast rx socket to " + ifname));
Davide Pesaventod7b44c82018-02-15 17:39:31 -0500127 }
128 });
Ju Panbc59dd02018-11-18 14:03:47 -0700129#endif
Davide Pesaventod7b44c82018-02-15 17:39:31 -0500130}
131
Davide Pesavento8215a3a2017-12-25 19:14:33 -0500132void
133MulticastUdpTransport::openRxSocket(protocol::socket& sock,
134 const protocol::endpoint& multicastGroup,
135 const boost::asio::ip::address& localAddress,
136 const shared_ptr<const ndn::net::NetworkInterface>& netif)
137{
138 BOOST_ASSERT(!sock.is_open());
139
140 sock.open(multicastGroup.protocol());
141 sock.set_option(protocol::socket::reuse_address(true));
Davide Pesavento8215a3a2017-12-25 19:14:33 -0500142
143 if (multicastGroup.address().is_v4()) {
144 BOOST_ASSERT(localAddress.is_v4());
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500145 sock.bind(multicastGroup);
Davide Pesavento8215a3a2017-12-25 19:14:33 -0500146 sock.set_option(boost::asio::ip::multicast::join_group(multicastGroup.address().to_v4(),
147 localAddress.to_v4()));
148 }
149 else {
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500150 BOOST_ASSERT(localAddress.is_v6());
151 sock.set_option(boost::asio::ip::v6_only(true));
Davide Pesavento264af772021-02-09 21:48:24 -0500152#ifdef NFD_WITH_TESTS
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500153 // To simplify unit tests, we bind to the "any" IPv6 address if the supplied multicast
154 // address lacks a scope id. Calling bind() without a scope id would otherwise fail.
155 if (multicastGroup.address().to_v6().scope_id() == 0)
156 sock.bind(protocol::endpoint(boost::asio::ip::address_v6::any(), multicastGroup.port()));
157 else
158#endif
159 sock.bind(multicastGroup);
160 sock.set_option(boost::asio::ip::multicast::join_group(multicastGroup.address().to_v6()));
Davide Pesavento8215a3a2017-12-25 19:14:33 -0500161 }
162
Davide Pesaventod7b44c82018-02-15 17:39:31 -0500163 if (netif)
164 bindToDevice(sock.native_handle(), netif->getName());
Davide Pesavento8215a3a2017-12-25 19:14:33 -0500165}
166
167void
168MulticastUdpTransport::openTxSocket(protocol::socket& sock,
169 const protocol::endpoint& localEndpoint,
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500170 const shared_ptr<const ndn::net::NetworkInterface>& netif,
Davide Pesavento8215a3a2017-12-25 19:14:33 -0500171 bool enableLoopback)
172{
173 BOOST_ASSERT(!sock.is_open());
174
175 sock.open(localEndpoint.protocol());
176 sock.set_option(protocol::socket::reuse_address(true));
177 sock.set_option(boost::asio::ip::multicast::enable_loopback(enableLoopback));
Davide Pesavento8215a3a2017-12-25 19:14:33 -0500178
179 if (localEndpoint.address().is_v4()) {
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500180 sock.bind(localEndpoint);
Davide Pesavento8215a3a2017-12-25 19:14:33 -0500181 if (!localEndpoint.address().is_unspecified())
182 sock.set_option(boost::asio::ip::multicast::outbound_interface(localEndpoint.address().to_v4()));
183 }
184 else {
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500185 sock.set_option(boost::asio::ip::v6_only(true));
186 sock.bind(localEndpoint);
187 if (netif)
188 sock.set_option(boost::asio::ip::multicast::outbound_interface(netif->getIndex()));
Davide Pesavento8215a3a2017-12-25 19:14:33 -0500189 }
190}
191
Davide Pesaventoeee7b972015-11-18 21:34:46 +0100192template<>
ashiqopu77d0bfd2019-02-20 20:37:31 +0000193EndpointId
Davide Pesaventoeee7b972015-11-18 21:34:46 +0100194DatagramTransport<boost::asio::ip::udp, Multicast>::makeEndpointId(const protocol::endpoint& ep)
195{
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500196 if (ep.address().is_v4()) {
197 return (static_cast<uint64_t>(ep.port()) << 32) |
198 static_cast<uint64_t>(ep.address().to_v4().to_ulong());
199 }
200 else {
201 size_t seed = 0;
202 const auto& addrBytes = ep.address().to_v6().to_bytes();
203 boost::hash_range(seed, addrBytes.begin(), addrBytes.end());
204 boost::hash_combine(seed, ep.port());
205 return seed;
206 }
Davide Pesaventoeee7b972015-11-18 21:34:46 +0100207}
208
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400209} // namespace nfd::face