blob: 3e290672e4042102d9c6ce82eab9c30a95261f3e [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"
Vince Lehman0a7da612014-10-29 14:39:29 -050023#include "common.hpp"
Vince Lehman904c2412014-09-23 19:36:11 -050024#include "conf-parameter.hpp"
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080025#include "lsa/lsa.hpp"
Nick Gordon9eac4d92017-08-29 17:31:29 -050026#include "logger.hpp"
Ashlesh Gawande32ec3fd2018-07-18 13:42:32 -050027#include "utility/name-helper.hpp"
Vince Lehman904c2412014-09-23 19:36:11 -050028
Ashlesh Gawande30d96e42021-03-21 19:15:33 -070029#include <boost/lexical_cast.hpp>
30
akmhoque53353462014-04-22 08:43:45 -050031namespace nlsr {
32
Ashlesh Gawande32ec3fd2018-07-18 13:42:32 -050033INIT_LOGGER(SyncLogicHandler);
34
Nick Gordon563611e2018-01-23 13:44:36 -060035SyncLogicHandler::SyncLogicHandler(ndn::Face& face, const IsLsaNew& isLsaNew,
36 const ConfParameter& conf)
Davide Pesaventoa08dc3f2018-05-24 00:40:28 -040037 : onNewLsa(std::make_unique<OnNewLsa>())
Nick Gordon9eac4d92017-08-29 17:31:29 -050038 , m_syncFace(face)
39 , m_isLsaNew(isLsaNew)
Vince Lehman0bcf9a32014-12-10 11:24:45 -060040 , m_confParam(conf)
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080041 , m_nameLsaUserPrefix(ndn::Name(m_confParam.getSyncUserPrefix()).append(boost::lexical_cast<std::string>(Lsa::Type::NAME)))
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -050042 , m_syncLogic(m_syncFace, m_confParam.getSyncProtocol(), m_confParam.getSyncPrefix(),
43 m_nameLsaUserPrefix, m_confParam.getSyncInterestLifetime(),
Alexander Afanasyev135288c2022-04-23 23:06:56 -040044 std::bind(&SyncLogicHandler::processUpdate, this, _1, _2, _3))
Vince Lehman0bcf9a32014-12-10 11:24:45 -060045{
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -050046 m_adjLsaUserPrefix = ndn::Name(m_confParam.getSyncUserPrefix())
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080047 .append(boost::lexical_cast<std::string>(Lsa::Type::ADJACENCY));
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -050048 m_coorLsaUserPrefix = ndn::Name(m_confParam.getSyncUserPrefix())
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080049 .append(boost::lexical_cast<std::string>(Lsa::Type::COORDINATE));
Vince Lehman0bcf9a32014-12-10 11:24:45 -060050
Ashlesh Gawande0421bc62020-05-08 20:42:19 -070051 if (m_confParam.getHyperbolicState() != HYPERBOLIC_STATE_ON) {
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -050052 m_syncLogic.addUserNode(m_adjLsaUserPrefix);
Vince Lehman9d097802015-03-16 17:55:59 -050053 }
Ashlesh Gawande0421bc62020-05-08 20:42:19 -070054
55 if (m_confParam.getHyperbolicState() != HYPERBOLIC_STATE_OFF) {
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -050056 m_syncLogic.addUserNode(m_coorLsaUserPrefix);
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050057 }
akmhoque53353462014-04-22 08:43:45 -050058}
59
60void
Alexander Afanasyev135288c2022-04-23 23:06:56 -040061SyncLogicHandler::processUpdate(const ndn::Name& updateName, uint64_t highSeq, uint64_t incomingFaceId)
akmhoque53353462014-04-22 08:43:45 -050062{
Ashlesh Gawande32ec3fd2018-07-18 13:42:32 -050063 NLSR_LOG_DEBUG("Update Name: " << updateName << " Seq no: " << highSeq);
Vince Lehman904c2412014-09-23 19:36:11 -050064
Ashlesh Gawande0421bc62020-05-08 20:42:19 -070065 int32_t nlsrPosition = util::getNameComponentPosition(updateName, NLSR_COMPONENT);
66 int32_t lsaPosition = util::getNameComponentPosition(updateName, LSA_COMPONENT);
Vince Lehman904c2412014-09-23 19:36:11 -050067
Ashlesh Gawande32ec3fd2018-07-18 13:42:32 -050068 if (nlsrPosition < 0 || lsaPosition < 0) {
69 NLSR_LOG_WARN("Received malformed sync update");
70 return;
akmhoque53353462014-04-22 08:43:45 -050071 }
Ashlesh Gawande32ec3fd2018-07-18 13:42:32 -050072
73 ndn::Name networkName = updateName.getSubName(1, nlsrPosition-1);
74 ndn::Name routerName = updateName.getSubName(lsaPosition + 1).getPrefix(-1);
75
76 ndn::Name originRouter = networkName;
77 originRouter.append(routerName);
78
Alexander Afanasyev135288c2022-04-23 23:06:56 -040079 processUpdateFromSync(originRouter, updateName, highSeq, incomingFaceId);
akmhoque53353462014-04-22 08:43:45 -050080}
81
82void
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050083SyncLogicHandler::processUpdateFromSync(const ndn::Name& originRouter,
Alexander Afanasyev135288c2022-04-23 23:06:56 -040084 const ndn::Name& updateName, uint64_t seqNo,
85 uint64_t incomingFaceId)
akmhoque53353462014-04-22 08:43:45 -050086{
dmcoomes5bcb39e2017-10-31 15:07:55 -050087 NLSR_LOG_DEBUG("Origin Router of update: " << originRouter);
akmhoque53353462014-04-22 08:43:45 -050088
Nick Gordon0f1bf1d2017-06-22 15:40:27 -050089 // A router should not try to fetch its own LSA
90 if (originRouter != m_confParam.getRouterPrefix()) {
Vince Lehman904c2412014-09-23 19:36:11 -050091
Nick Gordon727d4832017-10-13 18:04:25 -050092 Lsa::Type lsaType;
93 std::istringstream(updateName.get(updateName.size()-1).toUri()) >> lsaType;
Vince Lehman904c2412014-09-23 19:36:11 -050094
Ashlesh Gawande85998a12017-12-07 22:22:13 -060095 NLSR_LOG_DEBUG("Received sync update with higher " << lsaType <<
96 " sequence number than entry in LSDB");
Nick Gordon0f1bf1d2017-06-22 15:40:27 -050097
Alexander Afanasyev135288c2022-04-23 23:06:56 -040098 if (m_isLsaNew(originRouter, lsaType, seqNo, incomingFaceId)) {
Nick Gordon727d4832017-10-13 18:04:25 -050099 if (lsaType == Lsa::Type::ADJACENCY && seqNo != 0 &&
Nick Gordon0f1bf1d2017-06-22 15:40:27 -0500100 m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_ON) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600101 NLSR_LOG_ERROR("Got an update for adjacency LSA when hyperbolic routing " <<
102 "is enabled. Not going to fetch.");
Nick Gordon0f1bf1d2017-06-22 15:40:27 -0500103 return;
104 }
105
Nick Gordon727d4832017-10-13 18:04:25 -0500106 if (lsaType == Lsa::Type::COORDINATE && seqNo != 0 &&
Nick Gordon0f1bf1d2017-06-22 15:40:27 -0500107 m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_OFF) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600108 NLSR_LOG_ERROR("Got an update for coordinate LSA when link-state " <<
109 "is enabled. Not going to fetch.");
Nick Gordon0f1bf1d2017-06-22 15:40:27 -0500110 return;
111 }
Alexander Afanasyev135288c2022-04-23 23:06:56 -0400112 (*onNewLsa)(updateName, seqNo, originRouter, incomingFaceId);
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500113 }
akmhoque53353462014-04-22 08:43:45 -0500114 }
115}
116
Vince Lehman904c2412014-09-23 19:36:11 -0500117void
Nick Gordon727d4832017-10-13 18:04:25 -0500118SyncLogicHandler::publishRoutingUpdate(const Lsa::Type& type, const uint64_t& seqNo)
Vince Lehman904c2412014-09-23 19:36:11 -0500119{
Nick Gordon727d4832017-10-13 18:04:25 -0500120 switch (type) {
121 case Lsa::Type::ADJACENCY:
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500122 m_syncLogic.publishUpdate(m_adjLsaUserPrefix, seqNo);
Nick Gordon727d4832017-10-13 18:04:25 -0500123 break;
124 case Lsa::Type::COORDINATE:
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500125 m_syncLogic.publishUpdate(m_coorLsaUserPrefix, seqNo);
Nick Gordon727d4832017-10-13 18:04:25 -0500126 break;
127 case Lsa::Type::NAME:
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500128 m_syncLogic.publishUpdate(m_nameLsaUserPrefix, seqNo);
Nick Gordon727d4832017-10-13 18:04:25 -0500129 break;
130 default:
131 break;
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500132 }
Vince Lehman904c2412014-09-23 19:36:11 -0500133}
134
Nick Gordonfad8e252016-08-11 14:21:38 -0500135} // namespace nlsr