Davide Pesavento | 6bd6d0b | 2017-03-25 15:16:40 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | 0d82d04 | 2017-07-07 06:15:27 +0000 | [diff] [blame] | 2 | /* |
Davide Pesavento | 3dade00 | 2019-03-19 11:29:56 -0600 | [diff] [blame] | 3 | * Copyright (c) 2014-2019, Regents of the University of California, |
Davide Pesavento | 6bd6d0b | 2017-03-25 15:16:40 -0400 | [diff] [blame] | 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 | |
| 26 | #include "unicast-ethernet-transport.hpp" |
Davide Pesavento | 2cae8ca | 2019-04-18 20:48:05 -0400 | [diff] [blame] | 27 | #include "common/global.hpp" |
Davide Pesavento | 6bd6d0b | 2017-03-25 15:16:40 -0400 | [diff] [blame] | 28 | |
| 29 | #include <stdio.h> // for snprintf() |
| 30 | |
| 31 | namespace nfd { |
| 32 | namespace face { |
| 33 | |
Davide Pesavento | a314808 | 2018-04-12 18:21:54 -0400 | [diff] [blame] | 34 | NFD_LOG_INIT(UnicastEthernetTransport); |
Davide Pesavento | 6bd6d0b | 2017-03-25 15:16:40 -0400 | [diff] [blame] | 35 | |
Junxiao Shi | 0d82d04 | 2017-07-07 06:15:27 +0000 | [diff] [blame] | 36 | UnicastEthernetTransport::UnicastEthernetTransport(const ndn::net::NetworkInterface& localEndpoint, |
Davide Pesavento | 6bd6d0b | 2017-03-25 15:16:40 -0400 | [diff] [blame] | 37 | const ethernet::Address& remoteEndpoint, |
| 38 | ndn::nfd::FacePersistency persistency, |
Eric Newberry | 812d615 | 2018-06-06 15:06:01 -0700 | [diff] [blame] | 39 | time::nanoseconds idleTimeout, |
| 40 | optional<ssize_t> overrideMtu) |
Davide Pesavento | 6bd6d0b | 2017-03-25 15:16:40 -0400 | [diff] [blame] | 41 | : EthernetTransport(localEndpoint, remoteEndpoint) |
| 42 | , m_idleTimeout(idleTimeout) |
| 43 | { |
| 44 | this->setLocalUri(FaceUri::fromDev(m_interfaceName)); |
| 45 | this->setRemoteUri(FaceUri(m_destAddress)); |
| 46 | this->setScope(ndn::nfd::FACE_SCOPE_NON_LOCAL); |
| 47 | this->setPersistency(persistency); |
| 48 | this->setLinkType(ndn::nfd::LINK_TYPE_POINT_TO_POINT); |
Eric Newberry | 812d615 | 2018-06-06 15:06:01 -0700 | [diff] [blame] | 49 | |
| 50 | if (overrideMtu) { |
| 51 | this->setMtu(std::min<ssize_t>(localEndpoint.getMtu(), *overrideMtu)); |
| 52 | } |
| 53 | else { |
| 54 | this->setMtu(localEndpoint.getMtu()); |
| 55 | } |
Davide Pesavento | 6bd6d0b | 2017-03-25 15:16:40 -0400 | [diff] [blame] | 56 | |
Davide Pesavento | a681a24 | 2019-03-29 23:48:27 -0400 | [diff] [blame] | 57 | NFD_LOG_FACE_DEBUG("Creating transport"); |
Davide Pesavento | 6bd6d0b | 2017-03-25 15:16:40 -0400 | [diff] [blame] | 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)", |
| 66 | ethernet::ETHERTYPE_NDN, |
Davide Pesavento | fe0580c | 2017-05-12 02:02:10 -0400 | [diff] [blame] | 67 | m_destAddress.toString().data(), |
| 68 | m_srcAddress.toString().data()); |
| 69 | m_pcap.setPacketFilter(filter); |
Davide Pesavento | 6bd6d0b | 2017-03-25 15:16:40 -0400 | [diff] [blame] | 70 | |
Davide Pesavento | 1816d4b | 2017-07-02 12:20:48 -0400 | [diff] [blame] | 71 | if (getPersistency() == ndn::nfd::FACE_PERSISTENCY_ON_DEMAND && |
| 72 | m_idleTimeout > time::nanoseconds::zero()) { |
| 73 | scheduleClosureWhenIdle(); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | bool |
| 78 | UnicastEthernetTransport::canChangePersistencyToImpl(ndn::nfd::FacePersistency newPersistency) const |
| 79 | { |
| 80 | return true; |
| 81 | } |
| 82 | |
| 83 | void |
| 84 | UnicastEthernetTransport::afterChangePersistency(ndn::nfd::FacePersistency oldPersistency) |
| 85 | { |
| 86 | if (getPersistency() == ndn::nfd::FACE_PERSISTENCY_ON_DEMAND && |
| 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 | { |
Davide Pesavento | 3dade00 | 2019-03-19 11:29:56 -0600 | [diff] [blame] | 99 | m_closeIfIdleEvent = getScheduler().schedule(m_idleTimeout, [this] { |
Davide Pesavento | 0b0a71d | 2017-07-02 13:57:36 -0400 | [diff] [blame] | 100 | if (!hasRecentlyReceived()) { |
Davide Pesavento | 1816d4b | 2017-07-02 12:20:48 -0400 | [diff] [blame] | 101 | NFD_LOG_FACE_INFO("Closing due to inactivity"); |
| 102 | this->close(); |
| 103 | } |
| 104 | else { |
Davide Pesavento | 0b0a71d | 2017-07-02 13:57:36 -0400 | [diff] [blame] | 105 | resetRecentlyReceived(); |
Davide Pesavento | 1816d4b | 2017-07-02 12:20:48 -0400 | [diff] [blame] | 106 | scheduleClosureWhenIdle(); |
| 107 | } |
| 108 | }); |
| 109 | setExpirationTime(time::steady_clock::now() + m_idleTimeout); |
Davide Pesavento | 6bd6d0b | 2017-03-25 15:16:40 -0400 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | } // namespace face |
| 113 | } // namespace nfd |