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