akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 1 | #include <cstdlib> |
akmhoque | 92afde4 | 2014-02-18 14:04:07 -0600 | [diff] [blame] | 2 | #include <string> |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 3 | #include <sstream> |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame^] | 4 | #include <cstdio> |
akmhoque | eb764c5 | 2014-03-11 16:01:09 -0500 | [diff] [blame] | 5 | #include <ndn-cpp-dev/face.hpp> |
| 6 | #include <ndn-cpp-dev/security/key-chain.hpp> |
| 7 | #include <ndn-cpp-dev/security/identity-certificate.hpp> |
| 8 | #include <ndn-cpp-dev/util/scheduler.hpp> |
| 9 | |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 10 | |
| 11 | #include "nlsr.hpp" |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 12 | #include "nlsr_conf_processor.hpp" |
akmhoque | 2bb198e | 2014-02-28 11:46:27 -0600 | [diff] [blame] | 13 | #include "utility/nlsr_logger.hpp" |
akmhoque | eb764c5 | 2014-03-11 16:01:09 -0500 | [diff] [blame] | 14 | #include "security/nlsr_km.hpp" |
| 15 | #include "security/nlsr_cert_store.hpp" |
| 16 | #include "security/nlsr_cse.hpp" |
akmhoque | 2bb198e | 2014-02-28 11:46:27 -0600 | [diff] [blame] | 17 | |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame^] | 18 | #define THIS_FILE "nlsr.cpp" |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 19 | |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 20 | namespace nlsr |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 21 | { |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 22 | |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 23 | using namespace ndn; |
| 24 | using namespace std; |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 25 | |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 26 | void |
| 27 | Nlsr::nlsrRegistrationFailed(const ndn::Name& name) |
| 28 | { |
| 29 | cerr << "ERROR: Failed to register prefix in local hub's daemon" << endl; |
| 30 | getNlsrFace()->shutdown(); |
| 31 | } |
| 32 | |
| 33 | |
| 34 | void |
| 35 | Nlsr::setInterestFilterNlsr(const string& name) |
| 36 | { |
| 37 | getNlsrFace()->setInterestFilter(name, |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame^] | 38 | func_lib::bind(&InterestManager::processInterest, &m_im, |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 39 | boost::ref(*this), _1, _2), |
| 40 | func_lib::bind(&Nlsr::nlsrRegistrationFailed, this, _1)); |
| 41 | } |
| 42 | |
| 43 | void |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame^] | 44 | Nlsr::initialize() |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 45 | { |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame^] | 46 | src::logger lg; |
| 47 | m_confParam.buildRouterPrefix(); |
| 48 | m_nlsrLogger.initNlsrLogger(m_confParam.getLogDir()); |
| 49 | m_nlsrLsdb.setLsaRefreshTime(m_confParam.getLsaRefreshTime()); |
| 50 | m_nlsrLsdb.setThisRouterPrefix(m_confParam.getRouterPrefix()); |
| 51 | m_fib.setEntryRefreshTime(2*m_confParam.getLsaRefreshTime()); |
| 52 | if( ! m_km.initialize(m_confParam) ) |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 53 | { |
akmhoque | fa8ee9b | 2014-03-14 09:06:24 -0500 | [diff] [blame] | 54 | std::cerr<<"Can not initiate/load certificate"<<endl; |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame^] | 55 | BOOST_LOG(lg)<<" "<<THIS_FILE<<" "<<__LINE__<<": "<<"Certificate initiation" |
| 56 | <<" error"; |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 57 | } |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame^] | 58 | m_sm.setSeqFileName(m_confParam.getSeqFileDir()); |
| 59 | m_sm.initiateSeqNoFromFile(); |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 60 | /* debugging purpose start */ |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame^] | 61 | cout << m_confParam; |
| 62 | m_adl.printAdl(); |
| 63 | m_npl.print(); |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 64 | /* debugging purpose end */ |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame^] | 65 | m_nlsrLsdb.buildAndInstallOwnNameLsa(boost::ref(*this)); |
| 66 | m_nlsrLsdb.buildAndInstallOwnCorLsa(boost::ref(*this)); |
| 67 | setInterestFilterNlsr(m_confParam.getRouterPrefix()); |
| 68 | setInterestFilterNlsr(m_confParam.getChronosyncLsaPrefix()+ |
| 69 | m_confParam.getRouterPrefix()); |
| 70 | setInterestFilterNlsr(m_confParam.getRootKeyPrefix()); |
| 71 | m_slh.setSyncPrefix(m_confParam.getChronosyncSyncPrefix()); |
| 72 | m_slh.createSyncSocket(boost::ref(*this)); |
| 73 | m_slh.publishKeyUpdate(m_km); |
| 74 | m_im.scheduleInfoInterest(boost::ref(*this),10); |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 75 | } |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 76 | |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 77 | void |
| 78 | Nlsr::startEventLoop() |
| 79 | { |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame^] | 80 | m_io->run(); |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 81 | } |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 82 | |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 83 | int |
| 84 | Nlsr::usage(const string& progname) |
| 85 | { |
| 86 | cout << "Usage: " << progname << " [OPTIONS...]"<<endl; |
| 87 | cout << " NDN routing...." << endl; |
| 88 | cout << " -d, --daemon Run in daemon mode" << endl; |
| 89 | cout << " -f, --config_file Specify configuration file name" <<endl; |
| 90 | cout << " -p, --api_port port where api client will connect" <<endl; |
| 91 | cout << " -h, --help Display this help message" << endl; |
| 92 | exit(EXIT_FAILURE); |
| 93 | } |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 94 | |
akmhoque | b1710aa | 2014-02-19 17:13:36 -0600 | [diff] [blame] | 95 | |
| 96 | } // namespace nlsr |
| 97 | |
| 98 | using namespace nlsr; |
| 99 | |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 100 | int |
| 101 | main(int argc, char **argv) |
| 102 | { |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame^] | 103 | src::logger lg; |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 104 | nlsr::Nlsr nlsr_; |
| 105 | string programName(argv[0]); |
| 106 | nlsr_.setConfFileName("nlsr.conf"); |
| 107 | int opt; |
| 108 | while ((opt = getopt(argc, argv, "df:p:h")) != -1) |
| 109 | { |
| 110 | switch (opt) |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame^] | 111 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 112 | case 'f': |
| 113 | nlsr_.setConfFileName(optarg); |
| 114 | break; |
| 115 | case 'd': |
| 116 | nlsr_.setIsDaemonProcess(optarg); |
| 117 | break; |
| 118 | case 'p': |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 119 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 120 | stringstream sst(optarg); |
| 121 | int ap; |
| 122 | sst>>ap; |
| 123 | nlsr_.setApiPort(ap); |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 124 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 125 | break; |
| 126 | case 'h': |
| 127 | default: |
| 128 | nlsr_.usage(programName); |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 129 | return EXIT_FAILURE; |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame^] | 130 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 131 | } |
| 132 | ConfFileProcessor cfp(nlsr_.getConfFileName()); |
| 133 | int res=cfp.processConfFile(nlsr_); |
| 134 | if ( res < 0 ) |
| 135 | { |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame^] | 136 | std::cerr<<"Error in configuration file processing! Exiting from NLSR"<<std::endl; |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 137 | return EXIT_FAILURE; |
| 138 | } |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame^] | 139 | nlsr_.initialize(); |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 140 | try |
| 141 | { |
| 142 | nlsr_.startEventLoop(); |
| 143 | } |
| 144 | catch(std::exception &e) |
| 145 | { |
| 146 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 147 | } |
| 148 | return EXIT_SUCCESS; |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 149 | } |