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 | 0421bc6 | 2020-05-08 20:42:19 -0700 | [diff] [blame] | 2 | /* |
Davide Pesavento | d90338d | 2021-01-07 17:50:05 -0500 | [diff] [blame^] | 3 | * Copyright (c) 2014-2021, 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 | 0421bc6 | 2020-05-08 20:42:19 -0700 | [diff] [blame] | 20 | */ |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 21 | |
| 22 | #include "name-lsa.hpp" |
Ashlesh Gawande | 0421bc6 | 2020-05-08 20:42:19 -0700 | [diff] [blame] | 23 | #include "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 | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 27 | NameLsa::NameLsa(const ndn::Name& originRouter, uint64_t seqNo, |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 28 | const ndn::time::system_clock::TimePoint& timepoint, |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 29 | const NamePrefixList& npl) |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 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 | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 67 | if (m_wire.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) { |
Davide Pesavento | d90338d | 2021-01-07 17:50:05 -0500 | [diff] [blame^] | 88 | NDN_THROW(Error("NameLsa", m_wire.type())); |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | m_wire.parse(); |
| 92 | |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 93 | auto val = m_wire.elements_begin(); |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 94 | |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 95 | if (val != m_wire.elements_end() && val->type() == ndn::tlv::nlsr::Lsa) { |
| 96 | Lsa::wireDecode(*val); |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 97 | ++val; |
| 98 | } |
| 99 | else { |
Davide Pesavento | d90338d | 2021-01-07 17:50:05 -0500 | [diff] [blame^] | 100 | NDN_THROW(Error("Missing required Lsa field")); |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 101 | } |
| 102 | |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 103 | NamePrefixList npl; |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 104 | for (; val != m_wire.elements_end(); ++val) { |
| 105 | if (val->type() == ndn::tlv::Name) { |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 106 | npl.insert(ndn::Name(*val)); |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 107 | } |
| 108 | else { |
Davide Pesavento | d90338d | 2021-01-07 17:50:05 -0500 | [diff] [blame^] | 109 | NDN_THROW(Error("Name", val->type())); |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 110 | } |
| 111 | } |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 112 | m_npl = npl; |
| 113 | } |
| 114 | |
| 115 | bool |
| 116 | NameLsa::isEqualContent(const NameLsa& other) const |
| 117 | { |
| 118 | return m_npl == other.getNpl(); |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 119 | } |
| 120 | |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 121 | std::string |
| 122 | NameLsa::toString() const |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 123 | { |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 124 | std::ostringstream os; |
| 125 | os << Lsa::toString(); |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 126 | os << " Names:\n"; |
| 127 | int i = 0; |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 128 | for (const auto& name : m_npl.getNames()) { |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 129 | os << " Name " << i++ << ": " << name << "\n"; |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 130 | } |
| 131 | |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 132 | return os.str(); |
| 133 | } |
| 134 | |
| 135 | std::ostream& |
| 136 | operator<<(std::ostream& os, const NameLsa& lsa) |
| 137 | { |
| 138 | return os << lsa.toString(); |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 139 | } |
| 140 | |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 141 | } // namespace nlsr |