blob: 99ceb1923b2395a16848859b8beb38bd25e135d9 [file] [log] [blame]
shockjianga5ae48c2014-07-27 23:21:41 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014, Regents of the University of California.
4 *
5 * This file is part of NDNS (Named Data Networking Domain Name Service).
6 * See AUTHORS.md for complete list of NDNS authors and contributors.
7 *
8 * NDNS is free software: you can redistribute it and/or modify it under the terms
9 * of the GNU General Public License as published by the Free Software Foundation,
10 * either version 3 of the License, or (at your option) any later version.
11 *
12 * NDNS is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
13 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along with
17 * NDNS, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
18 */
19#include "name-server.hpp"
20
shockjiang99ad3892014-08-03 14:56:13 -070021namespace ndn {
22namespace ndns {
23NameServer::NameServer(const char *programName, const char *prefix,
24 const char *nameZone, const string dbfile)
25 : NDNApp(programName, prefix)
26 , m_zone(Name(nameZone))
27 , m_zoneMgr(m_zone)
shockjianga5ae48c2014-07-27 23:21:41 -070028{
shockjiang99ad3892014-08-03 14:56:13 -070029 m_zoneMgr.setDbfile(dbfile);
shockjianga5ae48c2014-07-27 23:21:41 -070030 //m_zoneMgr.lookupId();
shockjiang99ad3892014-08-03 14:56:13 -070031} //NameServer Construction
shockjianga5ae48c2014-07-27 23:21:41 -070032
shockjiang99ad3892014-08-03 14:56:13 -070033void NameServer::onInterest(const Name &name, const Interest &interest)
shockjianga5ae48c2014-07-27 23:21:41 -070034{
35
shockjiang99ad3892014-08-03 14:56:13 -070036 cout << "[* -> *] receive Interest: " << interest.getName().toUri()
37 << std::endl;
shockjianga5ae48c2014-07-27 23:21:41 -070038 Query query;
shockjiang99ad3892014-08-03 14:56:13 -070039 if (!query.fromInterest(interest)) {
40 cout << "can resolve the Query from Interest: " << endl;
shockjianga5ae48c2014-07-27 23:21:41 -070041 return;
42 }
43
44 /*
45 * query.getAuthorityZone is routable name, not the zone's service name
shockjiang99ad3892014-08-03 14:56:13 -070046 if (query.getAuthorityZone() != m_zoneMgr.getZone().getAuthorizedName())
47 {
48 cout<<"Query is intent to zone: "<<query.getAuthorityZone()
49 <<". This is "<<m_zoneMgr.getZone().getAuthorizedName()<<endl;
50 return;
51 }
52 */
shockjianga5ae48c2014-07-27 23:21:41 -070053
54 Response response;
55 Name name2 = interest.getName();
56 name2.appendVersion();
57 response.setQueryName(name2);
58 RRMgr mgr(m_zone, query, response);
59
shockjiang99ad3892014-08-03 14:56:13 -070060 if (mgr.lookup() < 0) {
61 cout << "[* !! *] lookup error, then exit: " << mgr.getErr() << endl;
shockjianga5ae48c2014-07-27 23:21:41 -070062 return;
63 }
64
shockjiang99ad3892014-08-03 14:56:13 -070065 if (response.getRrs().size() > 0) {
shockjianga5ae48c2014-07-27 23:21:41 -070066 response.setResponseType(Response::NDNS_Resp);
67 } else {
68
69 if (query.getRrType() == RR::NS) {
70 int count = mgr.count();
shockjiang99ad3892014-08-03 14:56:13 -070071 if (count < 0) {
72 cout << "[* !! *] lookup error, then exit: " << mgr.getErr() << endl;
shockjianga5ae48c2014-07-27 23:21:41 -070073 return;
shockjiang99ad3892014-08-03 14:56:13 -070074 } else if (count > 0) {
shockjianga5ae48c2014-07-27 23:21:41 -070075 response.setResponseType(Response::NDNS_Auth);
shockjiang99ad3892014-08-03 14:56:13 -070076 } else {
shockjianga5ae48c2014-07-27 23:21:41 -070077 response.setResponseType(Response::NDNS_Nack);
78 }
79 } else {
80 response.setResponseType(Response::NDNS_Nack);
81 }
82 }
83
shockjianga5ae48c2014-07-27 23:21:41 -070084 Data data = response.toData();
85 data.setFreshnessPeriod(response.getFreshness());
86
87 m_keyChain.sign(data);
88 m_face.put(data);
shockjiang99ad3892014-08-03 14:56:13 -070089 cout << "[* <- *] send response: " << response << ": " << data << endl;
90} //onInterest
shockjianga5ae48c2014-07-27 23:21:41 -070091
shockjiang99ad3892014-08-03 14:56:13 -070092void NameServer::run()
shockjianga5ae48c2014-07-27 23:21:41 -070093{
94 //m_zoneMgr.lookupId();
shockjiang99ad3892014-08-03 14:56:13 -070095 if (m_zoneMgr.getZone().getId() == 0) {
shockjianga5ae48c2014-07-27 23:21:41 -070096 m_hasError = true;
shockjiang99ad3892014-08-03 14:56:13 -070097 m_error = "cannot get Zone.id from database for name="
98 + m_zone.getAuthorizedName().toUri();
shockjianga5ae48c2014-07-27 23:21:41 -070099 stop();
100 }
101
102 boost::asio::signal_set signalSet(m_ioService, SIGINT, SIGTERM);
103 signalSet.async_wait(boost::bind(&NDNApp::signalHandler, this));
shockjiang99ad3892014-08-03 14:56:13 -0700104 // boost::bind(&NdnTlvPingServer::signalHandler, this)
shockjianga5ae48c2014-07-27 23:21:41 -0700105 Name name;
106 name.set(m_prefix);
107 name.append(Query::toString(Query::QUERY_DNS));
108
shockjiang99ad3892014-08-03 14:56:13 -0700109 std::cout<<"========= NDNS Name Server for Zone "
110 <<m_zoneMgr.getZone().getAuthorizedName().toUri()
111 <<" Starts with Prefix "<<m_prefix;
112 if (m_enableForwardingHint > 0) {
113 std::cout<<" & ForwardingHint "<<m_forwardingHint.toUri();
114 }
115 std::cout<<"============="<<std::endl;
shockjianga5ae48c2014-07-27 23:21:41 -0700116
shockjiang99ad3892014-08-03 14:56:13 -0700117 m_face.setInterestFilter(name, bind(&NameServer::onInterest, this, _1, _2),
118 bind(&NDNApp::onRegisterFailed, this, _1, _2));
119 std::cout<<"Name Server Register Name Prefix: "<<name<<std::endl;
120
121 if (m_enableForwardingHint > 0) {
122 Name name2 = Name(m_forwardingHint);
123 name2.append(ndn::ndns::label::ForwardingHintLabel);
124 name2.append(name);
125 m_face.setInterestFilter(name2, bind(&NameServer::onInterest, this, _1, _2),
126 bind(&NDNApp::onRegisterFailed, this, _1, _2));
127 std::cout<<"Name Server Register Name Prefix: "<<name2<<std::endl;
128 }
129
shockjianga5ae48c2014-07-27 23:21:41 -0700130
131 try {
132 m_face.processEvents();
shockjiang99ad3892014-08-03 14:56:13 -0700133 } catch (std::exception& e) {
shockjianga5ae48c2014-07-27 23:21:41 -0700134 m_hasError = true;
135 m_error = "ERROR: ";
136 m_error += e.what();
137 stop();
138 }
139
shockjiang99ad3892014-08-03 14:56:13 -0700140} //run
shockjianga5ae48c2014-07-27 23:21:41 -0700141
142} //namespace ndns
143} /* namespace ndn */
144