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