blob: 2b564261ca1c3b66e618844c3dfcea86ac7dd708 [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/*
3 * Copyright (c) 2014-2017, 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 {
26NDNS_LOG_INIT("IterQueryCtr")
27
28IterativeQueryController::IterativeQueryController(const Name& dstLabel,
29 const name::Component& rrType,
30 const time::milliseconds& interestLifetime,
31 const QuerySucceedCallback& onSucceed,
32 const QueryFailCallback& onFail,
Shock Jiang5d5928c2014-12-03 13:41:22 -080033 Face& face,
Yumin Xiad8b75fc2017-04-05 15:00:21 -070034 security::v2::Validator* validator,
35 ndn::InMemoryStorage* cache)
Shock Jiang698e6ed2014-11-09 11:22:24 -080036 : QueryController(dstLabel, rrType, interestLifetime, onSucceed, onFail, face)
Shock Jiang5d5928c2014-12-03 13:41:22 -080037 , m_validator(validator)
Shock Jiang698e6ed2014-11-09 11:22:24 -080038 , m_step(QUERY_STEP_QUERY_NS)
39 , m_nFinishedComps(0)
40 , m_nTryComps(1)
Yumin Xiad8b75fc2017-04-05 15:00:21 -070041 , m_nsCache(cache)
Shock Jiang698e6ed2014-11-09 11:22:24 -080042{
Shock Jiang698e6ed2014-11-09 11:22:24 -080043}
44
45void
46IterativeQueryController::onTimeout(const Interest& interest)
47{
48 NDNS_LOG_INFO("[* !! *] timeout happens: " << interest.getName());
49 NDNS_LOG_TRACE(*this);
50 this->abort();
51}
52
53void
54IterativeQueryController::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
64void
65IterativeQueryController::onData(const ndn::Interest& interest, const Data& data)
66{
Yumin Xiaa484ba72016-11-10 20:40:12 -080067 NdnsContentType contentType = NdnsContentType(data.getContentType());
Shock Jiang698e6ed2014-11-09 11:22:24 -080068
Yumin Xiaa484ba72016-11-10 20:40:12 -080069 NDNS_LOG_TRACE("[* -> *] get a " << contentType
Shock Jiang698e6ed2014-11-09 11:22:24 -080070 << " Response: " << data.getName());
Shock Jiang5d5928c2014-12-03 13:41:22 -080071 if (m_validator == nullptr) {
Yumin Xia2c509c22017-02-09 14:37:36 -080072 this->onDataValidated(data, contentType);
Shock Jiang5d5928c2014-12-03 13:41:22 -080073 }
74 else {
75 m_validator->validate(data,
Yumin Xiaa484ba72016-11-10 20:40:12 -080076 bind(&IterativeQueryController::onDataValidated, this, _1, contentType),
Yumin Xia2c509c22017-02-09 14:37:36 -080077 [this] (const Data& data, const security::v2::ValidationError& err) {
78 NDNS_LOG_WARN("data: " << data.getName() << " fails verification");
Shock Jiang5d5928c2014-12-03 13:41:22 -080079 this->abort();
80 }
81 );
82 }
83}
84void
Yumin Xia2c509c22017-02-09 14:37:36 -080085IterativeQueryController::onDataValidated(const Data& data, NdnsContentType contentType)
Shock Jiang5d5928c2014-12-03 13:41:22 -080086{
Yumin Xiad8b75fc2017-04-05 15:00:21 -070087 if (m_nsCache != nullptr && contentType == NDNS_LINK) {
88 m_nsCache->insert(data);
89 }
90
Shock Jiang698e6ed2014-11-09 11:22:24 -080091 switch (m_step) {
92 case QUERY_STEP_QUERY_NS:
Yumin Xiaa484ba72016-11-10 20:40:12 -080093 if (contentType == NDNS_NACK) {
Shock Jiang06cd2142014-11-23 17:36:02 -080094 m_step = QUERY_STEP_QUERY_RR;
Shock Jiang698e6ed2014-11-09 11:22:24 -080095 }
Yumin Xiaa484ba72016-11-10 20:40:12 -080096 else if (contentType == NDNS_LINK) {
Yumin Xia2c509c22017-02-09 14:37:36 -080097 Link link(data.wireEncode());
98 if (link.getDelegationList().empty()) {
Yumin Xiaa484ba72016-11-10 20:40:12 -080099 m_lastLink = Block();
Yumin Xia2c509c22017-02-09 14:37:36 -0800100 }
101 else {
102 m_lastLink = data.wireEncode();
Yumin Xia4e561892016-10-21 10:48:01 -0700103 }
Yumin Xiaa484ba72016-11-10 20:40:12 -0800104
105 // for NS query, if already received, just return, instead of more queries until NACK
Shock Jiang698e6ed2014-11-09 11:22:24 -0800106 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 Xiaa484ba72016-11-10 20:40:12 -0800115 else if (contentType == NDNS_AUTH) {
Shock Jiang698e6ed2014-11-09 11:22:24 -0800116 m_nTryComps += 1;
117 }
Yumin Xia2c509c22017-02-09 14:37:36 -0800118 else {
Yumin Xiaa484ba72016-11-10 20:40:12 -0800119 std::ostringstream oss;
120 oss << *this;
121 NDNS_LOG_WARN("get unexpected Response for QUERY_NS: " << oss.str());
Shock Jiang698e6ed2014-11-09 11:22:24 -0800122 }
Yumin Xiadb8e9372017-04-09 14:05:32 -0700123
Shock Jiang698e6ed2014-11-09 11:22:24 -0800124 if (m_nFinishedComps + m_nTryComps > m_dstLabel.size()) {
125 if (m_rrType == label::NS_RR_TYPE) {
126 m_step = QUERY_STEP_ANSWER_STUB;
127 }
128 else
129 m_step = QUERY_STEP_QUERY_RR;
130 }
131 break;
132 case QUERY_STEP_QUERY_RR:
133 m_step = QUERY_STEP_ANSWER_STUB;
134 break;
135 default:
136 NDNS_LOG_WARN("get unexpected Response at State " << *this);
137 // throw std::runtime_error("call makeLatestInterest() unexpected: " << *this);
138 // do not throw except since it may be duplicated Data
139 m_step = QUERY_STEP_ABORT;
140 break;
141 }
142
143 if (!hasEnded())
144 this->express(this->makeLatestInterest()); // express new Expres
145 else if (m_step == QUERY_STEP_ANSWER_STUB) {
146 NDNS_LOG_TRACE("query ends: " << *this);
Yumin Xia2c509c22017-02-09 14:37:36 -0800147 Response re = this->parseFinalResponse(data);
Shock Jiang698e6ed2014-11-09 11:22:24 -0800148 if (m_onSucceed != nullptr)
Yumin Xia2c509c22017-02-09 14:37:36 -0800149 m_onSucceed(data, re);
Shock Jiang698e6ed2014-11-09 11:22:24 -0800150 else
151 NDNS_LOG_TRACE("succeed callback is nullptr");
152 }
153 else if (m_step == QUERY_STEP_ABORT)
154 this->abort();
155}
156
157bool
158IterativeQueryController::hasEnded()
159{
160 return (m_step != QUERY_STEP_QUERY_NS && m_step != QUERY_STEP_QUERY_RR);
161}
162
163void
164IterativeQueryController::start()
165{
Shock Jiang5d5928c2014-12-03 13:41:22 -0800166 if (m_dstLabel.size() == m_nFinishedComps)
167 m_step = QUERY_STEP_QUERY_RR;
168
Shock Jiang698e6ed2014-11-09 11:22:24 -0800169 Interest interest = this->makeLatestInterest();
170 express(interest);
171}
172
173
174void
175IterativeQueryController::express(const Interest& interest)
176{
Yumin Xiad8b75fc2017-04-05 15:00:21 -0700177 if (m_nsCache != nullptr) {
178 shared_ptr<const Data> cachedData = m_nsCache->find(interest);
179 if (cachedData != nullptr) {
180 NDNS_LOG_DEBUG("[* cached *] NS record has been cached before: "
181 << interest.getName());
182 onData(interest, *cachedData);
183 return ;
184 }
185 }
186
Shock Jiang698e6ed2014-11-09 11:22:24 -0800187 NDNS_LOG_DEBUG("[* <- *] send a Query: " << interest.getName());
188 m_face.expressInterest(interest,
189 bind(&IterativeQueryController::onData, this, _1, _2),
Alexander Afanasyevf4193ea2017-06-12 15:35:13 -0700190 bind(&IterativeQueryController::onTimeout, this, _1), // nack
Shock Jiang698e6ed2014-11-09 11:22:24 -0800191 bind(&IterativeQueryController::onTimeout, this, _1)
192 );
193}
194
195
196const Response
197IterativeQueryController::parseFinalResponse(const Data& data)
198{
199 Response re;
200 Name zone = m_dstLabel.getPrefix(m_nFinishedComps);
Yumin Xia6343c5b2016-10-20 15:45:50 -0700201 re.fromData(zone, data);
Shock Jiang698e6ed2014-11-09 11:22:24 -0800202 return re;
203}
204
205const Interest
206IterativeQueryController::makeLatestInterest()
207{
208 // NDNS_LOG_TRACE("get latest Interest");
209 Query query;
210 //const Name& dstLabel = m_query.getRrLabel();
211
212 query.setZone(m_dstLabel.getPrefix(m_nFinishedComps));
213 query.setInterestLifetime(m_interestLifetime);
Yumin Xia4e561892016-10-21 10:48:01 -0700214
215 // addLink
216 if (m_lastLink.hasWire()) {
Yumin Xia2c509c22017-02-09 14:37:36 -0800217 query.setDelegationListFromLink(Link(m_lastLink));
Yumin Xia4e561892016-10-21 10:48:01 -0700218 }
219
Shock Jiang698e6ed2014-11-09 11:22:24 -0800220 switch (m_step) {
221 case QUERY_STEP_QUERY_NS:
222 query.setQueryType(label::NDNS_ITERATIVE_QUERY);
223 query.setRrLabel(m_dstLabel.getSubName(m_nFinishedComps, m_nTryComps));
224 query.setRrType(label::NS_RR_TYPE);
225 break;
226 case QUERY_STEP_QUERY_RR:
Yumin Xia918343d2017-03-17 19:04:55 -0700227 query.setQueryType(label::NDNS_ITERATIVE_QUERY);
Shock Jiang698e6ed2014-11-09 11:22:24 -0800228 query.setRrLabel(m_dstLabel.getSubName(m_nFinishedComps));
229 query.setRrType(m_rrType);
230 break;
231 default:
232 std::ostringstream oss;
233 oss << *this;
234 NDNS_LOG_WARN("unexpected state: " << oss.str());
Yumin Xia2c509c22017-02-09 14:37:36 -0800235 BOOST_THROW_EXCEPTION(std::runtime_error("call makeLatestInterest() unexpected: "
236 + oss.str()));
Shock Jiang698e6ed2014-11-09 11:22:24 -0800237 }
238
Shock Jiang698e6ed2014-11-09 11:22:24 -0800239 Interest interest = query.toInterest();
240 return interest;
241}
242
243std::ostream&
244operator<<(std::ostream& os, const IterativeQueryController::QueryStep step)
245{
246 switch (step) {
247 case IterativeQueryController::QUERY_STEP_QUERY_NS:
248 os << "QueryNS";
249 break;
250 case IterativeQueryController::QUERY_STEP_QUERY_RR:
251 os << "QueryRR";
252 break;
253 case IterativeQueryController::QUERY_STEP_ANSWER_STUB:
254 os << "AnswerStub";
255 break;
256 case IterativeQueryController::QUERY_STEP_ABORT:
257 os << "Abort";
258 break;
259 default:
260 os << "UNKNOW";
261 break;
262 }
263 return os;
264}
265
266std::ostream&
267operator<<(std::ostream& os, const IterativeQueryController& ctr)
268{
269 os << "InterativeQueryController: dstLabel=" << ctr.getDstLabel()
270 << " rrType=" << ctr.getRrType()
271 << " currentStep=" << ctr.getStep()
272 << " nFinishedComps=" << ctr.getNFinishedComps()
273 << " nTryComp=" << ctr.getNTryComps()
274 ;
275
276 return os;
277}
278
279} // namespace ndns
280} // namespace ndn