blob: 13e79061bf2e54e29aa7c43a71796144ce8776ed [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>
akmhoque05d5fcf2014-04-15 14:58:45 -05004#include <cstdio>
akmhoqueeb764c52014-03-11 16:01:09 -05005#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
akmhoque298385a2014-02-13 14:13:09 -060010
11#include "nlsr.hpp"
akmhoque298385a2014-02-13 14:13:09 -060012#include "nlsr_conf_processor.hpp"
akmhoque2bb198e2014-02-28 11:46:27 -060013#include "utility/nlsr_logger.hpp"
akmhoqueeb764c52014-03-11 16:01:09 -050014#include "security/nlsr_km.hpp"
15#include "security/nlsr_cert_store.hpp"
16#include "security/nlsr_cse.hpp"
akmhoque2bb198e2014-02-28 11:46:27 -060017
akmhoque05d5fcf2014-04-15 14:58:45 -050018#define THIS_FILE "nlsr.cpp"
akmhoque298385a2014-02-13 14:13:09 -060019
akmhoque1fd8c1e2014-02-19 19:41:49 -060020namespace nlsr
akmhoque298385a2014-02-13 14:13:09 -060021{
akmhoque1fd8c1e2014-02-19 19:41:49 -060022
akmhoque5a44dd42014-03-12 18:11:32 -050023 using namespace ndn;
24 using namespace std;
akmhoque1fd8c1e2014-02-19 19:41:49 -060025
akmhoque5a44dd42014-03-12 18:11:32 -050026 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,
akmhoque05d5fcf2014-04-15 14:58:45 -050038 func_lib::bind(&InterestManager::processInterest, &m_im,
akmhoque5a44dd42014-03-12 18:11:32 -050039 boost::ref(*this), _1, _2),
40 func_lib::bind(&Nlsr::nlsrRegistrationFailed, this, _1));
41 }
42
43 void
akmhoque05d5fcf2014-04-15 14:58:45 -050044 Nlsr::initialize()
akmhoque5a44dd42014-03-12 18:11:32 -050045 {
akmhoque05d5fcf2014-04-15 14:58:45 -050046 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) )
akmhoque1fd8c1e2014-02-19 19:41:49 -060053 {
akmhoquefa8ee9b2014-03-14 09:06:24 -050054 std::cerr<<"Can not initiate/load certificate"<<endl;
akmhoque05d5fcf2014-04-15 14:58:45 -050055 BOOST_LOG(lg)<<" "<<THIS_FILE<<" "<<__LINE__<<": "<<"Certificate initiation"
56 <<" error";
akmhoque1fd8c1e2014-02-19 19:41:49 -060057 }
akmhoque05d5fcf2014-04-15 14:58:45 -050058 m_sm.setSeqFileName(m_confParam.getSeqFileDir());
59 m_sm.initiateSeqNoFromFile();
akmhoque5a44dd42014-03-12 18:11:32 -050060 /* debugging purpose start */
akmhoque05d5fcf2014-04-15 14:58:45 -050061 cout << m_confParam;
62 m_adl.printAdl();
63 m_npl.print();
akmhoque5a44dd42014-03-12 18:11:32 -050064 /* debugging purpose end */
akmhoque05d5fcf2014-04-15 14:58:45 -050065 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);
akmhoque5a44dd42014-03-12 18:11:32 -050075 }
akmhoque298385a2014-02-13 14:13:09 -060076
akmhoque5a44dd42014-03-12 18:11:32 -050077 void
78 Nlsr::startEventLoop()
79 {
akmhoque05d5fcf2014-04-15 14:58:45 -050080 m_io->run();
akmhoque5a44dd42014-03-12 18:11:32 -050081 }
akmhoque298385a2014-02-13 14:13:09 -060082
akmhoque5a44dd42014-03-12 18:11:32 -050083 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 }
akmhoque298385a2014-02-13 14:13:09 -060094
akmhoqueb1710aa2014-02-19 17:13:36 -060095
96} // namespace nlsr
97
98using namespace nlsr;
99
akmhoque1fd8c1e2014-02-19 19:41:49 -0600100int
101main(int argc, char **argv)
102{
akmhoque05d5fcf2014-04-15 14:58:45 -0500103 src::logger lg;
akmhoque5a44dd42014-03-12 18:11:32 -0500104 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)
akmhoque05d5fcf2014-04-15 14:58:45 -0500111 {
akmhoque5a44dd42014-03-12 18:11:32 -0500112 case 'f':
113 nlsr_.setConfFileName(optarg);
114 break;
115 case 'd':
116 nlsr_.setIsDaemonProcess(optarg);
117 break;
118 case 'p':
akmhoque1fd8c1e2014-02-19 19:41:49 -0600119 {
akmhoque5a44dd42014-03-12 18:11:32 -0500120 stringstream sst(optarg);
121 int ap;
122 sst>>ap;
123 nlsr_.setApiPort(ap);
akmhoque1fd8c1e2014-02-19 19:41:49 -0600124 }
akmhoque5a44dd42014-03-12 18:11:32 -0500125 break;
126 case 'h':
127 default:
128 nlsr_.usage(programName);
akmhoque1fd8c1e2014-02-19 19:41:49 -0600129 return EXIT_FAILURE;
akmhoque05d5fcf2014-04-15 14:58:45 -0500130 }
akmhoque5a44dd42014-03-12 18:11:32 -0500131 }
132 ConfFileProcessor cfp(nlsr_.getConfFileName());
133 int res=cfp.processConfFile(nlsr_);
134 if ( res < 0 )
135 {
akmhoque05d5fcf2014-04-15 14:58:45 -0500136 std::cerr<<"Error in configuration file processing! Exiting from NLSR"<<std::endl;
akmhoque5a44dd42014-03-12 18:11:32 -0500137 return EXIT_FAILURE;
138 }
akmhoque05d5fcf2014-04-15 14:58:45 -0500139 nlsr_.initialize();
akmhoque5a44dd42014-03-12 18:11:32 -0500140 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;
akmhoque298385a2014-02-13 14:13:09 -0600149}