blob: c7649ef861d5d15d6d52e34135808398d473d1b4 [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 {
27NDNS_LOG_INIT("NameServer")
28
29const time::milliseconds NAME_SERVER_DEFAULT_CONTENT_FRESHNESS(4000);
30
31NameServer::NameServer(const Name& zoneName, const Name& certName, Face& face, DbMgr& dbMgr,
Yumin Xia2c509c22017-02-09 14:37:36 -080032 KeyChain& keyChain, security::v2::Validator& validator)
Shock Jiangcde28712014-10-19 21:17:20 -070033 : m_zone(zoneName)
34 , m_dbMgr(dbMgr)
35 , m_ndnsPrefix(zoneName)
Shock Jiangcde28712014-10-19 21:17:20 -070036 , m_certName(certName)
37 , m_contentFreshness(NAME_SERVER_DEFAULT_CONTENT_FRESHNESS)
38 , m_face(face)
39 , m_keyChain(keyChain)
40 , m_validator(validator)
41{
Shock Jiangcde28712014-10-19 21:17:20 -070042 m_dbMgr.find(m_zone);
43
44 if (m_zone.getId() == 0) {
45 NDNS_LOG_FATAL("m_zone does not exist: " << zoneName);
Yumin Xia2c509c22017-02-09 14:37:36 -080046 BOOST_THROW_EXCEPTION(Error("Zone " + zoneName.toUri() + " does not exist in the database"));
Shock Jiangcde28712014-10-19 21:17:20 -070047 }
48
49 m_ndnsPrefix.append(ndns::label::NDNS_ITERATIVE_QUERY);
Shock Jiangcde28712014-10-19 21:17:20 -070050
51 m_face.setInterestFilter(m_ndnsPrefix,
52 bind(&NameServer::onInterest, this, _1, _2),
53 bind(&NameServer::onRegisterFailed, this, _1, _2)
54 );
55
Shock Jiangcde28712014-10-19 21:17:20 -070056 NDNS_LOG_INFO("Zone: " << m_zone.getName() << " binds "
Yumin Xia2c509c22017-02-09 14:37:36 -080057 << "Prefix: " << m_ndnsPrefix
Shock Jiangcde28712014-10-19 21:17:20 -070058 << " with Certificate: " << m_certName
59 );
60}
61
62void
63NameServer::onInterest(const Name& prefix, const Interest& interest)
64{
65 label::MatchResult re;
Yumin Xia6343c5b2016-10-20 15:45:50 -070066 if (!label::matchName(interest, m_zone.getName(), re))
Shock Jiangcde28712014-10-19 21:17:20 -070067 return;
68
69 if (re.rrType == ndns::label::NDNS_UPDATE_LABEL) {
70 this->handleUpdate(prefix, interest, re); // NDNS Update
71 }
72 else {
73 this->handleQuery(prefix, interest, re); // NDNS Iterative query
74 }
75}
76
77void
78NameServer::handleQuery(const Name& prefix, const Interest& interest, const label::MatchResult& re)
79{
80 Rrset rrset(&m_zone);
81 rrset.setLabel(re.rrLabel);
82 rrset.setType(re.rrType);
83
84 NDNS_LOG_TRACE("query record: " << interest.getName());
85
Alexander Afanasyevaa46c272016-03-09 12:54:12 -080086 if (m_dbMgr.find(rrset) &&
87 (re.version.empty() || re.version == rrset.getVersion())) {
Shock Jiangcde28712014-10-19 21:17:20 -070088 // find the record: NDNS-RESP, NDNS-AUTH, NDNS-RAW, or NDNS-NACK
89 shared_ptr<Data> answer = make_shared<Data>(rrset.getData());
90 NDNS_LOG_TRACE("answer query with existing Data: " << answer->getName());
91 m_face.put(*answer);
92 }
93 else {
Shock Jiangcde28712014-10-19 21:17:20 -070094 Name name = interest.getName();
95 name.appendVersion();
96 shared_ptr<Data> answer = make_shared<Data>(name);
Yumin Xia55a7cc42017-05-14 18:43:34 -070097 Rrset doe(&m_zone);
98 // currently, there is only one DoE record contains everything
99 doe.setLabel(Name(re.rrLabel).append(re.rrType));
100 doe.setType(label::DOE_RR_TYPE);
101 if (!m_dbMgr.findLowerBound(doe)) {
102 NDNS_LOG_FATAL("fail to find DoE record of zone:" + m_zone.getName().toUri());
103 BOOST_THROW_EXCEPTION(std::runtime_error("fail to find DoE record of zone:" + m_zone.getName().toUri()));
104 }
105
106 answer->setContent(doe.getData());
Yumin Xiaa484ba72016-11-10 20:40:12 -0800107 answer->setFreshnessPeriod(this->getContentFreshness());
108 answer->setContentType(NDNS_NACK);
Yumin Xia55a7cc42017-05-14 18:43:34 -0700109 // give this NACk a random signature
110 m_keyChain.sign(*answer);
Shock Jiangcde28712014-10-19 21:17:20 -0700111
Shock Jiangcde28712014-10-19 21:17:20 -0700112 NDNS_LOG_TRACE("answer query with NDNS-NACK: " << answer->getName());
113 m_face.put(*answer);
114 }
115}
116
117void
118NameServer::handleUpdate(const Name& prefix, const Interest& interest, const label::MatchResult& re)
119{
120 if (re.rrLabel.size() == 1) {
121 // for current, we only allow Update message contains one Data, and ignore others
122 auto it = re.rrLabel.begin();
123 shared_ptr<Data> data;
124 try {
125 // blockFromValue may throw exception, which should not lead to failure of name server
126 const Block& block = it->blockFromValue();
127 data = make_shared<Data>(block);
128 }
Yumin Xia2c509c22017-02-09 14:37:36 -0800129 catch (const std::exception& e) {
Shock Jiangcde28712014-10-19 21:17:20 -0700130 NDNS_LOG_WARN("exception when getting update info: " << e.what());
131 return;
132 }
133 m_validator.validate(*data,
134 bind(&NameServer::doUpdate, this, interest.shared_from_this(), data),
Yumin Xia2c509c22017-02-09 14:37:36 -0800135 [this] (const Data& data, const security::v2::ValidationError& msg) {
Shock Jiange1a81fd2014-11-20 20:25:49 -0800136 NDNS_LOG_WARN("Ignoring update that did not pass the verification. "
Shock Jiang06cd2142014-11-23 17:36:02 -0800137 << "Check the root certificate")
Shock Jiangcde28712014-10-19 21:17:20 -0700138 });
139 }
140}
141
142void
143NameServer::onRegisterFailed(const ndn::Name& prefix, const std::string& reason)
144{
145 NDNS_LOG_FATAL("fail to register prefix=" << prefix << ". Due to: " << reason);
Yumin Xia2c509c22017-02-09 14:37:36 -0800146 BOOST_THROW_EXCEPTION(Error("zone " + m_zone.getName().toUri() + " register prefix: " +
147 prefix.toUri() + " fails. due to: " + reason));
Shock Jiangcde28712014-10-19 21:17:20 -0700148}
149
150void
151NameServer::doUpdate(const shared_ptr<const Interest>& interest,
152 const shared_ptr<const Data>& data)
153{
154 label::MatchResult re;
155 try {
Yumin Xia6343c5b2016-10-20 15:45:50 -0700156 if (!label::matchName(*data, m_zone.getName(), re))
Shock Jiangcde28712014-10-19 21:17:20 -0700157 return;
158 }
Yumin Xia2c509c22017-02-09 14:37:36 -0800159 catch (const std::exception& e) {
Shock Jiangcde28712014-10-19 21:17:20 -0700160 NDNS_LOG_INFO("Error while name/certificate matching: " << e.what());
161 }
162
163 Rrset rrset(&m_zone);
164 rrset.setLabel(re.rrLabel);
165 rrset.setType(re.rrType);
166
Shock Jiangcde28712014-10-19 21:17:20 -0700167 Name name = interest->getName();
168 name.appendVersion();
169 shared_ptr<Data> answer = make_shared<Data>(name);
Yumin Xiaa484ba72016-11-10 20:40:12 -0800170 answer->setFreshnessPeriod(this->getContentFreshness());
171 answer->setContentType(NDNS_RESP);
Shock Jiangcde28712014-10-19 21:17:20 -0700172
173 Block blk(ndn::ndns::tlv::RrData);
174 try {
175 if (m_dbMgr.find(rrset)) {
176 const name::Component& newVersion = re.version;
177 if (newVersion > rrset.getVersion()) {
178 // update existing record
179 rrset.setVersion(newVersion);
180 rrset.setData(data->wireEncode());
181 m_dbMgr.update(rrset);
Junxiao Shi767f35c2016-07-23 01:54:42 +0000182 blk.push_back(makeNonNegativeIntegerBlock(ndn::ndns::tlv::UpdateReturnCode, UPDATE_OK));
Shock Jiangcde28712014-10-19 21:17:20 -0700183 blk.encode(); // must
184 answer->setContent(blk);
185 NDNS_LOG_TRACE("replace old record and answer update with UPDATE_OK");
186 }
187 else {
Junxiao Shi767f35c2016-07-23 01:54:42 +0000188 blk.push_back(makeNonNegativeIntegerBlock(ndn::ndns::tlv::UpdateReturnCode, UPDATE_FAILURE));
Shock Jiangcde28712014-10-19 21:17:20 -0700189 blk.encode();
190 answer->setContent(blk);
191 NDNS_LOG_TRACE("answer update with UPDATE_FAILURE");
192 }
193 }
194 else {
195 // insert new record
196 rrset.setVersion(re.version);
197 rrset.setData(data->wireEncode());
198 rrset.setTtl(m_zone.getTtl());
199 m_dbMgr.insert(rrset);
Junxiao Shi767f35c2016-07-23 01:54:42 +0000200 blk.push_back(makeNonNegativeIntegerBlock(ndn::ndns::tlv::UpdateReturnCode, UPDATE_OK));
Shock Jiangcde28712014-10-19 21:17:20 -0700201 blk.encode();
202 answer->setContent(blk);
203 NDNS_LOG_TRACE("insert new record and answer update with UPDATE_OK");
204 }
205 }
Yumin Xia2c509c22017-02-09 14:37:36 -0800206 catch (const std::exception& e) {
Junxiao Shi767f35c2016-07-23 01:54:42 +0000207 blk.push_back(makeNonNegativeIntegerBlock(ndn::ndns::tlv::UpdateReturnCode, UPDATE_FAILURE));
Shock Jiangcde28712014-10-19 21:17:20 -0700208 blk.encode(); // must
209 answer->setContent(blk);
Shock Jiange1a81fd2014-11-20 20:25:49 -0800210 NDNS_LOG_INFO("Error processing the update: " << e.what()
211 << ". Update may need sudo privilege to write DbFile");
Shock Jiangcde28712014-10-19 21:17:20 -0700212 NDNS_LOG_TRACE("exception happens and answer update with UPDATE_FAILURE");
213 }
Yumin Xia2c509c22017-02-09 14:37:36 -0800214 m_keyChain.sign(*answer, signingByCertificate(m_certName));
Shock Jiangcde28712014-10-19 21:17:20 -0700215 m_face.put(*answer);
216}
217
218} // namespace ndns
219} // namespace ndn