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