Eric Newberry | 261dbc2 | 2015-07-22 23:18:18 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Eric Newberry | 8422f57 | 2017-02-04 21:53:58 -0700 | [diff] [blame] | 3 | * Copyright (c) 2013-2017 Regents of the University of California. |
Eric Newberry | 261dbc2 | 2015-07-22 23:18:18 -0700 | [diff] [blame] | 4 | * |
| 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 Afanasyev | e387423 | 2017-03-26 16:58:59 -0500 | [diff] [blame] | 23 | #include "field-info.hpp" |
Eric Newberry | 261dbc2 | 2015-07-22 23:18:18 -0700 | [diff] [blame] | 24 | |
| 25 | #include <boost/range/adaptor/reversed.hpp> |
| 26 | |
| 27 | namespace ndn { |
| 28 | namespace lp { |
| 29 | |
| 30 | Packet::Packet() |
| 31 | : m_wire(Block(tlv::LpPacket)) |
| 32 | { |
| 33 | } |
| 34 | |
| 35 | Packet::Packet(const Block& wire) |
| 36 | { |
| 37 | wireDecode(wire); |
| 38 | } |
| 39 | |
Eric Newberry | 82838be | 2015-11-07 13:00:35 -0700 | [diff] [blame] | 40 | Block |
Eric Newberry | 261dbc2 | 2015-07-22 23:18:18 -0700 | [diff] [blame] | 41 | Packet::wireEncode() const |
| 42 | { |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 43 | // 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 Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 47 | return elements.front().elements().front(); |
| 48 | } |
| 49 | |
Eric Newberry | 2a89077 | 2017-06-26 12:06:15 -0700 | [diff] [blame] | 50 | m_wire.encode(); |
Eric Newberry | 261dbc2 | 2015-07-22 23:18:18 -0700 | [diff] [blame] | 51 | return m_wire; |
| 52 | } |
| 53 | |
| 54 | void |
| 55 | Packet::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 Newberry | 43bf6bb | 2015-10-09 16:12:09 -0700 | [diff] [blame] | 63 | if (wire.type() != tlv::LpPacket) { |
| 64 | BOOST_THROW_EXCEPTION(Error("unrecognized TLV-TYPE " + to_string(wire.type()))); |
| 65 | } |
| 66 | |
Eric Newberry | 261dbc2 | 2015-07-22 23:18:18 -0700 | [diff] [blame] | 67 | wire.parse(); |
| 68 | |
| 69 | bool isFirst = true; |
Alexander Afanasyev | e387423 | 2017-03-26 16:58:59 -0500 | [diff] [blame] | 70 | FieldInfo prev; |
Eric Newberry | 261dbc2 | 2015-07-22 23:18:18 -0700 | [diff] [blame] | 71 | for (const Block& element : wire.elements()) { |
Alexander Afanasyev | e387423 | 2017-03-26 16:58:59 -0500 | [diff] [blame] | 72 | FieldInfo info(element.type()); |
Eric Newberry | 261dbc2 | 2015-07-22 23:18:18 -0700 | [diff] [blame] | 73 | |
| 74 | if (!info.isRecognized && !info.canIgnore) { |
Eric Newberry | 3ed6247 | 2016-12-11 22:11:38 -0700 | [diff] [blame] | 75 | BOOST_THROW_EXCEPTION(Error("unrecognized field " + to_string(element.type()) + " cannot be ignored")); |
Eric Newberry | 261dbc2 | 2015-07-22 23:18:18 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | if (!isFirst) { |
| 79 | if (info.tlvType == prev.tlvType && !info.isRepeatable) { |
Eric Newberry | 3ed6247 | 2016-12-11 22:11:38 -0700 | [diff] [blame] | 80 | BOOST_THROW_EXCEPTION(Error("non-repeatable field " + to_string(element.type()) + " cannot be repeated")); |
Eric Newberry | 261dbc2 | 2015-07-22 23:18:18 -0700 | [diff] [blame] | 81 | } |
| 82 | |
Alexander Afanasyev | e387423 | 2017-03-26 16:58:59 -0500 | [diff] [blame] | 83 | else if (info.tlvType != prev.tlvType && !compareFieldSortOrder(prev, info)) { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 84 | BOOST_THROW_EXCEPTION(Error("fields are not in correct sort order")); |
Eric Newberry | 261dbc2 | 2015-07-22 23:18:18 -0700 | [diff] [blame] | 85 | } |
| 86 | } |
| 87 | |
| 88 | isFirst = false; |
| 89 | prev = info; |
| 90 | } |
| 91 | |
| 92 | m_wire = wire; |
| 93 | } |
| 94 | |
| 95 | bool |
Eric Newberry | 8422f57 | 2017-02-04 21:53:58 -0700 | [diff] [blame] | 96 | Packet::comparePos(uint64_t first, const Block& second) |
Eric Newberry | 261dbc2 | 2015-07-22 23:18:18 -0700 | [diff] [blame] | 97 | { |
Alexander Afanasyev | e387423 | 2017-03-26 16:58:59 -0500 | [diff] [blame] | 98 | FieldInfo firstInfo(first); |
| 99 | FieldInfo secondInfo(second.type()); |
| 100 | return compareFieldSortOrder(firstInfo, secondInfo); |
Eric Newberry | 261dbc2 | 2015-07-22 23:18:18 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | } // namespace lp |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 104 | } // namespace ndn |