blob: 266649a021ed5bfd7a8edb551c2dd1a29c8b48a1 [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
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
153 m_npl.sort();
154
155 // Also remove any names that are no longer being advertised.
156 std::list<ndn::Name> namesToRemove;
157 std::set_difference(oldNames.begin(), oldNames.end(), newNames.begin(), newNames.end(),
158 std::inserter(namesToRemove, namesToRemove.begin()));
159 for (const auto& name : namesToRemove) {
160 removeName(name);
161 updated = true;
162 }
163
164 return std::make_tuple(updated, namesToAdd, namesToRemove);
165}
166
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700167std::ostream&
168operator<<(std::ostream& os, const NameLsa& lsa)
169{
170 return os << lsa.toString();
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800171}
172
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800173} // namespace nlsr