blob: 8193a60a9cad12342155d7c584b717bb5437236c [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Ashlesh Gawande30d96e42021-03-21 19:15:33 -07002/*
Junxiao Shif4674672024-01-06 02:27:36 +00003 * Copyright (c) 2014-2024, The University of Memphis,
Vince Lehmanc2e51f62015-01-20 15:03:11 -06004 * Regents of the University of California,
5 * Arizona Board of Regents.
akmhoque3d06e792014-05-27 16:23:20 -05006 *
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 Gawande0421bc62020-05-08 20:42:19 -070020 */
Vince Lehmanc2e51f62015-01-20 15:03:11 -060021
akmhoque53353462014-04-22 08:43:45 -050022#include "sync-logic-handler.hpp"
Davide Pesavento1954a0c2022-09-30 15:56:04 -040023#include "hello-protocol.hpp"
Nick Gordon9eac4d92017-08-29 17:31:29 -050024#include "logger.hpp"
Ashlesh Gawande32ec3fd2018-07-18 13:42:32 -050025#include "utility/name-helper.hpp"
Vince Lehman904c2412014-09-23 19:36:11 -050026
akmhoque53353462014-04-22 08:43:45 -050027namespace nlsr {
28
Ashlesh Gawande32ec3fd2018-07-18 13:42:32 -050029INIT_LOGGER(SyncLogicHandler);
30
Davide Pesavento1954a0c2022-09-30 15:56:04 -040031const std::string LSA_COMPONENT{"LSA"};
32
33SyncLogicHandler::SyncLogicHandler(ndn::Face& face, ndn::KeyChain& keyChain,
Junxiao Shif4674672024-01-06 02:27:36 +000034 IsLsaNew isLsaNew, const SyncLogicOptions& opts)
Davide Pesavento1954a0c2022-09-30 15:56:04 -040035 : m_isLsaNew(std::move(isLsaNew))
Junxiao Shif4674672024-01-06 02:27:36 +000036 , m_routerPrefix(opts.routerPrefix)
37 , m_hyperbolicState(opts.hyperbolicState)
38 , m_nameLsaUserPrefix(makeLsaUserPrefix(opts.userPrefix, Lsa::Type::NAME))
39 , m_adjLsaUserPrefix(makeLsaUserPrefix(opts.userPrefix, Lsa::Type::ADJACENCY))
40 , m_coorLsaUserPrefix(makeLsaUserPrefix(opts.userPrefix, Lsa::Type::COORDINATE))
41 , m_syncLogic(face, keyChain, opts.syncProtocol, opts.syncPrefix,
42 m_nameLsaUserPrefix, opts.syncInterestLifetime,
Alexander Afanasyev135288c2022-04-23 23:06:56 -040043 std::bind(&SyncLogicHandler::processUpdate, this, _1, _2, _3))
Vince Lehman0bcf9a32014-12-10 11:24:45 -060044{
Junxiao Shif4674672024-01-06 02:27:36 +000045 if (m_hyperbolicState != HYPERBOLIC_STATE_ON) {
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -050046 m_syncLogic.addUserNode(m_adjLsaUserPrefix);
Vince Lehman9d097802015-03-16 17:55:59 -050047 }
Ashlesh Gawande0421bc62020-05-08 20:42:19 -070048
Junxiao Shif4674672024-01-06 02:27:36 +000049 if (m_hyperbolicState != HYPERBOLIC_STATE_OFF) {
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -050050 m_syncLogic.addUserNode(m_coorLsaUserPrefix);
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050051 }
akmhoque53353462014-04-22 08:43:45 -050052}
53
54void
Alexander Afanasyev135288c2022-04-23 23:06:56 -040055SyncLogicHandler::processUpdate(const ndn::Name& updateName, uint64_t highSeq, uint64_t incomingFaceId)
akmhoque53353462014-04-22 08:43:45 -050056{
Ashlesh Gawande32ec3fd2018-07-18 13:42:32 -050057 NLSR_LOG_DEBUG("Update Name: " << updateName << " Seq no: " << highSeq);
Vince Lehman904c2412014-09-23 19:36:11 -050058
Davide Pesavento1954a0c2022-09-30 15:56:04 -040059 int32_t nlsrPosition = util::getNameComponentPosition(updateName, HelloProtocol::NLSR_COMPONENT);
Ashlesh Gawande0421bc62020-05-08 20:42:19 -070060 int32_t lsaPosition = util::getNameComponentPosition(updateName, LSA_COMPONENT);
Vince Lehman904c2412014-09-23 19:36:11 -050061
Ashlesh Gawande32ec3fd2018-07-18 13:42:32 -050062 if (nlsrPosition < 0 || lsaPosition < 0) {
63 NLSR_LOG_WARN("Received malformed sync update");
64 return;
akmhoque53353462014-04-22 08:43:45 -050065 }
Ashlesh Gawande32ec3fd2018-07-18 13:42:32 -050066
Davide Pesavento1954a0c2022-09-30 15:56:04 -040067 ndn::Name networkName = updateName.getSubName(1, nlsrPosition - 1);
Ashlesh Gawande32ec3fd2018-07-18 13:42:32 -050068 ndn::Name routerName = updateName.getSubName(lsaPosition + 1).getPrefix(-1);
Ashlesh Gawande32ec3fd2018-07-18 13:42:32 -050069 ndn::Name originRouter = networkName;
70 originRouter.append(routerName);
71
Alexander Afanasyev135288c2022-04-23 23:06:56 -040072 processUpdateFromSync(originRouter, updateName, highSeq, incomingFaceId);
akmhoque53353462014-04-22 08:43:45 -050073}
74
75void
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050076SyncLogicHandler::processUpdateFromSync(const ndn::Name& originRouter,
Alexander Afanasyev135288c2022-04-23 23:06:56 -040077 const ndn::Name& updateName, uint64_t seqNo,
78 uint64_t incomingFaceId)
akmhoque53353462014-04-22 08:43:45 -050079{
dmcoomes5bcb39e2017-10-31 15:07:55 -050080 NLSR_LOG_DEBUG("Origin Router of update: " << originRouter);
akmhoque53353462014-04-22 08:43:45 -050081
Junxiao Shif4674672024-01-06 02:27:36 +000082 if (originRouter == m_routerPrefix) {
83 // A router should not try to fetch its own LSA
84 return;
85 }
Vince Lehman904c2412014-09-23 19:36:11 -050086
Junxiao Shif4674672024-01-06 02:27:36 +000087 auto lsaType = boost::lexical_cast<Lsa::Type>(updateName.get(-1).toUri());
88 NLSR_LOG_DEBUG("Received sync update with higher " << lsaType <<
89 " sequence number than entry in LSDB");
Vince Lehman904c2412014-09-23 19:36:11 -050090
Junxiao Shif4674672024-01-06 02:27:36 +000091 if (m_isLsaNew(originRouter, lsaType, seqNo, incomingFaceId)) {
92 if (lsaType == Lsa::Type::ADJACENCY && seqNo != 0 &&
93 m_hyperbolicState == HYPERBOLIC_STATE_ON) {
94 NLSR_LOG_ERROR("Got an update for adjacency LSA when hyperbolic routing "
95 "is enabled. Not going to fetch.");
96 return;
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050097 }
Junxiao Shif4674672024-01-06 02:27:36 +000098
99 if (lsaType == Lsa::Type::COORDINATE && seqNo != 0 &&
100 m_hyperbolicState == HYPERBOLIC_STATE_OFF) {
101 NLSR_LOG_ERROR("Got an update for coordinate LSA when link-state "
102 "is enabled. Not going to fetch.");
103 return;
104 }
105
106 onNewLsa(updateName, seqNo, originRouter, incomingFaceId);
akmhoque53353462014-04-22 08:43:45 -0500107 }
108}
109
Vince Lehman904c2412014-09-23 19:36:11 -0500110void
Junxiao Shif4674672024-01-06 02:27:36 +0000111SyncLogicHandler::publishRoutingUpdate(Lsa::Type type, uint64_t seqNo)
Vince Lehman904c2412014-09-23 19:36:11 -0500112{
Nick Gordon727d4832017-10-13 18:04:25 -0500113 switch (type) {
114 case Lsa::Type::ADJACENCY:
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500115 m_syncLogic.publishUpdate(m_adjLsaUserPrefix, seqNo);
Nick Gordon727d4832017-10-13 18:04:25 -0500116 break;
117 case Lsa::Type::COORDINATE:
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500118 m_syncLogic.publishUpdate(m_coorLsaUserPrefix, seqNo);
Nick Gordon727d4832017-10-13 18:04:25 -0500119 break;
120 case Lsa::Type::NAME:
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500121 m_syncLogic.publishUpdate(m_nameLsaUserPrefix, seqNo);
Nick Gordon727d4832017-10-13 18:04:25 -0500122 break;
123 default:
124 break;
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500125 }
Vince Lehman904c2412014-09-23 19:36:11 -0500126}
127
Nick Gordonfad8e252016-08-11 14:21:38 -0500128} // namespace nlsr