blob: 7867b68559704cbebf21a4ae959e33291f6b046e [file] [log] [blame]
Jiewen Tan7a56d1c2015-01-26 23:26:51 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Ashlesh Gawande0421bc62020-05-08 20:42:19 -07002/*
Junxiao Shi153fbc12024-01-09 23:37:23 +00003 * Copyright (c) 2014-2024, 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/>.
Ashlesh Gawande0421bc62020-05-08 20:42:19 -070020 */
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080021
22#include "name-lsa.hpp"
Ashlesh Gawande0421bc62020-05-08 20:42:19 -070023#include "tlv-nlsr.hpp"
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080024
25namespace nlsr {
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080026
Ashlesh Gawande57a87172020-05-09 19:47:06 -070027NameLsa::NameLsa(const ndn::Name& originRouter, uint64_t seqNo,
Davide Pesavento658fd852023-05-10 22:15:03 -040028 const ndn::time::system_clock::time_point& timepoint,
Ashlesh Gawande57a87172020-05-09 19:47:06 -070029 const NamePrefixList& npl)
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080030 : Lsa(originRouter, seqNo, timepoint)
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080031{
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080032 for (const auto& name : npl.getNames()) {
33 addName(name);
34 }
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080035}
36
37NameLsa::NameLsa(const ndn::Block& block)
38{
39 wireDecode(block);
40}
41
Alexander Afanasyevf9f39102015-12-01 17:43:40 -080042template<ndn::encoding::Tag TAG>
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080043size_t
Alexander Afanasyevf9f39102015-12-01 17:43:40 -080044NameLsa::wireEncode(ndn::EncodingImpl<TAG>& block) const
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080045{
46 size_t totalLength = 0;
47
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080048 auto names = m_npl.getNames();
49
50 for (auto it = names.rbegin(); it != names.rend(); ++it) {
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080051 totalLength += it->wireEncode(block);
52 }
53
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080054 totalLength += Lsa::wireEncode(block);
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080055
56 totalLength += block.prependVarNumber(totalLength);
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -040057 totalLength += block.prependVarNumber(nlsr::tlv::NameLsa);
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080058
59 return totalLength;
60}
61
Alexander Afanasyev67758b12018-03-06 18:36:44 -050062NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(NameLsa);
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080063
64const ndn::Block&
65NameLsa::wireEncode() const
66{
Ashlesh Gawande57a87172020-05-09 19:47:06 -070067 if (m_wire.hasWire()) {
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080068 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
82void
83NameLsa::wireDecode(const ndn::Block& wire)
84{
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080085 m_wire = wire;
86
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -040087 if (m_wire.type() != nlsr::tlv::NameLsa) {
Davide Pesaventod90338d2021-01-07 17:50:05 -050088 NDN_THROW(Error("NameLsa", m_wire.type()));
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080089 }
90
91 m_wire.parse();
92
Ashlesh Gawande57a87172020-05-09 19:47:06 -070093 auto val = m_wire.elements_begin();
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080094
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -040095 if (val != m_wire.elements_end() && val->type() == nlsr::tlv::Lsa) {
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080096 Lsa::wireDecode(*val);
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080097 ++val;
98 }
99 else {
Davide Pesaventod90338d2021-01-07 17:50:05 -0500100 NDN_THROW(Error("Missing required Lsa field"));
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800101 }
102
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800103 NamePrefixList npl;
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800104 for (; val != m_wire.elements_end(); ++val) {
105 if (val->type() == ndn::tlv::Name) {
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800106 npl.insert(ndn::Name(*val));
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800107 }
108 else {
Davide Pesaventod90338d2021-01-07 17:50:05 -0500109 NDN_THROW(Error("Name", val->type()));
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800110 }
111 }
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800112 m_npl = npl;
113}
114
Junxiao Shi153fbc12024-01-09 23:37:23 +0000115void
116NameLsa::print(std::ostream& os) const
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800117{
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800118 os << " Names:\n";
119 int i = 0;
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700120 for (const auto& name : m_npl.getNames()) {
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800121 os << " Name " << i++ << ": " << name << "\n";
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800122 }
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700123}
124
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -0700125std::tuple<bool, std::list<ndn::Name>, std::list<ndn::Name>>
126NameLsa::update(const std::shared_ptr<Lsa>& lsa)
127{
128 auto nlsa = std::static_pointer_cast<NameLsa>(lsa);
129 bool updated = false;
130
131 // Obtain the set difference of the current and the incoming
132 // name prefix sets, and add those.
133 std::list<ndn::Name> newNames = nlsa->getNpl().getNames();
134 std::list<ndn::Name> oldNames = m_npl.getNames();
135 std::list<ndn::Name> namesToAdd;
136 std::set_difference(newNames.begin(), newNames.end(), oldNames.begin(), oldNames.end(),
137 std::inserter(namesToAdd, namesToAdd.begin()));
138 for (const auto& name : namesToAdd) {
139 addName(name);
140 updated = true;
141 }
142
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -0700143 // Also remove any names that are no longer being advertised.
144 std::list<ndn::Name> namesToRemove;
145 std::set_difference(oldNames.begin(), oldNames.end(), newNames.begin(), newNames.end(),
146 std::inserter(namesToRemove, namesToRemove.begin()));
147 for (const auto& name : namesToRemove) {
148 removeName(name);
149 updated = true;
150 }
151
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -0400152 return {updated, namesToAdd, namesToRemove};
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -0700153}
154
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800155} // namespace nlsr