blob: 0e400ea1e2caad3fafdc2806c647971f9493fe11 [file] [log] [blame]
Jiewen Tan7a56d1c2015-01-26 23:26:51 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
laqinfan35731852017-08-08 06:17:39 -05003 * Copyright (c) 2014-2018, The University of Memphis,
Jiewen Tan7a56d1c2015-01-26 23:26:51 -08004 * Regents of the University of California,
5 * Arizona Board of Regents.
6 *
7 * This file is part of NLSR (Named-data Link State Routing).
8 * See AUTHORS.md for complete list of NLSR authors and contributors.
9 *
10 * NLSR is free software: you can redistribute it and/or modify it under the terms
11 * of the GNU General Public License as published by the Free Software Foundation,
12 * either version 3 of the License, or (at your option) any later version.
13 *
14 * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
15 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
16 * PURPOSE. See the GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along with
19 * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
20 **/
21
22#ifndef NLSR_TLV_NLSR_HPP
23#define NLSR_TLV_NLSR_HPP
24
25#include <ndn-cxx/encoding/tlv.hpp>
Ashlesh Gawande8df15982018-05-09 17:52:33 -050026#include <ndn-cxx/encoding/block-helpers.hpp>
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080027
Nick Gordond0a7df32017-05-30 16:44:34 -050028namespace ndn {
29namespace tlv {
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080030namespace nlsr {
31
Nick Gordond0a7df32017-05-30 16:44:34 -050032/*! The TLV block types that NLSR uses to encode/decode LSA types. The
33 * way NLSR encodes LSAs to TLV is by encoding each element of the
34 * LSA as a separate TLV block. So, block types are needed. These are
35 * used in the LSDB Status Dataset.
36 */
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080037enum {
38 LsaInfo = 128,
39 OriginRouter = 129,
40 SequenceNumber = 130,
41 AdjacencyLsa = 131,
42 Adjacency = 132,
43 CoordinateLsa = 133,
44 Double = 134,
45 HyperbolicRadius = 135,
46 HyperbolicAngle = 136,
47 NameLsa = 137,
48 LsdbStatus = 138,
49 ExpirationPeriod = 139,
50 Cost = 140,
laqinfan35731852017-08-08 06:17:39 -050051 Uri = 141,
52 Destination = 142,
53 NextHop = 143,
54 RoutingTable = 144,
55 RouteTableEntry = 145
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080056};
57
Ashlesh Gawande8df15982018-05-09 17:52:33 -050058/*! \brief Read a double from a TLV element
59 * \param block the TLV element
60 * \throw ndn::tlv::Error block does not contain a double
61 * \sa prependDouble
62 */
63inline double
64readDouble(const ndn::Block& block)
65{
66 block.parse();
67 auto it = block.elements_begin();
68
69 double doubleFromBlock = 0.0;
70 if (it == it->elements_end() || it->type() != ndn::tlv::nlsr::Double ||
71 it->value_size() != sizeof(doubleFromBlock)) {
72 BOOST_THROW_EXCEPTION(ndn::tlv::Error("Block does not contain a double"));
73 }
74 memcpy(&doubleFromBlock, it->value(), sizeof(doubleFromBlock));
75 return doubleFromBlock;
76}
77
78/*! \brief Prepend a TLV element containing a double.
79 * \param encoder an EncodingBuffer or EncodingEstimator
80 * \param type TLV-TYPE number
81 * \param value double value
82 */
83template<ndn::encoding::Tag TAG>
84inline size_t
85prependDouble(ndn::EncodingImpl<TAG>& encoder, uint32_t type, double value)
86{
87 size_t totalLength = 0;
88
89 const uint8_t* doubleBytes = reinterpret_cast<const uint8_t*>(&value);
90 totalLength = encoder.prependByteArrayBlock(ndn::tlv::nlsr::Double, doubleBytes, 8);
91 totalLength += encoder.prependVarNumber(totalLength);
92 totalLength += encoder.prependVarNumber(type);
93
94 return totalLength;
95}
96
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080097} // namespace nlsr
98} // namespace tlv
99} // namespace ndn
100
101#endif // NLSR_TLV_NLSR_HPP