blob: e5fc8f3f8b06dfcf5faea400535218f094b86290 [file] [log] [blame]
Eric Newberry261dbc22015-07-22 23:18:18 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventob6355e82017-08-10 21:27:08 -04002/*
Davide Pesavento570b20d2018-07-15 21:53:14 -04003 * Copyright (c) 2013-2018 Regents of the University of California.
Eric Newberry261dbc22015-07-22 23:18:18 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6 *
7 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20 */
21
22#include "packet.hpp"
Davide Pesaventob6355e82017-08-10 21:27:08 -040023#include "fields.hpp"
Eric Newberry261dbc22015-07-22 23:18:18 -070024
Davide Pesaventob6355e82017-08-10 21:27:08 -040025#include <boost/bind.hpp>
26#include <boost/mpl/for_each.hpp>
Eric Newberry261dbc22015-07-22 23:18:18 -070027#include <boost/range/adaptor/reversed.hpp>
28
29namespace ndn {
30namespace lp {
31
Davide Pesaventob6355e82017-08-10 21:27:08 -040032namespace {
33
34template<typename TAG>
35int
Davide Pesavento570b20d2018-07-15 21:53:14 -040036getLocationSortOrder() noexcept;
Davide Pesaventob6355e82017-08-10 21:27:08 -040037
38template<>
Davide Pesavento570b20d2018-07-15 21:53:14 -040039constexpr int
40getLocationSortOrder<field_location_tags::Header>() noexcept
Davide Pesaventob6355e82017-08-10 21:27:08 -040041{
42 return 1;
43}
44
45template<>
Davide Pesavento570b20d2018-07-15 21:53:14 -040046constexpr int
47getLocationSortOrder<field_location_tags::Fragment>() noexcept
Davide Pesaventob6355e82017-08-10 21:27:08 -040048{
49 return 2;
50}
51
52class FieldInfo
53{
54public:
Davide Pesavento570b20d2018-07-15 21:53:14 -040055 constexpr
56 FieldInfo() noexcept = default;
Davide Pesaventob6355e82017-08-10 21:27:08 -040057
58 explicit
Davide Pesavento570b20d2018-07-15 21:53:14 -040059 FieldInfo(uint64_t tlv) noexcept;
Davide Pesaventob6355e82017-08-10 21:27:08 -040060
61public:
Davide Pesavento570b20d2018-07-15 21:53:14 -040062 uint64_t tlvType = 0; ///< TLV-TYPE of the field; 0 if field does not exist
63 bool isRecognized = false; ///< is this field known
64 bool canIgnore = false; ///< can this unknown field be ignored
65 bool isRepeatable = false; ///< is the field repeatable
66 int locationSortOrder = getLocationSortOrder<field_location_tags::Header>(); ///< sort order of field_location_tag
Davide Pesaventob6355e82017-08-10 21:27:08 -040067};
68
69class ExtractFieldInfo
70{
71public:
72 using result_type = void;
73
74 template<typename T>
Davide Pesavento570b20d2018-07-15 21:53:14 -040075 constexpr void
76 operator()(FieldInfo* info, const T&) const noexcept
Davide Pesaventob6355e82017-08-10 21:27:08 -040077 {
78 if (T::TlvType::value != info->tlvType) {
79 return;
80 }
81 info->isRecognized = true;
82 info->canIgnore = false;
83 info->isRepeatable = T::IsRepeatable::value;
84 info->locationSortOrder = getLocationSortOrder<typename T::FieldLocation>();
85 }
86};
87
Davide Pesavento570b20d2018-07-15 21:53:14 -040088FieldInfo::FieldInfo(uint64_t tlv) noexcept
Davide Pesaventob6355e82017-08-10 21:27:08 -040089 : tlvType(tlv)
Davide Pesaventob6355e82017-08-10 21:27:08 -040090{
91 boost::mpl::for_each<FieldSet>(boost::bind(ExtractFieldInfo(), this, _1));
92 if (!isRecognized) {
93 canIgnore = tlv::HEADER3_MIN <= tlvType &&
94 tlvType <= tlv::HEADER3_MAX &&
95 (tlvType & 0x03) == 0x00;
96 }
97}
98
Davide Pesavento570b20d2018-07-15 21:53:14 -040099constexpr bool
100compareFieldSortOrder(const FieldInfo& first, const FieldInfo& second) noexcept
Davide Pesaventob6355e82017-08-10 21:27:08 -0400101{
102 return (first.locationSortOrder < second.locationSortOrder) ||
103 (first.locationSortOrder == second.locationSortOrder && first.tlvType < second.tlvType);
104}
105
106} // namespace
107
Eric Newberry261dbc22015-07-22 23:18:18 -0700108Packet::Packet()
109 : m_wire(Block(tlv::LpPacket))
110{
111}
112
113Packet::Packet(const Block& wire)
114{
115 wireDecode(wire);
116}
117
Eric Newberry82838be2015-11-07 13:00:35 -0700118Block
Eric Newberry261dbc22015-07-22 23:18:18 -0700119Packet::wireEncode() const
120{
Eric Newberry83872fd2015-08-06 17:01:24 -0700121 // If no header or trailer, return bare network packet
122 Block::element_container elements = m_wire.elements();
123 if (elements.size() == 1 && elements.front().type() == FragmentField::TlvType::value) {
124 elements.front().parse();
Eric Newberry83872fd2015-08-06 17:01:24 -0700125 return elements.front().elements().front();
126 }
127
Eric Newberry2a890772017-06-26 12:06:15 -0700128 m_wire.encode();
Eric Newberry261dbc22015-07-22 23:18:18 -0700129 return m_wire;
130}
131
132void
133Packet::wireDecode(const Block& wire)
134{
135 if (wire.type() == ndn::tlv::Interest || wire.type() == ndn::tlv::Data) {
136 m_wire = Block(tlv::LpPacket);
137 add<FragmentField>(make_pair(wire.begin(), wire.end()));
138 return;
139 }
140
Eric Newberry43bf6bb2015-10-09 16:12:09 -0700141 if (wire.type() != tlv::LpPacket) {
142 BOOST_THROW_EXCEPTION(Error("unrecognized TLV-TYPE " + to_string(wire.type())));
143 }
144
Eric Newberry261dbc22015-07-22 23:18:18 -0700145 wire.parse();
146
147 bool isFirst = true;
Alexander Afanasyeve3874232017-03-26 16:58:59 -0500148 FieldInfo prev;
Eric Newberry261dbc22015-07-22 23:18:18 -0700149 for (const Block& element : wire.elements()) {
Alexander Afanasyeve3874232017-03-26 16:58:59 -0500150 FieldInfo info(element.type());
Eric Newberry261dbc22015-07-22 23:18:18 -0700151
152 if (!info.isRecognized && !info.canIgnore) {
Eric Newberry3ed62472016-12-11 22:11:38 -0700153 BOOST_THROW_EXCEPTION(Error("unrecognized field " + to_string(element.type()) + " cannot be ignored"));
Eric Newberry261dbc22015-07-22 23:18:18 -0700154 }
155
156 if (!isFirst) {
157 if (info.tlvType == prev.tlvType && !info.isRepeatable) {
Eric Newberry3ed62472016-12-11 22:11:38 -0700158 BOOST_THROW_EXCEPTION(Error("non-repeatable field " + to_string(element.type()) + " cannot be repeated"));
Eric Newberry261dbc22015-07-22 23:18:18 -0700159 }
160
Alexander Afanasyeve3874232017-03-26 16:58:59 -0500161 else if (info.tlvType != prev.tlvType && !compareFieldSortOrder(prev, info)) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700162 BOOST_THROW_EXCEPTION(Error("fields are not in correct sort order"));
Eric Newberry261dbc22015-07-22 23:18:18 -0700163 }
164 }
165
166 isFirst = false;
167 prev = info;
168 }
169
170 m_wire = wire;
171}
172
173bool
Davide Pesavento570b20d2018-07-15 21:53:14 -0400174Packet::comparePos(uint64_t first, const Block& second) noexcept
Eric Newberry261dbc22015-07-22 23:18:18 -0700175{
Alexander Afanasyeve3874232017-03-26 16:58:59 -0500176 FieldInfo firstInfo(first);
177 FieldInfo secondInfo(second.type());
178 return compareFieldSortOrder(firstInfo, secondInfo);
Eric Newberry261dbc22015-07-22 23:18:18 -0700179}
180
181} // namespace lp
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700182} // namespace ndn