blob: 7d557dda0323b5219058dfef418485806f441655 [file] [log] [blame]
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Ashlesh Gawande0421bc62020-05-08 20:42:19 -07002/*
Davide Pesavento658fd852023-05-10 22:15:03 -04003 * Copyright (c) 2014-2023, The University of Memphis,
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -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 */
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080021
22#include "lsa.hpp"
23#include "nlsr.hpp"
24#include "name-prefix-list.hpp"
25#include "adjacent.hpp"
Ashlesh Gawande0421bc62020-05-08 20:42:19 -070026#include "tlv-nlsr.hpp"
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080027
28namespace nlsr {
29
Ashlesh Gawande57a87172020-05-09 19:47:06 -070030Lsa::Lsa(const ndn::Name& originRouter, uint64_t seqNo,
Davide Pesavento658fd852023-05-10 22:15:03 -040031 ndn::time::system_clock::time_point expirationTimePoint)
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080032 : m_originRouter(originRouter)
33 , m_seqNo(seqNo)
34 , m_expirationTimePoint(expirationTimePoint)
35{
36}
37
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -070038Lsa::Lsa(const Lsa& lsa)
39 : m_originRouter(lsa.getOriginRouter())
40 , m_seqNo(lsa.getSeqNo())
41 , m_expirationTimePoint(lsa.getExpirationTimePoint())
42{
43}
44
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080045template<ndn::encoding::Tag TAG>
46size_t
47Lsa::wireEncode(ndn::EncodingImpl<TAG>& encoder) const
48{
49 size_t totalLength = 0;
50
51 totalLength += prependStringBlock(encoder,
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -040052 nlsr::tlv::ExpirationTime,
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080053 ndn::time::toString(m_expirationTimePoint));
54
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -040055 totalLength += prependNonNegativeIntegerBlock(encoder, nlsr::tlv::SequenceNumber, m_seqNo);
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080056
57 totalLength += m_originRouter.wireEncode(encoder);
58
59 totalLength += encoder.prependVarNumber(totalLength);
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -040060 totalLength += encoder.prependVarNumber(nlsr::tlv::Lsa);
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080061
62 return totalLength;
63}
64
65NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(Lsa);
66
67void
68Lsa::wireDecode(const ndn::Block& wire)
69{
70 m_originRouter.clear();
71 m_seqNo = 0;
72
Ashlesh Gawande57a87172020-05-09 19:47:06 -070073 ndn::Block baseWire = wire;
74 baseWire.parse();
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080075
Ashlesh Gawande57a87172020-05-09 19:47:06 -070076 auto val = baseWire.elements_begin();
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080077
Ashlesh Gawande57a87172020-05-09 19:47:06 -070078 if (val != baseWire.elements_end() && val->type() == ndn::tlv::Name) {
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080079 m_originRouter.wireDecode(*val);
80 }
81 else {
Davide Pesaventod90338d2021-01-07 17:50:05 -050082 NDN_THROW(Error("OriginRouter: Missing required Name field"));
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080083 }
84
85 ++val;
86
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -040087 if (val != baseWire.elements_end() && val->type() == nlsr::tlv::SequenceNumber) {
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080088 m_seqNo = ndn::readNonNegativeInteger(*val);
89 ++val;
90 }
91 else {
Davide Pesaventod90338d2021-01-07 17:50:05 -050092 NDN_THROW(Error("Missing required SequenceNumber field"));
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080093 }
94
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -040095 if (val != baseWire.elements_end() && val->type() == nlsr::tlv::ExpirationTime) {
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080096 m_expirationTimePoint = ndn::time::fromString(readString(*val));
97 }
98 else {
Davide Pesaventod90338d2021-01-07 17:50:05 -050099 NDN_THROW(Error("Missing required ExpirationTime field"));
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800100 }
101}
102
103std::ostream&
104operator<<(std::ostream& os, const Lsa::Type& type)
105{
106 switch (type) {
Davide Pesaventoe28d8752022-03-19 03:55:25 -0400107 case Lsa::Type::ADJACENCY:
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800108 os << "ADJACENCY";
109 break;
Davide Pesaventoe28d8752022-03-19 03:55:25 -0400110 case Lsa::Type::COORDINATE:
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800111 os << "COORDINATE";
112 break;
Davide Pesaventoe28d8752022-03-19 03:55:25 -0400113 case Lsa::Type::NAME:
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800114 os << "NAME";
115 break;
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800116 default:
117 os << "BASE";
118 break;
119 }
120 return os;
121}
122
123std::istream&
124operator>>(std::istream& is, Lsa::Type& type)
125{
126 std::string typeString;
127 is >> typeString;
128 if (typeString == "ADJACENCY") {
129 type = Lsa::Type::ADJACENCY;
130 }
131 else if (typeString == "COORDINATE") {
132 type = Lsa::Type::COORDINATE;
133 }
134 else if (typeString == "NAME") {
135 type = Lsa::Type::NAME;
136 }
137 else {
138 type = Lsa::Type::BASE;
139 }
140 return is;
141}
142
143std::string
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -0700144Lsa::getString() const
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800145{
146 std::ostringstream os;
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -0700147 auto duration = m_expirationTimePoint - ndn::time::system_clock::now();
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800148 os << " " << getType() << " LSA:\n"
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -0700149 << " Origin Router : " << m_originRouter << "\n"
150 << " Sequence Number : " << m_seqNo << "\n"
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800151 << " Expires in : " << ndn::time::duration_cast<ndn::time::milliseconds>(duration)
152 << "\n";
153 return os.str();
154}
155
156} // namespace nlsr