blob: bae9e68bdde437fa5b23c917bc22de65e5533837 [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Ashlesh Gawande85998a12017-12-07 22:22:13 -06003 * Copyright (c) 2014-2019, 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/>.
akmhoque3d06e792014-05-27 16:23:20 -050020 **/
Vince Lehmanc2e51f62015-01-20 15:03:11 -060021
Nick Gordone98480b2017-05-24 11:23:03 -050022#include "sequencing-manager.hpp"
23#include "logger.hpp"
24
akmhoque53353462014-04-22 08:43:45 -050025#include <string>
26#include <iostream>
27#include <fstream>
28#include <pwd.h>
29#include <cstdlib>
30#include <unistd.h>
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050031#include <boost/algorithm/string.hpp>
akmhoque53353462014-04-22 08:43:45 -050032
akmhoque53353462014-04-22 08:43:45 -050033namespace nlsr {
34
dmcoomescf8d0ed2017-02-21 11:39:01 -060035INIT_LOGGER(SequencingManager);
akmhoque674b0b12014-05-20 14:33:28 -050036
Ashlesh Gawande85998a12017-12-07 22:22:13 -060037SequencingManager::SequencingManager(std::string filePath, int hypState)
38 : m_nameLsaSeq(0)
39 , m_adjLsaSeq(0)
40 , m_corLsaSeq(0)
41 , m_hyperbolicState(hypState)
42{
43 setSeqFileDirectory(filePath);
44 initiateSeqNoFromFile();
45}
46
akmhoque53353462014-04-22 08:43:45 -050047void
Vince Lehman0bcf9a32014-12-10 11:24:45 -060048SequencingManager::writeSeqNoToFile() const
akmhoque53353462014-04-22 08:43:45 -050049{
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050050 writeLog();
51 std::ofstream outputFile(m_seqFileNameWithPath.c_str());
52 std::ostringstream os;
53 os << "NameLsaSeq " << std::to_string(m_nameLsaSeq) << "\n"
54 << "AdjLsaSeq " << std::to_string(m_adjLsaSeq) << "\n"
55 << "CorLsaSeq " << std::to_string(m_corLsaSeq);
56 outputFile << os.str();
akmhoque53353462014-04-22 08:43:45 -050057 outputFile.close();
58}
59
60void
Ashlesh Gawande85998a12017-12-07 22:22:13 -060061SequencingManager::initiateSeqNoFromFile()
akmhoque53353462014-04-22 08:43:45 -050062{
dmcoomes5bcb39e2017-10-31 15:07:55 -050063 NLSR_LOG_DEBUG("Seq File Name: " << m_seqFileNameWithPath);
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050064 std::ifstream inputFile(m_seqFileNameWithPath.c_str());
65
66 // Good checks that file is not (bad or eof or fail)
akmhoque157b0a42014-05-13 00:26:37 -050067 if (inputFile.good()) {
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050068 std::string lsaOrCombinedSeqNo;
69 uint64_t seqNo = 0;
70
71 // If file has a combined seq number, lsaOrCombinedSeqNo would hold it
72 // and seqNo will be zero everytime
73 inputFile >> lsaOrCombinedSeqNo >> seqNo;
74 m_nameLsaSeq = seqNo;
75
76 inputFile >> lsaOrCombinedSeqNo >> seqNo;
77 m_adjLsaSeq = seqNo;
78
Ashlesh Gawande08bce9c2019-04-05 11:08:07 -050079 inputFile >> lsaOrCombinedSeqNo >> seqNo;
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050080 m_corLsaSeq = seqNo;
81
82 // File was in old format and had a combined sequence number
83 // if all of the seqNo should are still zero and
84 // lsaOrCombinedSeqNo != CorLsaSeq
85 if (m_nameLsaSeq == 0 && m_adjLsaSeq == 0 && m_corLsaSeq == 0 &&
86 lsaOrCombinedSeqNo != "CorLsaSeq") {
dmcoomes5bcb39e2017-10-31 15:07:55 -050087 NLSR_LOG_DEBUG("Old file had combined sequence number: " << lsaOrCombinedSeqNo);
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050088 std::istringstream iss(lsaOrCombinedSeqNo);
89 iss >> seqNo;
90 m_adjLsaSeq = (seqNo & 0xFFFFF);
91 m_corLsaSeq = ((seqNo >> 20) & 0xFFFFF);
92 m_nameLsaSeq = ((seqNo >> 40) & 0xFFFFFF);
93 }
94
95 inputFile.close();
96
97 m_nameLsaSeq += 10;
Nick Gordon5c467f02016-07-13 13:40:10 -050098
99 // Increment the adjacency LSA seq. no. if link-state or dry HR is enabled
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600100 if (m_hyperbolicState != HYPERBOLIC_STATE_ON) {
Nick Gordon5c467f02016-07-13 13:40:10 -0500101 if (m_corLsaSeq != 0) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500102 NLSR_LOG_WARN("This router was previously configured for hyperbolic"
Nick Gordon5c467f02016-07-13 13:40:10 -0500103 << " routing without clearing the seq. no. file.");
104 m_corLsaSeq = 0;
105 }
106 m_adjLsaSeq += 10;
107 }
108
109 // Similarly, increment the coordinate LSA seq. no only if link-state is disabled.
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600110 if (m_hyperbolicState != HYPERBOLIC_STATE_OFF) {
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500111 if (m_adjLsaSeq != 0) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500112 NLSR_LOG_WARN("This router was previously configured for link-state"
Nick Gordon5c467f02016-07-13 13:40:10 -0500113 << " routing without clearing the seq. no. file.");
114 m_adjLsaSeq = 0;
115 }
116 m_corLsaSeq += 10;
117 }
akmhoque53353462014-04-22 08:43:45 -0500118 }
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500119 writeLog();
akmhoque53353462014-04-22 08:43:45 -0500120}
121
122void
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600123SequencingManager::setSeqFileDirectory(const std::string& filePath)
akmhoque53353462014-04-22 08:43:45 -0500124{
125 m_seqFileNameWithPath = filePath;
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500126
akmhoque157b0a42014-05-13 00:26:37 -0500127 if (m_seqFileNameWithPath.empty()) {
Nick Gordone98480b2017-05-24 11:23:03 -0500128 std::string homeDirPath(getpwuid(getuid())->pw_dir);
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500129 if (homeDirPath.empty()) {
130 homeDirPath = getenv("HOME");
131 }
132 m_seqFileNameWithPath = homeDirPath;
akmhoque53353462014-04-22 08:43:45 -0500133 }
134 m_seqFileNameWithPath = m_seqFileNameWithPath + "/nlsrSeqNo.txt";
135}
136
akmhoque2f423352014-06-03 11:49:35 -0500137void
Vince Lehman904c2412014-09-23 19:36:11 -0500138SequencingManager::writeLog() const
akmhoque53353462014-04-22 08:43:45 -0500139{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500140 NLSR_LOG_DEBUG("----SequencingManager----");
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500141 if (m_hyperbolicState == HYPERBOLIC_STATE_OFF ||
142 m_hyperbolicState == HYPERBOLIC_STATE_DRY_RUN) {
143 NLSR_LOG_DEBUG("Adj LSA seq no: " << m_adjLsaSeq);
144 }
145 if (m_hyperbolicState == HYPERBOLIC_STATE_ON ||
146 m_hyperbolicState == HYPERBOLIC_STATE_DRY_RUN) {
147 NLSR_LOG_DEBUG("Cor LSA Seq no: " << m_corLsaSeq);
148 }
dmcoomes5bcb39e2017-10-31 15:07:55 -0500149 NLSR_LOG_DEBUG("Name LSA Seq no: " << m_nameLsaSeq);
akmhoque53353462014-04-22 08:43:45 -0500150}
151
Nick Gordonfad8e252016-08-11 14:21:38 -0500152} // namespace nlsr