blob: 45307b2af9215b31d71959e5453448e143ddedb9 [file] [log] [blame]
Nick Gordonb50e51b2016-07-22 16:05:57 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Ashlesh Gawande0421bc62020-05-08 20:42:19 -07002/*
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -04003 * Copyright (c) 2014-2022, The University of Memphis,
Nick Gordonb50e51b2016-07-22 16:05:57 -05004 * Regents of the University of California
5 *
6 * This file is part of NLSR (Named-data Link State Routing).
7 * See AUTHORS.md for complete list of NLSR authors and contributors.
8 *
9 * NLSR is free software: you can redistribute it and/or modify it under the terms
10 * of the GNU General Public License as published by the Free Software Foundation,
11 * either version 3 of the License, or (at your option) any later version.
12 *
13 * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
14 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
Ashlesh Gawande0421bc62020-05-08 20:42:19 -070019 */
Nick Gordonb50e51b2016-07-22 16:05:57 -050020
21#include "routing-table-entry.hpp"
22#include "nexthop-list.hpp"
Ashlesh Gawande0421bc62020-05-08 20:42:19 -070023#include "tlv-nlsr.hpp"
Nick Gordonb50e51b2016-07-22 16:05:57 -050024
25namespace nlsr {
26
Ashlesh Gawande0421bc62020-05-08 20:42:19 -070027template<ndn::encoding::Tag TAG>
28size_t
29RoutingTableEntry::wireEncode(ndn::EncodingImpl<TAG>& block) const
30{
31 size_t totalLength = 0;
32
33 for (auto it = m_nexthopList.rbegin(); it != m_nexthopList.rend(); ++it) {
34 totalLength += it->wireEncode(block);
35 }
36
37 totalLength += m_destination.wireEncode(block);
38
39 totalLength += block.prependVarNumber(totalLength);
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -040040 totalLength += block.prependVarNumber(nlsr::tlv::RoutingTableEntry);
Ashlesh Gawande0421bc62020-05-08 20:42:19 -070041
42 return totalLength;
43}
44
45NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(RoutingTableEntry);
46
47const ndn::Block&
48RoutingTableEntry::wireEncode() const
49{
50 if (m_wire.hasWire()) {
51 return m_wire;
52 }
53
54 ndn::EncodingEstimator estimator;
55 size_t estimatedSize = wireEncode(estimator);
56
57 ndn::EncodingBuffer buffer(estimatedSize, 0);
58 wireEncode(buffer);
59
60 m_wire = buffer.block();
61
62 return m_wire;
63}
64
65void
66RoutingTableEntry::wireDecode(const ndn::Block& wire)
67{
68 m_nexthopList.clear();
69
70 m_wire = wire;
71
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -040072 if (m_wire.type() != nlsr::tlv::RoutingTableEntry) {
Davide Pesaventod90338d2021-01-07 17:50:05 -050073 NDN_THROW(Error("RoutingTableEntry", m_wire.type()));
Ashlesh Gawande0421bc62020-05-08 20:42:19 -070074 }
75
76 m_wire.parse();
Ashlesh Gawande0421bc62020-05-08 20:42:19 -070077 auto val = m_wire.elements_begin();
78
79 if (val != m_wire.elements_end() && val->type() == ndn::tlv::Name) {
80 m_destination.wireDecode(*val);
81 ++val;
82 }
83 else {
Davide Pesaventod90338d2021-01-07 17:50:05 -050084 NDN_THROW(Error("Missing required Name field"));
Ashlesh Gawande0421bc62020-05-08 20:42:19 -070085 }
86
87 for (; val != m_wire.elements_end(); ++val) {
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -040088 if (val->type() == nlsr::tlv::NextHop) {
Ashlesh Gawande0421bc62020-05-08 20:42:19 -070089 m_nexthopList.addNextHop(NextHop(*val));
90 }
91 else {
Davide Pesaventod90338d2021-01-07 17:50:05 -050092 NDN_THROW(Error("NextHop", val->type()));
Ashlesh Gawande0421bc62020-05-08 20:42:19 -070093 }
94 }
95}
96
Nick Gordonb50e51b2016-07-22 16:05:57 -050097std::ostream&
98operator<<(std::ostream& os, const RoutingTableEntry& rte)
99{
Davide Pesaventod90338d2021-01-07 17:50:05 -0500100 return os << " Destination: " << rte.getDestination() << "\n"
101 << rte.getNexthopList() << "\n";
Nick Gordonb50e51b2016-07-22 16:05:57 -0500102}
103
104} // namespace nlsr