blob: dd8267ef1a47025215b6729b05b19c9a6173d588 [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/*
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -08003 * Copyright (c) 2014-2020, 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/>.
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,
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080031 ndn::time::system_clock::TimePoint expirationTimePoint)
32 : m_originRouter(originRouter)
33 , m_seqNo(seqNo)
34 , m_expirationTimePoint(expirationTimePoint)
35{
36}
37
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080038template<ndn::encoding::Tag TAG>
39size_t
40Lsa::wireEncode(ndn::EncodingImpl<TAG>& encoder) const
41{
42 size_t totalLength = 0;
43
44 totalLength += prependStringBlock(encoder,
45 ndn::tlv::nlsr::ExpirationTime,
46 ndn::time::toString(m_expirationTimePoint));
47
48 totalLength += prependNonNegativeIntegerBlock(encoder, ndn::tlv::nlsr::SequenceNumber,
49 m_seqNo);
50
51 totalLength += m_originRouter.wireEncode(encoder);
52
53 totalLength += encoder.prependVarNumber(totalLength);
54 totalLength += encoder.prependVarNumber(ndn::tlv::nlsr::Lsa);
55
56 return totalLength;
57}
58
59NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(Lsa);
60
61void
62Lsa::wireDecode(const ndn::Block& wire)
63{
64 m_originRouter.clear();
65 m_seqNo = 0;
66
Ashlesh Gawande57a87172020-05-09 19:47:06 -070067 ndn::Block baseWire = wire;
68 baseWire.parse();
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080069
Ashlesh Gawande57a87172020-05-09 19:47:06 -070070 auto val = baseWire.elements_begin();
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080071
Ashlesh Gawande57a87172020-05-09 19:47:06 -070072 if (val != baseWire.elements_end() && val->type() == ndn::tlv::Name) {
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080073 m_originRouter.wireDecode(*val);
74 }
75 else {
76 BOOST_THROW_EXCEPTION(Error("OriginRouter: Missing required Name field"));
77 }
78
79 ++val;
80
Ashlesh Gawande57a87172020-05-09 19:47:06 -070081 if (val != baseWire.elements_end() && val->type() == ndn::tlv::nlsr::SequenceNumber) {
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080082 m_seqNo = ndn::readNonNegativeInteger(*val);
83 ++val;
84 }
85 else {
86 BOOST_THROW_EXCEPTION(Error("Missing required SequenceNumber field"));
87 }
88
Ashlesh Gawande57a87172020-05-09 19:47:06 -070089 if (val != baseWire.elements_end() && val->type() == ndn::tlv::nlsr::ExpirationTime) {
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080090 m_expirationTimePoint = ndn::time::fromString(readString(*val));
91 }
92 else {
93 BOOST_THROW_EXCEPTION(Error("Missing required ExpirationTimePoint field"));
94 }
95}
96
97std::ostream&
98operator<<(std::ostream& os, const Lsa::Type& type)
99{
100 switch (type) {
101 case nlsr::Lsa::Type::ADJACENCY:
102 os << "ADJACENCY";
103 break;
104
105 case nlsr::Lsa::Type::COORDINATE:
106 os << "COORDINATE";
107 break;
108
109 case nlsr::Lsa::Type::NAME:
110 os << "NAME";
111 break;
112
113 default:
114 os << "BASE";
115 break;
116 }
117 return os;
118}
119
120std::istream&
121operator>>(std::istream& is, Lsa::Type& type)
122{
123 std::string typeString;
124 is >> typeString;
125 if (typeString == "ADJACENCY") {
126 type = Lsa::Type::ADJACENCY;
127 }
128 else if (typeString == "COORDINATE") {
129 type = Lsa::Type::COORDINATE;
130 }
131 else if (typeString == "NAME") {
132 type = Lsa::Type::NAME;
133 }
134 else {
135 type = Lsa::Type::BASE;
136 }
137 return is;
138}
139
140std::string
141Lsa::toString() const
142{
143 std::ostringstream os;
144 auto duration = getExpirationTimePoint() - ndn::time::system_clock::now();
145 os << " " << getType() << " LSA:\n"
146 << " Origin Router : " << getOriginRouter() << "\n"
147 << " Sequence Number : " << getSeqNo() << "\n"
148 << " Expires in : " << ndn::time::duration_cast<ndn::time::milliseconds>(duration)
149 << "\n";
150 return os.str();
151}
152
153} // namespace nlsr