blob: 936ca381cc3ae0c31459d7a954be8317c0eca416 [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/*
Yumin Xia55a7cc42017-05-14 18:43:34 -07003 * Copyright (c) 2014-2018, 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#ifndef NDNS_CLIENTS_ITERATIVE_QUERY_CONTROLLER_HPP
21#define NDNS_CLIENTS_ITERATIVE_QUERY_CONTROLLER_HPP
22
23#include "ndns-enum.hpp"
Shock Jiang698e6ed2014-11-09 11:22:24 -080024#include "common.hpp"
Junxiao Shi15e51bc2018-12-12 13:48:56 -070025#include "query.hpp"
26#include "query-controller.hpp"
27#include "response.hpp"
Yumin Xia99c821a2017-04-07 11:01:08 -070028#include "validator/validator.hpp"
Shock Jiang698e6ed2014-11-09 11:22:24 -080029
Shock Jiang698e6ed2014-11-09 11:22:24 -080030#include <ndn-cxx/data.hpp>
31#include <ndn-cxx/interest.hpp>
Yumin Xia4e561892016-10-21 10:48:01 -070032#include <ndn-cxx/link.hpp>
Junxiao Shi15e51bc2018-12-12 13:48:56 -070033#include <ndn-cxx/name.hpp>
Yumin Xiad8b75fc2017-04-05 15:00:21 -070034#include <ndn-cxx/ims/in-memory-storage.hpp>
Shock Jiang698e6ed2014-11-09 11:22:24 -080035
36namespace ndn {
37namespace ndns {
38
39/**
40 * @brief controller which iteratively query a target label
41 */
42class IterativeQueryController : public QueryController
43{
44public:
45 /**
46 * @brief indicates a step in an iterative query process.
47 * Iterative Query contains multiple steps which are listed here
48 */
49 enum QueryStep {
50 QUERY_STEP_QUERY_NS = 1, ///< to query the naming server, before querying NS & waiting for Data
51 QUERY_STEP_QUERY_RR, ///< to query RR, before querying RR & waiting for it Data
52 QUERY_STEP_ANSWER_STUB, ///< to answer to stub resolver, after getting final Response,
53 ///< or NACK or timeout
54 QUERY_STEP_ABORT, ///< to abort the resolver process, if unexpected behavior happens
55 QUERY_STEP_UNKNOWN = 255
56 };
57
58public:
59 explicit
60 IterativeQueryController(const Name& dstLabel, const name::Component& rrType,
61 const time::milliseconds& interestLifetime,
62 const QuerySucceedCallback& onSucceed, const QueryFailCallback& onFail,
Yumin Xiad8b75fc2017-04-05 15:00:21 -070063 Face& face, security::v2::Validator* validator = nullptr,
64 ndn::InMemoryStorage* cache = nullptr);
Shock Jiang698e6ed2014-11-09 11:22:24 -080065
66 virtual void
67 start();
68
69 /**
70 * @brief return false if the current status is not QUEYR_STEP_QUERY_NS
71 * or QUERY_STEP_QUERY_RR
72 */
73 virtual bool
74 hasEnded();
75
76NDNS_PUBLIC_WITH_TESTS_ELSE_PRIVATE:
77 void
78 onData(const ndn::Interest& interest, const Data& data);
79
Yumin Xia55a7cc42017-05-14 18:43:34 -070080 /**
81 * @brief called when any data are validated.
82 * It will unwrap the NACK record,
83 * so onSucceed callback will be called with only inner DoE
84 */
Shock Jiang5d5928c2014-12-03 13:41:22 -080085 void
Yumin Xia2c509c22017-02-09 14:37:36 -080086 onDataValidated(const Data& data, NdnsContentType contentType);
Shock Jiang5d5928c2014-12-03 13:41:22 -080087
Shock Jiang698e6ed2014-11-09 11:22:24 -080088 /**
89 * @brief change the Controller state according to timeout. For current,
90 * abort the query when timeout
91 */
92 void
93 onTimeout(const Interest& interest);
94
95 void
96 abort();
97
98 /**
99 * @brief get the Interest according to current Controller state.
Yumin Xia2c509c22017-02-09 14:37:36 -0800100 * Only be valid on State QueryNS and QueryRR, or throw exception
Shock Jiang698e6ed2014-11-09 11:22:24 -0800101 */
102 const Interest
103 makeLatestInterest();
104
105 const Response
106 parseFinalResponse(const Data& data);
107
108 void
109 express(const Interest& interest);
110
Shock Jiang5d5928c2014-12-03 13:41:22 -0800111public:
Shock Jiang698e6ed2014-11-09 11:22:24 -0800112 void
Shock Jiang5d5928c2014-12-03 13:41:22 -0800113 setStartComponentIndex(size_t finished)
Shock Jiang698e6ed2014-11-09 11:22:24 -0800114 {
115 m_nFinishedComps = finished;
116 }
117
Shock Jiang698e6ed2014-11-09 11:22:24 -0800118 QueryStep
119 getStep() const
120 {
121 return m_step;
122 }
123
124 size_t
125 getNFinishedComps() const
126 {
127 return m_nFinishedComps;
128 }
129
130 size_t
131 getNTryComps() const
132 {
133 return m_nTryComps;
134 }
135
Yumin Xia55a7cc42017-05-14 18:43:34 -0700136private:
137 bool
138 isAbsentByDoe(const Data& data) const;
139
Shock Jiang698e6ed2014-11-09 11:22:24 -0800140protected:
Yumin Xia2c509c22017-02-09 14:37:36 -0800141 security::v2::Validator* m_validator;
Shock Jiang698e6ed2014-11-09 11:22:24 -0800142 /**
143 * @brief current query step
144 */
145 QueryStep m_step;
146
147 /*
148 * the number of label that has been resolved successfully
149 * This also define the next AuthZone prefix
150 * AuthZone = m_query.getRrLabel().getPrefix(m_nFinishedComps)
151 */
152 size_t m_nFinishedComps;
153
154 /*
155 * used when query the KSK (key signing key), e.g., /net/ndnsim/ksk-1
156 */
157 size_t m_nTryComps;
Yumin Xia4e561892016-10-21 10:48:01 -0700158
159private:
160 Block m_lastLink;
Yumin Xia55a7cc42017-05-14 18:43:34 -0700161 Data m_doe;
162 Name m_lastLabelType;
Yumin Xiad8b75fc2017-04-05 15:00:21 -0700163 ndn::InMemoryStorage* m_nsCache;
Shock Jiang698e6ed2014-11-09 11:22:24 -0800164};
165
166std::ostream&
167operator<<(std::ostream& os, const IterativeQueryController& iq);
168
169std::ostream&
170operator<<(std::ostream& os, const IterativeQueryController::QueryStep step);
171
Yumin Xiafa2bce72017-04-09 16:20:25 -0700172// Used if you want the controller's lifetime equals to other object inherited
173// from TagHost. For example, in the CertificateFetcher, the queryController's
174// lifetime is equal to ValidationState.
175using IterativeQueryTag = SimpleTag<shared_ptr<IterativeQueryController>, 1086>;
176
Shock Jiang698e6ed2014-11-09 11:22:24 -0800177} // namespace ndns
178} // namespace ndn
179
Yumin Xia2c509c22017-02-09 14:37:36 -0800180#endif // NDNS_CLIENTS_ITERATIVE_QUERY_CONTROLLER_HPP