blob: 54bbb9af68270756fa02f1676c7a69d823e60bcf [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"
Yumin Xia2c509c22017-02-09 14:37:36 -080021#include "validator.hpp"
Shock Jiang698e6ed2014-11-09 11:22:24 -080022#include "logger.hpp"
23#include <iostream>
24
25namespace ndn {
26namespace ndns {
27NDNS_LOG_INIT("IterQueryCtr")
28
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,
Yumin Xia2c509c22017-02-09 14:37:36 -080035 security::v2::Validator* validator)
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)
41{
Shock Jiang698e6ed2014-11-09 11:22:24 -080042}
43
44void
45IterativeQueryController::onTimeout(const Interest& interest)
46{
47 NDNS_LOG_INFO("[* !! *] timeout happens: " << interest.getName());
48 NDNS_LOG_TRACE(*this);
49 this->abort();
50}
51
52void
53IterativeQueryController::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
63void
64IterativeQueryController::onData(const ndn::Interest& interest, const Data& data)
65{
Yumin Xiaa484ba72016-11-10 20:40:12 -080066 NdnsContentType contentType = NdnsContentType(data.getContentType());
Shock Jiang698e6ed2014-11-09 11:22:24 -080067
Yumin Xiaa484ba72016-11-10 20:40:12 -080068 NDNS_LOG_TRACE("[* -> *] get a " << contentType
Shock Jiang698e6ed2014-11-09 11:22:24 -080069 << " Response: " << data.getName());
Shock Jiang5d5928c2014-12-03 13:41:22 -080070 if (m_validator == nullptr) {
Yumin Xia2c509c22017-02-09 14:37:36 -080071 this->onDataValidated(data, contentType);
Shock Jiang5d5928c2014-12-03 13:41:22 -080072 }
73 else {
74 m_validator->validate(data,
Yumin Xiaa484ba72016-11-10 20:40:12 -080075 bind(&IterativeQueryController::onDataValidated, this, _1, contentType),
Yumin Xia2c509c22017-02-09 14:37:36 -080076 [this] (const Data& data, const security::v2::ValidationError& err) {
77 NDNS_LOG_WARN("data: " << data.getName() << " fails verification");
Shock Jiang5d5928c2014-12-03 13:41:22 -080078 this->abort();
79 }
80 );
81 }
82}
83void
Yumin Xia2c509c22017-02-09 14:37:36 -080084IterativeQueryController::onDataValidated(const Data& data, NdnsContentType contentType)
Shock Jiang5d5928c2014-12-03 13:41:22 -080085{
Shock Jiang698e6ed2014-11-09 11:22:24 -080086 switch (m_step) {
87 case QUERY_STEP_QUERY_NS:
Yumin Xiaa484ba72016-11-10 20:40:12 -080088 if (contentType == NDNS_NACK) {
Shock Jiang06cd2142014-11-23 17:36:02 -080089 m_step = QUERY_STEP_QUERY_RR;
Shock Jiang698e6ed2014-11-09 11:22:24 -080090 }
Yumin Xiaa484ba72016-11-10 20:40:12 -080091 else if (contentType == NDNS_LINK) {
Yumin Xia2c509c22017-02-09 14:37:36 -080092 Link link(data.wireEncode());
93 if (link.getDelegationList().empty()) {
Yumin Xiaa484ba72016-11-10 20:40:12 -080094 m_lastLink = Block();
Yumin Xia2c509c22017-02-09 14:37:36 -080095 }
96 else {
97 m_lastLink = data.wireEncode();
Yumin Xia4e561892016-10-21 10:48:01 -070098 }
Yumin Xiaa484ba72016-11-10 20:40:12 -080099
100 // for NS query, if already received, just return, instead of more queries until NACK
Shock Jiang698e6ed2014-11-09 11:22:24 -0800101 if (m_nFinishedComps + m_nTryComps == m_dstLabel.size() && m_rrType == label::NS_RR_TYPE) {
102 // NS_RR_TYPE is different, since its record is stored at higher level
103 m_step = QUERY_STEP_ANSWER_STUB;
104 }
105 else {
106 m_nFinishedComps += m_nTryComps;
107 m_nTryComps = 1;
108 }
109 }
Yumin Xiaa484ba72016-11-10 20:40:12 -0800110 else if (contentType == NDNS_AUTH) {
Shock Jiang698e6ed2014-11-09 11:22:24 -0800111 m_nTryComps += 1;
112 }
Yumin Xiaa484ba72016-11-10 20:40:12 -0800113 else if (contentType == NDNS_BLOB) {
Shock Jiang698e6ed2014-11-09 11:22:24 -0800114 std::ostringstream oss;
115 oss << *this;
Yumin Xiaa484ba72016-11-10 20:40:12 -0800116 NDNS_LOG_WARN("get unexpected Response: NDNS_BLOB for QUERY_NS: " << oss.str());
Yumin Xia2c509c22017-02-09 14:37:36 -0800117 }
118 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 }
123 //
124 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{
177 NDNS_LOG_DEBUG("[* <- *] send a Query: " << interest.getName());
178 m_face.expressInterest(interest,
179 bind(&IterativeQueryController::onData, this, _1, _2),
Alexander Afanasyevf4193ea2017-06-12 15:35:13 -0700180 bind(&IterativeQueryController::onTimeout, this, _1), // nack
Shock Jiang698e6ed2014-11-09 11:22:24 -0800181 bind(&IterativeQueryController::onTimeout, this, _1)
182 );
183}
184
185
186const Response
187IterativeQueryController::parseFinalResponse(const Data& data)
188{
189 Response re;
190 Name zone = m_dstLabel.getPrefix(m_nFinishedComps);
Yumin Xia6343c5b2016-10-20 15:45:50 -0700191 re.fromData(zone, data);
Shock Jiang698e6ed2014-11-09 11:22:24 -0800192 return re;
193}
194
195const Interest
196IterativeQueryController::makeLatestInterest()
197{
198 // NDNS_LOG_TRACE("get latest Interest");
199 Query query;
200 //const Name& dstLabel = m_query.getRrLabel();
201
202 query.setZone(m_dstLabel.getPrefix(m_nFinishedComps));
203 query.setInterestLifetime(m_interestLifetime);
Yumin Xia4e561892016-10-21 10:48:01 -0700204
205 // addLink
206 if (m_lastLink.hasWire()) {
Yumin Xia2c509c22017-02-09 14:37:36 -0800207 query.setDelegationListFromLink(Link(m_lastLink));
Yumin Xia4e561892016-10-21 10:48:01 -0700208 }
209
Shock Jiang698e6ed2014-11-09 11:22:24 -0800210 switch (m_step) {
211 case QUERY_STEP_QUERY_NS:
212 query.setQueryType(label::NDNS_ITERATIVE_QUERY);
213 query.setRrLabel(m_dstLabel.getSubName(m_nFinishedComps, m_nTryComps));
214 query.setRrType(label::NS_RR_TYPE);
215 break;
216 case QUERY_STEP_QUERY_RR:
217 if (m_rrType == label::CERT_RR_TYPE) {
218 // this only works for dsk, and ksk needs different mechanism
219 query.setQueryType(label::NDNS_CERT_QUERY);
220 }
221 else {
222 query.setQueryType(label::NDNS_ITERATIVE_QUERY);
223 }
224 query.setRrLabel(m_dstLabel.getSubName(m_nFinishedComps));
225 query.setRrType(m_rrType);
226 break;
227 default:
228 std::ostringstream oss;
229 oss << *this;
230 NDNS_LOG_WARN("unexpected state: " << oss.str());
Yumin Xia2c509c22017-02-09 14:37:36 -0800231 BOOST_THROW_EXCEPTION(std::runtime_error("call makeLatestInterest() unexpected: "
232 + oss.str()));
Shock Jiang698e6ed2014-11-09 11:22:24 -0800233 }
234
Shock Jiang698e6ed2014-11-09 11:22:24 -0800235 Interest interest = query.toInterest();
236 return interest;
237}
238
239std::ostream&
240operator<<(std::ostream& os, const IterativeQueryController::QueryStep step)
241{
242 switch (step) {
243 case IterativeQueryController::QUERY_STEP_QUERY_NS:
244 os << "QueryNS";
245 break;
246 case IterativeQueryController::QUERY_STEP_QUERY_RR:
247 os << "QueryRR";
248 break;
249 case IterativeQueryController::QUERY_STEP_ANSWER_STUB:
250 os << "AnswerStub";
251 break;
252 case IterativeQueryController::QUERY_STEP_ABORT:
253 os << "Abort";
254 break;
255 default:
256 os << "UNKNOW";
257 break;
258 }
259 return os;
260}
261
262std::ostream&
263operator<<(std::ostream& os, const IterativeQueryController& ctr)
264{
265 os << "InterativeQueryController: dstLabel=" << ctr.getDstLabel()
266 << " rrType=" << ctr.getRrType()
267 << " currentStep=" << ctr.getStep()
268 << " nFinishedComps=" << ctr.getNFinishedComps()
269 << " nTryComp=" << ctr.getNTryComps()
270 ;
271
272 return os;
273}
274
275} // namespace ndns
276} // namespace ndn