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 | /* |
| 3 | * Copyright (c) 2014-2017, 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 { |
| 27 | NDNS_LOG_INIT("NameServer") |
| 28 | |
| 29 | const time::milliseconds NAME_SERVER_DEFAULT_CONTENT_FRESHNESS(4000); |
| 30 | |
| 31 | NameServer::NameServer(const Name& zoneName, const Name& certName, Face& face, DbMgr& dbMgr, |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 32 | KeyChain& keyChain, security::v2::Validator& validator) |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 33 | : m_zone(zoneName) |
| 34 | , m_dbMgr(dbMgr) |
| 35 | , m_ndnsPrefix(zoneName) |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 36 | , 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 Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 42 | m_dbMgr.find(m_zone); |
| 43 | |
| 44 | if (m_zone.getId() == 0) { |
| 45 | NDNS_LOG_FATAL("m_zone does not exist: " << zoneName); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 46 | 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] | 47 | } |
| 48 | |
| 49 | m_ndnsPrefix.append(ndns::label::NDNS_ITERATIVE_QUERY); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 50 | |
| 51 | m_face.setInterestFilter(m_ndnsPrefix, |
| 52 | bind(&NameServer::onInterest, this, _1, _2), |
| 53 | bind(&NameServer::onRegisterFailed, this, _1, _2) |
| 54 | ); |
| 55 | |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 56 | NDNS_LOG_INFO("Zone: " << m_zone.getName() << " binds " |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 57 | << "Prefix: " << m_ndnsPrefix |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 58 | << " with Certificate: " << m_certName |
| 59 | ); |
| 60 | } |
| 61 | |
| 62 | void |
| 63 | NameServer::onInterest(const Name& prefix, const Interest& interest) |
| 64 | { |
| 65 | label::MatchResult re; |
Yumin Xia | 6343c5b | 2016-10-20 15:45:50 -0700 | [diff] [blame] | 66 | if (!label::matchName(interest, m_zone.getName(), re)) |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 67 | 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 | |
| 77 | void |
| 78 | NameServer::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 Afanasyev | aa46c27 | 2016-03-09 12:54:12 -0800 | [diff] [blame] | 86 | if (m_dbMgr.find(rrset) && |
| 87 | (re.version.empty() || re.version == rrset.getVersion())) { |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 88 | // 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 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 | a484ba7 | 2016-11-10 20:40:12 -0800 | [diff] [blame] | 98 | answer->setFreshnessPeriod(this->getContentFreshness()); |
| 99 | answer->setContentType(NDNS_NACK); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 100 | |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 101 | m_keyChain.sign(*answer, signingByCertificate(m_certName)); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 102 | NDNS_LOG_TRACE("answer query with NDNS-NACK: " << answer->getName()); |
| 103 | m_face.put(*answer); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | void |
| 108 | NameServer::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 Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 119 | catch (const std::exception& e) { |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 120 | 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 Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 125 | [this] (const Data& data, const security::v2::ValidationError& msg) { |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 126 | NDNS_LOG_WARN("Ignoring update that did not pass the verification. " |
Shock Jiang | 06cd214 | 2014-11-23 17:36:02 -0800 | [diff] [blame] | 127 | << "Check the root certificate") |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 128 | }); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | void |
| 133 | NameServer::onRegisterFailed(const ndn::Name& prefix, const std::string& reason) |
| 134 | { |
| 135 | NDNS_LOG_FATAL("fail to register prefix=" << prefix << ". Due to: " << reason); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 136 | BOOST_THROW_EXCEPTION(Error("zone " + m_zone.getName().toUri() + " register prefix: " + |
| 137 | prefix.toUri() + " fails. due to: " + reason)); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | void |
| 141 | NameServer::doUpdate(const shared_ptr<const Interest>& interest, |
| 142 | const shared_ptr<const Data>& data) |
| 143 | { |
| 144 | label::MatchResult re; |
| 145 | try { |
Yumin Xia | 6343c5b | 2016-10-20 15:45:50 -0700 | [diff] [blame] | 146 | if (!label::matchName(*data, m_zone.getName(), re)) |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 147 | return; |
| 148 | } |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 149 | catch (const std::exception& e) { |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 150 | 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 Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 157 | Name name = interest->getName(); |
| 158 | name.appendVersion(); |
| 159 | shared_ptr<Data> answer = make_shared<Data>(name); |
Yumin Xia | a484ba7 | 2016-11-10 20:40:12 -0800 | [diff] [blame] | 160 | answer->setFreshnessPeriod(this->getContentFreshness()); |
| 161 | answer->setContentType(NDNS_RESP); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 162 | |
| 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 Shi | 767f35c | 2016-07-23 01:54:42 +0000 | [diff] [blame] | 172 | blk.push_back(makeNonNegativeIntegerBlock(ndn::ndns::tlv::UpdateReturnCode, UPDATE_OK)); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 173 | blk.encode(); // must |
| 174 | answer->setContent(blk); |
| 175 | NDNS_LOG_TRACE("replace old record and answer update with UPDATE_OK"); |
| 176 | } |
| 177 | else { |
Junxiao Shi | 767f35c | 2016-07-23 01:54:42 +0000 | [diff] [blame] | 178 | blk.push_back(makeNonNegativeIntegerBlock(ndn::ndns::tlv::UpdateReturnCode, UPDATE_FAILURE)); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 179 | 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 Shi | 767f35c | 2016-07-23 01:54:42 +0000 | [diff] [blame] | 190 | blk.push_back(makeNonNegativeIntegerBlock(ndn::ndns::tlv::UpdateReturnCode, UPDATE_OK)); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 191 | blk.encode(); |
| 192 | answer->setContent(blk); |
| 193 | NDNS_LOG_TRACE("insert new record and answer update with UPDATE_OK"); |
| 194 | } |
| 195 | } |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 196 | catch (const std::exception& e) { |
Junxiao Shi | 767f35c | 2016-07-23 01:54:42 +0000 | [diff] [blame] | 197 | blk.push_back(makeNonNegativeIntegerBlock(ndn::ndns::tlv::UpdateReturnCode, UPDATE_FAILURE)); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 198 | blk.encode(); // must |
| 199 | answer->setContent(blk); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 200 | NDNS_LOG_INFO("Error processing the update: " << e.what() |
| 201 | << ". Update may need sudo privilege to write DbFile"); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 202 | NDNS_LOG_TRACE("exception happens and answer update with UPDATE_FAILURE"); |
| 203 | } |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 204 | m_keyChain.sign(*answer, signingByCertificate(m_certName)); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 205 | m_face.put(*answer); |
| 206 | } |
| 207 | |
| 208 | } // namespace ndns |
| 209 | } // namespace ndn |