Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | a314808 | 2018-04-12 18:21:54 -0400 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2014-2018, Regents of the University of California, |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [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 "lp-reassembler.hpp" |
| 27 | #include "link-service.hpp" |
Davide Pesavento | a314808 | 2018-04-12 18:21:54 -0400 | [diff] [blame] | 28 | |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 29 | #include <numeric> |
| 30 | |
| 31 | namespace nfd { |
| 32 | namespace face { |
| 33 | |
Davide Pesavento | a314808 | 2018-04-12 18:21:54 -0400 | [diff] [blame] | 34 | NFD_LOG_INIT(LpReassembler); |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 35 | |
| 36 | LpReassembler::Options::Options() |
| 37 | : nMaxFragments(400) |
| 38 | , reassemblyTimeout(time::milliseconds(500)) |
| 39 | { |
| 40 | } |
| 41 | |
| 42 | LpReassembler::LpReassembler(const LpReassembler::Options& options, const LinkService* linkService) |
| 43 | : m_options(options) |
| 44 | , m_linkService(linkService) |
| 45 | { |
| 46 | } |
| 47 | |
| 48 | std::tuple<bool, Block, lp::Packet> |
| 49 | LpReassembler::receiveFragment(Transport::EndpointId remoteEndpoint, const lp::Packet& packet) |
| 50 | { |
| 51 | BOOST_ASSERT(packet.has<lp::FragmentField>()); |
| 52 | |
| 53 | static auto FALSE_RETURN = std::make_tuple(false, Block(), lp::Packet()); |
| 54 | |
| 55 | // read and check FragIndex and FragCount |
| 56 | uint64_t fragIndex = 0; |
| 57 | uint64_t fragCount = 1; |
| 58 | if (packet.has<lp::FragIndexField>()) { |
| 59 | fragIndex = packet.get<lp::FragIndexField>(); |
| 60 | } |
| 61 | if (packet.has<lp::FragCountField>()) { |
| 62 | fragCount = packet.get<lp::FragCountField>(); |
| 63 | } |
| 64 | |
| 65 | if (fragIndex >= fragCount) { |
| 66 | NFD_LOG_FACE_WARN("reassembly error, FragIndex>=FragCount: DROP"); |
| 67 | return FALSE_RETURN; |
| 68 | } |
| 69 | |
| 70 | if (fragCount > m_options.nMaxFragments) { |
| 71 | NFD_LOG_FACE_WARN("reassembly error, FragCount over limit: DROP"); |
| 72 | return FALSE_RETURN; |
| 73 | } |
| 74 | |
| 75 | // check for fast path |
| 76 | if (fragIndex == 0 && fragCount == 1) { |
| 77 | ndn::Buffer::const_iterator fragBegin, fragEnd; |
| 78 | std::tie(fragBegin, fragEnd) = packet.get<lp::FragmentField>(); |
| 79 | Block netPkt(&*fragBegin, std::distance(fragBegin, fragEnd)); |
| 80 | return std::make_tuple(true, netPkt, packet); |
| 81 | } |
| 82 | |
| 83 | // check Sequence and compute message identifier |
| 84 | if (!packet.has<lp::SequenceField>()) { |
| 85 | NFD_LOG_FACE_WARN("reassembly error, Sequence missing: DROP"); |
| 86 | return FALSE_RETURN; |
| 87 | } |
| 88 | lp::Sequence messageIdentifier = packet.get<lp::SequenceField>() - fragIndex; |
| 89 | Key key = std::make_tuple(remoteEndpoint, messageIdentifier); |
| 90 | |
| 91 | // add to PartialPacket |
| 92 | PartialPacket& pp = m_partialPackets[key]; |
| 93 | if (pp.fragCount == 0) { // new PartialPacket |
| 94 | pp.fragCount = fragCount; |
| 95 | pp.nReceivedFragments = 0; |
| 96 | pp.fragments.resize(fragCount); |
| 97 | } |
| 98 | else { |
| 99 | if (fragCount != pp.fragCount) { |
| 100 | NFD_LOG_FACE_WARN("reassembly error, FragCount changed: DROP"); |
| 101 | return FALSE_RETURN; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | if (pp.fragments[fragIndex].has<lp::SequenceField>()) { |
| 106 | NFD_LOG_FACE_TRACE("fragment already received: DROP"); |
| 107 | return FALSE_RETURN; |
| 108 | } |
| 109 | |
| 110 | pp.fragments[fragIndex] = packet; |
| 111 | ++pp.nReceivedFragments; |
| 112 | |
| 113 | // check complete condition |
| 114 | if (pp.nReceivedFragments == pp.fragCount) { |
| 115 | Block reassembled = doReassembly(key); |
| 116 | lp::Packet firstFrag(std::move(pp.fragments[0])); |
| 117 | m_partialPackets.erase(key); |
| 118 | return std::make_tuple(true, reassembled, firstFrag); |
| 119 | } |
| 120 | |
| 121 | // set drop timer |
| 122 | pp.dropTimer = scheduler::schedule(m_options.reassemblyTimeout, |
| 123 | bind(&LpReassembler::timeoutPartialPacket, this, key)); |
| 124 | |
| 125 | return FALSE_RETURN; |
| 126 | } |
| 127 | |
| 128 | Block |
| 129 | LpReassembler::doReassembly(const Key& key) |
| 130 | { |
| 131 | PartialPacket& pp = m_partialPackets[key]; |
| 132 | |
| 133 | size_t payloadSize = std::accumulate(pp.fragments.begin(), pp.fragments.end(), 0, |
| 134 | [&] (size_t sum, const lp::Packet& pkt) -> size_t { |
| 135 | ndn::Buffer::const_iterator fragBegin, fragEnd; |
| 136 | std::tie(fragBegin, fragEnd) = pkt.get<lp::FragmentField>(); |
| 137 | return sum + std::distance(fragBegin, fragEnd); |
| 138 | }); |
| 139 | |
| 140 | ndn::Buffer fragBuffer(payloadSize); |
| 141 | ndn::Buffer::iterator it = fragBuffer.begin(); |
| 142 | |
| 143 | for (const lp::Packet& frag : pp.fragments) { |
| 144 | ndn::Buffer::const_iterator fragBegin, fragEnd; |
| 145 | std::tie(fragBegin, fragEnd) = frag.get<lp::FragmentField>(); |
| 146 | it = std::copy(fragBegin, fragEnd, it); |
| 147 | } |
| 148 | |
| 149 | return Block(&*(fragBuffer.cbegin()), std::distance(fragBuffer.cbegin(), fragBuffer.cend())); |
| 150 | } |
| 151 | |
| 152 | void |
| 153 | LpReassembler::timeoutPartialPacket(const Key& key) |
| 154 | { |
| 155 | auto it = m_partialPackets.find(key); |
| 156 | if (it == m_partialPackets.end()) { |
| 157 | return; |
| 158 | } |
| 159 | |
| 160 | this->beforeTimeout(std::get<0>(key), it->second.nReceivedFragments); |
| 161 | m_partialPackets.erase(it); |
| 162 | } |
| 163 | |
| 164 | std::ostream& |
| 165 | operator<<(std::ostream& os, const FaceLogHelper<LpReassembler>& flh) |
| 166 | { |
| 167 | if (flh.obj.getLinkService() == nullptr) { |
| 168 | os << "[id=0,local=unknown,remote=unknown] "; |
| 169 | } |
| 170 | else { |
| 171 | os << FaceLogHelper<LinkService>(*flh.obj.getLinkService()); |
| 172 | } |
| 173 | return os; |
| 174 | } |
| 175 | |
| 176 | } // namespace face |
| 177 | } // namespace nfd |