unicast-ethernet-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-2022, 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 "common/global.hpp"
28 
29 #include <cstdio> // for snprintf()
30 
31 namespace nfd::face {
32 
33 NFD_LOG_INIT(UnicastEthernetTransport);
34 
35 UnicastEthernetTransport::UnicastEthernetTransport(const ndn::net::NetworkInterface& localEndpoint,
36  const ethernet::Address& remoteEndpoint,
37  ndn::nfd::FacePersistency persistency,
38  time::nanoseconds idleTimeout)
39  : EthernetTransport(localEndpoint, remoteEndpoint)
40  , m_idleTimeout(idleTimeout)
41 {
42  this->setLocalUri(FaceUri::fromDev(m_interfaceName));
43  this->setRemoteUri(FaceUri(m_destAddress));
44  this->setScope(ndn::nfd::FACE_SCOPE_NON_LOCAL);
45  this->setPersistency(persistency);
46  this->setLinkType(ndn::nfd::LINK_TYPE_POINT_TO_POINT);
47  this->setMtu(localEndpoint.getMtu());
48 
49  NFD_LOG_FACE_DEBUG("Creating transport");
50 
51  char filter[110];
52  // Note: "not vlan" must appear last in the filter expression, or the
53  // rest of the filter won't work as intended (see pcap-filter(7))
54  std::snprintf(filter, sizeof(filter),
55  "(ether proto 0x%x) && (ether src %s) && (ether dst %s) && (not vlan)",
56  ethernet::ETHERTYPE_NDN,
57  m_destAddress.toString().data(),
58  m_srcAddress.toString().data());
59  m_pcap.setPacketFilter(filter);
60 
61  if (getPersistency() == ndn::nfd::FACE_PERSISTENCY_ON_DEMAND &&
62  m_idleTimeout > time::nanoseconds::zero()) {
63  scheduleClosureWhenIdle();
64  }
65 }
66 
67 bool
68 UnicastEthernetTransport::canChangePersistencyToImpl(ndn::nfd::FacePersistency newPersistency) const
69 {
70  return true;
71 }
72 
73 void
74 UnicastEthernetTransport::afterChangePersistency(ndn::nfd::FacePersistency oldPersistency)
75 {
76  if (getPersistency() == ndn::nfd::FACE_PERSISTENCY_ON_DEMAND &&
77  m_idleTimeout > time::nanoseconds::zero()) {
78  scheduleClosureWhenIdle();
79  }
80  else {
81  m_closeIfIdleEvent.cancel();
82  setExpirationTime(time::steady_clock::time_point::max());
83  }
84 }
85 
86 void
87 UnicastEthernetTransport::scheduleClosureWhenIdle()
88 {
89  m_closeIfIdleEvent = getScheduler().schedule(m_idleTimeout, [this] {
90  if (!hasRecentlyReceived()) {
91  NFD_LOG_FACE_INFO("Closing due to inactivity");
92  this->close();
93  }
94  else {
96  scheduleClosureWhenIdle();
97  }
98  });
99  setExpirationTime(time::steady_clock::now() + m_idleTimeout);
100 }
101 
102 } // namespace nfd::face
Base class for Ethernet-based Transports.
void setPacketFilter(const char *filter) const
Install a BPF filter on the receiving socket.
void setScope(ndn::nfd::FaceScope scope) noexcept
Definition: transport.hpp:355
void setPersistency(ndn::nfd::FacePersistency newPersistency)
Changes the persistency setting of the transport.
Definition: transport.cpp:152
ndn::nfd::FacePersistency getPersistency() const noexcept
Returns the current persistency setting of the transport.
Definition: transport.hpp:236
void setMtu(ssize_t mtu) noexcept
Definition: transport.cpp:114
void setExpirationTime(const time::steady_clock::time_point &expirationTime) noexcept
Definition: transport.hpp:386
void setLocalUri(const FaceUri &uri) noexcept
Definition: transport.hpp:343
void setLinkType(ndn::nfd::LinkType linkType) noexcept
Definition: transport.hpp:361
void setRemoteUri(const FaceUri &uri) noexcept
Definition: transport.hpp:349
void close()
Request the transport to be closed.
Definition: transport.cpp:67
void afterChangePersistency(ndn::nfd::FacePersistency oldPersistency) final
Invoked after the persistency has been changed.
bool canChangePersistencyToImpl(ndn::nfd::FacePersistency newPersistency) const final
Invoked by canChangePersistencyTo to perform the check.
UnicastEthernetTransport(const ndn::net::NetworkInterface &localEndpoint, const ethernet::Address &remoteEndpoint, ndn::nfd::FacePersistency persistency, time::nanoseconds idleTimeout)
Creates an Ethernet-based transport for unicast communication.
#define NFD_LOG_FACE_DEBUG(msg)
Log a message at DEBUG level.
#define NFD_LOG_FACE_INFO(msg)
Log a message at INFO level.
#define NFD_LOG_INIT(name)
Definition: logger.hpp:31
ndn::Scheduler & getScheduler()
Returns the global Scheduler instance for the calling thread.
Definition: global.cpp:45