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