Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [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 | 698e6ed | 2014-11-09 11:22:24 -0800 | [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 "iterative-query-controller.hpp" |
| 21 | #include "logger.hpp" |
| 22 | #include <iostream> |
| 23 | |
| 24 | namespace ndn { |
| 25 | namespace ndns { |
| 26 | NDNS_LOG_INIT("IterQueryCtr") |
| 27 | |
| 28 | IterativeQueryController::IterativeQueryController(const Name& dstLabel, |
| 29 | const name::Component& rrType, |
| 30 | const time::milliseconds& interestLifetime, |
| 31 | const QuerySucceedCallback& onSucceed, |
| 32 | const QueryFailCallback& onFail, |
Shock Jiang | 5d5928c | 2014-12-03 13:41:22 -0800 | [diff] [blame] | 33 | Face& face, |
Yumin Xia | d8b75fc | 2017-04-05 15:00:21 -0700 | [diff] [blame] | 34 | security::v2::Validator* validator, |
| 35 | ndn::InMemoryStorage* cache) |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 36 | : QueryController(dstLabel, rrType, interestLifetime, onSucceed, onFail, face) |
Shock Jiang | 5d5928c | 2014-12-03 13:41:22 -0800 | [diff] [blame] | 37 | , m_validator(validator) |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 38 | , m_step(QUERY_STEP_QUERY_NS) |
| 39 | , m_nFinishedComps(0) |
| 40 | , m_nTryComps(1) |
Yumin Xia | d8b75fc | 2017-04-05 15:00:21 -0700 | [diff] [blame] | 41 | , m_nsCache(cache) |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 42 | { |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | void |
| 46 | IterativeQueryController::onTimeout(const Interest& interest) |
| 47 | { |
| 48 | NDNS_LOG_INFO("[* !! *] timeout happens: " << interest.getName()); |
| 49 | NDNS_LOG_TRACE(*this); |
| 50 | this->abort(); |
| 51 | } |
| 52 | |
| 53 | void |
| 54 | IterativeQueryController::abort() |
| 55 | { |
| 56 | NDNS_LOG_DEBUG("abort iterative query"); |
| 57 | if (m_onFail != nullptr) |
| 58 | m_onFail(0, "abort"); |
| 59 | else |
| 60 | NDNS_LOG_TRACE("m_onFail is 0"); |
| 61 | |
| 62 | } |
| 63 | |
| 64 | void |
| 65 | IterativeQueryController::onData(const ndn::Interest& interest, const Data& data) |
| 66 | { |
Yumin Xia | a484ba7 | 2016-11-10 20:40:12 -0800 | [diff] [blame] | 67 | NdnsContentType contentType = NdnsContentType(data.getContentType()); |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 68 | |
Yumin Xia | a484ba7 | 2016-11-10 20:40:12 -0800 | [diff] [blame] | 69 | NDNS_LOG_TRACE("[* -> *] get a " << contentType |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 70 | << " Response: " << data.getName()); |
Shock Jiang | 5d5928c | 2014-12-03 13:41:22 -0800 | [diff] [blame] | 71 | if (m_validator == nullptr) { |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 72 | this->onDataValidated(data, contentType); |
Shock Jiang | 5d5928c | 2014-12-03 13:41:22 -0800 | [diff] [blame] | 73 | } |
| 74 | else { |
| 75 | m_validator->validate(data, |
Yumin Xia | a484ba7 | 2016-11-10 20:40:12 -0800 | [diff] [blame] | 76 | bind(&IterativeQueryController::onDataValidated, this, _1, contentType), |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 77 | [this] (const Data& data, const security::v2::ValidationError& err) { |
| 78 | NDNS_LOG_WARN("data: " << data.getName() << " fails verification"); |
Shock Jiang | 5d5928c | 2014-12-03 13:41:22 -0800 | [diff] [blame] | 79 | this->abort(); |
| 80 | } |
| 81 | ); |
| 82 | } |
| 83 | } |
| 84 | void |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 85 | IterativeQueryController::onDataValidated(const Data& data, NdnsContentType contentType) |
Shock Jiang | 5d5928c | 2014-12-03 13:41:22 -0800 | [diff] [blame] | 86 | { |
Yumin Xia | d8b75fc | 2017-04-05 15:00:21 -0700 | [diff] [blame] | 87 | if (m_nsCache != nullptr && contentType == NDNS_LINK) { |
| 88 | m_nsCache->insert(data); |
| 89 | } |
| 90 | |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 91 | switch (m_step) { |
| 92 | case QUERY_STEP_QUERY_NS: |
Yumin Xia | a484ba7 | 2016-11-10 20:40:12 -0800 | [diff] [blame] | 93 | if (contentType == NDNS_NACK) { |
Shock Jiang | 06cd214 | 2014-11-23 17:36:02 -0800 | [diff] [blame] | 94 | m_step = QUERY_STEP_QUERY_RR; |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 95 | } |
Yumin Xia | a484ba7 | 2016-11-10 20:40:12 -0800 | [diff] [blame] | 96 | else if (contentType == NDNS_LINK) { |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 97 | Link link(data.wireEncode()); |
| 98 | if (link.getDelegationList().empty()) { |
Yumin Xia | a484ba7 | 2016-11-10 20:40:12 -0800 | [diff] [blame] | 99 | m_lastLink = Block(); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 100 | } |
| 101 | else { |
| 102 | m_lastLink = data.wireEncode(); |
Yumin Xia | 4e56189 | 2016-10-21 10:48:01 -0700 | [diff] [blame] | 103 | } |
Yumin Xia | a484ba7 | 2016-11-10 20:40:12 -0800 | [diff] [blame] | 104 | |
| 105 | // for NS query, if already received, just return, instead of more queries until NACK |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 106 | if (m_nFinishedComps + m_nTryComps == m_dstLabel.size() && m_rrType == label::NS_RR_TYPE) { |
| 107 | // NS_RR_TYPE is different, since its record is stored at higher level |
| 108 | m_step = QUERY_STEP_ANSWER_STUB; |
| 109 | } |
| 110 | else { |
| 111 | m_nFinishedComps += m_nTryComps; |
| 112 | m_nTryComps = 1; |
| 113 | } |
| 114 | } |
Yumin Xia | a484ba7 | 2016-11-10 20:40:12 -0800 | [diff] [blame] | 115 | else if (contentType == NDNS_AUTH) { |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 116 | m_nTryComps += 1; |
| 117 | } |
Yumin Xia | a484ba7 | 2016-11-10 20:40:12 -0800 | [diff] [blame] | 118 | else if (contentType == NDNS_BLOB) { |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 119 | std::ostringstream oss; |
| 120 | oss << *this; |
Yumin Xia | a484ba7 | 2016-11-10 20:40:12 -0800 | [diff] [blame] | 121 | NDNS_LOG_WARN("get unexpected Response: NDNS_BLOB for QUERY_NS: " << oss.str()); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 122 | } |
| 123 | else { |
Yumin Xia | a484ba7 | 2016-11-10 20:40:12 -0800 | [diff] [blame] | 124 | std::ostringstream oss; |
| 125 | oss << *this; |
| 126 | NDNS_LOG_WARN("get unexpected Response for QUERY_NS: " << oss.str()); |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 127 | } |
| 128 | // |
| 129 | if (m_nFinishedComps + m_nTryComps > m_dstLabel.size()) { |
| 130 | if (m_rrType == label::NS_RR_TYPE) { |
| 131 | m_step = QUERY_STEP_ANSWER_STUB; |
| 132 | } |
| 133 | else |
| 134 | m_step = QUERY_STEP_QUERY_RR; |
| 135 | } |
| 136 | break; |
| 137 | case QUERY_STEP_QUERY_RR: |
| 138 | m_step = QUERY_STEP_ANSWER_STUB; |
| 139 | break; |
| 140 | default: |
| 141 | NDNS_LOG_WARN("get unexpected Response at State " << *this); |
| 142 | // throw std::runtime_error("call makeLatestInterest() unexpected: " << *this); |
| 143 | // do not throw except since it may be duplicated Data |
| 144 | m_step = QUERY_STEP_ABORT; |
| 145 | break; |
| 146 | } |
| 147 | |
| 148 | if (!hasEnded()) |
| 149 | this->express(this->makeLatestInterest()); // express new Expres |
| 150 | else if (m_step == QUERY_STEP_ANSWER_STUB) { |
| 151 | NDNS_LOG_TRACE("query ends: " << *this); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 152 | Response re = this->parseFinalResponse(data); |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 153 | if (m_onSucceed != nullptr) |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 154 | m_onSucceed(data, re); |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 155 | else |
| 156 | NDNS_LOG_TRACE("succeed callback is nullptr"); |
| 157 | } |
| 158 | else if (m_step == QUERY_STEP_ABORT) |
| 159 | this->abort(); |
| 160 | } |
| 161 | |
| 162 | bool |
| 163 | IterativeQueryController::hasEnded() |
| 164 | { |
| 165 | return (m_step != QUERY_STEP_QUERY_NS && m_step != QUERY_STEP_QUERY_RR); |
| 166 | } |
| 167 | |
| 168 | void |
| 169 | IterativeQueryController::start() |
| 170 | { |
Shock Jiang | 5d5928c | 2014-12-03 13:41:22 -0800 | [diff] [blame] | 171 | if (m_dstLabel.size() == m_nFinishedComps) |
| 172 | m_step = QUERY_STEP_QUERY_RR; |
| 173 | |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 174 | Interest interest = this->makeLatestInterest(); |
| 175 | express(interest); |
| 176 | } |
| 177 | |
| 178 | |
| 179 | void |
| 180 | IterativeQueryController::express(const Interest& interest) |
| 181 | { |
Yumin Xia | d8b75fc | 2017-04-05 15:00:21 -0700 | [diff] [blame] | 182 | if (m_nsCache != nullptr) { |
| 183 | shared_ptr<const Data> cachedData = m_nsCache->find(interest); |
| 184 | if (cachedData != nullptr) { |
| 185 | NDNS_LOG_DEBUG("[* cached *] NS record has been cached before: " |
| 186 | << interest.getName()); |
| 187 | onData(interest, *cachedData); |
| 188 | return ; |
| 189 | } |
| 190 | } |
| 191 | |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 192 | NDNS_LOG_DEBUG("[* <- *] send a Query: " << interest.getName()); |
| 193 | m_face.expressInterest(interest, |
| 194 | bind(&IterativeQueryController::onData, this, _1, _2), |
Alexander Afanasyev | f4193ea | 2017-06-12 15:35:13 -0700 | [diff] [blame] | 195 | bind(&IterativeQueryController::onTimeout, this, _1), // nack |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 196 | bind(&IterativeQueryController::onTimeout, this, _1) |
| 197 | ); |
| 198 | } |
| 199 | |
| 200 | |
| 201 | const Response |
| 202 | IterativeQueryController::parseFinalResponse(const Data& data) |
| 203 | { |
| 204 | Response re; |
| 205 | Name zone = m_dstLabel.getPrefix(m_nFinishedComps); |
Yumin Xia | 6343c5b | 2016-10-20 15:45:50 -0700 | [diff] [blame] | 206 | re.fromData(zone, data); |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 207 | return re; |
| 208 | } |
| 209 | |
| 210 | const Interest |
| 211 | IterativeQueryController::makeLatestInterest() |
| 212 | { |
| 213 | // NDNS_LOG_TRACE("get latest Interest"); |
| 214 | Query query; |
| 215 | //const Name& dstLabel = m_query.getRrLabel(); |
| 216 | |
| 217 | query.setZone(m_dstLabel.getPrefix(m_nFinishedComps)); |
| 218 | query.setInterestLifetime(m_interestLifetime); |
Yumin Xia | 4e56189 | 2016-10-21 10:48:01 -0700 | [diff] [blame] | 219 | |
| 220 | // addLink |
| 221 | if (m_lastLink.hasWire()) { |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 222 | query.setDelegationListFromLink(Link(m_lastLink)); |
Yumin Xia | 4e56189 | 2016-10-21 10:48:01 -0700 | [diff] [blame] | 223 | } |
| 224 | |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 225 | switch (m_step) { |
| 226 | case QUERY_STEP_QUERY_NS: |
| 227 | query.setQueryType(label::NDNS_ITERATIVE_QUERY); |
| 228 | query.setRrLabel(m_dstLabel.getSubName(m_nFinishedComps, m_nTryComps)); |
| 229 | query.setRrType(label::NS_RR_TYPE); |
| 230 | break; |
| 231 | case QUERY_STEP_QUERY_RR: |
Yumin Xia | 918343d | 2017-03-17 19:04:55 -0700 | [diff] [blame] | 232 | query.setQueryType(label::NDNS_ITERATIVE_QUERY); |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 233 | query.setRrLabel(m_dstLabel.getSubName(m_nFinishedComps)); |
| 234 | query.setRrType(m_rrType); |
| 235 | break; |
| 236 | default: |
| 237 | std::ostringstream oss; |
| 238 | oss << *this; |
| 239 | NDNS_LOG_WARN("unexpected state: " << oss.str()); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 240 | BOOST_THROW_EXCEPTION(std::runtime_error("call makeLatestInterest() unexpected: " |
| 241 | + oss.str())); |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 242 | } |
| 243 | |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 244 | Interest interest = query.toInterest(); |
| 245 | return interest; |
| 246 | } |
| 247 | |
| 248 | std::ostream& |
| 249 | operator<<(std::ostream& os, const IterativeQueryController::QueryStep step) |
| 250 | { |
| 251 | switch (step) { |
| 252 | case IterativeQueryController::QUERY_STEP_QUERY_NS: |
| 253 | os << "QueryNS"; |
| 254 | break; |
| 255 | case IterativeQueryController::QUERY_STEP_QUERY_RR: |
| 256 | os << "QueryRR"; |
| 257 | break; |
| 258 | case IterativeQueryController::QUERY_STEP_ANSWER_STUB: |
| 259 | os << "AnswerStub"; |
| 260 | break; |
| 261 | case IterativeQueryController::QUERY_STEP_ABORT: |
| 262 | os << "Abort"; |
| 263 | break; |
| 264 | default: |
| 265 | os << "UNKNOW"; |
| 266 | break; |
| 267 | } |
| 268 | return os; |
| 269 | } |
| 270 | |
| 271 | std::ostream& |
| 272 | operator<<(std::ostream& os, const IterativeQueryController& ctr) |
| 273 | { |
| 274 | os << "InterativeQueryController: dstLabel=" << ctr.getDstLabel() |
| 275 | << " rrType=" << ctr.getRrType() |
| 276 | << " currentStep=" << ctr.getStep() |
| 277 | << " nFinishedComps=" << ctr.getNFinishedComps() |
| 278 | << " nTryComp=" << ctr.getNTryComps() |
| 279 | ; |
| 280 | |
| 281 | return os; |
| 282 | } |
| 283 | |
| 284 | } // namespace ndns |
| 285 | } // namespace ndn |