blob: a3062b6c269121cbc580b8c9b4e7f3e664db857d [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/*
Eric Newberry8422f572017-02-04 21:53:58 -07003 * Copyright (c) 2013-2017 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
36getLocationSortOrder();
37
38template<>
39int
40getLocationSortOrder<field_location_tags::Header>()
41{
42 return 1;
43}
44
45template<>
46int
47getLocationSortOrder<field_location_tags::Fragment>()
48{
49 return 2;
50}
51
52class FieldInfo
53{
54public:
55 FieldInfo();
56
57 explicit
58 FieldInfo(uint64_t tlv);
59
60public:
61 uint64_t tlvType; ///< TLV-TYPE of the field; 0 if field does not exist
62 bool isRecognized; ///< is this field known
63 bool canIgnore; ///< can this unknown field be ignored
64 bool isRepeatable; ///< is the field repeatable
65 int locationSortOrder; ///< sort order of field_location_tag
66};
67
68class ExtractFieldInfo
69{
70public:
71 using result_type = void;
72
73 template<typename T>
74 void
75 operator()(FieldInfo* info, T) const
76 {
77 if (T::TlvType::value != info->tlvType) {
78 return;
79 }
80 info->isRecognized = true;
81 info->canIgnore = false;
82 info->isRepeatable = T::IsRepeatable::value;
83 info->locationSortOrder = getLocationSortOrder<typename T::FieldLocation>();
84 }
85};
86
87FieldInfo::FieldInfo()
88 : tlvType(0)
89 , isRecognized(false)
90 , canIgnore(false)
91 , isRepeatable(false)
92 , locationSortOrder(getLocationSortOrder<field_location_tags::Header>())
93{
94}
95
96FieldInfo::FieldInfo(uint64_t tlv)
97 : tlvType(tlv)
98 , isRecognized(false)
99 , canIgnore(false)
100 , isRepeatable(false)
101 , locationSortOrder(getLocationSortOrder<field_location_tags::Header>())
102{
103 boost::mpl::for_each<FieldSet>(boost::bind(ExtractFieldInfo(), this, _1));
104 if (!isRecognized) {
105 canIgnore = tlv::HEADER3_MIN <= tlvType &&
106 tlvType <= tlv::HEADER3_MAX &&
107 (tlvType & 0x03) == 0x00;
108 }
109}
110
111bool
112compareFieldSortOrder(const FieldInfo& first, const FieldInfo& second)
113{
114 return (first.locationSortOrder < second.locationSortOrder) ||
115 (first.locationSortOrder == second.locationSortOrder && first.tlvType < second.tlvType);
116}
117
118} // namespace
119
Eric Newberry261dbc22015-07-22 23:18:18 -0700120Packet::Packet()
121 : m_wire(Block(tlv::LpPacket))
122{
123}
124
125Packet::Packet(const Block& wire)
126{
127 wireDecode(wire);
128}
129
Eric Newberry82838be2015-11-07 13:00:35 -0700130Block
Eric Newberry261dbc22015-07-22 23:18:18 -0700131Packet::wireEncode() const
132{
Eric Newberry83872fd2015-08-06 17:01:24 -0700133 // If no header or trailer, return bare network packet
134 Block::element_container elements = m_wire.elements();
135 if (elements.size() == 1 && elements.front().type() == FragmentField::TlvType::value) {
136 elements.front().parse();
Eric Newberry83872fd2015-08-06 17:01:24 -0700137 return elements.front().elements().front();
138 }
139
Eric Newberry2a890772017-06-26 12:06:15 -0700140 m_wire.encode();
Eric Newberry261dbc22015-07-22 23:18:18 -0700141 return m_wire;
142}
143
144void
145Packet::wireDecode(const Block& wire)
146{
147 if (wire.type() == ndn::tlv::Interest || wire.type() == ndn::tlv::Data) {
148 m_wire = Block(tlv::LpPacket);
149 add<FragmentField>(make_pair(wire.begin(), wire.end()));
150 return;
151 }
152
Eric Newberry43bf6bb2015-10-09 16:12:09 -0700153 if (wire.type() != tlv::LpPacket) {
154 BOOST_THROW_EXCEPTION(Error("unrecognized TLV-TYPE " + to_string(wire.type())));
155 }
156
Eric Newberry261dbc22015-07-22 23:18:18 -0700157 wire.parse();
158
159 bool isFirst = true;
Alexander Afanasyeve3874232017-03-26 16:58:59 -0500160 FieldInfo prev;
Eric Newberry261dbc22015-07-22 23:18:18 -0700161 for (const Block& element : wire.elements()) {
Alexander Afanasyeve3874232017-03-26 16:58:59 -0500162 FieldInfo info(element.type());
Eric Newberry261dbc22015-07-22 23:18:18 -0700163
164 if (!info.isRecognized && !info.canIgnore) {
Eric Newberry3ed62472016-12-11 22:11:38 -0700165 BOOST_THROW_EXCEPTION(Error("unrecognized field " + to_string(element.type()) + " cannot be ignored"));
Eric Newberry261dbc22015-07-22 23:18:18 -0700166 }
167
168 if (!isFirst) {
169 if (info.tlvType == prev.tlvType && !info.isRepeatable) {
Eric Newberry3ed62472016-12-11 22:11:38 -0700170 BOOST_THROW_EXCEPTION(Error("non-repeatable field " + to_string(element.type()) + " cannot be repeated"));
Eric Newberry261dbc22015-07-22 23:18:18 -0700171 }
172
Alexander Afanasyeve3874232017-03-26 16:58:59 -0500173 else if (info.tlvType != prev.tlvType && !compareFieldSortOrder(prev, info)) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700174 BOOST_THROW_EXCEPTION(Error("fields are not in correct sort order"));
Eric Newberry261dbc22015-07-22 23:18:18 -0700175 }
176 }
177
178 isFirst = false;
179 prev = info;
180 }
181
182 m_wire = wire;
183}
184
185bool
Eric Newberry8422f572017-02-04 21:53:58 -0700186Packet::comparePos(uint64_t first, const Block& second)
Eric Newberry261dbc22015-07-22 23:18:18 -0700187{
Alexander Afanasyeve3874232017-03-26 16:58:59 -0500188 FieldInfo firstInfo(first);
189 FieldInfo secondInfo(second.type());
190 return compareFieldSortOrder(firstInfo, secondInfo);
Eric Newberry261dbc22015-07-22 23:18:18 -0700191}
192
193} // namespace lp
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700194} // namespace ndn