Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
laqinfan | 3573185 | 2017-08-08 06:17:39 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2018, The University of Memphis, |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 4 | * 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 Gawande | 8df1598 | 2018-05-09 17:52:33 -0500 | [diff] [blame] | 26 | #include <ndn-cxx/encoding/block-helpers.hpp> |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 27 | |
Nick Gordon | d0a7df3 | 2017-05-30 16:44:34 -0500 | [diff] [blame] | 28 | namespace ndn { |
| 29 | namespace tlv { |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 30 | namespace nlsr { |
| 31 | |
Nick Gordon | d0a7df3 | 2017-05-30 16:44:34 -0500 | [diff] [blame] | 32 | /*! 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 Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 37 | enum { |
| 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, |
laqinfan | 3573185 | 2017-08-08 06:17:39 -0500 | [diff] [blame] | 51 | Uri = 141, |
| 52 | Destination = 142, |
| 53 | NextHop = 143, |
| 54 | RoutingTable = 144, |
| 55 | RouteTableEntry = 145 |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 56 | }; |
| 57 | |
Ashlesh Gawande | 8df1598 | 2018-05-09 17:52:33 -0500 | [diff] [blame] | 58 | /*! \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 | */ |
| 63 | inline double |
| 64 | readDouble(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 | */ |
| 83 | template<ndn::encoding::Tag TAG> |
| 84 | inline size_t |
| 85 | prependDouble(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 Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 97 | } // namespace nlsr |
| 98 | } // namespace tlv |
| 99 | } // namespace ndn |
| 100 | |
| 101 | #endif // NLSR_TLV_NLSR_HPP |