blob: 6eaab5d7ac9ccfcccf944e2417fee035ba8a5951 [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Nick Gordonfeae5572017-01-13 12:06:26 -06003 * Copyright (c) 2014-2017, 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
akmhoque53353462014-04-22 08:43:45 -050022#include <string>
23#include <iostream>
24#include <fstream>
25#include <pwd.h>
26#include <cstdlib>
27#include <unistd.h>
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050028#include <boost/algorithm/string.hpp>
akmhoque53353462014-04-22 08:43:45 -050029
30#include "sequencing-manager.hpp"
akmhoque674b0b12014-05-20 14:33:28 -050031#include "logger.hpp"
akmhoque53353462014-04-22 08:43:45 -050032
33namespace nlsr {
34
akmhoque674b0b12014-05-20 14:33:28 -050035INIT_LOGGER("SequencingManager");
36
akmhoque53353462014-04-22 08:43:45 -050037using namespace std;
38
39void
Vince Lehman0bcf9a32014-12-10 11:24:45 -060040SequencingManager::writeSeqNoToFile() const
akmhoque53353462014-04-22 08:43:45 -050041{
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050042 writeLog();
43 std::ofstream outputFile(m_seqFileNameWithPath.c_str());
44 std::ostringstream os;
45 os << "NameLsaSeq " << std::to_string(m_nameLsaSeq) << "\n"
46 << "AdjLsaSeq " << std::to_string(m_adjLsaSeq) << "\n"
47 << "CorLsaSeq " << std::to_string(m_corLsaSeq);
48 outputFile << os.str();
akmhoque53353462014-04-22 08:43:45 -050049 outputFile.close();
50}
51
52void
Nick Gordon5c467f02016-07-13 13:40:10 -050053SequencingManager::initiateSeqNoFromFile(int hypState)
akmhoque53353462014-04-22 08:43:45 -050054{
akmhoque2f423352014-06-03 11:49:35 -050055 _LOG_DEBUG("Seq File Name: " << m_seqFileNameWithPath);
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050056 std::ifstream inputFile(m_seqFileNameWithPath.c_str());
57
58 // Good checks that file is not (bad or eof or fail)
akmhoque157b0a42014-05-13 00:26:37 -050059 if (inputFile.good()) {
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050060 std::string lsaOrCombinedSeqNo;
61 uint64_t seqNo = 0;
62
63 // If file has a combined seq number, lsaOrCombinedSeqNo would hold it
64 // and seqNo will be zero everytime
65 inputFile >> lsaOrCombinedSeqNo >> seqNo;
66 m_nameLsaSeq = seqNo;
67
68 inputFile >> lsaOrCombinedSeqNo >> seqNo;
69 m_adjLsaSeq = seqNo;
70
71 inputFile >> lsaOrCombinedSeqNo >> seqNo;;
72 m_corLsaSeq = seqNo;
73
74 // File was in old format and had a combined sequence number
75 // if all of the seqNo should are still zero and
76 // lsaOrCombinedSeqNo != CorLsaSeq
77 if (m_nameLsaSeq == 0 && m_adjLsaSeq == 0 && m_corLsaSeq == 0 &&
78 lsaOrCombinedSeqNo != "CorLsaSeq") {
79 _LOG_DEBUG("Old file had combined sequence number: " << lsaOrCombinedSeqNo);
80 std::istringstream iss(lsaOrCombinedSeqNo);
81 iss >> seqNo;
82 m_adjLsaSeq = (seqNo & 0xFFFFF);
83 m_corLsaSeq = ((seqNo >> 20) & 0xFFFFF);
84 m_nameLsaSeq = ((seqNo >> 40) & 0xFFFFFF);
85 }
86
87 inputFile.close();
88
89 m_nameLsaSeq += 10;
Nick Gordon5c467f02016-07-13 13:40:10 -050090
91 // Increment the adjacency LSA seq. no. if link-state or dry HR is enabled
92 if (hypState != HYPERBOLIC_STATE_ON) {
93 if (m_corLsaSeq != 0) {
94 _LOG_WARN("This router was previously configured for hyperbolic"
95 << " routing without clearing the seq. no. file.");
96 m_corLsaSeq = 0;
97 }
98 m_adjLsaSeq += 10;
99 }
100
101 // Similarly, increment the coordinate LSA seq. no only if link-state is disabled.
102 if (hypState != HYPERBOLIC_STATE_OFF) {
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500103 if (m_adjLsaSeq != 0) {
Nick Gordon5c467f02016-07-13 13:40:10 -0500104 _LOG_WARN("This router was previously configured for link-state"
105 << " routing without clearing the seq. no. file.");
106 m_adjLsaSeq = 0;
107 }
108 m_corLsaSeq += 10;
109 }
akmhoque53353462014-04-22 08:43:45 -0500110 }
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500111 writeLog();
akmhoque53353462014-04-22 08:43:45 -0500112}
113
114void
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500115SequencingManager::setSeqFileDirectory(string filePath)
akmhoque53353462014-04-22 08:43:45 -0500116{
117 m_seqFileNameWithPath = filePath;
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500118
akmhoque157b0a42014-05-13 00:26:37 -0500119 if (m_seqFileNameWithPath.empty()) {
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500120 string homeDirPath(getpwuid(getuid())->pw_dir);
121 if (homeDirPath.empty()) {
122 homeDirPath = getenv("HOME");
123 }
124 m_seqFileNameWithPath = homeDirPath;
akmhoque53353462014-04-22 08:43:45 -0500125 }
126 m_seqFileNameWithPath = m_seqFileNameWithPath + "/nlsrSeqNo.txt";
127}
128
akmhoque2f423352014-06-03 11:49:35 -0500129void
Vince Lehman904c2412014-09-23 19:36:11 -0500130SequencingManager::writeLog() const
akmhoque53353462014-04-22 08:43:45 -0500131{
akmhoque2f423352014-06-03 11:49:35 -0500132 _LOG_DEBUG("----SequencingManager----");
133 _LOG_DEBUG("Adj LSA seq no: " << m_adjLsaSeq);
134 _LOG_DEBUG("Cor LSA Seq no: " << m_corLsaSeq);
135 _LOG_DEBUG("Name LSA Seq no: " << m_nameLsaSeq);
akmhoque53353462014-04-22 08:43:45 -0500136}
137
Nick Gordonfad8e252016-08-11 14:21:38 -0500138} // namespace nlsr