NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
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-2019, 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 
35 
37  const ethernet::Address& remoteEndpoint,
38  ndn::nfd::FacePersistency persistency,
39  time::nanoseconds idleTimeout,
40  optional<ssize_t> overrideMtu)
41  : EthernetTransport(localEndpoint, remoteEndpoint)
42  , m_idleTimeout(idleTimeout)
43 {
47  this->setPersistency(persistency);
49 
50  if (overrideMtu) {
51  this->setMtu(std::min<ssize_t>(localEndpoint.getMtu(), *overrideMtu));
52  }
53  else {
54  this->setMtu(localEndpoint.getMtu());
55  }
56 
57  NFD_LOG_FACE_DEBUG("Creating transport");
58 
59  char filter[110];
60  // note #1: we cannot use std::snprintf because it's not available
61  // on some platforms (see #2299)
62  // note #2: "not vlan" must appear last in the filter expression, or the
63  // rest of the filter won't work as intended (see pcap-filter(7))
64  snprintf(filter, sizeof(filter),
65  "(ether proto 0x%x) && (ether src %s) && (ether dst %s) && (not vlan)",
67  m_destAddress.toString().data(),
68  m_srcAddress.toString().data());
69  m_pcap.setPacketFilter(filter);
70 
72  m_idleTimeout > time::nanoseconds::zero()) {
73  scheduleClosureWhenIdle();
74  }
75 }
76 
77 bool
79 {
80  return true;
81 }
82 
83 void
85 {
87  m_idleTimeout > time::nanoseconds::zero()) {
88  scheduleClosureWhenIdle();
89  }
90  else {
91  m_closeIfIdleEvent.cancel();
92  setExpirationTime(time::steady_clock::TimePoint::max());
93  }
94 }
95 
96 void
97 UnicastEthernetTransport::scheduleClosureWhenIdle()
98 {
99  m_closeIfIdleEvent = getScheduler().schedule(m_idleTimeout, [this] {
100  if (!hasRecentlyReceived()) {
101  NFD_LOG_FACE_INFO("Closing due to inactivity");
102  this->close();
103  }
104  else {
106  scheduleClosureWhenIdle();
107  }
108  });
109  setExpirationTime(time::steady_clock::now() + m_idleTimeout);
110 }
111 
112 } // namespace face
113 } // namespace nfd
global.hpp
ndn::detail::ScopedCancelHandle::cancel
void cancel()
Cancel the operation.
Definition: cancel-handle.hpp:109
ndn::nfd::FACE_SCOPE_NON_LOCAL
@ FACE_SCOPE_NON_LOCAL
face is non-local
Definition: nfd-constants.hpp:36
nfd::face::Transport::setMtu
void setMtu(ssize_t mtu)
Definition: transport.hpp:448
nfd::face::Transport::setRemoteUri
void setRemoteUri(const FaceUri &uri)
Definition: transport.hpp:406
nfd::face::EthernetTransport::m_destAddress
ethernet::Address m_destAddress
Definition: ethernet-transport.hpp:105
ndn::net::NetworkInterface
Represents one network interface attached to the host.
Definition: network-interface.hpp:70
ndn::nfd::LINK_TYPE_POINT_TO_POINT
@ LINK_TYPE_POINT_TO_POINT
link is point-to-point
Definition: nfd-constants.hpp:59
ndn::FaceUri
represents the underlying protocol and address used by a Face
Definition: face-uri.hpp:45
ndn::ethernet::Address
represents an Ethernet hardware address
Definition: ethernet.hpp:53
nfd::face::Transport::setScope
void setScope(ndn::nfd::FaceScope scope)
Definition: transport.hpp:418
ndn::time::steady_clock::now
static time_point now() noexcept
Definition: time.cpp:80
nfd::face::EthernetTransport::m_pcap
PcapHelper m_pcap
Definition: ethernet-transport.hpp:103
ndn::ethernet::ETHERTYPE_NDN
const uint16_t ETHERTYPE_NDN
Definition: ethernet.hpp:39
nfd::face::EthernetTransport::m_srcAddress
ethernet::Address m_srcAddress
Definition: ethernet-transport.hpp:104
NFD_LOG_FACE_DEBUG
#define NFD_LOG_FACE_DEBUG(msg)
Log a message at DEBUG level.
Definition: face-common.hpp:136
nfd::face::UnicastEthernetTransport::UnicastEthernetTransport
UnicastEthernetTransport(const ndn::net::NetworkInterface &localEndpoint, const ethernet::Address &remoteEndpoint, ndn::nfd::FacePersistency persistency, time::nanoseconds idleTimeout, optional< ssize_t > overrideMtu={})
Creates an Ethernet-based transport for unicast communication.
Definition: unicast-ethernet-transport.cpp:36
nfd::face::EthernetTransport::hasRecentlyReceived
bool hasRecentlyReceived() const
Definition: ethernet-transport.hpp:68
nfd::face::Transport::getPersistency
ndn::nfd::FacePersistency getPersistency() const
Definition: transport.hpp:424
nfd::face::Transport::setExpirationTime
void setExpirationTime(const time::steady_clock::TimePoint &expirationTime)
Definition: transport.hpp:479
nfd::face::EthernetTransport::resetRecentlyReceived
void resetRecentlyReceived()
Definition: ethernet-transport.hpp:74
ndn::nfd::FacePersistency
FacePersistency
Definition: nfd-constants.hpp:45
nfd
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:40
unicast-ethernet-transport.hpp
nfd::getScheduler
Scheduler & getScheduler()
Returns the global Scheduler instance for the calling thread.
Definition: global.cpp:70
ndn::FaceUri::fromDev
static FaceUri fromDev(const std::string &ifname)
create dev FaceUri from network device name
Definition: face-uri.cpp:169
nfd::face::UnicastEthernetTransport
A unicast Transport that uses raw Ethernet II frames.
Definition: unicast-ethernet-transport.hpp:38
nfd::face::UnicastEthernetTransport::afterChangePersistency
void afterChangePersistency(ndn::nfd::FacePersistency oldPersistency) final
invoked after the persistency has been changed
Definition: unicast-ethernet-transport.cpp:84
nfd::face::Transport::setLinkType
void setLinkType(ndn::nfd::LinkType linkType)
Definition: transport.hpp:436
ndn::net::NetworkInterface::getMtu
uint32_t getMtu() const
Returns the MTU (maximum transmission unit) of the interface.
Definition: network-interface.hpp:132
ndn::ethernet::Address::toString
std::string toString(char sep=':') const
Converts the address to a human-readable string.
Definition: ethernet.cpp:78
nfd::face::EthernetTransport
Base class for Ethernet-based Transports.
Definition: ethernet-transport.hpp:42
ndn::nfd::FACE_PERSISTENCY_ON_DEMAND
@ FACE_PERSISTENCY_ON_DEMAND
face is on-demand
Definition: nfd-constants.hpp:48
nfd::face::EthernetTransport::m_interfaceName
std::string m_interfaceName
Definition: ethernet-transport.hpp:106
nfd::face::Transport::setPersistency
void setPersistency(ndn::nfd::FacePersistency newPersistency)
changes face persistency setting
Definition: transport.cpp:150
NFD_LOG_FACE_INFO
#define NFD_LOG_FACE_INFO(msg)
Log a message at INFO level.
Definition: face-common.hpp:139
nfd::face::UnicastEthernetTransport::canChangePersistencyToImpl
bool canChangePersistencyToImpl(ndn::nfd::FacePersistency newPersistency) const final
invoked by canChangePersistencyTo to perform the check
Definition: unicast-ethernet-transport.cpp:78
nfd::face::PcapHelper::setPacketFilter
void setPacketFilter(const char *filter) const
Install a BPF filter on the receiving socket.
Definition: pcap-helper.cpp:115
nfd::face::Transport::close
void close()
Request the transport to be closed.
Definition: transport.cpp:81
NFD_LOG_INIT
#define NFD_LOG_INIT(name)
Definition: logger.hpp:31
nfd::face::Transport::setLocalUri
void setLocalUri(const FaceUri &uri)
Definition: transport.hpp:394