blob: f28835f51bb3a1aef421ce89f347be0cce188ee5 [file] [log] [blame]
akmhoque298385a2014-02-13 14:13:09 -06001#include <cstdlib>
akmhoque92afde42014-02-18 14:04:07 -06002#include <string>
akmhoque298385a2014-02-13 14:13:09 -06003#include <sstream>
akmhoqueeb764c52014-03-11 16:01:09 -05004#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
akmhoque298385a2014-02-13 14:13:09 -06009
10#include "nlsr.hpp"
akmhoque298385a2014-02-13 14:13:09 -060011#include "nlsr_conf_processor.hpp"
akmhoque2bb198e2014-02-28 11:46:27 -060012#include "utility/nlsr_logger.hpp"
akmhoqueeb764c52014-03-11 16:01:09 -050013#include "security/nlsr_km.hpp"
14#include "security/nlsr_cert_store.hpp"
15#include "security/nlsr_cse.hpp"
akmhoque2bb198e2014-02-28 11:46:27 -060016
akmhoque298385a2014-02-13 14:13:09 -060017
akmhoque1fd8c1e2014-02-19 19:41:49 -060018namespace nlsr
akmhoque298385a2014-02-13 14:13:09 -060019{
akmhoque1fd8c1e2014-02-19 19:41:49 -060020
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;
akmhoque2bb198e2014-02-28 11:46:27 -060028 getNlsrFace()->shutdown();
akmhoque1fd8c1e2014-02-19 19:41:49 -060029 }
akmhoque298385a2014-02-13 14:13:09 -060030
31
akmhoque1fd8c1e2014-02-19 19:41:49 -060032 void
33 Nlsr::setInterestFilterNlsr(const string& name)
34 {
akmhoque2bb198e2014-02-28 11:46:27 -060035 getNlsrFace()->setInterestFilter(name,
36 func_lib::bind(&interestManager::processInterest, &im,
37 boost::ref(*this), _1, _2),
38 func_lib::bind(&Nlsr::nlsrRegistrationFailed, this, _1));
akmhoque1fd8c1e2014-02-19 19:41:49 -060039 }
akmhoque298385a2014-02-13 14:13:09 -060040
akmhoque2bb198e2014-02-28 11:46:27 -060041 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());
akmhoqueeb764c52014-03-11 16:01:09 -050049 if( ! km.initKeyManager(confParam) )
50 {
51 std::cerr<<"Can not initiate certificate"<<endl;
52 }
53
akmhoque2bb198e2014-02-28 11:46:27 -060054 sm.setSeqFileName(confParam.getSeqFileDir());
55 sm.initiateSeqNoFromFile();
akmhoqueeb764c52014-03-11 16:01:09 -050056
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());
akmhoque2bb198e2014-02-28 11:46:27 -060069 slh.setSyncPrefix(confParam.getChronosyncSyncPrefix());
akmhoqueeb764c52014-03-11 16:01:09 -050070 slh.createSyncSocket(boost::ref(*this));
71 slh.publishKeyUpdate(km);
72
73 im.scheduleInfoInterest(boost::ref(*this),10);
akmhoque2bb198e2014-02-28 11:46:27 -060074 }
akmhoque298385a2014-02-13 14:13:09 -060075
akmhoque1fd8c1e2014-02-19 19:41:49 -060076 void
77 Nlsr::startEventLoop()
78 {
akmhoque2bb198e2014-02-28 11:46:27 -060079 io->run();
akmhoque1fd8c1e2014-02-19 19:41:49 -060080 }
akmhoque298385a2014-02-13 14:13:09 -060081
akmhoque1fd8c1e2014-02-19 19:41:49 -060082 int
83 Nlsr::usage(const string& progname)
84 {
akmhoque298385a2014-02-13 14:13:09 -060085 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;
akmhoque298385a2014-02-13 14:13:09 -060091 exit(EXIT_FAILURE);
akmhoque1fd8c1e2014-02-19 19:41:49 -060092 }
akmhoque298385a2014-02-13 14:13:09 -060093
akmhoqueb1710aa2014-02-19 17:13:36 -060094
95} // namespace nlsr
96
97using namespace nlsr;
98
akmhoque1fd8c1e2014-02-19 19:41:49 -060099int
100main(int argc, char **argv)
101{
102 nlsr::Nlsr nlsr_;
103 string programName(argv[0]);
104 nlsr_.setConfFileName("nlsr.conf");
akmhoque1fd8c1e2014-02-19 19:41:49 -0600105 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 }
akmhoque298385a2014-02-13 14:13:09 -0600129 }
akmhoque1fd8c1e2014-02-19 19:41:49 -0600130 ConfFileProcessor cfp(nlsr_.getConfFileName());
131 int res=cfp.processConfFile(nlsr_);
132 if ( res < 0 )
133 {
134 return EXIT_FAILURE;
135 }
akmhoque2bb198e2014-02-28 11:46:27 -0600136 nlsr_.initNlsr();
akmhoqueeb764c52014-03-11 16:01:09 -0500137
akmhoque1fd8c1e2014-02-19 19:41:49 -0600138 try
139 {
140 nlsr_.startEventLoop();
141 }
142 catch(std::exception &e)
143 {
144 std::cerr << "ERROR: " << e.what() << std::endl;
145 }
akmhoque1fd8c1e2014-02-19 19:41:49 -0600146 return EXIT_SUCCESS;
akmhoque298385a2014-02-13 14:13:09 -0600147}