blob: 99bdeac42c7f1d9afecbaa055929ca82cddb9d20 [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -08003 * Copyright (c) 2014-2020, 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
akmhoque53353462014-04-22 08:43:45 -050029namespace nlsr {
30
Ashlesh Gawande32ec3fd2018-07-18 13:42:32 -050031INIT_LOGGER(SyncLogicHandler);
32
Nick Gordon563611e2018-01-23 13:44:36 -060033SyncLogicHandler::SyncLogicHandler(ndn::Face& face, const IsLsaNew& isLsaNew,
34 const ConfParameter& conf)
Davide Pesaventoa08dc3f2018-05-24 00:40:28 -040035 : onNewLsa(std::make_unique<OnNewLsa>())
Nick Gordon9eac4d92017-08-29 17:31:29 -050036 , m_syncFace(face)
37 , m_isLsaNew(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)))
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -050040 , m_syncLogic(m_syncFace, m_confParam.getSyncProtocol(), m_confParam.getSyncPrefix(),
41 m_nameLsaUserPrefix, m_confParam.getSyncInterestLifetime(),
42 std::bind(&SyncLogicHandler::processUpdate, this, _1, _2))
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
Ashlesh Gawande32ec3fd2018-07-18 13:42:32 -050059SyncLogicHandler::processUpdate(const ndn::Name& updateName, uint64_t highSeq)
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
Ashlesh Gawande0421bc62020-05-08 20:42:19 -070063 int32_t nlsrPosition = util::getNameComponentPosition(updateName, NLSR_COMPONENT);
64 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
71 ndn::Name networkName = updateName.getSubName(1, nlsrPosition-1);
72 ndn::Name routerName = updateName.getSubName(lsaPosition + 1).getPrefix(-1);
73
74 ndn::Name originRouter = networkName;
75 originRouter.append(routerName);
76
77 processUpdateFromSync(originRouter, updateName, highSeq);
akmhoque53353462014-04-22 08:43:45 -050078}
79
80void
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050081SyncLogicHandler::processUpdateFromSync(const ndn::Name& originRouter,
Ashlesh Gawande32ec3fd2018-07-18 13:42:32 -050082 const ndn::Name& updateName, uint64_t seqNo)
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
Nick Gordon9eac4d92017-08-29 17:31:29 -050095 if (m_isLsaNew(originRouter, lsaType, seqNo)) {
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 }
Ashlesh Gawande08bce9c2019-04-05 11:08:07 -0500109 (*onNewLsa)(updateName, seqNo, originRouter);
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500110 }
akmhoque53353462014-04-22 08:43:45 -0500111 }
112}
113
Vince Lehman904c2412014-09-23 19:36:11 -0500114void
Nick Gordon727d4832017-10-13 18:04:25 -0500115SyncLogicHandler::publishRoutingUpdate(const Lsa::Type& type, const uint64_t& seqNo)
Vince Lehman904c2412014-09-23 19:36:11 -0500116{
Nick Gordon727d4832017-10-13 18:04:25 -0500117 switch (type) {
118 case Lsa::Type::ADJACENCY:
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500119 m_syncLogic.publishUpdate(m_adjLsaUserPrefix, seqNo);
Nick Gordon727d4832017-10-13 18:04:25 -0500120 break;
121 case Lsa::Type::COORDINATE:
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500122 m_syncLogic.publishUpdate(m_coorLsaUserPrefix, seqNo);
Nick Gordon727d4832017-10-13 18:04:25 -0500123 break;
124 case Lsa::Type::NAME:
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500125 m_syncLogic.publishUpdate(m_nameLsaUserPrefix, seqNo);
Nick Gordon727d4832017-10-13 18:04:25 -0500126 break;
127 default:
128 break;
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500129 }
Vince Lehman904c2412014-09-23 19:36:11 -0500130}
131
Nick Gordonfad8e252016-08-11 14:21:38 -0500132} // namespace nlsr