blob: 15f77c3b45a001479d91bf1dca5cbf2235143034 [file] [log] [blame]
Shock Jiangcde28712014-10-19 21:17:20 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi767f35c2016-07-23 01:54:42 +00003 * Copyright (c) 2014-2016, 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"
22#include "clients/response.hpp"
23#include <ndn-cxx/encoding/encoding-buffer.hpp>
24
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,
32 KeyChain& keyChain, Validator& validator)
33 : m_zone(zoneName)
34 , m_dbMgr(dbMgr)
35 , m_ndnsPrefix(zoneName)
36 , m_keyPrefix(zoneName)
37 , m_certName(certName)
38 , m_contentFreshness(NAME_SERVER_DEFAULT_CONTENT_FRESHNESS)
39 , m_face(face)
40 , m_keyChain(keyChain)
41 , m_validator(validator)
42{
43 if (!m_keyChain.doesCertificateExist(m_certName)) {
44 NDNS_LOG_FATAL("Certificate: " << m_certName << " does not exist");
45 throw Error("certificate does not exist in the KeyChain: " + m_certName.toUri());
46 }
47
48 m_dbMgr.find(m_zone);
49
50 if (m_zone.getId() == 0) {
51 NDNS_LOG_FATAL("m_zone does not exist: " << zoneName);
52 throw Error("Zone " + zoneName.toUri() + " does not exist in the database");
53 }
54
55 m_ndnsPrefix.append(ndns::label::NDNS_ITERATIVE_QUERY);
56 m_keyPrefix.append(ndns::label::NDNS_CERT_QUERY);
57
58 m_face.setInterestFilter(m_ndnsPrefix,
59 bind(&NameServer::onInterest, this, _1, _2),
60 bind(&NameServer::onRegisterFailed, this, _1, _2)
61 );
62
63 m_face.setInterestFilter(m_keyPrefix,
64 bind(&NameServer::onInterest, this, _1, _2),
65 bind(&NameServer::onRegisterFailed, this, _1, _2)
66 );
67
68 NDNS_LOG_INFO("Zone: " << m_zone.getName() << " binds "
69 << "Prefix: " << m_ndnsPrefix << " and " << m_keyPrefix
70 << " with Certificate: " << m_certName
71 );
72}
73
74void
75NameServer::onInterest(const Name& prefix, const Interest& interest)
76{
77 label::MatchResult re;
Yumin Xia6343c5b2016-10-20 15:45:50 -070078 if (!label::matchName(interest, m_zone.getName(), re))
Shock Jiangcde28712014-10-19 21:17:20 -070079 return;
80
81 if (re.rrType == ndns::label::NDNS_UPDATE_LABEL) {
82 this->handleUpdate(prefix, interest, re); // NDNS Update
83 }
84 else {
85 this->handleQuery(prefix, interest, re); // NDNS Iterative query
86 }
87}
88
89void
90NameServer::handleQuery(const Name& prefix, const Interest& interest, const label::MatchResult& re)
91{
92 Rrset rrset(&m_zone);
93 rrset.setLabel(re.rrLabel);
94 rrset.setType(re.rrType);
95
96 NDNS_LOG_TRACE("query record: " << interest.getName());
97
Alexander Afanasyevaa46c272016-03-09 12:54:12 -080098 if (m_dbMgr.find(rrset) &&
99 (re.version.empty() || re.version == rrset.getVersion())) {
Shock Jiangcde28712014-10-19 21:17:20 -0700100 // find the record: NDNS-RESP, NDNS-AUTH, NDNS-RAW, or NDNS-NACK
101 shared_ptr<Data> answer = make_shared<Data>(rrset.getData());
102 NDNS_LOG_TRACE("answer query with existing Data: " << answer->getName());
103 m_face.put(*answer);
104 }
105 else {
106 // no record, construct NACK
Junxiao Shi767f35c2016-07-23 01:54:42 +0000107 Block block = makeNonNegativeIntegerBlock(tlv::NdnsType, NDNS_NACK);
Shock Jiangcde28712014-10-19 21:17:20 -0700108 MetaInfo info;
109 info.addAppMetaInfo(block);
110 info.setFreshnessPeriod(this->getContentFreshness());
111 Name name = interest.getName();
112 name.appendVersion();
113 shared_ptr<Data> answer = make_shared<Data>(name);
114 answer->setMetaInfo(info);
115
116 m_keyChain.sign(*answer, m_certName);
117 NDNS_LOG_TRACE("answer query with NDNS-NACK: " << answer->getName());
118 m_face.put(*answer);
119 }
120}
121
122void
123NameServer::handleUpdate(const Name& prefix, const Interest& interest, const label::MatchResult& re)
124{
125 if (re.rrLabel.size() == 1) {
126 // for current, we only allow Update message contains one Data, and ignore others
127 auto it = re.rrLabel.begin();
128 shared_ptr<Data> data;
129 try {
130 // blockFromValue may throw exception, which should not lead to failure of name server
131 const Block& block = it->blockFromValue();
132 data = make_shared<Data>(block);
133 }
134 catch (std::exception& e) {
135 NDNS_LOG_WARN("exception when getting update info: " << e.what());
136 return;
137 }
138 m_validator.validate(*data,
139 bind(&NameServer::doUpdate, this, interest.shared_from_this(), data),
140 [this] (const shared_ptr<const Data>& data, const std::string& msg) {
Shock Jiange1a81fd2014-11-20 20:25:49 -0800141 NDNS_LOG_WARN("Ignoring update that did not pass the verification. "
Shock Jiang06cd2142014-11-23 17:36:02 -0800142 << "Check the root certificate")
Shock Jiangcde28712014-10-19 21:17:20 -0700143 });
144 }
145}
146
147void
148NameServer::onRegisterFailed(const ndn::Name& prefix, const std::string& reason)
149{
150 NDNS_LOG_FATAL("fail to register prefix=" << prefix << ". Due to: " << reason);
151 throw Error("zone " + m_zone.getName().toUri() + " register prefix: " +
152 prefix.toUri() + " fails. due to: " + reason);
153}
154
155void
156NameServer::doUpdate(const shared_ptr<const Interest>& interest,
157 const shared_ptr<const Data>& data)
158{
159 label::MatchResult re;
160 try {
Yumin Xia6343c5b2016-10-20 15:45:50 -0700161 if (!label::matchName(*data, m_zone.getName(), re))
Shock Jiangcde28712014-10-19 21:17:20 -0700162 return;
163 }
164 catch (std::exception& e) {
165 NDNS_LOG_INFO("Error while name/certificate matching: " << e.what());
166 }
167
168 Rrset rrset(&m_zone);
169 rrset.setLabel(re.rrLabel);
170 rrset.setType(re.rrType);
171
Junxiao Shi767f35c2016-07-23 01:54:42 +0000172 Block ndnsType = makeNonNegativeIntegerBlock(::ndn::ndns::tlv::NdnsType, NDNS_RESP);
Shock Jiangcde28712014-10-19 21:17:20 -0700173 MetaInfo info;
174 info.addAppMetaInfo(ndnsType);
175 info.setFreshnessPeriod(this->getContentFreshness());
176 Name name = interest->getName();
177 name.appendVersion();
178 shared_ptr<Data> answer = make_shared<Data>(name);
179 answer->setMetaInfo(info);
180
181 Block blk(ndn::ndns::tlv::RrData);
182 try {
183 if (m_dbMgr.find(rrset)) {
184 const name::Component& newVersion = re.version;
185 if (newVersion > rrset.getVersion()) {
186 // update existing record
187 rrset.setVersion(newVersion);
188 rrset.setData(data->wireEncode());
189 m_dbMgr.update(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(); // must
192 answer->setContent(blk);
193 NDNS_LOG_TRACE("replace old record and answer update with UPDATE_OK");
194 }
195 else {
Junxiao Shi767f35c2016-07-23 01:54:42 +0000196 blk.push_back(makeNonNegativeIntegerBlock(ndn::ndns::tlv::UpdateReturnCode, UPDATE_FAILURE));
Shock Jiangcde28712014-10-19 21:17:20 -0700197 blk.encode();
198 answer->setContent(blk);
199 NDNS_LOG_TRACE("answer update with UPDATE_FAILURE");
200 }
201 }
202 else {
203 // insert new record
204 rrset.setVersion(re.version);
205 rrset.setData(data->wireEncode());
206 rrset.setTtl(m_zone.getTtl());
207 m_dbMgr.insert(rrset);
Junxiao Shi767f35c2016-07-23 01:54:42 +0000208 blk.push_back(makeNonNegativeIntegerBlock(ndn::ndns::tlv::UpdateReturnCode, UPDATE_OK));
Shock Jiangcde28712014-10-19 21:17:20 -0700209 blk.encode();
210 answer->setContent(blk);
211 NDNS_LOG_TRACE("insert new record and answer update with UPDATE_OK");
212 }
213 }
214 catch (std::exception& e) {
Junxiao Shi767f35c2016-07-23 01:54:42 +0000215 blk.push_back(makeNonNegativeIntegerBlock(ndn::ndns::tlv::UpdateReturnCode, UPDATE_FAILURE));
Shock Jiangcde28712014-10-19 21:17:20 -0700216 blk.encode(); // must
217 answer->setContent(blk);
Shock Jiange1a81fd2014-11-20 20:25:49 -0800218 NDNS_LOG_INFO("Error processing the update: " << e.what()
219 << ". Update may need sudo privilege to write DbFile");
Shock Jiangcde28712014-10-19 21:17:20 -0700220 NDNS_LOG_TRACE("exception happens and answer update with UPDATE_FAILURE");
221 }
222 m_keyChain.sign(*answer, m_certName);
223 m_face.put(*answer);
224}
225
226} // namespace ndns
227} // namespace ndn