blob: 33e80bd5e492bdcfa52be127c2f3cb1c84777e99 [file] [log] [blame]
Davide Pesavento6bd6d0b2017-03-25 15:16:40 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi0d82d042017-07-07 06:15:27 +00002/*
Eric Newberrycb6551e2020-03-02 14:12:16 -08003 * Copyright (c) 2014-2020, Regents of the University of California,
Davide Pesavento6bd6d0b2017-03-25 15:16:40 -04004 * 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
26#include "unicast-ethernet-transport.hpp"
Davide Pesavento2cae8ca2019-04-18 20:48:05 -040027#include "common/global.hpp"
Davide Pesavento6bd6d0b2017-03-25 15:16:40 -040028
29#include <stdio.h> // for snprintf()
30
31namespace nfd {
32namespace face {
33
Davide Pesaventoa3148082018-04-12 18:21:54 -040034NFD_LOG_INIT(UnicastEthernetTransport);
Davide Pesavento6bd6d0b2017-03-25 15:16:40 -040035
Junxiao Shi0d82d042017-07-07 06:15:27 +000036UnicastEthernetTransport::UnicastEthernetTransport(const ndn::net::NetworkInterface& localEndpoint,
Davide Pesavento6bd6d0b2017-03-25 15:16:40 -040037 const ethernet::Address& remoteEndpoint,
38 ndn::nfd::FacePersistency persistency,
Eric Newberrycb6551e2020-03-02 14:12:16 -080039 time::nanoseconds idleTimeout)
Davide Pesavento6bd6d0b2017-03-25 15:16:40 -040040 : 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);
Eric Newberrycb6551e2020-03-02 14:12:16 -080048 this->setMtu(localEndpoint.getMtu());
Davide Pesavento6bd6d0b2017-03-25 15:16:40 -040049
Davide Pesaventoa681a242019-03-29 23:48:27 -040050 NFD_LOG_FACE_DEBUG("Creating transport");
Davide Pesavento6bd6d0b2017-03-25 15:16:40 -040051
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,
Davide Pesaventofe0580c2017-05-12 02:02:10 -040060 m_destAddress.toString().data(),
61 m_srcAddress.toString().data());
62 m_pcap.setPacketFilter(filter);
Davide Pesavento6bd6d0b2017-03-25 15:16:40 -040063
Davide Pesavento1816d4b2017-07-02 12:20:48 -040064 if (getPersistency() == ndn::nfd::FACE_PERSISTENCY_ON_DEMAND &&
65 m_idleTimeout > time::nanoseconds::zero()) {
66 scheduleClosureWhenIdle();
67 }
68}
69
70bool
71UnicastEthernetTransport::canChangePersistencyToImpl(ndn::nfd::FacePersistency newPersistency) const
72{
73 return true;
74}
75
76void
77UnicastEthernetTransport::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
89void
90UnicastEthernetTransport::scheduleClosureWhenIdle()
91{
Davide Pesavento3dade002019-03-19 11:29:56 -060092 m_closeIfIdleEvent = getScheduler().schedule(m_idleTimeout, [this] {
Davide Pesavento0b0a71d2017-07-02 13:57:36 -040093 if (!hasRecentlyReceived()) {
Davide Pesavento1816d4b2017-07-02 12:20:48 -040094 NFD_LOG_FACE_INFO("Closing due to inactivity");
95 this->close();
96 }
97 else {
Davide Pesavento0b0a71d2017-07-02 13:57:36 -040098 resetRecentlyReceived();
Davide Pesavento1816d4b2017-07-02 12:20:48 -040099 scheduleClosureWhenIdle();
100 }
101 });
102 setExpirationTime(time::steady_clock::now() + m_idleTimeout);
Davide Pesavento6bd6d0b2017-03-25 15:16:40 -0400103}
104
105} // namespace face
106} // namespace nfd