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