blob: 37b74084ba9e53e7fd474db4b4aaad4445ebd359 [file] [log] [blame]
Jiewen Tan7a56d1c2015-01-26 23:26:51 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -08002/**
3 * 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 Gawande0db4d4d2020-02-05 20:30:02 -080020 **/
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080021
22#include "name-lsa.hpp"
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080023#include "tlv/tlv-nlsr.hpp"
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080024
25namespace nlsr {
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080026
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080027NameLsa::NameLsa(const ndn::Name& originRouter, uint32_t seqNo,
28 const ndn::time::system_clock::TimePoint& timepoint,
29 NamePrefixList& npl)
30 : 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 Gawande0db4d4d2020-02-05 20:30:02 -080067 if (m_wire.hasWire() && m_baseWire.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
94 ndn::Block::element_const_iterator val = m_wire.elements_begin();
95
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
124std::ostream&
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800125operator<<(std::ostream& os, const NameLsa& lsa)
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800126{
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800127 os << lsa.toString();
128 os << " Names:\n";
129 int i = 0;
130 auto names = lsa.getNpl().getNames();
131 for (const auto& name : names) {
132 os << " Name " << i++ << ": " << name << "\n";
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800133 }
134
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800135 return os;
136}
137
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800138} // namespace nlsr