blob: f796e1312388c32b3ec10189a04074cb2eb01a83 [file] [log] [blame]
Shock Jiangcde28712014-10-19 21:17:20 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014, Regents of the University of California.
4 *
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;
78 if (!label::matchName(interest, "", m_zone.getName(), re))
79 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
98 if (m_dbMgr.find(rrset)) {
99 // find the record: NDNS-RESP, NDNS-AUTH, NDNS-RAW, or NDNS-NACK
100 shared_ptr<Data> answer = make_shared<Data>(rrset.getData());
101 NDNS_LOG_TRACE("answer query with existing Data: " << answer->getName());
102 m_face.put(*answer);
103 }
104 else {
105 // no record, construct NACK
106 Block block = nonNegativeIntegerBlock(::ndn::ndns::tlv::NdnsType, NDNS_NACK);
107 MetaInfo info;
108 info.addAppMetaInfo(block);
109 info.setFreshnessPeriod(this->getContentFreshness());
110 Name name = interest.getName();
111 name.appendVersion();
112 shared_ptr<Data> answer = make_shared<Data>(name);
113 answer->setMetaInfo(info);
114
115 m_keyChain.sign(*answer, m_certName);
116 NDNS_LOG_TRACE("answer query with NDNS-NACK: " << answer->getName());
117 m_face.put(*answer);
118 }
119}
120
121void
122NameServer::handleUpdate(const Name& prefix, const Interest& interest, const label::MatchResult& re)
123{
124 if (re.rrLabel.size() == 1) {
125 // for current, we only allow Update message contains one Data, and ignore others
126 auto it = re.rrLabel.begin();
127 shared_ptr<Data> data;
128 try {
129 // blockFromValue may throw exception, which should not lead to failure of name server
130 const Block& block = it->blockFromValue();
131 data = make_shared<Data>(block);
132 }
133 catch (std::exception& e) {
134 NDNS_LOG_WARN("exception when getting update info: " << e.what());
135 return;
136 }
137 m_validator.validate(*data,
138 bind(&NameServer::doUpdate, this, interest.shared_from_this(), data),
139 [this] (const shared_ptr<const Data>& data, const std::string& msg) {
Shock Jiange1a81fd2014-11-20 20:25:49 -0800140 NDNS_LOG_WARN("Ignoring update that did not pass the verification. "
Shock Jiang06cd2142014-11-23 17:36:02 -0800141 << "Check the root certificate")
Shock Jiangcde28712014-10-19 21:17:20 -0700142 });
143 }
144}
145
146void
147NameServer::onRegisterFailed(const ndn::Name& prefix, const std::string& reason)
148{
149 NDNS_LOG_FATAL("fail to register prefix=" << prefix << ". Due to: " << reason);
150 throw Error("zone " + m_zone.getName().toUri() + " register prefix: " +
151 prefix.toUri() + " fails. due to: " + reason);
152}
153
154void
155NameServer::doUpdate(const shared_ptr<const Interest>& interest,
156 const shared_ptr<const Data>& data)
157{
158 label::MatchResult re;
159 try {
160 if (!label::matchName(*data, "", m_zone.getName(), re))
161 return;
162 }
163 catch (std::exception& e) {
164 NDNS_LOG_INFO("Error while name/certificate matching: " << e.what());
165 }
166
167 Rrset rrset(&m_zone);
168 rrset.setLabel(re.rrLabel);
169 rrset.setType(re.rrType);
170
171 Block ndnsType = nonNegativeIntegerBlock(::ndn::ndns::tlv::NdnsType, NDNS_RESP);
172 MetaInfo info;
173 info.addAppMetaInfo(ndnsType);
174 info.setFreshnessPeriod(this->getContentFreshness());
175 Name name = interest->getName();
176 name.appendVersion();
177 shared_ptr<Data> answer = make_shared<Data>(name);
178 answer->setMetaInfo(info);
179
180 Block blk(ndn::ndns::tlv::RrData);
181 try {
182 if (m_dbMgr.find(rrset)) {
183 const name::Component& newVersion = re.version;
184 if (newVersion > rrset.getVersion()) {
185 // update existing record
186 rrset.setVersion(newVersion);
187 rrset.setData(data->wireEncode());
188 m_dbMgr.update(rrset);
189 blk.push_back(nonNegativeIntegerBlock(ndn::ndns::tlv::UpdateReturnCode, UPDATE_OK));
190 blk.encode(); // must
191 answer->setContent(blk);
192 NDNS_LOG_TRACE("replace old record and answer update with UPDATE_OK");
193 }
194 else {
195 blk.push_back(nonNegativeIntegerBlock(ndn::ndns::tlv::UpdateReturnCode, UPDATE_FAILURE));
196 blk.encode();
197 answer->setContent(blk);
198 NDNS_LOG_TRACE("answer update with UPDATE_FAILURE");
199 }
200 }
201 else {
202 // insert new record
203 rrset.setVersion(re.version);
204 rrset.setData(data->wireEncode());
205 rrset.setTtl(m_zone.getTtl());
206 m_dbMgr.insert(rrset);
207 blk.push_back(nonNegativeIntegerBlock(ndn::ndns::tlv::UpdateReturnCode, UPDATE_OK));
208 blk.encode();
209 answer->setContent(blk);
210 NDNS_LOG_TRACE("insert new record and answer update with UPDATE_OK");
211 }
212 }
213 catch (std::exception& e) {
214 blk.push_back(nonNegativeIntegerBlock(ndn::ndns::tlv::UpdateReturnCode, UPDATE_FAILURE));
215 blk.encode(); // must
216 answer->setContent(blk);
Shock Jiange1a81fd2014-11-20 20:25:49 -0800217 NDNS_LOG_INFO("Error processing the update: " << e.what()
218 << ". Update may need sudo privilege to write DbFile");
Shock Jiangcde28712014-10-19 21:17:20 -0700219 NDNS_LOG_TRACE("exception happens and answer update with UPDATE_FAILURE");
220 }
221 m_keyChain.sign(*answer, m_certName);
222 m_face.put(*answer);
223}
224
225} // namespace ndns
226} // namespace ndn