Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame^] | 2 | /** |
| 3 | * Copyright (c) 2014-2020, 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/>. |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame^] | 20 | **/ |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 21 | |
| 22 | #include "name-lsa.hpp" |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame^] | 23 | #include "tlv/tlv-nlsr.hpp" |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 24 | |
| 25 | namespace nlsr { |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 26 | |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame^] | 27 | NameLsa::NameLsa(const ndn::Name& originRouter, uint32_t seqNo, |
| 28 | const ndn::time::system_clock::TimePoint& timepoint, |
| 29 | NamePrefixList& npl) |
| 30 | : Lsa(originRouter, seqNo, timepoint) |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 31 | { |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame^] | 32 | for (const auto& name : npl.getNames()) { |
| 33 | addName(name); |
| 34 | } |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | NameLsa::NameLsa(const ndn::Block& block) |
| 38 | { |
| 39 | wireDecode(block); |
| 40 | } |
| 41 | |
Alexander Afanasyev | f9f3910 | 2015-12-01 17:43:40 -0800 | [diff] [blame] | 42 | template<ndn::encoding::Tag TAG> |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 43 | size_t |
Alexander Afanasyev | f9f3910 | 2015-12-01 17:43:40 -0800 | [diff] [blame] | 44 | NameLsa::wireEncode(ndn::EncodingImpl<TAG>& block) const |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 45 | { |
| 46 | size_t totalLength = 0; |
| 47 | |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame^] | 48 | auto names = m_npl.getNames(); |
| 49 | |
| 50 | for (auto it = names.rbegin(); it != names.rend(); ++it) { |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 51 | totalLength += it->wireEncode(block); |
| 52 | } |
| 53 | |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame^] | 54 | totalLength += Lsa::wireEncode(block); |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 55 | |
| 56 | totalLength += block.prependVarNumber(totalLength); |
| 57 | totalLength += block.prependVarNumber(ndn::tlv::nlsr::NameLsa); |
| 58 | |
| 59 | return totalLength; |
| 60 | } |
| 61 | |
Alexander Afanasyev | 67758b1 | 2018-03-06 18:36:44 -0500 | [diff] [blame] | 62 | NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(NameLsa); |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 63 | |
| 64 | const ndn::Block& |
| 65 | NameLsa::wireEncode() const |
| 66 | { |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame^] | 67 | if (m_wire.hasWire() && m_baseWire.hasWire()) { |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 68 | return m_wire; |
| 69 | } |
| 70 | |
| 71 | ndn::EncodingEstimator estimator; |
| 72 | size_t estimatedSize = wireEncode(estimator); |
| 73 | |
| 74 | ndn::EncodingBuffer buffer(estimatedSize, 0); |
| 75 | wireEncode(buffer); |
| 76 | |
| 77 | m_wire = buffer.block(); |
| 78 | |
| 79 | return m_wire; |
| 80 | } |
| 81 | |
| 82 | void |
| 83 | NameLsa::wireDecode(const ndn::Block& wire) |
| 84 | { |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 85 | m_wire = wire; |
| 86 | |
| 87 | if (m_wire.type() != ndn::tlv::nlsr::NameLsa) { |
Alexander Afanasyev | 67758b1 | 2018-03-06 18:36:44 -0500 | [diff] [blame] | 88 | BOOST_THROW_EXCEPTION(Error("Expected NameLsa Block, but Block is of a different type: #" + |
| 89 | ndn::to_string(m_wire.type()))); |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | m_wire.parse(); |
| 93 | |
| 94 | ndn::Block::element_const_iterator val = m_wire.elements_begin(); |
| 95 | |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame^] | 96 | if (val != m_wire.elements_end() && val->type() == ndn::tlv::nlsr::Lsa) { |
| 97 | Lsa::wireDecode(*val); |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 98 | ++val; |
| 99 | } |
| 100 | else { |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame^] | 101 | BOOST_THROW_EXCEPTION(Error("Missing required Lsa field")); |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 102 | } |
| 103 | |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame^] | 104 | NamePrefixList npl; |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 105 | for (; val != m_wire.elements_end(); ++val) { |
| 106 | if (val->type() == ndn::tlv::Name) { |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame^] | 107 | npl.insert(ndn::Name(*val)); |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 108 | } |
| 109 | else { |
Alexander Afanasyev | 67758b1 | 2018-03-06 18:36:44 -0500 | [diff] [blame] | 110 | BOOST_THROW_EXCEPTION(Error("Expected Name Block, but Block is of a different type: #" + |
| 111 | ndn::to_string(m_wire.type()))); |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 112 | } |
| 113 | } |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame^] | 114 | |
| 115 | m_npl = npl; |
| 116 | } |
| 117 | |
| 118 | bool |
| 119 | NameLsa::isEqualContent(const NameLsa& other) const |
| 120 | { |
| 121 | return m_npl == other.getNpl(); |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | std::ostream& |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame^] | 125 | operator<<(std::ostream& os, const NameLsa& lsa) |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 126 | { |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame^] | 127 | os << lsa.toString(); |
| 128 | os << " Names:\n"; |
| 129 | int i = 0; |
| 130 | auto names = lsa.getNpl().getNames(); |
| 131 | for (const auto& name : names) { |
| 132 | os << " Name " << i++ << ": " << name << "\n"; |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 133 | } |
| 134 | |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 135 | return os; |
| 136 | } |
| 137 | |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 138 | } // namespace nlsr |