NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.0: NDN, CCN, CCNx, content centric networks
API Documentation
packet.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
22 #include "packet.hpp"
23 #include "detail/field-info.hpp"
24 
25 #include <boost/range/adaptor/reversed.hpp>
26 
27 namespace ndn {
28 namespace lp {
29 
31  : m_wire(Block(tlv::LpPacket))
32 {
33 }
34 
35 Packet::Packet(const Block& wire)
36 {
37  wireDecode(wire);
38 }
39 
40 template<encoding::Tag TAG>
41 size_t
42 Packet::wireEncode(EncodingImpl<TAG>& encoder) const
43 {
44  if (m_wire.hasWire()) {
45  return m_wire.size();
46  }
47 
48  size_t length = 0;
49 
50  for (const Block& element : boost::adaptors::reverse(m_wire.elements())) {
51  length += encoder.prependBlock(element);
52  }
53 
54  length += encoder.prependVarNumber(length);
55  length += encoder.prependVarNumber(tlv::LpPacket);
56 
57  return length;
58 }
59 
60 template size_t
61 Packet::wireEncode<encoding::EncoderTag>(EncodingImpl<encoding::EncoderTag>& encoder) const;
62 
63 template size_t
64 Packet::wireEncode<encoding::EstimatorTag>(EncodingImpl<encoding::EstimatorTag>& encoder) const;
65 
66 const Block&
68 {
69  if (m_wire.hasWire()) {
70  return m_wire;
71  }
72 
73  EncodingEstimator estimator;
74  size_t estimatedSize = wireEncode(estimator);
75 
76  EncodingBuffer buffer(estimatedSize, 0);
77  wireEncode(buffer);
78 
79  m_wire = buffer.block();
80  return m_wire;
81 }
82 
83 void
85 {
86  if (wire.type() == ndn::tlv::Interest || wire.type() == ndn::tlv::Data) {
87  m_wire = Block(tlv::LpPacket);
88  add<FragmentField>(make_pair(wire.begin(), wire.end()));
89  return;
90  }
91 
92  wire.parse();
93 
94  bool isFirst = true;
95  detail::FieldInfo prev;
96  for (const Block& element : wire.elements()) {
97  detail::FieldInfo info(element.type());
98 
99  if (!info.isRecognized && !info.canIgnore) {
100  BOOST_THROW_EXCEPTION(Error("unknown field cannot be ignored"));
101  }
102 
103  if (!isFirst) {
104  if (info.tlvType == prev.tlvType && !info.isRepeatable) {
105  BOOST_THROW_EXCEPTION(Error("non-repeatable field cannot be repeated"));
106  }
107 
108  else if (info.tlvType != prev.tlvType && !detail::compareFieldSortOrder(prev, info)) {
109  BOOST_THROW_EXCEPTION(Error("fields are not in correct sort order"));
110  }
111  }
112 
113  isFirst = false;
114  prev = info;
115  }
116 
117  m_wire = wire;
118 }
119 
120 bool
121 Packet::comparePos(const Block& first, const uint64_t second)
122 {
123  detail::FieldInfo firstInfo(first.type());
124  detail::FieldInfo secondInfo(second);
125  return detail::compareFieldSortOrder(firstInfo, secondInfo);
126 }
127 
128 } // namespace lp
129 } // namespace ndn
const Block & wireEncode() const
encode packet into wire format
Definition: packet.cpp:67
Copyright (c) 2011-2015 Regents of the University of California.
Buffer::const_iterator end() const
Definition: block.cpp:486
EncodingImpl< EstimatorTag > EncodingEstimator
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
size_t size() const
Definition: block.cpp:504
bool compareFieldSortOrder(const FieldInfo &first, const FieldInfo &second)
Definition: field-info.hpp:87
void wireDecode(const Block &wire)
decode packet from wire format
Definition: packet.cpp:84
EncodingImpl< EncoderTag > EncodingBuffer
void parse() const
Parse wire buffer into subblocks.
Definition: block.cpp:322
uint32_t type() const
Definition: block.hpp:346
bool hasWire() const
Check if the Block has fully encoded wire.
Definition: block.cpp:471
uint64_t tlvType
TLV-TYPE of the field; 0 if field does not exist.
Definition: field-info.hpp:45
Buffer::const_iterator begin() const
Definition: block.cpp:477