blob: 21d959fd76806826fe1564e412c1834825f27935 [file] [log] [blame]
Junxiao Shid6dcd2c2014-02-16 14:49:54 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -07003 * 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 Shid6dcd2c2014-02-16 14:49:54 -070024
25#include "ndnlp-parse.hpp"
26
27namespace nfd {
28namespace ndnlp {
29
30void
31NdnlpData::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 Shidf3b4382014-02-23 11:28:21 -070041
Junxiao Shid6dcd2c2014-02-16 14:49:54 -070042 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 Shidf3b4382014-02-23 11:28:21 -070050
Junxiao Shid6dcd2c2014-02-16 14:49:54 -070051 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 Shidf3b4382014-02-23 11:28:21 -070065
Junxiao Shid6dcd2c2014-02-16 14:49:54 -070066 const Block& fragIndexElement = elements.at(1);
67 if (fragIndexElement.type() != tlv::NdnlpFragIndex) {
68 throw ParseError("NdnlpFragIndex element is missing");
69 }
Junxiao Shidf3b4382014-02-23 11:28:21 -070070 uint64_t fragIndex = ndn::readNonNegativeInteger(fragIndexElement);
71 if (fragIndex > std::numeric_limits<uint16_t>::max()) {
72 throw ParseError("NdnlpFragIndex is too large");
Junxiao Shid6dcd2c2014-02-16 14:49:54 -070073 }
Junxiao Shidf3b4382014-02-23 11:28:21 -070074 m_fragIndex = static_cast<uint16_t>(fragIndex);
75
Junxiao Shid6dcd2c2014-02-16 14:49:54 -070076 const Block& fragCountElement = elements.at(2);
77 if (fragCountElement.type() != tlv::NdnlpFragCount) {
78 throw ParseError("NdnlpFragCount element is missing");
79 }
Junxiao Shidf3b4382014-02-23 11:28:21 -070080 uint64_t fragCount = ndn::readNonNegativeInteger(fragCountElement);
81 if (fragCount > std::numeric_limits<uint16_t>::max()) {
82 throw ParseError("NdnlpFragCount is too large");
Junxiao Shid6dcd2c2014-02-16 14:49:54 -070083 }
Junxiao Shidf3b4382014-02-23 11:28:21 -070084 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 Shid6dcd2c2014-02-16 14:49:54 -070089}
90
91} // namespace ndnlp
92} // namespace nfd