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 | |
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 | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 38 | template<ndn::encoding::Tag TAG> |
| 39 | size_t |
| 40 | Lsa::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 | |
| 59 | NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(Lsa); |
| 60 | |
| 61 | void |
| 62 | Lsa::wireDecode(const ndn::Block& wire) |
| 63 | { |
| 64 | m_originRouter.clear(); |
| 65 | m_seqNo = 0; |
| 66 | |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 67 | ndn::Block baseWire = wire; |
| 68 | baseWire.parse(); |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 69 | |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 70 | auto val = baseWire.elements_begin(); |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 71 | |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 72 | if (val != baseWire.elements_end() && val->type() == ndn::tlv::Name) { |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 73 | m_originRouter.wireDecode(*val); |
| 74 | } |
| 75 | else { |
| 76 | BOOST_THROW_EXCEPTION(Error("OriginRouter: Missing required Name field")); |
| 77 | } |
| 78 | |
| 79 | ++val; |
| 80 | |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 81 | if (val != baseWire.elements_end() && val->type() == ndn::tlv::nlsr::SequenceNumber) { |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 82 | m_seqNo = ndn::readNonNegativeInteger(*val); |
| 83 | ++val; |
| 84 | } |
| 85 | else { |
| 86 | BOOST_THROW_EXCEPTION(Error("Missing required SequenceNumber field")); |
| 87 | } |
| 88 | |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 89 | if (val != baseWire.elements_end() && val->type() == ndn::tlv::nlsr::ExpirationTime) { |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 90 | m_expirationTimePoint = ndn::time::fromString(readString(*val)); |
| 91 | } |
| 92 | else { |
| 93 | BOOST_THROW_EXCEPTION(Error("Missing required ExpirationTimePoint field")); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | std::ostream& |
| 98 | operator<<(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 | |
| 120 | std::istream& |
| 121 | operator>>(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 | |
| 140 | std::string |
| 141 | Lsa::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 |