unicast-udp-transport.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
27 #include "udp-protocol.hpp"
28 
29 #ifdef __linux__
30 #include <cerrno> // for errno
31 #include <cstring> // for std::strerror()
32 #include <netinet/in.h> // for IP_MTU_DISCOVER and IP_PMTUDISC_DONT
33 #include <sys/socket.h> // for setsockopt()
34 #endif
35 
36 namespace nfd {
37 namespace face {
38 
40  "UnicastUdpTransport");
41 
42 UnicastUdpTransport::UnicastUdpTransport(protocol::socket&& socket,
43  ndn::nfd::FacePersistency persistency,
44  time::nanoseconds idleTimeout)
45  : DatagramTransport(std::move(socket))
46  , m_idleTimeout(idleTimeout)
47 {
48  this->setLocalUri(FaceUri(m_socket.local_endpoint()));
49  this->setRemoteUri(FaceUri(m_socket.remote_endpoint()));
50  this->setScope(ndn::nfd::FACE_SCOPE_NON_LOCAL);
51  this->setPersistency(persistency);
52  this->setLinkType(ndn::nfd::LINK_TYPE_POINT_TO_POINT);
53  this->setMtu(udp::computeMtu(m_socket.local_endpoint()));
54 
55  NFD_LOG_FACE_INFO("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 
77  if (getPersistency() == ndn::nfd::FACE_PERSISTENCY_ON_DEMAND &&
78  m_idleTimeout > time::nanoseconds::zero()) {
79  scheduleClosureWhenIdle();
80  }
81 }
82 
83 bool
84 UnicastUdpTransport::canChangePersistencyToImpl(ndn::nfd::FacePersistency newPersistency) const
85 {
86  return true;
87 }
88 
89 void
90 UnicastUdpTransport::afterChangePersistency(ndn::nfd::FacePersistency oldPersistency)
91 {
92  if (getPersistency() == ndn::nfd::FACE_PERSISTENCY_ON_DEMAND &&
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 = scheduler::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 setLocalUri(const FaceUri &uri)
Definition: transport.hpp:384
void setScope(ndn::nfd::FaceScope scope)
Definition: transport.hpp:408
void setMtu(ssize_t mtu)
Definition: transport.hpp:438
STL namespace.
ssize_t computeMtu(const Endpoint &localEndpoint)
computes maximum payload size in a UDP packet
#define NFD_LOG_FACE_WARN(msg)
Log a message at WARN level.
Definition: face-log.hpp:88
Copyright (c) 2014-2015, Regents of the University of California, Arizona Board of Regents...
Definition: algorithm.hpp:32
void afterChangePersistency(ndn::nfd::FacePersistency oldPersistency) final
invoked after the persistency has been changed
#define NFD_LOG_FACE_INFO(msg)
Log a message at INFO level.
Definition: face-log.hpp:85
#define NFD_LOG_INCLASS_TEMPLATE_SPECIALIZATION_DEFINE(cls, specialization, name)
Definition: logger.hpp:135
void setLinkType(ndn::nfd::LinkType linkType)
Definition: transport.hpp:426
void setPersistency(ndn::nfd::FacePersistency newPersistency)
changes face persistency setting
Definition: transport.cpp:151
void close()
request the transport to be closed
Definition: transport.cpp:84
ndn::nfd::FacePersistency getPersistency() const
Definition: transport.hpp:414
Implements Transport for datagram-based protocols.
EventId schedule(time::nanoseconds after, const EventCallback &event)
schedule an event
Definition: scheduler.cpp:47
bool canChangePersistencyToImpl(ndn::nfd::FacePersistency newPersistency) const final
invoked by canChangePersistencyTo to perform the check
void setRemoteUri(const FaceUri &uri)
Definition: transport.hpp:396
void cancel()
cancels the event manually
Definition: scheduler.cpp:95
UnicastUdpTransport(protocol::socket &&socket, ndn::nfd::FacePersistency persistency, time::nanoseconds idleTimeout)
void setExpirationTime(const time::steady_clock::TimePoint &expirationTime)
Definition: transport.hpp:457