blob: 1c40759d8148f83d6a6d1949aa82a101dea8541b [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/*
Davide Pesavento872f18e2022-09-20 17:03:22 -04003 * Copyright (c) 2014-2022, 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"
Davide Pesavento872f18e2022-09-20 17:03:22 -040022
Shock Jiangcde28712014-10-19 21:17:20 -070023#include <ndn-cxx/encoding/encoding-buffer.hpp>
Yumin Xia2c509c22017-02-09 14:37:36 -080024#include <ndn-cxx/security/signing-helpers.hpp>
Shock Jiangcde28712014-10-19 21:17:20 -070025
26namespace ndn {
27namespace ndns {
Alexander Afanasyev08d18742018-03-15 16:31:28 -040028
29NDNS_LOG_INIT(NameServer);
Shock Jiangcde28712014-10-19 21:17:20 -070030
Davide Pesavento872f18e2022-09-20 17:03:22 -040031constexpr time::milliseconds NAME_SERVER_DEFAULT_CONTENT_FRESHNESS{4000};
Shock Jiangcde28712014-10-19 21:17:20 -070032
33NameServer::NameServer(const Name& zoneName, const Name& certName, Face& face, DbMgr& dbMgr,
Alexander Afanasyev60514ec2020-06-03 14:18:53 -040034 KeyChain& keyChain, security::Validator& validator)
Shock Jiangcde28712014-10-19 21:17:20 -070035 : m_zone(zoneName)
36 , m_dbMgr(dbMgr)
37 , m_ndnsPrefix(zoneName)
Shock Jiangcde28712014-10-19 21:17:20 -070038 , m_certName(certName)
39 , m_contentFreshness(NAME_SERVER_DEFAULT_CONTENT_FRESHNESS)
40 , m_face(face)
41 , m_keyChain(keyChain)
42 , m_validator(validator)
43{
Shock Jiangcde28712014-10-19 21:17:20 -070044 m_dbMgr.find(m_zone);
45
46 if (m_zone.getId() == 0) {
47 NDNS_LOG_FATAL("m_zone does not exist: " << zoneName);
Davide Pesavento948c50c2020-12-26 21:30:45 -050048 NDN_THROW(Error("Zone " + zoneName.toUri() + " does not exist in the database"));
Shock Jiangcde28712014-10-19 21:17:20 -070049 }
50
51 m_ndnsPrefix.append(ndns::label::NDNS_ITERATIVE_QUERY);
Shock Jiangcde28712014-10-19 21:17:20 -070052
53 m_face.setInterestFilter(m_ndnsPrefix,
54 bind(&NameServer::onInterest, this, _1, _2),
55 bind(&NameServer::onRegisterFailed, this, _1, _2)
56 );
57
Shock Jiangcde28712014-10-19 21:17:20 -070058 NDNS_LOG_INFO("Zone: " << m_zone.getName() << " binds "
Yumin Xia2c509c22017-02-09 14:37:36 -080059 << "Prefix: " << m_ndnsPrefix
Shock Jiangcde28712014-10-19 21:17:20 -070060 << " with Certificate: " << m_certName
61 );
62}
63
64void
65NameServer::onInterest(const Name& prefix, const Interest& interest)
66{
67 label::MatchResult re;
Yumin Xia6343c5b2016-10-20 15:45:50 -070068 if (!label::matchName(interest, m_zone.getName(), re))
Shock Jiangcde28712014-10-19 21:17:20 -070069 return;
70
71 if (re.rrType == ndns::label::NDNS_UPDATE_LABEL) {
72 this->handleUpdate(prefix, interest, re); // NDNS Update
73 }
74 else {
75 this->handleQuery(prefix, interest, re); // NDNS Iterative query
76 }
77}
78
79void
80NameServer::handleQuery(const Name& prefix, const Interest& interest, const label::MatchResult& re)
81{
82 Rrset rrset(&m_zone);
83 rrset.setLabel(re.rrLabel);
84 rrset.setType(re.rrType);
85
86 NDNS_LOG_TRACE("query record: " << interest.getName());
87
Alexander Afanasyevaa46c272016-03-09 12:54:12 -080088 if (m_dbMgr.find(rrset) &&
89 (re.version.empty() || re.version == rrset.getVersion())) {
Shock Jiangcde28712014-10-19 21:17:20 -070090 // find the record: NDNS-RESP, NDNS-AUTH, NDNS-RAW, or NDNS-NACK
91 shared_ptr<Data> answer = make_shared<Data>(rrset.getData());
92 NDNS_LOG_TRACE("answer query with existing Data: " << answer->getName());
93 m_face.put(*answer);
94 }
95 else {
Shock Jiangcde28712014-10-19 21:17:20 -070096 Name name = interest.getName();
97 name.appendVersion();
98 shared_ptr<Data> answer = make_shared<Data>(name);
Yumin Xia55a7cc42017-05-14 18:43:34 -070099 Rrset doe(&m_zone);
100 // currently, there is only one DoE record contains everything
101 doe.setLabel(Name(re.rrLabel).append(re.rrType));
102 doe.setType(label::DOE_RR_TYPE);
103 if (!m_dbMgr.findLowerBound(doe)) {
104 NDNS_LOG_FATAL("fail to find DoE record of zone:" + m_zone.getName().toUri());
Davide Pesavento948c50c2020-12-26 21:30:45 -0500105 NDN_THROW(std::runtime_error("fail to find DoE record of zone:" + m_zone.getName().toUri()));
Yumin Xia55a7cc42017-05-14 18:43:34 -0700106 }
107
108 answer->setContent(doe.getData());
Yumin Xiaa484ba72016-11-10 20:40:12 -0800109 answer->setFreshnessPeriod(this->getContentFreshness());
110 answer->setContentType(NDNS_NACK);
Yumin Xia55a7cc42017-05-14 18:43:34 -0700111 // give this NACk a random signature
112 m_keyChain.sign(*answer);
Shock Jiangcde28712014-10-19 21:17:20 -0700113
Shock Jiangcde28712014-10-19 21:17:20 -0700114 NDNS_LOG_TRACE("answer query with NDNS-NACK: " << answer->getName());
115 m_face.put(*answer);
116 }
117}
118
119void
120NameServer::handleUpdate(const Name& prefix, const Interest& interest, const label::MatchResult& re)
121{
122 if (re.rrLabel.size() == 1) {
123 // for current, we only allow Update message contains one Data, and ignore others
124 auto it = re.rrLabel.begin();
125 shared_ptr<Data> data;
126 try {
127 // blockFromValue may throw exception, which should not lead to failure of name server
128 const Block& block = it->blockFromValue();
129 data = make_shared<Data>(block);
130 }
Yumin Xia2c509c22017-02-09 14:37:36 -0800131 catch (const std::exception& e) {
Shock Jiangcde28712014-10-19 21:17:20 -0700132 NDNS_LOG_WARN("exception when getting update info: " << e.what());
133 return;
134 }
135 m_validator.validate(*data,
136 bind(&NameServer::doUpdate, this, interest.shared_from_this(), data),
Davide Pesavento948c50c2020-12-26 21:30:45 -0500137 [] (const Data&, const security::ValidationError&) {
Shock Jiange1a81fd2014-11-20 20:25:49 -0800138 NDNS_LOG_WARN("Ignoring update that did not pass the verification. "
Davide Pesavento948c50c2020-12-26 21:30:45 -0500139 "Check the root certificate");
Shock Jiangcde28712014-10-19 21:17:20 -0700140 });
141 }
142}
143
144void
145NameServer::onRegisterFailed(const ndn::Name& prefix, const std::string& reason)
146{
147 NDNS_LOG_FATAL("fail to register prefix=" << prefix << ". Due to: " << reason);
Davide Pesavento948c50c2020-12-26 21:30:45 -0500148 NDN_THROW(Error("zone " + m_zone.getName().toUri() + " register prefix: " +
149 prefix.toUri() + " fails. due to: " + reason));
Shock Jiangcde28712014-10-19 21:17:20 -0700150}
151
152void
153NameServer::doUpdate(const shared_ptr<const Interest>& interest,
154 const shared_ptr<const Data>& data)
155{
156 label::MatchResult re;
157 try {
Yumin Xia6343c5b2016-10-20 15:45:50 -0700158 if (!label::matchName(*data, m_zone.getName(), re))
Shock Jiangcde28712014-10-19 21:17:20 -0700159 return;
160 }
Yumin Xia2c509c22017-02-09 14:37:36 -0800161 catch (const std::exception& e) {
Shock Jiangcde28712014-10-19 21:17:20 -0700162 NDNS_LOG_INFO("Error while name/certificate matching: " << e.what());
163 }
164
165 Rrset rrset(&m_zone);
166 rrset.setLabel(re.rrLabel);
167 rrset.setType(re.rrType);
168
Shock Jiangcde28712014-10-19 21:17:20 -0700169 Name name = interest->getName();
170 name.appendVersion();
171 shared_ptr<Data> answer = make_shared<Data>(name);
Yumin Xiaa484ba72016-11-10 20:40:12 -0800172 answer->setFreshnessPeriod(this->getContentFreshness());
173 answer->setContentType(NDNS_RESP);
Shock Jiangcde28712014-10-19 21:17:20 -0700174
175 Block blk(ndn::ndns::tlv::RrData);
176 try {
177 if (m_dbMgr.find(rrset)) {
178 const name::Component& newVersion = re.version;
179 if (newVersion > rrset.getVersion()) {
180 // update existing record
181 rrset.setVersion(newVersion);
182 rrset.setData(data->wireEncode());
183 m_dbMgr.update(rrset);
Junxiao Shi767f35c2016-07-23 01:54:42 +0000184 blk.push_back(makeNonNegativeIntegerBlock(ndn::ndns::tlv::UpdateReturnCode, UPDATE_OK));
Shock Jiangcde28712014-10-19 21:17:20 -0700185 blk.encode(); // must
186 answer->setContent(blk);
187 NDNS_LOG_TRACE("replace old record and answer update with UPDATE_OK");
188 }
189 else {
Junxiao Shi767f35c2016-07-23 01:54:42 +0000190 blk.push_back(makeNonNegativeIntegerBlock(ndn::ndns::tlv::UpdateReturnCode, UPDATE_FAILURE));
Shock Jiangcde28712014-10-19 21:17:20 -0700191 blk.encode();
192 answer->setContent(blk);
193 NDNS_LOG_TRACE("answer update with UPDATE_FAILURE");
194 }
195 }
196 else {
197 // insert new record
198 rrset.setVersion(re.version);
199 rrset.setData(data->wireEncode());
200 rrset.setTtl(m_zone.getTtl());
201 m_dbMgr.insert(rrset);
Junxiao Shi767f35c2016-07-23 01:54:42 +0000202 blk.push_back(makeNonNegativeIntegerBlock(ndn::ndns::tlv::UpdateReturnCode, UPDATE_OK));
Shock Jiangcde28712014-10-19 21:17:20 -0700203 blk.encode();
204 answer->setContent(blk);
205 NDNS_LOG_TRACE("insert new record and answer update with UPDATE_OK");
206 }
207 }
Yumin Xia2c509c22017-02-09 14:37:36 -0800208 catch (const std::exception& e) {
Junxiao Shi767f35c2016-07-23 01:54:42 +0000209 blk.push_back(makeNonNegativeIntegerBlock(ndn::ndns::tlv::UpdateReturnCode, UPDATE_FAILURE));
Shock Jiangcde28712014-10-19 21:17:20 -0700210 blk.encode(); // must
211 answer->setContent(blk);
Shock Jiange1a81fd2014-11-20 20:25:49 -0800212 NDNS_LOG_INFO("Error processing the update: " << e.what()
213 << ". Update may need sudo privilege to write DbFile");
Shock Jiangcde28712014-10-19 21:17:20 -0700214 NDNS_LOG_TRACE("exception happens and answer update with UPDATE_FAILURE");
215 }
Yumin Xia2c509c22017-02-09 14:37:36 -0800216 m_keyChain.sign(*answer, signingByCertificate(m_certName));
Shock Jiangcde28712014-10-19 21:17:20 -0700217 m_face.put(*answer);
218}
219
220} // namespace ndns
221} // namespace ndn