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