blob: cd9e17a52c714de2bf38f2953aa8da4ffc0cc759 [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/*
Alexander Afanasyev135288c2022-04-23 23:06:56 -04003 * Copyright (c) 2014-2022, 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
Ashlesh Gawande30d96e42021-03-21 19:15:33 -070027#include <boost/lexical_cast.hpp>
28
akmhoque53353462014-04-22 08:43:45 -050029namespace nlsr {
30
Ashlesh Gawande32ec3fd2018-07-18 13:42:32 -050031INIT_LOGGER(SyncLogicHandler);
32
Davide Pesavento1954a0c2022-09-30 15:56:04 -040033const std::string LSA_COMPONENT{"LSA"};
34
35SyncLogicHandler::SyncLogicHandler(ndn::Face& face, ndn::KeyChain& keyChain,
36 IsLsaNew isLsaNew, const ConfParameter& conf)
37 : m_isLsaNew(std::move(isLsaNew))
Vince Lehman0bcf9a32014-12-10 11:24:45 -060038 , m_confParam(conf)
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080039 , m_nameLsaUserPrefix(ndn::Name(m_confParam.getSyncUserPrefix()).append(boost::lexical_cast<std::string>(Lsa::Type::NAME)))
Davide Pesavento1954a0c2022-09-30 15:56:04 -040040 , m_syncLogic(face, keyChain, m_confParam.getSyncProtocol(), m_confParam.getSyncPrefix(),
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -050041 m_nameLsaUserPrefix, m_confParam.getSyncInterestLifetime(),
Alexander Afanasyev135288c2022-04-23 23:06:56 -040042 std::bind(&SyncLogicHandler::processUpdate, this, _1, _2, _3))
Vince Lehman0bcf9a32014-12-10 11:24:45 -060043{
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -050044 m_adjLsaUserPrefix = ndn::Name(m_confParam.getSyncUserPrefix())
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080045 .append(boost::lexical_cast<std::string>(Lsa::Type::ADJACENCY));
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -050046 m_coorLsaUserPrefix = ndn::Name(m_confParam.getSyncUserPrefix())
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080047 .append(boost::lexical_cast<std::string>(Lsa::Type::COORDINATE));
Vince Lehman0bcf9a32014-12-10 11:24:45 -060048
Ashlesh Gawande0421bc62020-05-08 20:42:19 -070049 if (m_confParam.getHyperbolicState() != HYPERBOLIC_STATE_ON) {
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -050050 m_syncLogic.addUserNode(m_adjLsaUserPrefix);
Vince Lehman9d097802015-03-16 17:55:59 -050051 }
Ashlesh Gawande0421bc62020-05-08 20:42:19 -070052
53 if (m_confParam.getHyperbolicState() != HYPERBOLIC_STATE_OFF) {
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -050054 m_syncLogic.addUserNode(m_coorLsaUserPrefix);
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050055 }
akmhoque53353462014-04-22 08:43:45 -050056}
57
58void
Alexander Afanasyev135288c2022-04-23 23:06:56 -040059SyncLogicHandler::processUpdate(const ndn::Name& updateName, uint64_t highSeq, uint64_t incomingFaceId)
akmhoque53353462014-04-22 08:43:45 -050060{
Ashlesh Gawande32ec3fd2018-07-18 13:42:32 -050061 NLSR_LOG_DEBUG("Update Name: " << updateName << " Seq no: " << highSeq);
Vince Lehman904c2412014-09-23 19:36:11 -050062
Davide Pesavento1954a0c2022-09-30 15:56:04 -040063 int32_t nlsrPosition = util::getNameComponentPosition(updateName, HelloProtocol::NLSR_COMPONENT);
Ashlesh Gawande0421bc62020-05-08 20:42:19 -070064 int32_t lsaPosition = util::getNameComponentPosition(updateName, LSA_COMPONENT);
Vince Lehman904c2412014-09-23 19:36:11 -050065
Ashlesh Gawande32ec3fd2018-07-18 13:42:32 -050066 if (nlsrPosition < 0 || lsaPosition < 0) {
67 NLSR_LOG_WARN("Received malformed sync update");
68 return;
akmhoque53353462014-04-22 08:43:45 -050069 }
Ashlesh Gawande32ec3fd2018-07-18 13:42:32 -050070
Davide Pesavento1954a0c2022-09-30 15:56:04 -040071 ndn::Name networkName = updateName.getSubName(1, nlsrPosition - 1);
Ashlesh Gawande32ec3fd2018-07-18 13:42:32 -050072 ndn::Name routerName = updateName.getSubName(lsaPosition + 1).getPrefix(-1);
Ashlesh Gawande32ec3fd2018-07-18 13:42:32 -050073 ndn::Name originRouter = networkName;
74 originRouter.append(routerName);
75
Alexander Afanasyev135288c2022-04-23 23:06:56 -040076 processUpdateFromSync(originRouter, updateName, highSeq, incomingFaceId);
akmhoque53353462014-04-22 08:43:45 -050077}
78
79void
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050080SyncLogicHandler::processUpdateFromSync(const ndn::Name& originRouter,
Alexander Afanasyev135288c2022-04-23 23:06:56 -040081 const ndn::Name& updateName, uint64_t seqNo,
82 uint64_t incomingFaceId)
akmhoque53353462014-04-22 08:43:45 -050083{
dmcoomes5bcb39e2017-10-31 15:07:55 -050084 NLSR_LOG_DEBUG("Origin Router of update: " << originRouter);
akmhoque53353462014-04-22 08:43:45 -050085
Nick Gordon0f1bf1d2017-06-22 15:40:27 -050086 // A router should not try to fetch its own LSA
87 if (originRouter != m_confParam.getRouterPrefix()) {
Vince Lehman904c2412014-09-23 19:36:11 -050088
Nick Gordon727d4832017-10-13 18:04:25 -050089 Lsa::Type lsaType;
90 std::istringstream(updateName.get(updateName.size()-1).toUri()) >> lsaType;
Vince Lehman904c2412014-09-23 19:36:11 -050091
Ashlesh Gawande85998a12017-12-07 22:22:13 -060092 NLSR_LOG_DEBUG("Received sync update with higher " << lsaType <<
93 " sequence number than entry in LSDB");
Nick Gordon0f1bf1d2017-06-22 15:40:27 -050094
Alexander Afanasyev135288c2022-04-23 23:06:56 -040095 if (m_isLsaNew(originRouter, lsaType, seqNo, incomingFaceId)) {
Nick Gordon727d4832017-10-13 18:04:25 -050096 if (lsaType == Lsa::Type::ADJACENCY && seqNo != 0 &&
Nick Gordon0f1bf1d2017-06-22 15:40:27 -050097 m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_ON) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -060098 NLSR_LOG_ERROR("Got an update for adjacency LSA when hyperbolic routing " <<
99 "is enabled. Not going to fetch.");
Nick Gordon0f1bf1d2017-06-22 15:40:27 -0500100 return;
101 }
102
Nick Gordon727d4832017-10-13 18:04:25 -0500103 if (lsaType == Lsa::Type::COORDINATE && seqNo != 0 &&
Nick Gordon0f1bf1d2017-06-22 15:40:27 -0500104 m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_OFF) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600105 NLSR_LOG_ERROR("Got an update for coordinate LSA when link-state " <<
106 "is enabled. Not going to fetch.");
Nick Gordon0f1bf1d2017-06-22 15:40:27 -0500107 return;
108 }
Davide Pesavento1954a0c2022-09-30 15:56:04 -0400109
110 onNewLsa(updateName, seqNo, originRouter, incomingFaceId);
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500111 }
akmhoque53353462014-04-22 08:43:45 -0500112 }
113}
114
Vince Lehman904c2412014-09-23 19:36:11 -0500115void
Nick Gordon727d4832017-10-13 18:04:25 -0500116SyncLogicHandler::publishRoutingUpdate(const Lsa::Type& type, const uint64_t& seqNo)
Vince Lehman904c2412014-09-23 19:36:11 -0500117{
Nick Gordon727d4832017-10-13 18:04:25 -0500118 switch (type) {
119 case Lsa::Type::ADJACENCY:
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500120 m_syncLogic.publishUpdate(m_adjLsaUserPrefix, seqNo);
Nick Gordon727d4832017-10-13 18:04:25 -0500121 break;
122 case Lsa::Type::COORDINATE:
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500123 m_syncLogic.publishUpdate(m_coorLsaUserPrefix, seqNo);
Nick Gordon727d4832017-10-13 18:04:25 -0500124 break;
125 case Lsa::Type::NAME:
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500126 m_syncLogic.publishUpdate(m_nameLsaUserPrefix, seqNo);
Nick Gordon727d4832017-10-13 18:04:25 -0500127 break;
128 default:
129 break;
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500130 }
Vince Lehman904c2412014-09-23 19:36:11 -0500131}
132
Nick Gordonfad8e252016-08-11 14:21:38 -0500133} // namespace nlsr