blob: 60059bb408d10869cb0d967ddb3a8475c036cca6 [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/*
Davide Pesaventoa3148082018-04-12 18:21:54 -04003 * Copyright (c) 2014-2018, 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"
27
28#include <stdio.h> // for snprintf()
29
30namespace nfd {
31namespace face {
32
Davide Pesaventoa3148082018-04-12 18:21:54 -040033NFD_LOG_INIT(UnicastEthernetTransport);
Davide Pesavento6bd6d0b2017-03-25 15:16:40 -040034
Junxiao Shi0d82d042017-07-07 06:15:27 +000035UnicastEthernetTransport::UnicastEthernetTransport(const ndn::net::NetworkInterface& localEndpoint,
Davide Pesavento6bd6d0b2017-03-25 15:16:40 -040036 const ethernet::Address& remoteEndpoint,
37 ndn::nfd::FacePersistency persistency,
Eric Newberry812d6152018-06-06 15:06:01 -070038 time::nanoseconds idleTimeout,
39 optional<ssize_t> overrideMtu)
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 Newberry812d6152018-06-06 15:06:01 -070048
49 if (overrideMtu) {
50 this->setMtu(std::min<ssize_t>(localEndpoint.getMtu(), *overrideMtu));
51 }
52 else {
53 this->setMtu(localEndpoint.getMtu());
54 }
Davide Pesavento6bd6d0b2017-03-25 15:16:40 -040055
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)",
65 ethernet::ETHERTYPE_NDN,
Davide Pesaventofe0580c2017-05-12 02:02:10 -040066 m_destAddress.toString().data(),
67 m_srcAddress.toString().data());
68 m_pcap.setPacketFilter(filter);
Davide Pesavento6bd6d0b2017-03-25 15:16:40 -040069
Davide Pesavento1816d4b2017-07-02 12:20:48 -040070 if (getPersistency() == ndn::nfd::FACE_PERSISTENCY_ON_DEMAND &&
71 m_idleTimeout > time::nanoseconds::zero()) {
72 scheduleClosureWhenIdle();
73 }
74}
75
76bool
77UnicastEthernetTransport::canChangePersistencyToImpl(ndn::nfd::FacePersistency newPersistency) const
78{
79 return true;
80}
81
82void
83UnicastEthernetTransport::afterChangePersistency(ndn::nfd::FacePersistency oldPersistency)
84{
85 if (getPersistency() == ndn::nfd::FACE_PERSISTENCY_ON_DEMAND &&
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
95void
96UnicastEthernetTransport::scheduleClosureWhenIdle()
97{
98 m_closeIfIdleEvent = scheduler::schedule(m_idleTimeout, [this] {
Davide Pesavento0b0a71d2017-07-02 13:57:36 -040099 if (!hasRecentlyReceived()) {
Davide Pesavento1816d4b2017-07-02 12:20:48 -0400100 NFD_LOG_FACE_INFO("Closing due to inactivity");
101 this->close();
102 }
103 else {
Davide Pesavento0b0a71d2017-07-02 13:57:36 -0400104 resetRecentlyReceived();
Davide Pesavento1816d4b2017-07-02 12:20:48 -0400105 scheduleClosureWhenIdle();
106 }
107 });
108 setExpirationTime(time::steady_clock::now() + m_idleTimeout);
Davide Pesavento6bd6d0b2017-03-25 15:16:40 -0400109}
110
111} // namespace face
112} // namespace nfd