Junxiao Shi | d6dcd2c | 2014-02-16 14:49:54 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014 Regents of the University of California, |
| 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology |
| 9 | * |
| 10 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 11 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 12 | * |
| 13 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 14 | * of the GNU General Public License as published by the Free Software Foundation, |
| 15 | * either version 3 of the License, or (at your option) any later version. |
| 16 | * |
| 17 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 18 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 19 | * PURPOSE. See the GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License along with |
| 22 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 23 | **/ |
Junxiao Shi | d6dcd2c | 2014-02-16 14:49:54 -0700 | [diff] [blame] | 24 | |
| 25 | #include "ndnlp-parse.hpp" |
| 26 | |
| 27 | namespace nfd { |
| 28 | namespace ndnlp { |
| 29 | |
| 30 | void |
| 31 | NdnlpData::wireDecode(const Block& wire) |
| 32 | { |
| 33 | if (wire.type() != tlv::NdnlpData) { |
| 34 | throw ParseError("top element is not NdnlpData"); |
| 35 | } |
| 36 | wire.parse(); |
| 37 | const Block::element_container& elements = wire.elements(); |
| 38 | if (elements.size() < 2) { |
| 39 | throw ParseError("NdnlpData element has incorrect number of children"); |
| 40 | } |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 41 | |
Junxiao Shi | d6dcd2c | 2014-02-16 14:49:54 -0700 | [diff] [blame] | 42 | const Block& sequenceElement = elements.front(); |
| 43 | if (sequenceElement.type() != tlv::NdnlpSequence) { |
| 44 | throw ParseError("NdnlpSequence element is missing"); |
| 45 | } |
| 46 | if (sequenceElement.value_size() != sizeof(uint64_t)) { |
| 47 | throw ParseError("NdnlpSequence element has incorrect length"); |
| 48 | } |
| 49 | m_seq = be64toh(*reinterpret_cast<const uint64_t*>(&*sequenceElement.value_begin())); |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 50 | |
Junxiao Shi | d6dcd2c | 2014-02-16 14:49:54 -0700 | [diff] [blame] | 51 | const Block& payloadElement = elements.back(); |
| 52 | if (payloadElement.type() != tlv::NdnlpPayload) { |
| 53 | throw ParseError("NdnlpPayload element is missing"); |
| 54 | } |
| 55 | m_payload = payloadElement; |
| 56 | |
| 57 | if (elements.size() == 2) { // single wire packet |
| 58 | m_fragIndex = 0; |
| 59 | m_fragCount = 1; |
| 60 | return; |
| 61 | } |
| 62 | if (elements.size() != 4) { |
| 63 | throw ParseError("NdnlpData element has incorrect number of children"); |
| 64 | } |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 65 | |
Junxiao Shi | d6dcd2c | 2014-02-16 14:49:54 -0700 | [diff] [blame] | 66 | const Block& fragIndexElement = elements.at(1); |
| 67 | if (fragIndexElement.type() != tlv::NdnlpFragIndex) { |
| 68 | throw ParseError("NdnlpFragIndex element is missing"); |
| 69 | } |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 70 | uint64_t fragIndex = ndn::readNonNegativeInteger(fragIndexElement); |
| 71 | if (fragIndex > std::numeric_limits<uint16_t>::max()) { |
| 72 | throw ParseError("NdnlpFragIndex is too large"); |
Junxiao Shi | d6dcd2c | 2014-02-16 14:49:54 -0700 | [diff] [blame] | 73 | } |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 74 | m_fragIndex = static_cast<uint16_t>(fragIndex); |
| 75 | |
Junxiao Shi | d6dcd2c | 2014-02-16 14:49:54 -0700 | [diff] [blame] | 76 | const Block& fragCountElement = elements.at(2); |
| 77 | if (fragCountElement.type() != tlv::NdnlpFragCount) { |
| 78 | throw ParseError("NdnlpFragCount element is missing"); |
| 79 | } |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 80 | uint64_t fragCount = ndn::readNonNegativeInteger(fragCountElement); |
| 81 | if (fragCount > std::numeric_limits<uint16_t>::max()) { |
| 82 | throw ParseError("NdnlpFragCount is too large"); |
Junxiao Shi | d6dcd2c | 2014-02-16 14:49:54 -0700 | [diff] [blame] | 83 | } |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 84 | m_fragCount = static_cast<uint16_t>(fragCount); |
| 85 | |
| 86 | if (m_fragIndex >= m_fragCount) { |
| 87 | throw ParseError("NdnlpFragIndex must be less than NdnlpFragCount"); |
| 88 | } |
Junxiao Shi | d6dcd2c | 2014-02-16 14:49:54 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | } // namespace ndnlp |
| 92 | } // namespace nfd |