Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Ashlesh Gawande | 0421bc6 | 2020-05-08 20:42:19 -0700 | [diff] [blame] | 2 | /* |
Junxiao Shi | 153fbc1 | 2024-01-09 23:37:23 +0000 | [diff] [blame^] | 3 | * Copyright (c) 2014-2024, The University of Memphis, |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 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 Gawande | 0421bc6 | 2020-05-08 20:42:19 -0700 | [diff] [blame] | 20 | */ |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 21 | |
| 22 | #include "lsa.hpp" |
Ashlesh Gawande | 0421bc6 | 2020-05-08 20:42:19 -0700 | [diff] [blame] | 23 | #include "tlv-nlsr.hpp" |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 24 | |
| 25 | namespace nlsr { |
| 26 | |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 27 | Lsa::Lsa(const ndn::Name& originRouter, uint64_t seqNo, |
Davide Pesavento | 658fd85 | 2023-05-10 22:15:03 -0400 | [diff] [blame] | 28 | ndn::time::system_clock::time_point expirationTimePoint) |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 29 | : m_originRouter(originRouter) |
| 30 | , m_seqNo(seqNo) |
| 31 | , m_expirationTimePoint(expirationTimePoint) |
| 32 | { |
| 33 | } |
| 34 | |
Ashlesh Gawande | 5d93aa5 | 2020-06-13 18:57:45 -0700 | [diff] [blame] | 35 | Lsa::Lsa(const Lsa& lsa) |
| 36 | : m_originRouter(lsa.getOriginRouter()) |
| 37 | , m_seqNo(lsa.getSeqNo()) |
| 38 | , m_expirationTimePoint(lsa.getExpirationTimePoint()) |
| 39 | { |
| 40 | } |
| 41 | |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 42 | template<ndn::encoding::Tag TAG> |
| 43 | size_t |
| 44 | Lsa::wireEncode(ndn::EncodingImpl<TAG>& encoder) const |
| 45 | { |
| 46 | size_t totalLength = 0; |
| 47 | |
| 48 | totalLength += prependStringBlock(encoder, |
Davide Pesavento | c1d0e8e | 2022-06-15 14:26:02 -0400 | [diff] [blame] | 49 | nlsr::tlv::ExpirationTime, |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 50 | ndn::time::toString(m_expirationTimePoint)); |
| 51 | |
Davide Pesavento | c1d0e8e | 2022-06-15 14:26:02 -0400 | [diff] [blame] | 52 | totalLength += prependNonNegativeIntegerBlock(encoder, nlsr::tlv::SequenceNumber, m_seqNo); |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 53 | |
| 54 | totalLength += m_originRouter.wireEncode(encoder); |
| 55 | |
| 56 | totalLength += encoder.prependVarNumber(totalLength); |
Davide Pesavento | c1d0e8e | 2022-06-15 14:26:02 -0400 | [diff] [blame] | 57 | totalLength += encoder.prependVarNumber(nlsr::tlv::Lsa); |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 58 | |
| 59 | return totalLength; |
| 60 | } |
| 61 | |
| 62 | NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(Lsa); |
| 63 | |
| 64 | void |
| 65 | Lsa::wireDecode(const ndn::Block& wire) |
| 66 | { |
| 67 | m_originRouter.clear(); |
| 68 | m_seqNo = 0; |
| 69 | |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 70 | ndn::Block baseWire = wire; |
| 71 | baseWire.parse(); |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 72 | |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 73 | auto val = baseWire.elements_begin(); |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 74 | |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 75 | if (val != baseWire.elements_end() && val->type() == ndn::tlv::Name) { |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 76 | m_originRouter.wireDecode(*val); |
| 77 | } |
| 78 | else { |
Davide Pesavento | d90338d | 2021-01-07 17:50:05 -0500 | [diff] [blame] | 79 | NDN_THROW(Error("OriginRouter: Missing required Name field")); |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | ++val; |
| 83 | |
Davide Pesavento | c1d0e8e | 2022-06-15 14:26:02 -0400 | [diff] [blame] | 84 | if (val != baseWire.elements_end() && val->type() == nlsr::tlv::SequenceNumber) { |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 85 | m_seqNo = ndn::readNonNegativeInteger(*val); |
| 86 | ++val; |
| 87 | } |
| 88 | else { |
Davide Pesavento | d90338d | 2021-01-07 17:50:05 -0500 | [diff] [blame] | 89 | NDN_THROW(Error("Missing required SequenceNumber field")); |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 90 | } |
| 91 | |
Davide Pesavento | c1d0e8e | 2022-06-15 14:26:02 -0400 | [diff] [blame] | 92 | if (val != baseWire.elements_end() && val->type() == nlsr::tlv::ExpirationTime) { |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 93 | m_expirationTimePoint = ndn::time::fromString(readString(*val)); |
| 94 | } |
| 95 | else { |
Davide Pesavento | d90338d | 2021-01-07 17:50:05 -0500 | [diff] [blame] | 96 | NDN_THROW(Error("Missing required ExpirationTime field")); |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 97 | } |
| 98 | } |
| 99 | |
| 100 | std::ostream& |
Junxiao Shi | 153fbc1 | 2024-01-09 23:37:23 +0000 | [diff] [blame^] | 101 | operator<<(std::ostream& os, const Lsa& lsa) |
| 102 | { |
| 103 | auto duration = lsa.m_expirationTimePoint - ndn::time::system_clock::now(); |
| 104 | os << " " << lsa.getType() << " LSA:\n" |
| 105 | << " Origin Router : " << lsa.m_originRouter << "\n" |
| 106 | << " Sequence Number : " << lsa.m_seqNo << "\n" |
| 107 | << " Expires in : " << ndn::time::duration_cast<ndn::time::milliseconds>(duration) |
| 108 | << "\n"; |
| 109 | lsa.print(os); |
| 110 | return os; |
| 111 | } |
| 112 | |
| 113 | std::ostream& |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 114 | operator<<(std::ostream& os, const Lsa::Type& type) |
| 115 | { |
| 116 | switch (type) { |
Davide Pesavento | e28d875 | 2022-03-19 03:55:25 -0400 | [diff] [blame] | 117 | case Lsa::Type::ADJACENCY: |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 118 | os << "ADJACENCY"; |
| 119 | break; |
Davide Pesavento | e28d875 | 2022-03-19 03:55:25 -0400 | [diff] [blame] | 120 | case Lsa::Type::COORDINATE: |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 121 | os << "COORDINATE"; |
| 122 | break; |
Davide Pesavento | e28d875 | 2022-03-19 03:55:25 -0400 | [diff] [blame] | 123 | case Lsa::Type::NAME: |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 124 | os << "NAME"; |
| 125 | break; |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 126 | default: |
| 127 | os << "BASE"; |
| 128 | break; |
| 129 | } |
| 130 | return os; |
| 131 | } |
| 132 | |
| 133 | std::istream& |
| 134 | operator>>(std::istream& is, Lsa::Type& type) |
| 135 | { |
| 136 | std::string typeString; |
| 137 | is >> typeString; |
| 138 | if (typeString == "ADJACENCY") { |
| 139 | type = Lsa::Type::ADJACENCY; |
| 140 | } |
| 141 | else if (typeString == "COORDINATE") { |
| 142 | type = Lsa::Type::COORDINATE; |
| 143 | } |
| 144 | else if (typeString == "NAME") { |
| 145 | type = Lsa::Type::NAME; |
| 146 | } |
| 147 | else { |
| 148 | type = Lsa::Type::BASE; |
| 149 | } |
| 150 | return is; |
| 151 | } |
| 152 | |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 153 | } // namespace nlsr |