blob: 5714547ff3a15cf93c4d403b0d168171d8daee9e [file] [log] [blame]
Eric Newberry261dbc22015-07-22 23:18:18 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
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"
Alexander Afanasyeve3874232017-03-26 16:58:59 -050023#include "field-info.hpp"
Eric Newberry261dbc22015-07-22 23:18:18 -070024
25#include <boost/range/adaptor/reversed.hpp>
26
27namespace ndn {
28namespace lp {
29
30Packet::Packet()
31 : m_wire(Block(tlv::LpPacket))
32{
33}
34
35Packet::Packet(const Block& wire)
36{
37 wireDecode(wire);
38}
39
Eric Newberry82838be2015-11-07 13:00:35 -070040Block
Eric Newberry261dbc22015-07-22 23:18:18 -070041Packet::wireEncode() const
42{
Eric Newberry83872fd2015-08-06 17:01:24 -070043 // If no header or trailer, return bare network packet
44 Block::element_container elements = m_wire.elements();
45 if (elements.size() == 1 && elements.front().type() == FragmentField::TlvType::value) {
46 elements.front().parse();
Eric Newberry83872fd2015-08-06 17:01:24 -070047 return elements.front().elements().front();
48 }
49
Eric Newberry2a890772017-06-26 12:06:15 -070050 m_wire.encode();
Eric Newberry261dbc22015-07-22 23:18:18 -070051 return m_wire;
52}
53
54void
55Packet::wireDecode(const Block& wire)
56{
57 if (wire.type() == ndn::tlv::Interest || wire.type() == ndn::tlv::Data) {
58 m_wire = Block(tlv::LpPacket);
59 add<FragmentField>(make_pair(wire.begin(), wire.end()));
60 return;
61 }
62
Eric Newberry43bf6bb2015-10-09 16:12:09 -070063 if (wire.type() != tlv::LpPacket) {
64 BOOST_THROW_EXCEPTION(Error("unrecognized TLV-TYPE " + to_string(wire.type())));
65 }
66
Eric Newberry261dbc22015-07-22 23:18:18 -070067 wire.parse();
68
69 bool isFirst = true;
Alexander Afanasyeve3874232017-03-26 16:58:59 -050070 FieldInfo prev;
Eric Newberry261dbc22015-07-22 23:18:18 -070071 for (const Block& element : wire.elements()) {
Alexander Afanasyeve3874232017-03-26 16:58:59 -050072 FieldInfo info(element.type());
Eric Newberry261dbc22015-07-22 23:18:18 -070073
74 if (!info.isRecognized && !info.canIgnore) {
Eric Newberry3ed62472016-12-11 22:11:38 -070075 BOOST_THROW_EXCEPTION(Error("unrecognized field " + to_string(element.type()) + " cannot be ignored"));
Eric Newberry261dbc22015-07-22 23:18:18 -070076 }
77
78 if (!isFirst) {
79 if (info.tlvType == prev.tlvType && !info.isRepeatable) {
Eric Newberry3ed62472016-12-11 22:11:38 -070080 BOOST_THROW_EXCEPTION(Error("non-repeatable field " + to_string(element.type()) + " cannot be repeated"));
Eric Newberry261dbc22015-07-22 23:18:18 -070081 }
82
Alexander Afanasyeve3874232017-03-26 16:58:59 -050083 else if (info.tlvType != prev.tlvType && !compareFieldSortOrder(prev, info)) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -070084 BOOST_THROW_EXCEPTION(Error("fields are not in correct sort order"));
Eric Newberry261dbc22015-07-22 23:18:18 -070085 }
86 }
87
88 isFirst = false;
89 prev = info;
90 }
91
92 m_wire = wire;
93}
94
95bool
Eric Newberry8422f572017-02-04 21:53:58 -070096Packet::comparePos(uint64_t first, const Block& second)
Eric Newberry261dbc22015-07-22 23:18:18 -070097{
Alexander Afanasyeve3874232017-03-26 16:58:59 -050098 FieldInfo firstInfo(first);
99 FieldInfo secondInfo(second.type());
100 return compareFieldSortOrder(firstInfo, secondInfo);
Eric Newberry261dbc22015-07-22 23:18:18 -0700101}
102
103} // namespace lp
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700104} // namespace ndn