Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 2 | /* |
Yumin Xia | 55a7cc4 | 2017-05-14 18:43:34 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014-2018, Regents of the University of California. |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 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" |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 22 | #include <ndn-cxx/encoding/encoding-buffer.hpp> |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 23 | #include <ndn-cxx/security/signing-helpers.hpp> |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 24 | |
| 25 | namespace ndn { |
| 26 | namespace ndns { |
Alexander Afanasyev | 08d1874 | 2018-03-15 16:31:28 -0400 | [diff] [blame] | 27 | |
| 28 | NDNS_LOG_INIT(NameServer); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 29 | |
| 30 | const time::milliseconds NAME_SERVER_DEFAULT_CONTENT_FRESHNESS(4000); |
| 31 | |
| 32 | NameServer::NameServer(const Name& zoneName, const Name& certName, Face& face, DbMgr& dbMgr, |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 33 | KeyChain& keyChain, security::v2::Validator& validator) |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 34 | : m_zone(zoneName) |
| 35 | , m_dbMgr(dbMgr) |
| 36 | , m_ndnsPrefix(zoneName) |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 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 | { |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 43 | m_dbMgr.find(m_zone); |
| 44 | |
| 45 | if (m_zone.getId() == 0) { |
| 46 | NDNS_LOG_FATAL("m_zone does not exist: " << zoneName); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 47 | BOOST_THROW_EXCEPTION(Error("Zone " + zoneName.toUri() + " does not exist in the database")); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | m_ndnsPrefix.append(ndns::label::NDNS_ITERATIVE_QUERY); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 51 | |
| 52 | m_face.setInterestFilter(m_ndnsPrefix, |
| 53 | bind(&NameServer::onInterest, this, _1, _2), |
| 54 | bind(&NameServer::onRegisterFailed, this, _1, _2) |
| 55 | ); |
| 56 | |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 57 | NDNS_LOG_INFO("Zone: " << m_zone.getName() << " binds " |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 58 | << "Prefix: " << m_ndnsPrefix |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 59 | << " with Certificate: " << m_certName |
| 60 | ); |
| 61 | } |
| 62 | |
| 63 | void |
| 64 | NameServer::onInterest(const Name& prefix, const Interest& interest) |
| 65 | { |
| 66 | label::MatchResult re; |
Yumin Xia | 6343c5b | 2016-10-20 15:45:50 -0700 | [diff] [blame] | 67 | if (!label::matchName(interest, m_zone.getName(), re)) |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 68 | return; |
| 69 | |
| 70 | if (re.rrType == ndns::label::NDNS_UPDATE_LABEL) { |
| 71 | this->handleUpdate(prefix, interest, re); // NDNS Update |
| 72 | } |
| 73 | else { |
| 74 | this->handleQuery(prefix, interest, re); // NDNS Iterative query |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | void |
| 79 | NameServer::handleQuery(const Name& prefix, const Interest& interest, const label::MatchResult& re) |
| 80 | { |
| 81 | Rrset rrset(&m_zone); |
| 82 | rrset.setLabel(re.rrLabel); |
| 83 | rrset.setType(re.rrType); |
| 84 | |
| 85 | NDNS_LOG_TRACE("query record: " << interest.getName()); |
| 86 | |
Alexander Afanasyev | aa46c27 | 2016-03-09 12:54:12 -0800 | [diff] [blame] | 87 | if (m_dbMgr.find(rrset) && |
| 88 | (re.version.empty() || re.version == rrset.getVersion())) { |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 89 | // find the record: NDNS-RESP, NDNS-AUTH, NDNS-RAW, or NDNS-NACK |
| 90 | shared_ptr<Data> answer = make_shared<Data>(rrset.getData()); |
| 91 | NDNS_LOG_TRACE("answer query with existing Data: " << answer->getName()); |
| 92 | m_face.put(*answer); |
| 93 | } |
| 94 | else { |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 95 | Name name = interest.getName(); |
| 96 | name.appendVersion(); |
| 97 | shared_ptr<Data> answer = make_shared<Data>(name); |
Yumin Xia | 55a7cc4 | 2017-05-14 18:43:34 -0700 | [diff] [blame] | 98 | Rrset doe(&m_zone); |
| 99 | // currently, there is only one DoE record contains everything |
| 100 | doe.setLabel(Name(re.rrLabel).append(re.rrType)); |
| 101 | doe.setType(label::DOE_RR_TYPE); |
| 102 | if (!m_dbMgr.findLowerBound(doe)) { |
| 103 | NDNS_LOG_FATAL("fail to find DoE record of zone:" + m_zone.getName().toUri()); |
| 104 | BOOST_THROW_EXCEPTION(std::runtime_error("fail to find DoE record of zone:" + m_zone.getName().toUri())); |
| 105 | } |
| 106 | |
| 107 | answer->setContent(doe.getData()); |
Yumin Xia | a484ba7 | 2016-11-10 20:40:12 -0800 | [diff] [blame] | 108 | answer->setFreshnessPeriod(this->getContentFreshness()); |
| 109 | answer->setContentType(NDNS_NACK); |
Yumin Xia | 55a7cc4 | 2017-05-14 18:43:34 -0700 | [diff] [blame] | 110 | // give this NACk a random signature |
| 111 | m_keyChain.sign(*answer); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 112 | |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 113 | NDNS_LOG_TRACE("answer query with NDNS-NACK: " << answer->getName()); |
| 114 | m_face.put(*answer); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | void |
| 119 | NameServer::handleUpdate(const Name& prefix, const Interest& interest, const label::MatchResult& re) |
| 120 | { |
| 121 | if (re.rrLabel.size() == 1) { |
| 122 | // for current, we only allow Update message contains one Data, and ignore others |
| 123 | auto it = re.rrLabel.begin(); |
| 124 | shared_ptr<Data> data; |
| 125 | try { |
| 126 | // blockFromValue may throw exception, which should not lead to failure of name server |
| 127 | const Block& block = it->blockFromValue(); |
| 128 | data = make_shared<Data>(block); |
| 129 | } |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 130 | catch (const std::exception& e) { |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 131 | NDNS_LOG_WARN("exception when getting update info: " << e.what()); |
| 132 | return; |
| 133 | } |
| 134 | m_validator.validate(*data, |
| 135 | bind(&NameServer::doUpdate, this, interest.shared_from_this(), data), |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 136 | [this] (const Data& data, const security::v2::ValidationError& msg) { |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 137 | NDNS_LOG_WARN("Ignoring update that did not pass the verification. " |
Alexander Afanasyev | 08d1874 | 2018-03-15 16:31:28 -0400 | [diff] [blame] | 138 | << "Check the root certificate"); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 139 | }); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | void |
| 144 | NameServer::onRegisterFailed(const ndn::Name& prefix, const std::string& reason) |
| 145 | { |
| 146 | NDNS_LOG_FATAL("fail to register prefix=" << prefix << ". Due to: " << reason); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 147 | BOOST_THROW_EXCEPTION(Error("zone " + m_zone.getName().toUri() + " register prefix: " + |
| 148 | prefix.toUri() + " fails. due to: " + reason)); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | void |
| 152 | NameServer::doUpdate(const shared_ptr<const Interest>& interest, |
| 153 | const shared_ptr<const Data>& data) |
| 154 | { |
| 155 | label::MatchResult re; |
| 156 | try { |
Yumin Xia | 6343c5b | 2016-10-20 15:45:50 -0700 | [diff] [blame] | 157 | if (!label::matchName(*data, m_zone.getName(), re)) |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 158 | return; |
| 159 | } |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 160 | catch (const std::exception& e) { |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 161 | NDNS_LOG_INFO("Error while name/certificate matching: " << e.what()); |
| 162 | } |
| 163 | |
| 164 | Rrset rrset(&m_zone); |
| 165 | rrset.setLabel(re.rrLabel); |
| 166 | rrset.setType(re.rrType); |
| 167 | |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 168 | Name name = interest->getName(); |
| 169 | name.appendVersion(); |
| 170 | shared_ptr<Data> answer = make_shared<Data>(name); |
Yumin Xia | a484ba7 | 2016-11-10 20:40:12 -0800 | [diff] [blame] | 171 | answer->setFreshnessPeriod(this->getContentFreshness()); |
| 172 | answer->setContentType(NDNS_RESP); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 173 | |
| 174 | Block blk(ndn::ndns::tlv::RrData); |
| 175 | try { |
| 176 | if (m_dbMgr.find(rrset)) { |
| 177 | const name::Component& newVersion = re.version; |
| 178 | if (newVersion > rrset.getVersion()) { |
| 179 | // update existing record |
| 180 | rrset.setVersion(newVersion); |
| 181 | rrset.setData(data->wireEncode()); |
| 182 | m_dbMgr.update(rrset); |
Junxiao Shi | 767f35c | 2016-07-23 01:54:42 +0000 | [diff] [blame] | 183 | blk.push_back(makeNonNegativeIntegerBlock(ndn::ndns::tlv::UpdateReturnCode, UPDATE_OK)); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 184 | blk.encode(); // must |
| 185 | answer->setContent(blk); |
| 186 | NDNS_LOG_TRACE("replace old record and answer update with UPDATE_OK"); |
| 187 | } |
| 188 | else { |
Junxiao Shi | 767f35c | 2016-07-23 01:54:42 +0000 | [diff] [blame] | 189 | blk.push_back(makeNonNegativeIntegerBlock(ndn::ndns::tlv::UpdateReturnCode, UPDATE_FAILURE)); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 190 | blk.encode(); |
| 191 | answer->setContent(blk); |
| 192 | NDNS_LOG_TRACE("answer update with UPDATE_FAILURE"); |
| 193 | } |
| 194 | } |
| 195 | else { |
| 196 | // insert new record |
| 197 | rrset.setVersion(re.version); |
| 198 | rrset.setData(data->wireEncode()); |
| 199 | rrset.setTtl(m_zone.getTtl()); |
| 200 | m_dbMgr.insert(rrset); |
Junxiao Shi | 767f35c | 2016-07-23 01:54:42 +0000 | [diff] [blame] | 201 | blk.push_back(makeNonNegativeIntegerBlock(ndn::ndns::tlv::UpdateReturnCode, UPDATE_OK)); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 202 | blk.encode(); |
| 203 | answer->setContent(blk); |
| 204 | NDNS_LOG_TRACE("insert new record and answer update with UPDATE_OK"); |
| 205 | } |
| 206 | } |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 207 | catch (const std::exception& e) { |
Junxiao Shi | 767f35c | 2016-07-23 01:54:42 +0000 | [diff] [blame] | 208 | blk.push_back(makeNonNegativeIntegerBlock(ndn::ndns::tlv::UpdateReturnCode, UPDATE_FAILURE)); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 209 | blk.encode(); // must |
| 210 | answer->setContent(blk); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 211 | NDNS_LOG_INFO("Error processing the update: " << e.what() |
| 212 | << ". Update may need sudo privilege to write DbFile"); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 213 | NDNS_LOG_TRACE("exception happens and answer update with UPDATE_FAILURE"); |
| 214 | } |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 215 | m_keyChain.sign(*answer, signingByCertificate(m_certName)); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 216 | m_face.put(*answer); |
| 217 | } |
| 218 | |
| 219 | } // namespace ndns |
| 220 | } // namespace ndn |