Eric Newberry | 261dbc2 | 2015-07-22 23:18:18 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | b6355e8 | 2017-08-10 21:27:08 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | 570b20d | 2018-07-15 21:53:14 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2018 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" |
Davide Pesavento | b6355e8 | 2017-08-10 21:27:08 -0400 | [diff] [blame] | 23 | #include "fields.hpp" |
Eric Newberry | 261dbc2 | 2015-07-22 23:18:18 -0700 | [diff] [blame] | 24 | |
Davide Pesavento | b6355e8 | 2017-08-10 21:27:08 -0400 | [diff] [blame] | 25 | #include <boost/bind.hpp> |
| 26 | #include <boost/mpl/for_each.hpp> |
Eric Newberry | 261dbc2 | 2015-07-22 23:18:18 -0700 | [diff] [blame] | 27 | #include <boost/range/adaptor/reversed.hpp> |
| 28 | |
| 29 | namespace ndn { |
| 30 | namespace lp { |
| 31 | |
Davide Pesavento | b6355e8 | 2017-08-10 21:27:08 -0400 | [diff] [blame] | 32 | namespace { |
| 33 | |
| 34 | template<typename TAG> |
| 35 | int |
Davide Pesavento | 570b20d | 2018-07-15 21:53:14 -0400 | [diff] [blame] | 36 | getLocationSortOrder() noexcept; |
Davide Pesavento | b6355e8 | 2017-08-10 21:27:08 -0400 | [diff] [blame] | 37 | |
| 38 | template<> |
Davide Pesavento | 570b20d | 2018-07-15 21:53:14 -0400 | [diff] [blame] | 39 | constexpr int |
| 40 | getLocationSortOrder<field_location_tags::Header>() noexcept |
Davide Pesavento | b6355e8 | 2017-08-10 21:27:08 -0400 | [diff] [blame] | 41 | { |
| 42 | return 1; |
| 43 | } |
| 44 | |
| 45 | template<> |
Davide Pesavento | 570b20d | 2018-07-15 21:53:14 -0400 | [diff] [blame] | 46 | constexpr int |
| 47 | getLocationSortOrder<field_location_tags::Fragment>() noexcept |
Davide Pesavento | b6355e8 | 2017-08-10 21:27:08 -0400 | [diff] [blame] | 48 | { |
| 49 | return 2; |
| 50 | } |
| 51 | |
| 52 | class FieldInfo |
| 53 | { |
| 54 | public: |
Davide Pesavento | 570b20d | 2018-07-15 21:53:14 -0400 | [diff] [blame] | 55 | constexpr |
| 56 | FieldInfo() noexcept = default; |
Davide Pesavento | b6355e8 | 2017-08-10 21:27:08 -0400 | [diff] [blame] | 57 | |
| 58 | explicit |
Davide Pesavento | 570b20d | 2018-07-15 21:53:14 -0400 | [diff] [blame] | 59 | FieldInfo(uint64_t tlv) noexcept; |
Davide Pesavento | b6355e8 | 2017-08-10 21:27:08 -0400 | [diff] [blame] | 60 | |
| 61 | public: |
Davide Pesavento | 570b20d | 2018-07-15 21:53:14 -0400 | [diff] [blame] | 62 | 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 Pesavento | b6355e8 | 2017-08-10 21:27:08 -0400 | [diff] [blame] | 67 | }; |
| 68 | |
| 69 | class ExtractFieldInfo |
| 70 | { |
| 71 | public: |
| 72 | using result_type = void; |
| 73 | |
| 74 | template<typename T> |
Davide Pesavento | 570b20d | 2018-07-15 21:53:14 -0400 | [diff] [blame] | 75 | constexpr void |
| 76 | operator()(FieldInfo* info, const T&) const noexcept |
Davide Pesavento | b6355e8 | 2017-08-10 21:27:08 -0400 | [diff] [blame] | 77 | { |
| 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 Pesavento | 570b20d | 2018-07-15 21:53:14 -0400 | [diff] [blame] | 88 | FieldInfo::FieldInfo(uint64_t tlv) noexcept |
Davide Pesavento | b6355e8 | 2017-08-10 21:27:08 -0400 | [diff] [blame] | 89 | : tlvType(tlv) |
Davide Pesavento | b6355e8 | 2017-08-10 21:27:08 -0400 | [diff] [blame] | 90 | { |
| 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 Pesavento | 570b20d | 2018-07-15 21:53:14 -0400 | [diff] [blame] | 99 | constexpr bool |
| 100 | compareFieldSortOrder(const FieldInfo& first, const FieldInfo& second) noexcept |
Davide Pesavento | b6355e8 | 2017-08-10 21:27:08 -0400 | [diff] [blame] | 101 | { |
| 102 | return (first.locationSortOrder < second.locationSortOrder) || |
| 103 | (first.locationSortOrder == second.locationSortOrder && first.tlvType < second.tlvType); |
| 104 | } |
| 105 | |
| 106 | } // namespace |
| 107 | |
Eric Newberry | 261dbc2 | 2015-07-22 23:18:18 -0700 | [diff] [blame] | 108 | Packet::Packet() |
| 109 | : m_wire(Block(tlv::LpPacket)) |
| 110 | { |
| 111 | } |
| 112 | |
| 113 | Packet::Packet(const Block& wire) |
| 114 | { |
| 115 | wireDecode(wire); |
| 116 | } |
| 117 | |
Eric Newberry | 82838be | 2015-11-07 13:00:35 -0700 | [diff] [blame] | 118 | Block |
Eric Newberry | 261dbc2 | 2015-07-22 23:18:18 -0700 | [diff] [blame] | 119 | Packet::wireEncode() const |
| 120 | { |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 121 | // 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 Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 125 | return elements.front().elements().front(); |
| 126 | } |
| 127 | |
Eric Newberry | 2a89077 | 2017-06-26 12:06:15 -0700 | [diff] [blame] | 128 | m_wire.encode(); |
Eric Newberry | 261dbc2 | 2015-07-22 23:18:18 -0700 | [diff] [blame] | 129 | return m_wire; |
| 130 | } |
| 131 | |
| 132 | void |
| 133 | Packet::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 Newberry | 43bf6bb | 2015-10-09 16:12:09 -0700 | [diff] [blame] | 141 | if (wire.type() != tlv::LpPacket) { |
| 142 | BOOST_THROW_EXCEPTION(Error("unrecognized TLV-TYPE " + to_string(wire.type()))); |
| 143 | } |
| 144 | |
Eric Newberry | 261dbc2 | 2015-07-22 23:18:18 -0700 | [diff] [blame] | 145 | wire.parse(); |
| 146 | |
| 147 | bool isFirst = true; |
Alexander Afanasyev | e387423 | 2017-03-26 16:58:59 -0500 | [diff] [blame] | 148 | FieldInfo prev; |
Eric Newberry | 261dbc2 | 2015-07-22 23:18:18 -0700 | [diff] [blame] | 149 | for (const Block& element : wire.elements()) { |
Alexander Afanasyev | e387423 | 2017-03-26 16:58:59 -0500 | [diff] [blame] | 150 | FieldInfo info(element.type()); |
Eric Newberry | 261dbc2 | 2015-07-22 23:18:18 -0700 | [diff] [blame] | 151 | |
| 152 | if (!info.isRecognized && !info.canIgnore) { |
Eric Newberry | 3ed6247 | 2016-12-11 22:11:38 -0700 | [diff] [blame] | 153 | 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] | 154 | } |
| 155 | |
| 156 | if (!isFirst) { |
| 157 | if (info.tlvType == prev.tlvType && !info.isRepeatable) { |
Eric Newberry | 3ed6247 | 2016-12-11 22:11:38 -0700 | [diff] [blame] | 158 | 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] | 159 | } |
| 160 | |
Alexander Afanasyev | e387423 | 2017-03-26 16:58:59 -0500 | [diff] [blame] | 161 | else if (info.tlvType != prev.tlvType && !compareFieldSortOrder(prev, info)) { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 162 | BOOST_THROW_EXCEPTION(Error("fields are not in correct sort order")); |
Eric Newberry | 261dbc2 | 2015-07-22 23:18:18 -0700 | [diff] [blame] | 163 | } |
| 164 | } |
| 165 | |
| 166 | isFirst = false; |
| 167 | prev = info; |
| 168 | } |
| 169 | |
| 170 | m_wire = wire; |
| 171 | } |
| 172 | |
| 173 | bool |
Davide Pesavento | 570b20d | 2018-07-15 21:53:14 -0400 | [diff] [blame] | 174 | Packet::comparePos(uint64_t first, const Block& second) noexcept |
Eric Newberry | 261dbc2 | 2015-07-22 23:18:18 -0700 | [diff] [blame] | 175 | { |
Alexander Afanasyev | e387423 | 2017-03-26 16:58:59 -0500 | [diff] [blame] | 176 | FieldInfo firstInfo(first); |
| 177 | FieldInfo secondInfo(second.type()); |
| 178 | return compareFieldSortOrder(firstInfo, secondInfo); |
Eric Newberry | 261dbc2 | 2015-07-22 23:18:18 -0700 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | } // namespace lp |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 182 | } // namespace ndn |