Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Junxiao Shi | 767f35c | 2016-07-23 01:54:42 +0000 | [diff] [blame^] | 3 | * Copyright (c) 2014-2016, 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" |
| 22 | #include "clients/response.hpp" |
| 23 | #include <ndn-cxx/encoding/encoding-buffer.hpp> |
| 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, |
| 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 | |
| 74 | void |
| 75 | NameServer::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 | |
| 89 | void |
| 90 | NameServer::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 Afanasyev | aa46c27 | 2016-03-09 12:54:12 -0800 | [diff] [blame] | 98 | if (m_dbMgr.find(rrset) && |
| 99 | (re.version.empty() || re.version == rrset.getVersion())) { |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 100 | // 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 Shi | 767f35c | 2016-07-23 01:54:42 +0000 | [diff] [blame^] | 107 | Block block = makeNonNegativeIntegerBlock(tlv::NdnsType, NDNS_NACK); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 108 | 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 | |
| 122 | void |
| 123 | NameServer::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 Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 141 | NDNS_LOG_WARN("Ignoring update that did not pass the verification. " |
Shock Jiang | 06cd214 | 2014-11-23 17:36:02 -0800 | [diff] [blame] | 142 | << "Check the root certificate") |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 143 | }); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | void |
| 148 | NameServer::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 | |
| 155 | void |
| 156 | NameServer::doUpdate(const shared_ptr<const Interest>& interest, |
| 157 | const shared_ptr<const Data>& data) |
| 158 | { |
| 159 | label::MatchResult re; |
| 160 | try { |
| 161 | if (!label::matchName(*data, "", m_zone.getName(), re)) |
| 162 | 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 Shi | 767f35c | 2016-07-23 01:54:42 +0000 | [diff] [blame^] | 172 | Block ndnsType = makeNonNegativeIntegerBlock(::ndn::ndns::tlv::NdnsType, NDNS_RESP); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 173 | 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 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(); // must |
| 192 | answer->setContent(blk); |
| 193 | NDNS_LOG_TRACE("replace old record and answer update with UPDATE_OK"); |
| 194 | } |
| 195 | else { |
Junxiao Shi | 767f35c | 2016-07-23 01:54:42 +0000 | [diff] [blame^] | 196 | blk.push_back(makeNonNegativeIntegerBlock(ndn::ndns::tlv::UpdateReturnCode, UPDATE_FAILURE)); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 197 | 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 Shi | 767f35c | 2016-07-23 01:54:42 +0000 | [diff] [blame^] | 208 | blk.push_back(makeNonNegativeIntegerBlock(ndn::ndns::tlv::UpdateReturnCode, UPDATE_OK)); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 209 | 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 Shi | 767f35c | 2016-07-23 01:54:42 +0000 | [diff] [blame^] | 215 | blk.push_back(makeNonNegativeIntegerBlock(ndn::ndns::tlv::UpdateReturnCode, UPDATE_FAILURE)); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 216 | blk.encode(); // must |
| 217 | answer->setContent(blk); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 218 | NDNS_LOG_INFO("Error processing the update: " << e.what() |
| 219 | << ". Update may need sudo privilege to write DbFile"); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 220 | 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 |