NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
unicast-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-2020, 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 "udp-protocol.hpp"
28 #include "common/global.hpp"
29 
30 #ifdef __linux__
31 #include <cerrno> // for errno
32 #include <cstring> // for std::strerror()
33 #include <netinet/in.h> // for IP_MTU_DISCOVER and IP_PMTUDISC_DONT
34 #include <sys/socket.h> // for setsockopt()
35 #endif
36 
37 namespace nfd {
38 namespace face {
39 
40 NFD_LOG_MEMBER_INIT_SPECIALIZED((DatagramTransport<boost::asio::ip::udp, Unicast>), UnicastUdpTransport);
41 
43  ndn::nfd::FacePersistency persistency,
44  time::nanoseconds idleTimeout)
46  , m_idleTimeout(idleTimeout)
47 {
48  this->setLocalUri(FaceUri(m_socket.local_endpoint()));
49  this->setRemoteUri(FaceUri(m_socket.remote_endpoint()));
51  this->setPersistency(persistency);
53  this->setMtu(udp::computeMtu(m_socket.local_endpoint()));
54 
55  NFD_LOG_FACE_DEBUG("Creating transport");
56 
57 #ifdef __linux__
58  //
59  // By default, Linux does path MTU discovery on IPv4 sockets,
60  // and sets the DF (Don't Fragment) flag on datagrams smaller
61  // than the interface MTU. However this does not work for us,
62  // because we cannot properly respond to ICMP "packet too big"
63  // messages by fragmenting the packet at the application level,
64  // since we want to rely on IP for fragmentation and reassembly.
65  //
66  // Therefore, we disable PMTU discovery, which prevents the kernel
67  // from setting the DF flag on outgoing datagrams, and thus allows
68  // routers along the path to perform fragmentation as needed.
69  //
70  const int value = IP_PMTUDISC_DONT;
71  if (::setsockopt(m_socket.native_handle(), IPPROTO_IP,
72  IP_MTU_DISCOVER, &value, sizeof(value)) < 0) {
73  NFD_LOG_FACE_WARN("Failed to disable path MTU discovery: " << std::strerror(errno));
74  }
75 #endif
76 
78  m_idleTimeout > time::nanoseconds::zero()) {
79  scheduleClosureWhenIdle();
80  }
81 }
82 
83 bool
85 {
86  return true;
87 }
88 
89 void
91 {
93  m_idleTimeout > time::nanoseconds::zero()) {
94  scheduleClosureWhenIdle();
95  }
96  else {
97  m_closeIfIdleEvent.cancel();
98  setExpirationTime(time::steady_clock::TimePoint::max());
99  }
100 }
101 
102 void
103 UnicastUdpTransport::scheduleClosureWhenIdle()
104 {
105  m_closeIfIdleEvent = getScheduler().schedule(m_idleTimeout, [this] {
106  if (!hasRecentlyReceived()) {
107  NFD_LOG_FACE_INFO("Closing due to inactivity");
108  this->close();
109  }
110  else {
112  scheduleClosureWhenIdle();
113  }
114  });
115  setExpirationTime(time::steady_clock::now() + m_idleTimeout);
116 }
117 
118 } // namespace face
119 } // namespace nfd
void setPersistency(ndn::nfd::FacePersistency newPersistency)
changes face persistency setting
Definition: transport.cpp:164
void setExpirationTime(const time::steady_clock::TimePoint &expirationTime)
Definition: transport.hpp:463
NFD_LOG_MEMBER_INIT_SPECIALIZED((DatagramTransport< boost::asio::ip::udp, Multicast >), MulticastUdpTransport)
bool canChangePersistencyToImpl(ndn::nfd::FacePersistency newPersistency) const final
invoked by canChangePersistencyTo to perform the check
void setRemoteUri(const FaceUri &uri)
Definition: transport.hpp:397
static time_point now() noexcept
Definition: time.cpp:80
STL namespace.
void close()
Request the transport to be closed.
Definition: transport.cpp:79
void setLinkType(ndn::nfd::LinkType linkType)
Definition: transport.hpp:427
Implements Transport for datagram-based protocols.
void setMtu(ssize_t mtu)
Definition: transport.cpp:126
ssize_t computeMtu(const Endpoint &localEndpoint)
computes maximum payload size in a UDP packet
UnicastUdpTransport(protocol::socket &&socket, ndn::nfd::FacePersistency persistency, time::nanoseconds idleTimeout)
void setScope(ndn::nfd::FaceScope scope)
Definition: transport.hpp:409
Scheduler & getScheduler()
Returns the global Scheduler instance for the calling thread.
Definition: global.cpp:70
#define NFD_LOG_FACE_DEBUG(msg)
Log a message at DEBUG level.
#define NFD_LOG_FACE_INFO(msg)
Log a message at INFO level.
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:39
void setLocalUri(const FaceUri &uri)
Definition: transport.hpp:385
represents the underlying protocol and address used by a Face
Definition: face-uri.hpp:44
void afterChangePersistency(ndn::nfd::FacePersistency oldPersistency) final
invoked after the persistency has been changed
ndn::nfd::FacePersistency getPersistency() const
Definition: transport.hpp:415
void cancel()
Cancel the operation.
Catch-all error for socket component errors that don&#39;t fit in other categories.
Definition: base.hpp:83
boost::chrono::nanoseconds nanoseconds
Definition: time.hpp:50
#define NFD_LOG_FACE_WARN(msg)
Log a message at WARN level.