|
NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
|
API Documentation
|
Go to the documentation of this file.
32 #include <boost/asio/buffer.hpp>
33 #include <boost/range/adaptor/reversed.hpp>
38 BOOST_CONCEPT_ASSERT((boost::EqualityComparable<Block>));
52 :
Block(buffer.getBuffer(), buffer.begin(), buffer.end(), true)
57 :
Block(buffer, buffer->begin(), buffer->end(), true)
63 : m_buffer(std::
move(buffer))
66 , m_valueBegin(m_begin)
68 , m_size(m_end - m_begin)
71 NDN_THROW(std::invalid_argument(
"Buffer is empty"));
74 const uint8_t* bufferBegin = &
m_buffer->front();
75 const uint8_t* bufferEnd = bufferBegin +
m_buffer->size();
76 if (&*begin < bufferBegin || &*begin > bufferEnd ||
77 &*end < bufferBegin || &*end > bufferEnd) {
78 NDN_THROW(std::invalid_argument(
"Begin/end iterators point outside the buffer"));
86 NDN_THROW(Error(
"TLV-LENGTH does not match buffer size"));
90 Block::Block(
const Block& block, Buffer::const_iterator begin, Buffer::const_iterator end,
92 : Block(block.m_buffer, begin, end, verifyLength)
97 Buffer::const_iterator begin, Buffer::const_iterator end,
98 Buffer::const_iterator valueBegin, Buffer::const_iterator valueEnd)
99 : m_buffer(std::
move(buffer))
102 , m_valueBegin(valueBegin)
103 , m_valueEnd(valueEnd)
105 , m_size(m_end - m_begin)
111 const uint8_t* pos =
buf;
112 const uint8_t*
const end =
buf + bufSize;
118 BOOST_ASSERT(pos <=
end);
119 if (length >
static_cast<uint64_t
>(
end - pos)) {
120 NDN_THROW(
Error(
"Not enough bytes in the buffer to fully parse TLV"));
123 BOOST_ASSERT(pos >
buf);
124 uint64_t typeLengthSize =
static_cast<uint64_t
>(pos -
buf);
125 m_size = typeLengthSize + length;
140 : m_buffer(std::
move(value))
141 , m_begin(m_buffer->end())
142 , m_end(m_buffer->end())
143 , m_valueBegin(m_buffer->begin())
144 , m_valueEnd(m_buffer->end())
151 : m_buffer(value.m_buffer)
152 , m_begin(m_buffer->end())
153 , m_end(m_buffer->end())
154 , m_valueBegin(value.begin())
155 , m_valueEnd(value.end())
164 std::istream_iterator<uint8_t>
begin(is >> std::noskipws);
165 std::istream_iterator<uint8_t>
end;
179 uint8_t* valueBuf = eb.buf();
180 is.read(
reinterpret_cast<char*
>(valueBuf), length);
181 if (length !=
static_cast<uint64_t
>(is.gcount())) {
182 NDN_THROW(
Error(
"Not enough bytes from stream to fully parse TLV"));
185 eb.prependVarNumber(length);
186 eb.prependVarNumber(
type);
190 return Block(eb.getBuffer());
193 std::tuple<bool, Block>
196 auto begin = buffer->begin() + offset;
202 return std::make_tuple(
false,
Block());
208 return std::make_tuple(
false,
Block());
212 if (length >
static_cast<uint64_t
>(buffer->end() - pos)) {
213 return std::make_tuple(
false,
Block());
219 std::tuple<bool, Block>
222 const uint8_t* pos =
buf;
223 const uint8_t*
const end =
buf + bufSize;
228 return std::make_tuple(
false,
Block());
233 return std::make_tuple(
false,
Block());
237 if (length >
static_cast<uint64_t
>(
end - pos)) {
238 return std::make_tuple(
false,
Block());
241 size_t typeLengthSize = pos -
buf;
242 auto b = make_shared<Buffer>(
buf, pos + length);
243 return std::make_tuple(
true,
Block(b,
type, b->begin(), b->end(),
244 b->begin() + typeLengthSize, b->end()));
262 Buffer::const_iterator
271 Buffer::const_iterator
334 Buffer::const_iterator pos =
begin;
338 if (length >
static_cast<uint64_t
>(
end - pos)) {
341 " exceeds TLV-VALUE boundary of parent block"));
345 Buffer::const_iterator subEnd = pos + length;
359 size_t estimatedSize =
encode(estimator);
372 size_t len = encodeValue(estimator);
373 len += estimator.prependVarNumber(len);
374 len += estimator.prependVarNumber(
m_type);
383 len += element.encode(estimator);
392 m_end = encoder.begin();
398 len += element.encode(encoder);
404 len += encoder.prependVarNumber(len);
405 len += encoder.prependVarNumber(
m_type);
416 auto it = this->
find(type);
429 [
type] (
const Block& subBlock) { return subBlock.type() == type; });
438 [
type] (
const Block& subBlock) { return subBlock.type() == type; });
472 Block::operator boost::asio::const_buffer()
const
474 return boost::asio::const_buffer(wire(), size());
489 auto oldFmt = os.flags(std::ios_base::dec);
496 size_t tlvLength = block.encodeValue(estimator);
497 os << block.
type() <<
'[' << tlvLength <<
"]={";
506 os << block.
type() <<
"[empty]";
514 operator "" _block(
const char* input, std::size_t len)
516 namespace t = security::transform;
521 for (
const char* end = input + len; input != end; ++input) {
522 if (std::strchr(
"0123456789ABCDEF", *input) !=
nullptr) {
523 ss.write(
reinterpret_cast<const uint8_t*
>(input), 1);
530 catch (
const t::Error&) {
531 NDN_THROW(std::invalid_argument(
"Input has odd number of hexadecimal digits"));
size_t m_size
Total size including Type-Length-Value.
const size_t MAX_SIZE_OF_BLOCK_FROM_STREAM
Block blockFromValue() const
size_t value_size() const noexcept
Return the size of TLV-VALUE, aka TLV-LENGTH.
element_iterator insert(element_const_iterator pos, const Block &element)
Insert a sub-element.
Block()
Create an invalid Block.
bool hasValue() const noexcept
Check if the Block has a non-empty TLV-VALUE.
Buffer::const_iterator begin() const
Get begin iterator of encoded wire.
element_const_iterator elements_begin() const
Equivalent to elements().begin()
Buffer::const_iterator m_valueBegin
element_const_iterator find(uint32_t type) const
Find the first sub-element of the specified TLV-TYPE.
Buffer::const_iterator value_end() const
Get end iterator of TLV-VALUE.
ostream_joiner< std::decay_t< DelimT >, CharT, Traits > make_ostream_joiner(std::basic_ostream< CharT, Traits > &os, DelimT &&delimiter)
void reset() noexcept
Reset the Block to a default-constructed state.
bool isValid() const noexcept
Check if the Block is valid.
Block & operator=(const Block &)
Copy assignment operator.
element_container::const_iterator element_const_iterator
void push_back(const Block &element)
Append a sub-element.
element_container m_elements
Contains the sub-elements.
EncodingImpl< EstimatorTag > EncodingEstimator
const Block & get(uint32_t type) const
Return the first sub-element of the specified TLV-TYPE.
void remove(uint32_t type)
Remove all sub-elements of the specified TLV-TYPE.
static NDN_CXX_NODISCARD std::tuple< bool, Block > fromBuffer(ConstBufferPtr buffer, size_t offset)
Try to parse Block from a wire buffer.
uint32_t type() const
Return the TLV-TYPE of the Block.
Backport of ostream_joiner from the Library Fundamentals v2 TS.
element_iterator erase(element_const_iterator position)
Erase a sub-element.
Buffer::const_iterator value_begin() const
Get begin iterator of TLV-VALUE.
Buffer::const_iterator m_end
element_container::iterator element_iterator
void printHex(std::ostream &os, uint64_t num, bool wantUpperCase)
Output the hex representation of num to the output stream os.
NDN_CXX_NODISCARD bool readType(Iterator &begin, Iterator end, uint32_t &type) noexcept
Read TLV-TYPE.
constexpr size_t sizeOfVarNumber(uint64_t number) noexcept
Get the number of bytes necessary to hold the value of number encoded as VAR-NUMBER.
NDN_CXX_NODISCARD bool readVarNumber(Iterator &begin, Iterator end, uint64_t &number) noexcept
Read VAR-NUMBER in NDN-TLV encoding.
void resetWire() noexcept
Reset wire buffer but keep TLV-TYPE and sub-elements (if any)
void parse() const
Parse TLV-VALUE into sub-elements.
implements an output stream that constructs ndn::Buffer
Buffer::const_iterator m_begin
Buffer::const_iterator end() const
Get end iterator of encoded wire.
static Block fromStream(std::istream &is)
Parse Block from an input stream.
const size_t MAX_NDN_PACKET_SIZE
practical limit of network layer packet size
const uint8_t * value() const noexcept
Return a raw pointer to the beginning of TLV-VALUE.
Represents a TLV element of NDN packet format.
size_t size() const
Return the size of the encoded wire, i.e.
std::string to_string(const T &val)
element_const_iterator elements_end() const
Equivalent to elements().end()
Buffer::const_iterator m_valueEnd
bool operator==(const Data &lhs, const Data &rhs)
std::ostream & operator<<(std::ostream &os, const Data &data)
bool hasWire() const noexcept
Check if the Block contains a fully encoded wire representation.
shared_ptr< Buffer > buf()
Flush written data to the stream and return shared pointer to the underlying buffer.
const uint8_t * wire() const
Return a raw pointer to the beginning of the encoded wire.
void encode()
Encode sub-elements into TLV-VALUE.
shared_ptr< const Buffer > ConstBufferPtr
Copyright (c) 2011-2015 Regents of the University of California.
EncodingImpl< EncoderTag > EncodingBuffer
shared_ptr< const Buffer > m_buffer
Underlying buffer storing TLV-VALUE and possibly TLV-TYPE and TLV-LENGTH fields.