blob: b40ba9b37a22c42e9cb8d9b46feb7a4edc8f4ab9 [file] [log] [blame]
Shock Jiang698e6ed2014-11-09 11:22:24 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Yumin Xia2c509c22017-02-09 14:37:36 -08002/*
Alexander Afanasyev60514ec2020-06-03 14:18:53 -04003 * Copyright (c) 2014-2020, Regents of the University of California.
Shock Jiang698e6ed2014-11-09 11:22:24 -08004 *
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
24namespace ndn {
25namespace ndns {
Alexander Afanasyev08d18742018-03-15 16:31:28 -040026
27NDNS_LOG_INIT(IterQueryCtr);
Shock Jiang698e6ed2014-11-09 11:22:24 -080028
29IterativeQueryController::IterativeQueryController(const Name& dstLabel,
30 const name::Component& rrType,
31 const time::milliseconds& interestLifetime,
32 const QuerySucceedCallback& onSucceed,
33 const QueryFailCallback& onFail,
Shock Jiang5d5928c2014-12-03 13:41:22 -080034 Face& face,
Alexander Afanasyev60514ec2020-06-03 14:18:53 -040035 security::Validator* validator,
Yumin Xiad8b75fc2017-04-05 15:00:21 -070036 ndn::InMemoryStorage* cache)
Shock Jiang698e6ed2014-11-09 11:22:24 -080037 : QueryController(dstLabel, rrType, interestLifetime, onSucceed, onFail, face)
Shock Jiang5d5928c2014-12-03 13:41:22 -080038 , m_validator(validator)
Shock Jiang698e6ed2014-11-09 11:22:24 -080039 , m_step(QUERY_STEP_QUERY_NS)
40 , m_nFinishedComps(0)
41 , m_nTryComps(1)
Yumin Xiad8b75fc2017-04-05 15:00:21 -070042 , m_nsCache(cache)
Shock Jiang698e6ed2014-11-09 11:22:24 -080043{
Shock Jiang698e6ed2014-11-09 11:22:24 -080044}
45
46void
47IterativeQueryController::onTimeout(const Interest& interest)
48{
49 NDNS_LOG_INFO("[* !! *] timeout happens: " << interest.getName());
50 NDNS_LOG_TRACE(*this);
51 this->abort();
52}
53
54void
55IterativeQueryController::abort()
56{
57 NDNS_LOG_DEBUG("abort iterative query");
58 if (m_onFail != nullptr)
59 m_onFail(0, "abort");
60 else
61 NDNS_LOG_TRACE("m_onFail is 0");
62
63}
64
65void
66IterativeQueryController::onData(const ndn::Interest& interest, const Data& data)
67{
Yumin Xiaa484ba72016-11-10 20:40:12 -080068 NdnsContentType contentType = NdnsContentType(data.getContentType());
Shock Jiang698e6ed2014-11-09 11:22:24 -080069
Yumin Xiaa484ba72016-11-10 20:40:12 -080070 NDNS_LOG_TRACE("[* -> *] get a " << contentType
Shock Jiang698e6ed2014-11-09 11:22:24 -080071 << " Response: " << data.getName());
Yumin Xia55a7cc42017-05-14 18:43:34 -070072
73 const Data* toBeValidatedData = nullptr;
74 if (contentType == NDNS_NACK) {
75 m_doe = Data(data.getContent().blockFromValue());
76 toBeValidatedData = &m_doe;
77 contentType = NDNS_DOE;
Shock Jiang5d5928c2014-12-03 13:41:22 -080078 }
79 else {
Yumin Xia55a7cc42017-05-14 18:43:34 -070080 toBeValidatedData = &data;
81 }
82
83 if (m_validator == nullptr) {
84 this->onDataValidated(*toBeValidatedData, contentType);
85 }
86 else {
87 m_validator->validate(*toBeValidatedData,
Yumin Xiaa484ba72016-11-10 20:40:12 -080088 bind(&IterativeQueryController::onDataValidated, this, _1, contentType),
Alexander Afanasyev60514ec2020-06-03 14:18:53 -040089 [this] (const Data& data, const security::ValidationError& err) {
Yumin Xia2c509c22017-02-09 14:37:36 -080090 NDNS_LOG_WARN("data: " << data.getName() << " fails verification");
Shock Jiang5d5928c2014-12-03 13:41:22 -080091 this->abort();
92 }
93 );
94 }
95}
Yumin Xia55a7cc42017-05-14 18:43:34 -070096
Shock Jiang5d5928c2014-12-03 13:41:22 -080097void
Yumin Xia2c509c22017-02-09 14:37:36 -080098IterativeQueryController::onDataValidated(const Data& data, NdnsContentType contentType)
Shock Jiang5d5928c2014-12-03 13:41:22 -080099{
Yumin Xiad8b75fc2017-04-05 15:00:21 -0700100 if (m_nsCache != nullptr && contentType == NDNS_LINK) {
101 m_nsCache->insert(data);
102 }
103
Shock Jiang698e6ed2014-11-09 11:22:24 -0800104 switch (m_step) {
105 case QUERY_STEP_QUERY_NS:
Yumin Xia55a7cc42017-05-14 18:43:34 -0700106 if (contentType == NDNS_DOE) {
107 // check if requested record is absent by looking up in doe
108 if (isAbsentByDoe(data)) {
109 m_step = QUERY_STEP_QUERY_RR;
110 }
111 else {
112 std::ostringstream oss;
113 oss << "In onDataValidated, absence of record can not be infered from DoE.";
114 oss << " Last query:" << m_lastLabelType << " ";
115 oss << *this;
116 BOOST_THROW_EXCEPTION(std::runtime_error(oss.str()));
117 }
Shock Jiang698e6ed2014-11-09 11:22:24 -0800118 }
Yumin Xiaa484ba72016-11-10 20:40:12 -0800119 else if (contentType == NDNS_LINK) {
Yumin Xia2c509c22017-02-09 14:37:36 -0800120 Link link(data.wireEncode());
121 if (link.getDelegationList().empty()) {
Yumin Xiaa484ba72016-11-10 20:40:12 -0800122 m_lastLink = Block();
Yumin Xia2c509c22017-02-09 14:37:36 -0800123 }
124 else {
125 m_lastLink = data.wireEncode();
Yumin Xia4e561892016-10-21 10:48:01 -0700126 }
Yumin Xiaa484ba72016-11-10 20:40:12 -0800127
128 // for NS query, if already received, just return, instead of more queries until NACK
Shock Jiang698e6ed2014-11-09 11:22:24 -0800129 if (m_nFinishedComps + m_nTryComps == m_dstLabel.size() && m_rrType == label::NS_RR_TYPE) {
130 // NS_RR_TYPE is different, since its record is stored at higher level
131 m_step = QUERY_STEP_ANSWER_STUB;
132 }
133 else {
134 m_nFinishedComps += m_nTryComps;
135 m_nTryComps = 1;
136 }
137 }
Yumin Xiaa484ba72016-11-10 20:40:12 -0800138 else if (contentType == NDNS_AUTH) {
Shock Jiang698e6ed2014-11-09 11:22:24 -0800139 m_nTryComps += 1;
140 }
Yumin Xia2c509c22017-02-09 14:37:36 -0800141 else {
Yumin Xiaa484ba72016-11-10 20:40:12 -0800142 std::ostringstream oss;
143 oss << *this;
144 NDNS_LOG_WARN("get unexpected Response for QUERY_NS: " << oss.str());
Shock Jiang698e6ed2014-11-09 11:22:24 -0800145 }
Yumin Xiadb8e9372017-04-09 14:05:32 -0700146
Shock Jiang698e6ed2014-11-09 11:22:24 -0800147 if (m_nFinishedComps + m_nTryComps > m_dstLabel.size()) {
148 if (m_rrType == label::NS_RR_TYPE) {
149 m_step = QUERY_STEP_ANSWER_STUB;
150 }
151 else
152 m_step = QUERY_STEP_QUERY_RR;
153 }
154 break;
155 case QUERY_STEP_QUERY_RR:
156 m_step = QUERY_STEP_ANSWER_STUB;
157 break;
158 default:
159 NDNS_LOG_WARN("get unexpected Response at State " << *this);
160 // throw std::runtime_error("call makeLatestInterest() unexpected: " << *this);
161 // do not throw except since it may be duplicated Data
162 m_step = QUERY_STEP_ABORT;
163 break;
164 }
165
166 if (!hasEnded())
167 this->express(this->makeLatestInterest()); // express new Expres
168 else if (m_step == QUERY_STEP_ANSWER_STUB) {
169 NDNS_LOG_TRACE("query ends: " << *this);
Yumin Xia2c509c22017-02-09 14:37:36 -0800170 Response re = this->parseFinalResponse(data);
Shock Jiang698e6ed2014-11-09 11:22:24 -0800171 if (m_onSucceed != nullptr)
Yumin Xia2c509c22017-02-09 14:37:36 -0800172 m_onSucceed(data, re);
Shock Jiang698e6ed2014-11-09 11:22:24 -0800173 else
174 NDNS_LOG_TRACE("succeed callback is nullptr");
175 }
176 else if (m_step == QUERY_STEP_ABORT)
177 this->abort();
178}
179
180bool
181IterativeQueryController::hasEnded()
182{
183 return (m_step != QUERY_STEP_QUERY_NS && m_step != QUERY_STEP_QUERY_RR);
184}
185
186void
187IterativeQueryController::start()
188{
Shock Jiang5d5928c2014-12-03 13:41:22 -0800189 if (m_dstLabel.size() == m_nFinishedComps)
190 m_step = QUERY_STEP_QUERY_RR;
191
Shock Jiang698e6ed2014-11-09 11:22:24 -0800192 Interest interest = this->makeLatestInterest();
193 express(interest);
194}
195
196
197void
198IterativeQueryController::express(const Interest& interest)
199{
Yumin Xiad8b75fc2017-04-05 15:00:21 -0700200 if (m_nsCache != nullptr) {
201 shared_ptr<const Data> cachedData = m_nsCache->find(interest);
202 if (cachedData != nullptr) {
203 NDNS_LOG_DEBUG("[* cached *] NS record has been cached before: "
204 << interest.getName());
205 onData(interest, *cachedData);
206 return ;
207 }
208 }
209
Shock Jiang698e6ed2014-11-09 11:22:24 -0800210 NDNS_LOG_DEBUG("[* <- *] send a Query: " << interest.getName());
211 m_face.expressInterest(interest,
212 bind(&IterativeQueryController::onData, this, _1, _2),
Alexander Afanasyevf4193ea2017-06-12 15:35:13 -0700213 bind(&IterativeQueryController::onTimeout, this, _1), // nack
Shock Jiang698e6ed2014-11-09 11:22:24 -0800214 bind(&IterativeQueryController::onTimeout, this, _1)
215 );
216}
217
218
219const Response
220IterativeQueryController::parseFinalResponse(const Data& data)
221{
222 Response re;
223 Name zone = m_dstLabel.getPrefix(m_nFinishedComps);
Yumin Xia6343c5b2016-10-20 15:45:50 -0700224 re.fromData(zone, data);
Shock Jiang698e6ed2014-11-09 11:22:24 -0800225 return re;
226}
227
228const Interest
229IterativeQueryController::makeLatestInterest()
230{
231 // NDNS_LOG_TRACE("get latest Interest");
232 Query query;
233 //const Name& dstLabel = m_query.getRrLabel();
234
235 query.setZone(m_dstLabel.getPrefix(m_nFinishedComps));
236 query.setInterestLifetime(m_interestLifetime);
Yumin Xia4e561892016-10-21 10:48:01 -0700237
238 // addLink
239 if (m_lastLink.hasWire()) {
Yumin Xia2c509c22017-02-09 14:37:36 -0800240 query.setDelegationListFromLink(Link(m_lastLink));
Yumin Xia4e561892016-10-21 10:48:01 -0700241 }
242
Shock Jiang698e6ed2014-11-09 11:22:24 -0800243 switch (m_step) {
244 case QUERY_STEP_QUERY_NS:
245 query.setQueryType(label::NDNS_ITERATIVE_QUERY);
246 query.setRrLabel(m_dstLabel.getSubName(m_nFinishedComps, m_nTryComps));
247 query.setRrType(label::NS_RR_TYPE);
248 break;
249 case QUERY_STEP_QUERY_RR:
Yumin Xia918343d2017-03-17 19:04:55 -0700250 query.setQueryType(label::NDNS_ITERATIVE_QUERY);
Shock Jiang698e6ed2014-11-09 11:22:24 -0800251 query.setRrLabel(m_dstLabel.getSubName(m_nFinishedComps));
252 query.setRrType(m_rrType);
253 break;
254 default:
255 std::ostringstream oss;
256 oss << *this;
257 NDNS_LOG_WARN("unexpected state: " << oss.str());
Yumin Xia2c509c22017-02-09 14:37:36 -0800258 BOOST_THROW_EXCEPTION(std::runtime_error("call makeLatestInterest() unexpected: "
259 + oss.str()));
Shock Jiang698e6ed2014-11-09 11:22:24 -0800260 }
261
Yumin Xia55a7cc42017-05-14 18:43:34 -0700262 m_lastLabelType = Name(query.getRrLabel()).append(query.getRrType());
Shock Jiang698e6ed2014-11-09 11:22:24 -0800263 Interest interest = query.toInterest();
264 return interest;
265}
266
Yumin Xia55a7cc42017-05-14 18:43:34 -0700267bool
268IterativeQueryController::isAbsentByDoe(const Data& data) const
269{
270 std::pair<Name, Name> range = Response::wireDecodeDoe(data.getContent());
271
272 // should not be simple <, use our own definition of compare
273 if (range.first < m_lastLabelType && m_lastLabelType < range.second) {
274 return true;
275 }
276 if (range.second < range.first &&
277 (m_lastLabelType < range.first || range.second < m_lastLabelType)) {
278 return true;
279 }
280 return false;
281}
282
Shock Jiang698e6ed2014-11-09 11:22:24 -0800283std::ostream&
284operator<<(std::ostream& os, const IterativeQueryController::QueryStep step)
285{
286 switch (step) {
287 case IterativeQueryController::QUERY_STEP_QUERY_NS:
288 os << "QueryNS";
289 break;
290 case IterativeQueryController::QUERY_STEP_QUERY_RR:
291 os << "QueryRR";
292 break;
293 case IterativeQueryController::QUERY_STEP_ANSWER_STUB:
294 os << "AnswerStub";
295 break;
296 case IterativeQueryController::QUERY_STEP_ABORT:
297 os << "Abort";
298 break;
299 default:
300 os << "UNKNOW";
301 break;
302 }
303 return os;
304}
305
306std::ostream&
307operator<<(std::ostream& os, const IterativeQueryController& ctr)
308{
309 os << "InterativeQueryController: dstLabel=" << ctr.getDstLabel()
310 << " rrType=" << ctr.getRrType()
311 << " currentStep=" << ctr.getStep()
312 << " nFinishedComps=" << ctr.getNFinishedComps()
313 << " nTryComp=" << ctr.getNTryComps()
314 ;
315
316 return os;
317}
318
319} // namespace ndns
320} // namespace ndn