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 | /* |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 3 | * 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 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 | |
| 30 | Lsa::Lsa(const ndn::Name& originRouter, uint32_t seqNo, |
| 31 | ndn::time::system_clock::TimePoint expirationTimePoint) |
| 32 | : m_originRouter(originRouter) |
| 33 | , m_seqNo(seqNo) |
| 34 | , m_expirationTimePoint(expirationTimePoint) |
| 35 | { |
| 36 | } |
| 37 | |
| 38 | ndn::Name |
| 39 | Lsa::getKey() const |
| 40 | { |
| 41 | return ndn::Name(m_originRouter).append(boost::lexical_cast<std::string>((getType()))); |
| 42 | } |
| 43 | |
| 44 | template<ndn::encoding::Tag TAG> |
| 45 | size_t |
| 46 | Lsa::wireEncode(ndn::EncodingImpl<TAG>& encoder) const |
| 47 | { |
| 48 | size_t totalLength = 0; |
| 49 | |
| 50 | totalLength += prependStringBlock(encoder, |
| 51 | ndn::tlv::nlsr::ExpirationTime, |
| 52 | ndn::time::toString(m_expirationTimePoint)); |
| 53 | |
| 54 | totalLength += prependNonNegativeIntegerBlock(encoder, ndn::tlv::nlsr::SequenceNumber, |
| 55 | m_seqNo); |
| 56 | |
| 57 | totalLength += m_originRouter.wireEncode(encoder); |
| 58 | |
| 59 | totalLength += encoder.prependVarNumber(totalLength); |
| 60 | totalLength += encoder.prependVarNumber(ndn::tlv::nlsr::Lsa); |
| 61 | |
| 62 | return totalLength; |
| 63 | } |
| 64 | |
| 65 | NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(Lsa); |
| 66 | |
| 67 | void |
| 68 | Lsa::wireDecode(const ndn::Block& wire) |
| 69 | { |
| 70 | m_originRouter.clear(); |
| 71 | m_seqNo = 0; |
| 72 | |
| 73 | m_baseWire = wire; |
| 74 | |
| 75 | if (m_baseWire.type() != ndn::tlv::nlsr::Lsa) { |
| 76 | std::stringstream error; |
| 77 | error << "Expected Lsa Block, but Block is of a different type: #" |
| 78 | << m_baseWire.type(); |
| 79 | BOOST_THROW_EXCEPTION(Error(error.str())); |
| 80 | } |
| 81 | |
| 82 | m_baseWire.parse(); |
| 83 | |
| 84 | ndn::Block::element_const_iterator val = m_baseWire.elements_begin(); |
| 85 | |
| 86 | if (val != m_baseWire.elements_end() && val->type() == ndn::tlv::Name) { |
| 87 | m_originRouter.wireDecode(*val); |
| 88 | } |
| 89 | else { |
| 90 | BOOST_THROW_EXCEPTION(Error("OriginRouter: Missing required Name field")); |
| 91 | } |
| 92 | |
| 93 | ++val; |
| 94 | |
| 95 | if (val != m_baseWire.elements_end() && val->type() == ndn::tlv::nlsr::SequenceNumber) { |
| 96 | m_seqNo = ndn::readNonNegativeInteger(*val); |
| 97 | ++val; |
| 98 | } |
| 99 | else { |
| 100 | BOOST_THROW_EXCEPTION(Error("Missing required SequenceNumber field")); |
| 101 | } |
| 102 | |
| 103 | if (val != m_baseWire.elements_end() && val->type() == ndn::tlv::nlsr::ExpirationTime) { |
| 104 | m_expirationTimePoint = ndn::time::fromString(readString(*val)); |
| 105 | } |
| 106 | else { |
| 107 | BOOST_THROW_EXCEPTION(Error("Missing required ExpirationTimePoint field")); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | std::ostream& |
| 112 | operator<<(std::ostream& os, const Lsa::Type& type) |
| 113 | { |
| 114 | switch (type) { |
| 115 | case nlsr::Lsa::Type::ADJACENCY: |
| 116 | os << "ADJACENCY"; |
| 117 | break; |
| 118 | |
| 119 | case nlsr::Lsa::Type::COORDINATE: |
| 120 | os << "COORDINATE"; |
| 121 | break; |
| 122 | |
| 123 | case nlsr::Lsa::Type::NAME: |
| 124 | os << "NAME"; |
| 125 | break; |
| 126 | |
| 127 | default: |
| 128 | os << "BASE"; |
| 129 | break; |
| 130 | } |
| 131 | return os; |
| 132 | } |
| 133 | |
| 134 | std::istream& |
| 135 | operator>>(std::istream& is, Lsa::Type& type) |
| 136 | { |
| 137 | std::string typeString; |
| 138 | is >> typeString; |
| 139 | if (typeString == "ADJACENCY") { |
| 140 | type = Lsa::Type::ADJACENCY; |
| 141 | } |
| 142 | else if (typeString == "COORDINATE") { |
| 143 | type = Lsa::Type::COORDINATE; |
| 144 | } |
| 145 | else if (typeString == "NAME") { |
| 146 | type = Lsa::Type::NAME; |
| 147 | } |
| 148 | else { |
| 149 | type = Lsa::Type::BASE; |
| 150 | } |
| 151 | return is; |
| 152 | } |
| 153 | |
| 154 | std::string |
| 155 | Lsa::toString() const |
| 156 | { |
| 157 | std::ostringstream os; |
| 158 | auto duration = getExpirationTimePoint() - ndn::time::system_clock::now(); |
| 159 | os << " " << getType() << " LSA:\n" |
| 160 | << " Origin Router : " << getOriginRouter() << "\n" |
| 161 | << " Sequence Number : " << getSeqNo() << "\n" |
| 162 | << " Expires in : " << ndn::time::duration_cast<ndn::time::milliseconds>(duration) |
| 163 | << "\n"; |
| 164 | return os.str(); |
| 165 | } |
| 166 | |
| 167 | } // namespace nlsr |