blob: 53281e107e53ca6a3a3118ec221cf12099a73a5b [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/*
Davide Pesaventod90338d2021-01-07 17:50:05 -05003 * Copyright (c) 2014-2021, 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,
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080028 const ndn::time::system_clock::TimePoint& 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);
57 totalLength += block.prependVarNumber(ndn::tlv::nlsr::NameLsa);
58
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
87 if (m_wire.type() != ndn::tlv::nlsr::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
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080095 if (val != m_wire.elements_end() && val->type() == ndn::tlv::nlsr::Lsa) {
96 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
115bool
116NameLsa::isEqualContent(const NameLsa& other) const
117{
118 return m_npl == other.getNpl();
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800119}
120
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700121std::string
122NameLsa::toString() const
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800123{
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700124 std::ostringstream os;
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -0700125 os << getString();
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800126 os << " Names:\n";
127 int i = 0;
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700128 for (const auto& name : m_npl.getNames()) {
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800129 os << " Name " << i++ << ": " << name << "\n";
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800130 }
131
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700132 return os.str();
133}
134
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -0700135std::tuple<bool, std::list<ndn::Name>, std::list<ndn::Name>>
136NameLsa::update(const std::shared_ptr<Lsa>& lsa)
137{
138 auto nlsa = std::static_pointer_cast<NameLsa>(lsa);
139 bool updated = false;
140
Ashlesh Gawande3781c7e2021-05-22 22:36:26 -0700141 m_npl.sort();
142 nlsa->getNpl().sort();
143
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -0700144 // Obtain the set difference of the current and the incoming
145 // name prefix sets, and add those.
146 std::list<ndn::Name> newNames = nlsa->getNpl().getNames();
147 std::list<ndn::Name> oldNames = m_npl.getNames();
148 std::list<ndn::Name> namesToAdd;
149 std::set_difference(newNames.begin(), newNames.end(), oldNames.begin(), oldNames.end(),
150 std::inserter(namesToAdd, namesToAdd.begin()));
151 for (const auto& name : namesToAdd) {
152 addName(name);
153 updated = true;
154 }
155
156 m_npl.sort();
157
158 // Also remove any names that are no longer being advertised.
159 std::list<ndn::Name> namesToRemove;
160 std::set_difference(oldNames.begin(), oldNames.end(), newNames.begin(), newNames.end(),
161 std::inserter(namesToRemove, namesToRemove.begin()));
162 for (const auto& name : namesToRemove) {
163 removeName(name);
164 updated = true;
165 }
166
167 return std::make_tuple(updated, namesToAdd, namesToRemove);
168}
169
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700170std::ostream&
171operator<<(std::ostream& os, const NameLsa& lsa)
172{
173 return os << lsa.toString();
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800174}
175
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800176} // namespace nlsr