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-2018, 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 
28 #include <stdio.h> // for snprintf()
29 
30 namespace nfd {
31 namespace face {
32 
34 
36  const ethernet::Address& remoteEndpoint,
37  ndn::nfd::FacePersistency persistency,
38  time::nanoseconds idleTimeout,
39  optional<ssize_t> overrideMtu)
40  : EthernetTransport(localEndpoint, remoteEndpoint)
41  , m_idleTimeout(idleTimeout)
42 {
46  this->setPersistency(persistency);
48 
49  if (overrideMtu) {
50  this->setMtu(std::min<ssize_t>(localEndpoint.getMtu(), *overrideMtu));
51  }
52  else {
53  this->setMtu(localEndpoint.getMtu());
54  }
55 
56  NFD_LOG_FACE_INFO("Creating transport");
57 
58  char filter[110];
59  // note #1: we cannot use std::snprintf because it's not available
60  // on some platforms (see #2299)
61  // note #2: "not vlan" must appear last in the filter expression, or the
62  // rest of the filter won't work as intended (see pcap-filter(7))
63  snprintf(filter, sizeof(filter),
64  "(ether proto 0x%x) && (ether src %s) && (ether dst %s) && (not vlan)",
66  m_destAddress.toString().data(),
67  m_srcAddress.toString().data());
68  m_pcap.setPacketFilter(filter);
69 
71  m_idleTimeout > time::nanoseconds::zero()) {
72  scheduleClosureWhenIdle();
73  }
74 }
75 
76 bool
78 {
79  return true;
80 }
81 
82 void
84 {
86  m_idleTimeout > time::nanoseconds::zero()) {
87  scheduleClosureWhenIdle();
88  }
89  else {
90  m_closeIfIdleEvent.cancel();
91  setExpirationTime(time::steady_clock::TimePoint::max());
92  }
93 }
94 
95 void
96 UnicastEthernetTransport::scheduleClosureWhenIdle()
97 {
98  m_closeIfIdleEvent = scheduler::schedule(m_idleTimeout, [this] {
99  if (!hasRecentlyReceived()) {
100  NFD_LOG_FACE_INFO("Closing due to inactivity");
101  this->close();
102  }
103  else {
105  scheduleClosureWhenIdle();
106  }
107  });
108  setExpirationTime(time::steady_clock::now() + m_idleTimeout);
109 }
110 
111 } // namespace face
112 } // namespace nfd
void setPersistency(ndn::nfd::FacePersistency newPersistency)
changes face persistency setting
Definition: transport.cpp:154
void setExpirationTime(const time::steady_clock::TimePoint &expirationTime)
Definition: transport.hpp:506
void setRemoteUri(const FaceUri &uri)
Definition: transport.hpp:433
static time_point now() noexcept
Definition: time.cpp:80
void close()
request the transport to be closed
Definition: transport.cpp:87
void afterChangePersistency(ndn::nfd::FacePersistency oldPersistency) final
invoked after the persistency has been changed
void setLinkType(ndn::nfd::LinkType linkType)
Definition: transport.hpp:463
std::string toString(char sep=':') const
Converts the address to a human-readable string.
Definition: ethernet.cpp:78
void setPacketFilter(const char *filter) const
Install a BPF filter on the receiving socket.
void setMtu(ssize_t mtu)
Definition: transport.hpp:475
void setScope(ndn::nfd::FaceScope scope)
Definition: transport.hpp:445
Represents one network interface attached to the host.
#define NFD_LOG_FACE_INFO(msg)
Log a message at INFO level.
Definition: face-log.hpp:85
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:40
bool canChangePersistencyToImpl(ndn::nfd::FacePersistency newPersistency) const final
invoked by canChangePersistencyTo to perform the check
void cancel()
Cancel the operation.
void setLocalUri(const FaceUri &uri)
Definition: transport.hpp:421
const uint16_t ETHERTYPE_NDN
Definition: ethernet.hpp:39
represents the underlying protocol and address used by a Face
Definition: face-uri.hpp:44
uint32_t getMtu() const
Returns the MTU (maximum transmission unit) of the interface.
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.
represents an Ethernet hardware address
Definition: ethernet.hpp:52
ndn::nfd::FacePersistency getPersistency() const
Definition: transport.hpp:451
EventId schedule(time::nanoseconds after, const EventCallback &event)
Schedule an event.
Definition: scheduler.cpp:48
Base class for Ethernet-based Transports.
#define NFD_LOG_INIT(name)
Definition: logger.hpp:31
A unicast Transport that uses raw Ethernet II frames.
static FaceUri fromDev(const std::string &ifname)
create dev FaceUri from network device name
Definition: face-uri.cpp:169