Junxiao Shi | d6dcd2c | 2014-02-16 14:49:54 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (C) 2014 Named Data Networking Project |
| 4 | * See COPYING for copyright and distribution information. |
| 5 | */ |
| 6 | |
| 7 | #include "ndnlp-parse.hpp" |
| 8 | |
| 9 | namespace nfd { |
| 10 | namespace ndnlp { |
| 11 | |
| 12 | void |
| 13 | NdnlpData::wireDecode(const Block& wire) |
| 14 | { |
| 15 | if (wire.type() != tlv::NdnlpData) { |
| 16 | throw ParseError("top element is not NdnlpData"); |
| 17 | } |
| 18 | wire.parse(); |
| 19 | const Block::element_container& elements = wire.elements(); |
| 20 | if (elements.size() < 2) { |
| 21 | throw ParseError("NdnlpData element has incorrect number of children"); |
| 22 | } |
| 23 | |
| 24 | const Block& sequenceElement = elements.front(); |
| 25 | if (sequenceElement.type() != tlv::NdnlpSequence) { |
| 26 | throw ParseError("NdnlpSequence element is missing"); |
| 27 | } |
| 28 | if (sequenceElement.value_size() != sizeof(uint64_t)) { |
| 29 | throw ParseError("NdnlpSequence element has incorrect length"); |
| 30 | } |
| 31 | m_seq = be64toh(*reinterpret_cast<const uint64_t*>(&*sequenceElement.value_begin())); |
| 32 | |
| 33 | const Block& payloadElement = elements.back(); |
| 34 | if (payloadElement.type() != tlv::NdnlpPayload) { |
| 35 | throw ParseError("NdnlpPayload element is missing"); |
| 36 | } |
| 37 | m_payload = payloadElement; |
| 38 | |
| 39 | if (elements.size() == 2) { // single wire packet |
| 40 | m_fragIndex = 0; |
| 41 | m_fragCount = 1; |
| 42 | return; |
| 43 | } |
| 44 | if (elements.size() != 4) { |
| 45 | throw ParseError("NdnlpData element has incorrect number of children"); |
| 46 | } |
| 47 | |
| 48 | const Block& fragIndexElement = elements.at(1); |
| 49 | if (fragIndexElement.type() != tlv::NdnlpFragIndex) { |
| 50 | throw ParseError("NdnlpFragIndex element is missing"); |
| 51 | } |
| 52 | if (fragIndexElement.value_size() != sizeof(uint16_t)) { |
| 53 | throw ParseError("NdnlpFragIndex element has incorrect length"); |
| 54 | } |
| 55 | m_fragIndex = be16toh(*reinterpret_cast<const uint16_t*>(&*fragIndexElement.value_begin())); |
| 56 | |
| 57 | const Block& fragCountElement = elements.at(2); |
| 58 | if (fragCountElement.type() != tlv::NdnlpFragCount) { |
| 59 | throw ParseError("NdnlpFragCount element is missing"); |
| 60 | } |
| 61 | if (fragCountElement.value_size() != sizeof(uint16_t)) { |
| 62 | throw ParseError("NdnlpFragCount element has incorrect length"); |
| 63 | } |
| 64 | m_fragCount = be16toh(*reinterpret_cast<const uint16_t*>(&*fragCountElement.value_begin())); |
| 65 | } |
| 66 | |
| 67 | } // namespace ndnlp |
| 68 | } // namespace nfd |