blob: cc1c881db312bb157cef15dd1594f8bae0f5f519 [file] [log] [blame]
Shock Jiangcde28712014-10-19 21:17:20 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Yumin Xia2c509c22017-02-09 14:37:36 -08002/*
Yumin Xia55a7cc42017-05-14 18:43:34 -07003 * Copyright (c) 2014-2018, Regents of the University of California.
Shock Jiangcde28712014-10-19 21:17:20 -07004 *
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
20#include "name-server.hpp"
21#include "logger.hpp"
Shock Jiangcde28712014-10-19 21:17:20 -070022#include <ndn-cxx/encoding/encoding-buffer.hpp>
Yumin Xia2c509c22017-02-09 14:37:36 -080023#include <ndn-cxx/security/signing-helpers.hpp>
Shock Jiangcde28712014-10-19 21:17:20 -070024
25namespace ndn {
26namespace ndns {
Alexander Afanasyev08d18742018-03-15 16:31:28 -040027
28NDNS_LOG_INIT(NameServer);
Shock Jiangcde28712014-10-19 21:17:20 -070029
30const time::milliseconds NAME_SERVER_DEFAULT_CONTENT_FRESHNESS(4000);
31
32NameServer::NameServer(const Name& zoneName, const Name& certName, Face& face, DbMgr& dbMgr,
Yumin Xia2c509c22017-02-09 14:37:36 -080033 KeyChain& keyChain, security::v2::Validator& validator)
Shock Jiangcde28712014-10-19 21:17:20 -070034 : m_zone(zoneName)
35 , m_dbMgr(dbMgr)
36 , m_ndnsPrefix(zoneName)
Shock Jiangcde28712014-10-19 21:17:20 -070037 , m_certName(certName)
38 , m_contentFreshness(NAME_SERVER_DEFAULT_CONTENT_FRESHNESS)
39 , m_face(face)
40 , m_keyChain(keyChain)
41 , m_validator(validator)
42{
Shock Jiangcde28712014-10-19 21:17:20 -070043 m_dbMgr.find(m_zone);
44
45 if (m_zone.getId() == 0) {
46 NDNS_LOG_FATAL("m_zone does not exist: " << zoneName);
Yumin Xia2c509c22017-02-09 14:37:36 -080047 BOOST_THROW_EXCEPTION(Error("Zone " + zoneName.toUri() + " does not exist in the database"));
Shock Jiangcde28712014-10-19 21:17:20 -070048 }
49
50 m_ndnsPrefix.append(ndns::label::NDNS_ITERATIVE_QUERY);
Shock Jiangcde28712014-10-19 21:17:20 -070051
52 m_face.setInterestFilter(m_ndnsPrefix,
53 bind(&NameServer::onInterest, this, _1, _2),
54 bind(&NameServer::onRegisterFailed, this, _1, _2)
55 );
56
Shock Jiangcde28712014-10-19 21:17:20 -070057 NDNS_LOG_INFO("Zone: " << m_zone.getName() << " binds "
Yumin Xia2c509c22017-02-09 14:37:36 -080058 << "Prefix: " << m_ndnsPrefix
Shock Jiangcde28712014-10-19 21:17:20 -070059 << " with Certificate: " << m_certName
60 );
61}
62
63void
64NameServer::onInterest(const Name& prefix, const Interest& interest)
65{
66 label::MatchResult re;
Yumin Xia6343c5b2016-10-20 15:45:50 -070067 if (!label::matchName(interest, m_zone.getName(), re))
Shock Jiangcde28712014-10-19 21:17:20 -070068 return;
69
70 if (re.rrType == ndns::label::NDNS_UPDATE_LABEL) {
71 this->handleUpdate(prefix, interest, re); // NDNS Update
72 }
73 else {
74 this->handleQuery(prefix, interest, re); // NDNS Iterative query
75 }
76}
77
78void
79NameServer::handleQuery(const Name& prefix, const Interest& interest, const label::MatchResult& re)
80{
81 Rrset rrset(&m_zone);
82 rrset.setLabel(re.rrLabel);
83 rrset.setType(re.rrType);
84
85 NDNS_LOG_TRACE("query record: " << interest.getName());
86
Alexander Afanasyevaa46c272016-03-09 12:54:12 -080087 if (m_dbMgr.find(rrset) &&
88 (re.version.empty() || re.version == rrset.getVersion())) {
Shock Jiangcde28712014-10-19 21:17:20 -070089 // find the record: NDNS-RESP, NDNS-AUTH, NDNS-RAW, or NDNS-NACK
90 shared_ptr<Data> answer = make_shared<Data>(rrset.getData());
91 NDNS_LOG_TRACE("answer query with existing Data: " << answer->getName());
92 m_face.put(*answer);
93 }
94 else {
Shock Jiangcde28712014-10-19 21:17:20 -070095 Name name = interest.getName();
96 name.appendVersion();
97 shared_ptr<Data> answer = make_shared<Data>(name);
Yumin Xia55a7cc42017-05-14 18:43:34 -070098 Rrset doe(&m_zone);
99 // currently, there is only one DoE record contains everything
100 doe.setLabel(Name(re.rrLabel).append(re.rrType));
101 doe.setType(label::DOE_RR_TYPE);
102 if (!m_dbMgr.findLowerBound(doe)) {
103 NDNS_LOG_FATAL("fail to find DoE record of zone:" + m_zone.getName().toUri());
104 BOOST_THROW_EXCEPTION(std::runtime_error("fail to find DoE record of zone:" + m_zone.getName().toUri()));
105 }
106
107 answer->setContent(doe.getData());
Yumin Xiaa484ba72016-11-10 20:40:12 -0800108 answer->setFreshnessPeriod(this->getContentFreshness());
109 answer->setContentType(NDNS_NACK);
Yumin Xia55a7cc42017-05-14 18:43:34 -0700110 // give this NACk a random signature
111 m_keyChain.sign(*answer);
Shock Jiangcde28712014-10-19 21:17:20 -0700112
Shock Jiangcde28712014-10-19 21:17:20 -0700113 NDNS_LOG_TRACE("answer query with NDNS-NACK: " << answer->getName());
114 m_face.put(*answer);
115 }
116}
117
118void
119NameServer::handleUpdate(const Name& prefix, const Interest& interest, const label::MatchResult& re)
120{
121 if (re.rrLabel.size() == 1) {
122 // for current, we only allow Update message contains one Data, and ignore others
123 auto it = re.rrLabel.begin();
124 shared_ptr<Data> data;
125 try {
126 // blockFromValue may throw exception, which should not lead to failure of name server
127 const Block& block = it->blockFromValue();
128 data = make_shared<Data>(block);
129 }
Yumin Xia2c509c22017-02-09 14:37:36 -0800130 catch (const std::exception& e) {
Shock Jiangcde28712014-10-19 21:17:20 -0700131 NDNS_LOG_WARN("exception when getting update info: " << e.what());
132 return;
133 }
134 m_validator.validate(*data,
135 bind(&NameServer::doUpdate, this, interest.shared_from_this(), data),
Yumin Xia2c509c22017-02-09 14:37:36 -0800136 [this] (const Data& data, const security::v2::ValidationError& msg) {
Shock Jiange1a81fd2014-11-20 20:25:49 -0800137 NDNS_LOG_WARN("Ignoring update that did not pass the verification. "
Alexander Afanasyev08d18742018-03-15 16:31:28 -0400138 << "Check the root certificate");
Shock Jiangcde28712014-10-19 21:17:20 -0700139 });
140 }
141}
142
143void
144NameServer::onRegisterFailed(const ndn::Name& prefix, const std::string& reason)
145{
146 NDNS_LOG_FATAL("fail to register prefix=" << prefix << ". Due to: " << reason);
Yumin Xia2c509c22017-02-09 14:37:36 -0800147 BOOST_THROW_EXCEPTION(Error("zone " + m_zone.getName().toUri() + " register prefix: " +
148 prefix.toUri() + " fails. due to: " + reason));
Shock Jiangcde28712014-10-19 21:17:20 -0700149}
150
151void
152NameServer::doUpdate(const shared_ptr<const Interest>& interest,
153 const shared_ptr<const Data>& data)
154{
155 label::MatchResult re;
156 try {
Yumin Xia6343c5b2016-10-20 15:45:50 -0700157 if (!label::matchName(*data, m_zone.getName(), re))
Shock Jiangcde28712014-10-19 21:17:20 -0700158 return;
159 }
Yumin Xia2c509c22017-02-09 14:37:36 -0800160 catch (const std::exception& e) {
Shock Jiangcde28712014-10-19 21:17:20 -0700161 NDNS_LOG_INFO("Error while name/certificate matching: " << e.what());
162 }
163
164 Rrset rrset(&m_zone);
165 rrset.setLabel(re.rrLabel);
166 rrset.setType(re.rrType);
167
Shock Jiangcde28712014-10-19 21:17:20 -0700168 Name name = interest->getName();
169 name.appendVersion();
170 shared_ptr<Data> answer = make_shared<Data>(name);
Yumin Xiaa484ba72016-11-10 20:40:12 -0800171 answer->setFreshnessPeriod(this->getContentFreshness());
172 answer->setContentType(NDNS_RESP);
Shock Jiangcde28712014-10-19 21:17:20 -0700173
174 Block blk(ndn::ndns::tlv::RrData);
175 try {
176 if (m_dbMgr.find(rrset)) {
177 const name::Component& newVersion = re.version;
178 if (newVersion > rrset.getVersion()) {
179 // update existing record
180 rrset.setVersion(newVersion);
181 rrset.setData(data->wireEncode());
182 m_dbMgr.update(rrset);
Junxiao Shi767f35c2016-07-23 01:54:42 +0000183 blk.push_back(makeNonNegativeIntegerBlock(ndn::ndns::tlv::UpdateReturnCode, UPDATE_OK));
Shock Jiangcde28712014-10-19 21:17:20 -0700184 blk.encode(); // must
185 answer->setContent(blk);
186 NDNS_LOG_TRACE("replace old record and answer update with UPDATE_OK");
187 }
188 else {
Junxiao Shi767f35c2016-07-23 01:54:42 +0000189 blk.push_back(makeNonNegativeIntegerBlock(ndn::ndns::tlv::UpdateReturnCode, UPDATE_FAILURE));
Shock Jiangcde28712014-10-19 21:17:20 -0700190 blk.encode();
191 answer->setContent(blk);
192 NDNS_LOG_TRACE("answer update with UPDATE_FAILURE");
193 }
194 }
195 else {
196 // insert new record
197 rrset.setVersion(re.version);
198 rrset.setData(data->wireEncode());
199 rrset.setTtl(m_zone.getTtl());
200 m_dbMgr.insert(rrset);
Junxiao Shi767f35c2016-07-23 01:54:42 +0000201 blk.push_back(makeNonNegativeIntegerBlock(ndn::ndns::tlv::UpdateReturnCode, UPDATE_OK));
Shock Jiangcde28712014-10-19 21:17:20 -0700202 blk.encode();
203 answer->setContent(blk);
204 NDNS_LOG_TRACE("insert new record and answer update with UPDATE_OK");
205 }
206 }
Yumin Xia2c509c22017-02-09 14:37:36 -0800207 catch (const std::exception& e) {
Junxiao Shi767f35c2016-07-23 01:54:42 +0000208 blk.push_back(makeNonNegativeIntegerBlock(ndn::ndns::tlv::UpdateReturnCode, UPDATE_FAILURE));
Shock Jiangcde28712014-10-19 21:17:20 -0700209 blk.encode(); // must
210 answer->setContent(blk);
Shock Jiange1a81fd2014-11-20 20:25:49 -0800211 NDNS_LOG_INFO("Error processing the update: " << e.what()
212 << ". Update may need sudo privilege to write DbFile");
Shock Jiangcde28712014-10-19 21:17:20 -0700213 NDNS_LOG_TRACE("exception happens and answer update with UPDATE_FAILURE");
214 }
Yumin Xia2c509c22017-02-09 14:37:36 -0800215 m_keyChain.sign(*answer, signingByCertificate(m_certName));
Shock Jiangcde28712014-10-19 21:17:20 -0700216 m_face.put(*answer);
217}
218
219} // namespace ndns
220} // namespace ndn