blob: a52a4f70882f630a07b52cb5313d91401eaf235d [file] [log] [blame]
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
awlane6d7c37f2025-03-07 11:53:58 -06003 * Copyright (c) 2014-2025, 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/>.
20 */
21
22#ifndef NLSR_LSA_LSA_HPP
23#define NLSR_LSA_LSA_HPP
24
Junxiao Shi20377642023-08-24 23:55:40 +000025#include "common.hpp"
awlane6d7c37f2025-03-07 11:53:58 -060026#include "name-prefix-list.hpp"
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080027#include "test-access-control.hpp"
28
29#include <ndn-cxx/util/scheduler.hpp>
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080030
Junxiao Shi20377642023-08-24 23:55:40 +000031#include <list>
32
awlane6d7c37f2025-03-07 11:53:58 -060033
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080034namespace nlsr {
35
Junxiao Shi153fbc12024-01-09 23:37:23 +000036/**
37 * @brief Represents a Link State Announcement (LSA).
38 *
39 * The base level LSA is encoded as:
40 * @code{.abnf}
41 * Lsa = LSA-TYPE TLV-LENGTH
42 * Name ; origin router
43 * SequenceNumber
44 * ExpirationTime
45 * @endcode
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080046 */
47class Lsa
48{
49public:
50 class Error : public ndn::tlv::Error
51 {
52 public:
Davide Pesaventod90338d2021-01-07 17:50:05 -050053 using ndn::tlv::Error::Error;
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080054 };
55
56 enum class Type {
57 ADJACENCY,
58 COORDINATE,
59 NAME,
60 BASE
61 };
62
63protected:
Junxiao Shi153fbc12024-01-09 23:37:23 +000064 Lsa() = default;
65
Ashlesh Gawande57a87172020-05-09 19:47:06 -070066 Lsa(const ndn::Name& originRouter, uint64_t seqNo,
Davide Pesavento658fd852023-05-10 22:15:03 -040067 ndn::time::system_clock::time_point expirationTimePoint);
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080068
Junxiao Shi153fbc12024-01-09 23:37:23 +000069 explicit
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -070070 Lsa(const Lsa& lsa);
71
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080072public:
73 virtual
74 ~Lsa() = default;
75
76 virtual Type
Ashlesh Gawande57a87172020-05-09 19:47:06 -070077 getType() const = 0;
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080078
79 void
80 setSeqNo(uint64_t seqNo)
81 {
82 m_seqNo = seqNo;
Ashlesh Gawande57a87172020-05-09 19:47:06 -070083 m_wire.reset();
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080084 }
85
86 uint64_t
87 getSeqNo() const
88 {
89 return m_seqNo;
90 }
91
92 const ndn::Name&
93 getOriginRouter() const
94 {
95 return m_originRouter;
96 }
97
Davide Pesavento658fd852023-05-10 22:15:03 -040098 const ndn::time::system_clock::time_point&
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080099 getExpirationTimePoint() const
100 {
101 return m_expirationTimePoint;
102 }
103
104 void
Davide Pesavento658fd852023-05-10 22:15:03 -0400105 setExpirationTimePoint(const ndn::time::system_clock::time_point& lt)
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800106 {
107 m_expirationTimePoint = lt;
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700108 m_wire.reset();
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800109 }
110
111 void
112 setExpiringEventId(ndn::scheduler::EventId eid)
113 {
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -0700114 m_expiringEventId = eid;
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800115 }
116
awlane6d7c37f2025-03-07 11:53:58 -0600117 virtual std::tuple<bool, std::list<PrefixInfo>, std::list<PrefixInfo>>
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -0700118 update(const std::shared_ptr<Lsa>& lsa) = 0;
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800119
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700120 virtual const ndn::Block&
121 wireEncode() const = 0;
122
123protected:
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800124 template<ndn::encoding::Tag TAG>
125 size_t
126 wireEncode(ndn::EncodingImpl<TAG>& block) const;
127
128 void
129 wireDecode(const ndn::Block& wire);
130
Junxiao Shi153fbc12024-01-09 23:37:23 +0000131private:
132 virtual void
133 print(std::ostream& os) const = 0;
134
135 friend std::ostream&
136 operator<<(std::ostream& os, const Lsa& lsa);
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -0700137
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800138PUBLIC_WITH_TESTS_ELSE_PROTECTED:
139 ndn::Name m_originRouter;
140 uint64_t m_seqNo = 0;
Davide Pesavento658fd852023-05-10 22:15:03 -0400141 ndn::time::system_clock::time_point m_expirationTimePoint;
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -0700142 ndn::scheduler::ScopedEventId m_expiringEventId;
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800143
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700144 mutable ndn::Block m_wire;
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800145};
146
147NDN_CXX_DECLARE_WIRE_ENCODE_INSTANTIATIONS(Lsa);
148
149std::ostream&
150operator<<(std::ostream& os, const Lsa::Type& type);
151
152std::istream&
153operator>>(std::istream& is, Lsa::Type& type);
154
155} // namespace nlsr
156
157#endif // NLSR_LSA_LSA_HPP