main.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2020, The University of Memphis,
4  * Regents of the University of California,
5  * Arizona Board of Regents.
6  *
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/>.
20  **/
21 
22 #include "conf-file-processor.hpp"
24 #include "nlsr.hpp"
25 #include "version.hpp"
26 
27 #include <boost/exception/get_error_info.hpp>
28 #include <sstream>
29 
30 template<typename E>
31 static std::string
32 getExtendedErrorMessage(const E& exception)
33 {
34  std::ostringstream errorMessage;
35  errorMessage << exception.what();
36 
37  const char* const* file = boost::get_error_info<boost::throw_file>(exception);
38  const int* line = boost::get_error_info<boost::throw_line>(exception);
39  const char* const* func = boost::get_error_info<boost::throw_function>(exception);
40  if (file && line) {
41  errorMessage << " [from " << *file << ":" << *line;
42  if (func) {
43  errorMessage << " in " << *func;
44  }
45  errorMessage << "]";
46  }
47 
48  return errorMessage.str();
49 }
50 
51 static void
52 printUsage(std::ostream& os, const std::string& programName)
53 {
54  os << "Usage: " << programName << " [OPTIONS...]\n"
55  << "\n"
56  << "Options:\n"
57  << " -f <FILE> Path to configuration file\n"
58  << " -h Display this help message\n"
59  << " -V Display version information\n"
60  << std::endl;
61 }
62 
63 int
64 main(int argc, char** argv)
65 {
66  std::string programName(argv[0]);
67  std::string configFileName("nlsr.conf");
68 
69  int opt;
70  while ((opt = getopt(argc, argv, "hf:V")) != -1) {
71  switch (opt) {
72  case 'h':
73  printUsage(std::cout, programName);
74  return 0;
75  case 'f':
76  configFileName = optarg;
77  break;
78  case 'V':
79  std::cout << NLSR_VERSION_BUILD_STRING << std::endl;
80  return 0;
81  default:
82  printUsage(std::cerr, programName);
83  return 2;
84  }
85  }
86 
87  boost::asio::io_service ioService;
88  ndn::Face face(ioService);
89  ndn::KeyChain keyChain;
90 
91  nlsr::ConfParameter confParam(face, keyChain, configFileName);
92  nlsr::ConfFileProcessor configProcessor(confParam);
93 
94  if (!configProcessor.processConfFile()) {
95  std::cerr << "Error in configuration file processing" << std::endl;
96  return 2;
97  }
98  // Since confParam is already populated, key is initialized here before
99  // and independent of the NLSR class
100  auto certificate = confParam.initializeKey();
101 
102  nlsr::Nlsr nlsr(face, keyChain, confParam);
103 
104  nlsr::security::CertificateStore certStore(face, confParam, nlsr.getLsdb());
105 
106  if (certificate) {
107  certStore.insert(*certificate);
108  }
109 
110  try {
111  face.processEvents();
112  }
113  catch (const std::exception& e) {
114  nlsr.getFib().clean();
115  std::cerr << "FATAL: " << getExtendedErrorMessage(e) << std::endl;
116  return 1;
117  }
118 
119  return 0;
120 }
A class to house all the configuration parameters for NLSR.
Store certificates for names.
int main(int argc, char **argv)
Definition: main.cpp:64
static void printUsage(std::ostream &os, const std::string &programName)
Definition: main.cpp:52
void insert(const ndn::security::v2::Certificate &certificate)
shared_ptr< ndn::security::v2::Certificate > initializeKey()
void clean()
Remove all entries from the FIB.
Definition: fib.cpp:165
static std::string getExtendedErrorMessage(const E &exception)
Definition: main.cpp:32
A class containing methods to parse an NLSR configuration file.
Copyright (c) 2014-2019, The University of Memphis, Regents of the University of California, Arizona Board of Regents.
Lsdb & getLsdb()
Definition: nlsr.hpp:108
bool processConfFile()
Load and parse the configuration file, then populate NLSR.
Fib & getFib()
Definition: nlsr.hpp:114