blob: fd0d87405307b941b1182e1fb833b4e59bf65287 [file] [log] [blame]
Eric Newberry4c3e6b82015-11-10 16:48:42 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014-2015, 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
26#ifndef NFD_DAEMON_FACE_LP_REASSEMBLER_HPP
27#define NFD_DAEMON_FACE_LP_REASSEMBLER_HPP
28
29#include "core/scheduler.hpp"
30#include "face-log.hpp"
31#include "transport.hpp"
32
33#include <ndn-cxx/lp/packet.hpp>
34
35namespace nfd {
36namespace face {
37
38class LinkService;
39
40/** \brief reassembles fragmented network-layer packets
41 * \sa http://redmine.named-data.net/projects/nfd/wiki/NDNLPv2
42 */
43class LpReassembler : noncopyable
44{
45public:
46 /** \brief Options that control the behavior of LpReassembler
47 */
48 class Options
49 {
50 public:
51 Options();
52
53 public:
54 /** \brief maximum number of fragments in a packet
55 *
56 * LpPackets with FragCount over this limit are dropped.
57 */
58 size_t nMaxFragments;
59
60 /** \brief timeout before a partially reassembled packet is dropped
61 */
62 time::nanoseconds reassemblyTimeout;
63 };
64
65 explicit
66 LpReassembler(const Options& options = Options(), const LinkService* linkService = nullptr);
67
68 /** \brief set options for reassembler
69 */
70 void
71 setOptions(const Options& options);
72
73 /** \return LinkService that owns this instance
74 *
75 * This is only used for logging, and may be nullptr.
76 */
77 const LinkService*
78 getLinkService() const;
79
80 /** \brief adds received fragment to buffer
81 * \param remoteEndpoint endpoint whose sends the packet
82 * \param packet received fragment;
83 * must have Fragment field
84 * \return whether network-layer packet has been completely received,
85 * the reassembled network-layer packet,
86 * and the first fragment for inspecting other NDNLPv2 headers
87 * \throw tlv::Error packet is malformed
88 */
89 std::tuple<bool, Block, lp::Packet>
90 receiveFragment(Transport::EndpointId remoteEndpoint, const lp::Packet& packet);
91
92 /** \brief count of partial packets
93 */
94 size_t
95 size() const;
96
97 /** \brief signals before a partial packet is dropped due to timeout
98 *
99 * If a partial packet is incomplete and no new fragment is received
100 * within Options::reassemblyTimeout, it would be dropped due to timeout.
101 * Before it's erased, this signal is emitted with the remote endpoint,
102 * and the number of fragments being dropped.
103 */
104 signal::Signal<LpReassembler, Transport::EndpointId, size_t> beforeTimeout;
105
106private:
107 /** \brief holds all fragments of packet until reassembled
108 */
109 struct PartialPacket
110 {
111 std::vector<lp::Packet> fragments;
112 size_t fragCount; ///< total fragments
113 size_t nReceivedFragments; ///< number of received fragments
114 scheduler::ScopedEventId dropTimer;
115 };
116
117 /** \brief index key for PartialPackets
118 */
119 typedef std::tuple<
120 Transport::EndpointId, // remoteEndpoint
121 lp::Sequence // message identifier (sequence of the first fragment)
122 > Key;
123
124 Block
125 doReassembly(const Key& key);
126
127 void
128 timeoutPartialPacket(const Key& key);
129
130private:
131 Options m_options;
132 std::map<Key, PartialPacket> m_partialPackets;
133 const LinkService* m_linkService;
134};
135
136std::ostream&
137operator<<(std::ostream& os, const FaceLogHelper<LpReassembler>& flh);
138
139inline void
140LpReassembler::setOptions(const Options& options)
141{
142 m_options = options;
143}
144
145inline const LinkService*
146LpReassembler::getLinkService() const
147{
148 return m_linkService;
149}
150
151inline size_t
152LpReassembler::size() const
153{
154 return m_partialPackets.size();
155}
156
157} // namespace face
158} // namespace nfd
159
160#endif // NFD_DAEMON_FACE_LP_REASSEMBLER_HPP