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 | 658fd85 | 2023-05-10 22:15:03 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2023, 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, |
Davide Pesavento | 658fd85 | 2023-05-10 22:15:03 -0400 | [diff] [blame] | 28 | const ndn::time::system_clock::time_point& 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); |
Davide Pesavento | c1d0e8e | 2022-06-15 14:26:02 -0400 | [diff] [blame] | 57 | totalLength += block.prependVarNumber(nlsr::tlv::NameLsa); |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 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 | |
Davide Pesavento | c1d0e8e | 2022-06-15 14:26:02 -0400 | [diff] [blame] | 87 | if (m_wire.type() != nlsr::tlv::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 | |
Davide Pesavento | c1d0e8e | 2022-06-15 14:26:02 -0400 | [diff] [blame] | 95 | if (val != m_wire.elements_end() && val->type() == nlsr::tlv::Lsa) { |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 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; |
Ashlesh Gawande | 5d93aa5 | 2020-06-13 18:57:45 -0700 | [diff] [blame] | 125 | os << getString(); |
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 | |
Ashlesh Gawande | 5d93aa5 | 2020-06-13 18:57:45 -0700 | [diff] [blame] | 135 | std::tuple<bool, std::list<ndn::Name>, std::list<ndn::Name>> |
| 136 | NameLsa::update(const std::shared_ptr<Lsa>& lsa) |
| 137 | { |
| 138 | auto nlsa = std::static_pointer_cast<NameLsa>(lsa); |
| 139 | bool updated = false; |
| 140 | |
| 141 | // Obtain the set difference of the current and the incoming |
| 142 | // name prefix sets, and add those. |
| 143 | std::list<ndn::Name> newNames = nlsa->getNpl().getNames(); |
| 144 | std::list<ndn::Name> oldNames = m_npl.getNames(); |
| 145 | std::list<ndn::Name> namesToAdd; |
| 146 | std::set_difference(newNames.begin(), newNames.end(), oldNames.begin(), oldNames.end(), |
| 147 | std::inserter(namesToAdd, namesToAdd.begin())); |
| 148 | for (const auto& name : namesToAdd) { |
| 149 | addName(name); |
| 150 | updated = true; |
| 151 | } |
| 152 | |
Ashlesh Gawande | 5d93aa5 | 2020-06-13 18:57:45 -0700 | [diff] [blame] | 153 | // Also remove any names that are no longer being advertised. |
| 154 | std::list<ndn::Name> namesToRemove; |
| 155 | std::set_difference(oldNames.begin(), oldNames.end(), newNames.begin(), newNames.end(), |
| 156 | std::inserter(namesToRemove, namesToRemove.begin())); |
| 157 | for (const auto& name : namesToRemove) { |
| 158 | removeName(name); |
| 159 | updated = true; |
| 160 | } |
| 161 | |
Davide Pesavento | c1d0e8e | 2022-06-15 14:26:02 -0400 | [diff] [blame] | 162 | return {updated, namesToAdd, namesToRemove}; |
Ashlesh Gawande | 5d93aa5 | 2020-06-13 18:57:45 -0700 | [diff] [blame] | 163 | } |
| 164 | |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 165 | std::ostream& |
| 166 | operator<<(std::ostream& os, const NameLsa& lsa) |
| 167 | { |
| 168 | return os << lsa.toString(); |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 169 | } |
| 170 | |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 171 | } // namespace nlsr |