blob: 11ad3a87075937dcb285ca28416e6a546b81fc01 [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/*
3 * Copyright (c) 2014-2017, 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 {
94 // no record, construct NACK
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 Xiaa484ba72016-11-10 20:40:12 -080098 answer->setFreshnessPeriod(this->getContentFreshness());
99 answer->setContentType(NDNS_NACK);
Shock Jiangcde28712014-10-19 21:17:20 -0700100
Yumin Xia2c509c22017-02-09 14:37:36 -0800101 m_keyChain.sign(*answer, signingByCertificate(m_certName));
Shock Jiangcde28712014-10-19 21:17:20 -0700102 NDNS_LOG_TRACE("answer query with NDNS-NACK: " << answer->getName());
103 m_face.put(*answer);
104 }
105}
106
107void
108NameServer::handleUpdate(const Name& prefix, const Interest& interest, const label::MatchResult& re)
109{
110 if (re.rrLabel.size() == 1) {
111 // for current, we only allow Update message contains one Data, and ignore others
112 auto it = re.rrLabel.begin();
113 shared_ptr<Data> data;
114 try {
115 // blockFromValue may throw exception, which should not lead to failure of name server
116 const Block& block = it->blockFromValue();
117 data = make_shared<Data>(block);
118 }
Yumin Xia2c509c22017-02-09 14:37:36 -0800119 catch (const std::exception& e) {
Shock Jiangcde28712014-10-19 21:17:20 -0700120 NDNS_LOG_WARN("exception when getting update info: " << e.what());
121 return;
122 }
123 m_validator.validate(*data,
124 bind(&NameServer::doUpdate, this, interest.shared_from_this(), data),
Yumin Xia2c509c22017-02-09 14:37:36 -0800125 [this] (const Data& data, const security::v2::ValidationError& msg) {
Shock Jiange1a81fd2014-11-20 20:25:49 -0800126 NDNS_LOG_WARN("Ignoring update that did not pass the verification. "
Shock Jiang06cd2142014-11-23 17:36:02 -0800127 << "Check the root certificate")
Shock Jiangcde28712014-10-19 21:17:20 -0700128 });
129 }
130}
131
132void
133NameServer::onRegisterFailed(const ndn::Name& prefix, const std::string& reason)
134{
135 NDNS_LOG_FATAL("fail to register prefix=" << prefix << ". Due to: " << reason);
Yumin Xia2c509c22017-02-09 14:37:36 -0800136 BOOST_THROW_EXCEPTION(Error("zone " + m_zone.getName().toUri() + " register prefix: " +
137 prefix.toUri() + " fails. due to: " + reason));
Shock Jiangcde28712014-10-19 21:17:20 -0700138}
139
140void
141NameServer::doUpdate(const shared_ptr<const Interest>& interest,
142 const shared_ptr<const Data>& data)
143{
144 label::MatchResult re;
145 try {
Yumin Xia6343c5b2016-10-20 15:45:50 -0700146 if (!label::matchName(*data, m_zone.getName(), re))
Shock Jiangcde28712014-10-19 21:17:20 -0700147 return;
148 }
Yumin Xia2c509c22017-02-09 14:37:36 -0800149 catch (const std::exception& e) {
Shock Jiangcde28712014-10-19 21:17:20 -0700150 NDNS_LOG_INFO("Error while name/certificate matching: " << e.what());
151 }
152
153 Rrset rrset(&m_zone);
154 rrset.setLabel(re.rrLabel);
155 rrset.setType(re.rrType);
156
Shock Jiangcde28712014-10-19 21:17:20 -0700157 Name name = interest->getName();
158 name.appendVersion();
159 shared_ptr<Data> answer = make_shared<Data>(name);
Yumin Xiaa484ba72016-11-10 20:40:12 -0800160 answer->setFreshnessPeriod(this->getContentFreshness());
161 answer->setContentType(NDNS_RESP);
Shock Jiangcde28712014-10-19 21:17:20 -0700162
163 Block blk(ndn::ndns::tlv::RrData);
164 try {
165 if (m_dbMgr.find(rrset)) {
166 const name::Component& newVersion = re.version;
167 if (newVersion > rrset.getVersion()) {
168 // update existing record
169 rrset.setVersion(newVersion);
170 rrset.setData(data->wireEncode());
171 m_dbMgr.update(rrset);
Junxiao Shi767f35c2016-07-23 01:54:42 +0000172 blk.push_back(makeNonNegativeIntegerBlock(ndn::ndns::tlv::UpdateReturnCode, UPDATE_OK));
Shock Jiangcde28712014-10-19 21:17:20 -0700173 blk.encode(); // must
174 answer->setContent(blk);
175 NDNS_LOG_TRACE("replace old record and answer update with UPDATE_OK");
176 }
177 else {
Junxiao Shi767f35c2016-07-23 01:54:42 +0000178 blk.push_back(makeNonNegativeIntegerBlock(ndn::ndns::tlv::UpdateReturnCode, UPDATE_FAILURE));
Shock Jiangcde28712014-10-19 21:17:20 -0700179 blk.encode();
180 answer->setContent(blk);
181 NDNS_LOG_TRACE("answer update with UPDATE_FAILURE");
182 }
183 }
184 else {
185 // insert new record
186 rrset.setVersion(re.version);
187 rrset.setData(data->wireEncode());
188 rrset.setTtl(m_zone.getTtl());
189 m_dbMgr.insert(rrset);
Junxiao Shi767f35c2016-07-23 01:54:42 +0000190 blk.push_back(makeNonNegativeIntegerBlock(ndn::ndns::tlv::UpdateReturnCode, UPDATE_OK));
Shock Jiangcde28712014-10-19 21:17:20 -0700191 blk.encode();
192 answer->setContent(blk);
193 NDNS_LOG_TRACE("insert new record and answer update with UPDATE_OK");
194 }
195 }
Yumin Xia2c509c22017-02-09 14:37:36 -0800196 catch (const std::exception& e) {
Junxiao Shi767f35c2016-07-23 01:54:42 +0000197 blk.push_back(makeNonNegativeIntegerBlock(ndn::ndns::tlv::UpdateReturnCode, UPDATE_FAILURE));
Shock Jiangcde28712014-10-19 21:17:20 -0700198 blk.encode(); // must
199 answer->setContent(blk);
Shock Jiange1a81fd2014-11-20 20:25:49 -0800200 NDNS_LOG_INFO("Error processing the update: " << e.what()
201 << ". Update may need sudo privilege to write DbFile");
Shock Jiangcde28712014-10-19 21:17:20 -0700202 NDNS_LOG_TRACE("exception happens and answer update with UPDATE_FAILURE");
203 }
Yumin Xia2c509c22017-02-09 14:37:36 -0800204 m_keyChain.sign(*answer, signingByCertificate(m_certName));
Shock Jiangcde28712014-10-19 21:17:20 -0700205 m_face.put(*answer);
206}
207
208} // namespace ndns
209} // namespace ndn