blob: 4f8eba2ebd8d50d30fbdd181df13565c5bac5ed9 [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/*
3 * Copyright (c) 2014-2021, 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(),
44 std::bind(&SyncLogicHandler::processUpdate, this, _1, _2))
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
Ashlesh Gawande32ec3fd2018-07-18 13:42:32 -050061SyncLogicHandler::processUpdate(const ndn::Name& updateName, uint64_t highSeq)
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
79 processUpdateFromSync(originRouter, updateName, highSeq);
akmhoque53353462014-04-22 08:43:45 -050080}
81
82void
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050083SyncLogicHandler::processUpdateFromSync(const ndn::Name& originRouter,
Ashlesh Gawande32ec3fd2018-07-18 13:42:32 -050084 const ndn::Name& updateName, uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -050085{
dmcoomes5bcb39e2017-10-31 15:07:55 -050086 NLSR_LOG_DEBUG("Origin Router of update: " << originRouter);
akmhoque53353462014-04-22 08:43:45 -050087
Nick Gordon0f1bf1d2017-06-22 15:40:27 -050088 // A router should not try to fetch its own LSA
89 if (originRouter != m_confParam.getRouterPrefix()) {
Vince Lehman904c2412014-09-23 19:36:11 -050090
Nick Gordon727d4832017-10-13 18:04:25 -050091 Lsa::Type lsaType;
92 std::istringstream(updateName.get(updateName.size()-1).toUri()) >> lsaType;
Vince Lehman904c2412014-09-23 19:36:11 -050093
Ashlesh Gawande85998a12017-12-07 22:22:13 -060094 NLSR_LOG_DEBUG("Received sync update with higher " << lsaType <<
95 " sequence number than entry in LSDB");
Nick Gordon0f1bf1d2017-06-22 15:40:27 -050096
Nick Gordon9eac4d92017-08-29 17:31:29 -050097 if (m_isLsaNew(originRouter, lsaType, seqNo)) {
Nick Gordon727d4832017-10-13 18:04:25 -050098 if (lsaType == Lsa::Type::ADJACENCY && seqNo != 0 &&
Nick Gordon0f1bf1d2017-06-22 15:40:27 -050099 m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_ON) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600100 NLSR_LOG_ERROR("Got an update for adjacency LSA when hyperbolic routing " <<
101 "is enabled. Not going to fetch.");
Nick Gordon0f1bf1d2017-06-22 15:40:27 -0500102 return;
103 }
104
Nick Gordon727d4832017-10-13 18:04:25 -0500105 if (lsaType == Lsa::Type::COORDINATE && seqNo != 0 &&
Nick Gordon0f1bf1d2017-06-22 15:40:27 -0500106 m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_OFF) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600107 NLSR_LOG_ERROR("Got an update for coordinate LSA when link-state " <<
108 "is enabled. Not going to fetch.");
Nick Gordon0f1bf1d2017-06-22 15:40:27 -0500109 return;
110 }
Ashlesh Gawande08bce9c2019-04-05 11:08:07 -0500111 (*onNewLsa)(updateName, seqNo, originRouter);
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500112 }
akmhoque53353462014-04-22 08:43:45 -0500113 }
114}
115
Vince Lehman904c2412014-09-23 19:36:11 -0500116void
Nick Gordon727d4832017-10-13 18:04:25 -0500117SyncLogicHandler::publishRoutingUpdate(const Lsa::Type& type, const uint64_t& seqNo)
Vince Lehman904c2412014-09-23 19:36:11 -0500118{
Nick Gordon727d4832017-10-13 18:04:25 -0500119 switch (type) {
120 case Lsa::Type::ADJACENCY:
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500121 m_syncLogic.publishUpdate(m_adjLsaUserPrefix, seqNo);
Nick Gordon727d4832017-10-13 18:04:25 -0500122 break;
123 case Lsa::Type::COORDINATE:
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500124 m_syncLogic.publishUpdate(m_coorLsaUserPrefix, seqNo);
Nick Gordon727d4832017-10-13 18:04:25 -0500125 break;
126 case Lsa::Type::NAME:
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500127 m_syncLogic.publishUpdate(m_nameLsaUserPrefix, seqNo);
Nick Gordon727d4832017-10-13 18:04:25 -0500128 break;
129 default:
130 break;
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500131 }
Vince Lehman904c2412014-09-23 19:36:11 -0500132}
133
Nick Gordonfad8e252016-08-11 14:21:38 -0500134} // namespace nlsr