Loading...
Searching...
No Matches
name-lsa.cpp
Go to the documentation of this file.
1/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2014-2025, The University of Memphis,
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/>.
20 */
21
22#include "name-lsa.hpp"
23#include "tlv-nlsr.hpp"
24
25namespace nlsr {
26
27NameLsa::NameLsa(const ndn::Name& originRouter, uint64_t seqNo,
28 const ndn::time::system_clock::time_point& timepoint,
29 const NamePrefixList& npl)
30 : Lsa(originRouter, seqNo, timepoint)
31{
32 for (const auto& name : npl.getPrefixInfo()) {
33 addName(name);
34 }
35}
36
37NameLsa::NameLsa(const ndn::Block& block)
38{
39 wireDecode(block);
40}
41
42template<ndn::encoding::Tag TAG>
43size_t
44NameLsa::wireEncode(ndn::EncodingImpl<TAG>& block) const
45{
46 size_t totalLength = 0;
47
48 auto names = m_npl.getPrefixInfo();
49
50 for (auto it = names.rbegin(); it != names.rend(); ++it) {
51 totalLength += it->wireEncode(block);
52 }
53
54 totalLength += Lsa::wireEncode(block);
55
56 totalLength += block.prependVarNumber(totalLength);
57 totalLength += block.prependVarNumber(nlsr::tlv::NameLsa);
58
59 return totalLength;
60}
61
63
64const ndn::Block&
66{
67 if (m_wire.hasWire()) {
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
82void
83NameLsa::wireDecode(const ndn::Block& wire)
84{
85 m_wire = wire;
86
87 if (m_wire.type() != nlsr::tlv::NameLsa) {
88 NDN_THROW(Error("NameLsa", m_wire.type()));
89 }
90
91 m_wire.parse();
92
93 auto val = m_wire.elements_begin();
94
95 if (val != m_wire.elements_end() && val->type() == nlsr::tlv::Lsa) {
96 Lsa::wireDecode(*val);
97 ++val;
98 }
99 else {
100 NDN_THROW(Error("Missing required Lsa field"));
101 }
102
103 NamePrefixList npl;
104 for (; val != m_wire.elements_end(); ++val) {
105 if (val->type() == nlsr::tlv::PrefixInfo) {
106 //TODO: Implement this structure as a type instead and add decoding
107 npl.insert(PrefixInfo(*val));
108 }
109 else {
110 NDN_THROW(Error("Name", val->type()));
111 }
112 }
113 m_npl = npl;
114}
115
116void
117NameLsa::print(std::ostream& os) const
118{
119 os << " Names:\n";
120 int i = 0;
121 for (const auto& name : m_npl.getPrefixInfo()) {
122 os << " Name " << i << ": " << name.getName()
123 << " | Cost: " << name.getCost() << "\n";
124 i++;
125 }
126}
127
128std::tuple<bool, std::list<PrefixInfo>, std::list<PrefixInfo>>
129NameLsa::update(const std::shared_ptr<Lsa>& lsa)
130{
131 auto nlsa = std::static_pointer_cast<NameLsa>(lsa);
132 bool updated = false;
133
134 // Obtain the set difference of the current and the incoming
135 // name prefix sets, and add those.
136
137 std::list<ndn::Name> newNames = nlsa->getNpl().getNames();
138 std::list<ndn::Name> oldNames = m_npl.getNames();
139 std::list<ndn::Name> nameRefToAdd;
140 std::list<PrefixInfo> namesToAdd;
141
142 std::set_difference(newNames.begin(), newNames.end(), oldNames.begin(), oldNames.end(),
143 std::inserter(nameRefToAdd, nameRefToAdd.begin()));
144 for (const auto& name : nameRefToAdd) {
145 namesToAdd.push_back(nlsa->getNpl().getPrefixInfoForName(name));
146 addName(nlsa->getNpl().getPrefixInfoForName(name));
147 updated = true;
148 }
149
150 // Also remove any names that are no longer being advertised.
151 std::list<ndn::Name> nameRefToRemove;
152 std::list<PrefixInfo> namesToRemove;
153 std::set_difference(oldNames.begin(), oldNames.end(), newNames.begin(), newNames.end(),
154 std::inserter(nameRefToRemove, nameRefToRemove.begin()));
155 for (const auto& name : nameRefToRemove) {
156 namesToRemove.push_back(m_npl.getPrefixInfoForName(name));
157 removeName(m_npl.getPrefixInfoForName(name));
158
159 updated = true;
160 }
161 return {updated, namesToAdd, namesToRemove};
162}
163
164} // namespace nlsr
Represents a Link State Announcement (LSA).
Definition lsa.hpp:48
ndn::Block m_wire
Definition lsa.hpp:144
void wireDecode(const ndn::Block &wire)
Definition lsa.cpp:65
virtual const ndn::Block & wireEncode() const =0
Represents an LSA of name prefixes announced by the origin router.
Definition name-lsa.hpp:43
NameLsa()=default
void removeName(const PrefixInfo &name)
Definition name-lsa.hpp:86
void addName(const PrefixInfo &name)
Definition name-lsa.hpp:79
std::tuple< bool, std::list< PrefixInfo >, std::list< PrefixInfo > > update(const std::shared_ptr< Lsa > &lsa) override
Definition name-lsa.cpp:129
void wireDecode(const ndn::Block &wire)
Definition name-lsa.cpp:83
const ndn::Block & wireEncode() const override
Definition name-lsa.cpp:65
std::list< PrefixInfo > getPrefixInfo() const
const PrefixInfo & getPrefixInfoForName(const ndn::Name &name) const
std::list< ndn::Name > getNames() const
bool insert(const ndn::Name &name, const std::string &source="", double cost=0)
Inserts name and source combination.
Copyright (c) 2014-2020, The University of Memphis, Regents of the University of California.
NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(Adjacent)