blob: cfdfd77b89157b30295e8304dbe4fcb012a064c0 [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/*
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -08003 * Copyright (c) 2014-2020, 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) {
Alexander Afanasyev67758b12018-03-06 18:36:44 -050088 BOOST_THROW_EXCEPTION(Error("Expected NameLsa Block, but Block is of a different type: #" +
89 ndn::to_string(m_wire.type())));
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080090 }
91
92 m_wire.parse();
93
Ashlesh Gawande57a87172020-05-09 19:47:06 -070094 auto val = m_wire.elements_begin();
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080095
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080096 if (val != m_wire.elements_end() && val->type() == ndn::tlv::nlsr::Lsa) {
97 Lsa::wireDecode(*val);
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080098 ++val;
99 }
100 else {
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800101 BOOST_THROW_EXCEPTION(Error("Missing required Lsa field"));
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800102 }
103
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800104 NamePrefixList npl;
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800105 for (; val != m_wire.elements_end(); ++val) {
106 if (val->type() == ndn::tlv::Name) {
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800107 npl.insert(ndn::Name(*val));
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800108 }
109 else {
Alexander Afanasyev67758b12018-03-06 18:36:44 -0500110 BOOST_THROW_EXCEPTION(Error("Expected Name Block, but Block is of a different type: #" +
111 ndn::to_string(m_wire.type())));
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800112 }
113 }
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800114
115 m_npl = npl;
116}
117
118bool
119NameLsa::isEqualContent(const NameLsa& other) const
120{
121 return m_npl == other.getNpl();
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800122}
123
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700124std::string
125NameLsa::toString() const
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800126{
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700127 std::ostringstream os;
128 os << Lsa::toString();
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800129 os << " Names:\n";
130 int i = 0;
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700131 for (const auto& name : m_npl.getNames()) {
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800132 os << " Name " << i++ << ": " << name << "\n";
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800133 }
134
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700135 return os.str();
136}
137
138std::ostream&
139operator<<(std::ostream& os, const NameLsa& lsa)
140{
141 return os << lsa.toString();
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800142}
143
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800144} // namespace nlsr