blob: 16d9c2a27c15c2cb87954b3e98bf27e743a3a849 [file] [log] [blame]
Yingdi Yu40cd1c32014-04-17 15:02:17 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventoe3c741d2019-01-29 20:28:15 -05002/*
Saurab Dulal427e0122019-11-28 11:58:02 -06003 * Copyright (c) 2014-2020, The University of Memphis,
Vince Lehman9dce0c92015-02-09 12:53:41 -06004 * Regents of the University of California,
5 * Arizona Board of Regents.
Yingdi Yu40cd1c32014-04-17 15:02:17 -07006 *
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 **/
Alexander Afanasyevb669f9c2014-11-14 12:41:54 -080021
Ashlesh Gawande85998a12017-12-07 22:22:13 -060022#include "conf-file-processor.hpp"
Saurab Dulal427e0122019-11-28 11:58:02 -060023#include "security/certificate-store.hpp"
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -050024#include "nlsr.hpp"
Vince Lehmanb722b102014-08-24 16:33:49 -050025#include "version.hpp"
Yingdi Yu40cd1c32014-04-17 15:02:17 -070026
Alexander Afanasyev7afe22f2018-02-13 16:17:40 -050027#include <boost/exception/get_error_info.hpp>
28#include <sstream>
29
30template<typename E>
Davide Pesaventoe3c741d2019-01-29 20:28:15 -050031static std::string
Alexander Afanasyev7afe22f2018-02-13 16:17:40 -050032getExtendedErrorMessage(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
Davide Pesaventoe3c741d2019-01-29 20:28:15 -050051static void
52printUsage(std::ostream& os, const std::string& programName)
Yingdi Yu40cd1c32014-04-17 15:02:17 -070053{
Davide Pesaventoe3c741d2019-01-29 20:28:15 -050054 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}
Vince Lehman7c603292014-09-11 17:48:16 -050062
Davide Pesaventoe3c741d2019-01-29 20:28:15 -050063int
64main(int argc, char** argv)
65{
akmhoquefdbddb12014-05-02 18:35:19 -050066 std::string programName(argv[0]);
Davide Pesaventoe3c741d2019-01-29 20:28:15 -050067 std::string configFileName("nlsr.conf");
Vince Lehmanc57c64b2015-08-10 12:21:31 -050068
Davide Pesaventoe3c741d2019-01-29 20:28:15 -050069 int opt;
70 while ((opt = getopt(argc, argv, "hf:V")) != -1) {
Vince Lehmanc57c64b2015-08-10 12:21:31 -050071 switch (opt) {
Davide Pesaventoe3c741d2019-01-29 20:28:15 -050072 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;
Vince Lehmanc57c64b2015-08-10 12:21:31 -050084 }
Yingdi Yu40cd1c32014-04-17 15:02:17 -070085 }
Muktadir R Chowdhurybfa27602014-10-31 10:57:41 -050086
Ashlesh Gawande85998a12017-12-07 22:22:13 -060087 boost::asio::io_service ioService;
88 ndn::Face face(ioService);
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -050089 ndn::KeyChain keyChain;
Ashlesh Gawande85998a12017-12-07 22:22:13 -060090
Saurab Dulal427e0122019-11-28 11:58:02 -060091 nlsr::ConfParameter confParam(face, keyChain, configFileName);
Ashlesh Gawande85998a12017-12-07 22:22:13 -060092 nlsr::ConfFileProcessor configProcessor(confParam);
93
94 if (!configProcessor.processConfFile()) {
95 std::cerr << "Error in configuration file processing" << std::endl;
96 return 2;
97 }
Saurab Dulal427e0122019-11-28 11:58:02 -060098 // Since confParam is already populated, key is initialized here before
99 // and independent of the NLSR class
100 auto certificate = confParam.initializeKey();
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600101
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500102 nlsr::Nlsr nlsr(face, keyChain, confParam);
Vince Lehmanc57c64b2015-08-10 12:21:31 -0500103
Saurab Dulal427e0122019-11-28 11:58:02 -0600104 nlsr::security::CertificateStore certStore(face, confParam, nlsr.getLsdb());
105
106 if (certificate) {
107 certStore.insert(*certificate);
108 }
109
Vince Lehmanc57c64b2015-08-10 12:21:31 -0500110 try {
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500111 face.processEvents();
Vince Lehmanc57c64b2015-08-10 12:21:31 -0500112 }
113 catch (const std::exception& e) {
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500114 nlsr.getFib().clean();
Davide Pesaventoe3c741d2019-01-29 20:28:15 -0500115 std::cerr << "FATAL: " << getExtendedErrorMessage(e) << std::endl;
116 return 1;
Yingdi Yu40cd1c32014-04-17 15:02:17 -0700117 }
Vince Lehmanf99b87f2014-08-26 15:54:27 -0500118
Davide Pesaventoe3c741d2019-01-29 20:28:15 -0500119 return 0;
Yingdi Yu40cd1c32014-04-17 15:02:17 -0700120}