NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.0: NDN, CCN, CCNx, content centric networks
API Documentation
ndnlp-data.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
26 #include "ndnlp-data.hpp"
27 
28 namespace nfd {
29 namespace ndnlp {
30 
31 std::tuple<bool, NdnlpData>
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
represents a NdnlpData packet
Definition: ndnlp-data.hpp:43
const element_container & elements() const
Get all subelements.
Definition: block.hpp:364
Class representing a wire element of NDN-TLV packet format.
Definition: block.hpp:43
static std::tuple< bool, NdnlpData > fromBlock(const Block &wire)
parse a NdnlpData packet
Definition: ndnlp-data.cpp:32
Buffer::const_iterator value_begin() const
Definition: block.hpp:352
uint64_t readNonNegativeInteger(const Block &block)
Helper to read a non-negative integer from a block.
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:38
size_t value_size() const
Definition: block.cpp:529
void parse() const
Parse wire buffer into subblocks.
Definition: block.cpp:322
std::vector< Block > element_container
Definition: block.hpp:46
uint32_t type() const
Definition: block.hpp:346