Eric Newberry | 261dbc2 | 2015-07-22 23:18:18 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2013-2015 Regents of the University of California. |
| 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" |
| 23 | #include "detail/field-info.hpp" |
| 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 | |
| 40 | template<encoding::Tag TAG> |
| 41 | size_t |
| 42 | Packet::wireEncode(EncodingImpl<TAG>& encoder) const |
| 43 | { |
| 44 | if (m_wire.hasWire()) { |
| 45 | return m_wire.size(); |
| 46 | } |
| 47 | |
| 48 | size_t length = 0; |
| 49 | |
| 50 | for (const Block& element : boost::adaptors::reverse(m_wire.elements())) { |
| 51 | length += encoder.prependBlock(element); |
| 52 | } |
| 53 | |
| 54 | length += encoder.prependVarNumber(length); |
| 55 | length += encoder.prependVarNumber(tlv::LpPacket); |
| 56 | |
| 57 | return length; |
| 58 | } |
| 59 | |
| 60 | template size_t |
| 61 | Packet::wireEncode<encoding::EncoderTag>(EncodingImpl<encoding::EncoderTag>& encoder) const; |
| 62 | |
| 63 | template size_t |
| 64 | Packet::wireEncode<encoding::EstimatorTag>(EncodingImpl<encoding::EstimatorTag>& encoder) const; |
| 65 | |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame^] | 66 | const Block |
Eric Newberry | 261dbc2 | 2015-07-22 23:18:18 -0700 | [diff] [blame] | 67 | Packet::wireEncode() const |
| 68 | { |
| 69 | if (m_wire.hasWire()) { |
| 70 | return m_wire; |
| 71 | } |
| 72 | |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame^] | 73 | // If no header or trailer, return bare network packet |
| 74 | Block::element_container elements = m_wire.elements(); |
| 75 | if (elements.size() == 1 && elements.front().type() == FragmentField::TlvType::value) { |
| 76 | elements.front().parse(); |
| 77 | elements.front().elements().front().parse(); |
| 78 | return elements.front().elements().front(); |
| 79 | } |
| 80 | |
Eric Newberry | 261dbc2 | 2015-07-22 23:18:18 -0700 | [diff] [blame] | 81 | EncodingEstimator estimator; |
| 82 | size_t estimatedSize = wireEncode(estimator); |
| 83 | |
| 84 | EncodingBuffer buffer(estimatedSize, 0); |
| 85 | wireEncode(buffer); |
| 86 | |
| 87 | m_wire = buffer.block(); |
| 88 | return m_wire; |
| 89 | } |
| 90 | |
| 91 | void |
| 92 | Packet::wireDecode(const Block& wire) |
| 93 | { |
| 94 | if (wire.type() == ndn::tlv::Interest || wire.type() == ndn::tlv::Data) { |
| 95 | m_wire = Block(tlv::LpPacket); |
| 96 | add<FragmentField>(make_pair(wire.begin(), wire.end())); |
| 97 | return; |
| 98 | } |
| 99 | |
| 100 | wire.parse(); |
| 101 | |
| 102 | bool isFirst = true; |
| 103 | detail::FieldInfo prev; |
| 104 | for (const Block& element : wire.elements()) { |
| 105 | detail::FieldInfo info(element.type()); |
| 106 | |
| 107 | if (!info.isRecognized && !info.canIgnore) { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 108 | BOOST_THROW_EXCEPTION(Error("unknown field cannot be ignored")); |
Eric Newberry | 261dbc2 | 2015-07-22 23:18:18 -0700 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | if (!isFirst) { |
| 112 | if (info.tlvType == prev.tlvType && !info.isRepeatable) { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 113 | BOOST_THROW_EXCEPTION(Error("non-repeatable field cannot be repeated")); |
Eric Newberry | 261dbc2 | 2015-07-22 23:18:18 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | else if (info.tlvType != prev.tlvType && !detail::compareFieldSortOrder(prev, info)) { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 117 | BOOST_THROW_EXCEPTION(Error("fields are not in correct sort order")); |
Eric Newberry | 261dbc2 | 2015-07-22 23:18:18 -0700 | [diff] [blame] | 118 | } |
| 119 | } |
| 120 | |
| 121 | isFirst = false; |
| 122 | prev = info; |
| 123 | } |
| 124 | |
| 125 | m_wire = wire; |
| 126 | } |
| 127 | |
| 128 | bool |
| 129 | Packet::comparePos(const Block& first, const uint64_t second) |
| 130 | { |
| 131 | detail::FieldInfo firstInfo(first.type()); |
| 132 | detail::FieldInfo secondInfo(second); |
| 133 | return detail::compareFieldSortOrder(firstInfo, secondInfo); |
| 134 | } |
| 135 | |
| 136 | } // namespace lp |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 137 | } // namespace ndn |