blob: e2c8e57d8e3b8d4a4d2b2f004b91fb0d560ed1b2 [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
akmhoque5a44dd42014-03-12 18:11:32 -050021 using namespace ndn;
22 using namespace std;
akmhoque1fd8c1e2014-02-19 19:41:49 -060023
akmhoque5a44dd42014-03-12 18:11:32 -050024 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) )
akmhoque1fd8c1e2014-02-19 19:41:49 -060050 {
akmhoque5a44dd42014-03-12 18:11:32 -050051 std::cerr<<"Can not initiate certificate"<<endl;
akmhoque1fd8c1e2014-02-19 19:41:49 -060052 }
akmhoque5a44dd42014-03-12 18:11:32 -050053 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 }
akmhoque298385a2014-02-13 14:13:09 -060071
akmhoque5a44dd42014-03-12 18:11:32 -050072 void
73 Nlsr::startEventLoop()
74 {
75 io->run();
76 }
akmhoque298385a2014-02-13 14:13:09 -060077
akmhoque5a44dd42014-03-12 18:11:32 -050078 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 }
akmhoque298385a2014-02-13 14:13:09 -060089
akmhoqueb1710aa2014-02-19 17:13:36 -060090
91} // namespace nlsr
92
93using namespace nlsr;
94
akmhoque1fd8c1e2014-02-19 19:41:49 -060095int
96main(int argc, char **argv)
97{
akmhoque5a44dd42014-03-12 18:11:32 -050098 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)
akmhoque1fd8c1e2014-02-19 19:41:49 -0600105 {
akmhoque5a44dd42014-03-12 18:11:32 -0500106 case 'f':
107 nlsr_.setConfFileName(optarg);
108 break;
109 case 'd':
110 nlsr_.setIsDaemonProcess(optarg);
111 break;
112 case 'p':
akmhoque1fd8c1e2014-02-19 19:41:49 -0600113 {
akmhoque5a44dd42014-03-12 18:11:32 -0500114 stringstream sst(optarg);
115 int ap;
116 sst>>ap;
117 nlsr_.setApiPort(ap);
akmhoque1fd8c1e2014-02-19 19:41:49 -0600118 }
akmhoque5a44dd42014-03-12 18:11:32 -0500119 break;
120 case 'h':
121 default:
122 nlsr_.usage(programName);
akmhoque1fd8c1e2014-02-19 19:41:49 -0600123 return EXIT_FAILURE;
124 }
akmhoque5a44dd42014-03-12 18:11:32 -0500125 }
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;
akmhoque298385a2014-02-13 14:13:09 -0600142}