blob: a04424e4efadb22bd97316532acb35121a3d025b [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"
24#include "query.hpp"
25#include "response.hpp"
26#include "query-controller.hpp"
27#include "config.hpp"
28#include "common.hpp"
Yumin Xia99c821a2017-04-07 11:01:08 -070029#include "validator/validator.hpp"
Shock Jiang698e6ed2014-11-09 11:22:24 -080030
31#include <ndn-cxx/common.hpp>
32#include <ndn-cxx/data.hpp>
33#include <ndn-cxx/interest.hpp>
34#include <ndn-cxx/name.hpp>
Yumin Xia4e561892016-10-21 10:48:01 -070035#include <ndn-cxx/link.hpp>
Yumin Xiad8b75fc2017-04-05 15:00:21 -070036#include <ndn-cxx/ims/in-memory-storage.hpp>
Shock Jiang698e6ed2014-11-09 11:22:24 -080037
38namespace ndn {
39namespace ndns {
40
41/**
42 * @brief controller which iteratively query a target label
43 */
44class IterativeQueryController : public QueryController
45{
46public:
47 /**
48 * @brief indicates a step in an iterative query process.
49 * Iterative Query contains multiple steps which are listed here
50 */
51 enum QueryStep {
52 QUERY_STEP_QUERY_NS = 1, ///< to query the naming server, before querying NS & waiting for Data
53 QUERY_STEP_QUERY_RR, ///< to query RR, before querying RR & waiting for it Data
54 QUERY_STEP_ANSWER_STUB, ///< to answer to stub resolver, after getting final Response,
55 ///< or NACK or timeout
56 QUERY_STEP_ABORT, ///< to abort the resolver process, if unexpected behavior happens
57 QUERY_STEP_UNKNOWN = 255
58 };
59
60public:
61 explicit
62 IterativeQueryController(const Name& dstLabel, const name::Component& rrType,
63 const time::milliseconds& interestLifetime,
64 const QuerySucceedCallback& onSucceed, const QueryFailCallback& onFail,
Yumin Xiad8b75fc2017-04-05 15:00:21 -070065 Face& face, security::v2::Validator* validator = nullptr,
66 ndn::InMemoryStorage* cache = nullptr);
Shock Jiang698e6ed2014-11-09 11:22:24 -080067
68 virtual void
69 start();
70
71 /**
72 * @brief return false if the current status is not QUEYR_STEP_QUERY_NS
73 * or QUERY_STEP_QUERY_RR
74 */
75 virtual bool
76 hasEnded();
77
78NDNS_PUBLIC_WITH_TESTS_ELSE_PRIVATE:
79 void
80 onData(const ndn::Interest& interest, const Data& data);
81
Yumin Xia55a7cc42017-05-14 18:43:34 -070082 /**
83 * @brief called when any data are validated.
84 * It will unwrap the NACK record,
85 * so onSucceed callback will be called with only inner DoE
86 */
Shock Jiang5d5928c2014-12-03 13:41:22 -080087 void
Yumin Xia2c509c22017-02-09 14:37:36 -080088 onDataValidated(const Data& data, NdnsContentType contentType);
Shock Jiang5d5928c2014-12-03 13:41:22 -080089
Shock Jiang698e6ed2014-11-09 11:22:24 -080090 /**
91 * @brief change the Controller state according to timeout. For current,
92 * abort the query when timeout
93 */
94 void
95 onTimeout(const Interest& interest);
96
97 void
98 abort();
99
100 /**
101 * @brief get the Interest according to current Controller state.
Yumin Xia2c509c22017-02-09 14:37:36 -0800102 * Only be valid on State QueryNS and QueryRR, or throw exception
Shock Jiang698e6ed2014-11-09 11:22:24 -0800103 */
104 const Interest
105 makeLatestInterest();
106
107 const Response
108 parseFinalResponse(const Data& data);
109
110 void
111 express(const Interest& interest);
112
Shock Jiang5d5928c2014-12-03 13:41:22 -0800113public:
Shock Jiang698e6ed2014-11-09 11:22:24 -0800114 void
Shock Jiang5d5928c2014-12-03 13:41:22 -0800115 setStartComponentIndex(size_t finished)
Shock Jiang698e6ed2014-11-09 11:22:24 -0800116 {
117 m_nFinishedComps = finished;
118 }
119
Shock Jiang698e6ed2014-11-09 11:22:24 -0800120 QueryStep
121 getStep() const
122 {
123 return m_step;
124 }
125
126 size_t
127 getNFinishedComps() const
128 {
129 return m_nFinishedComps;
130 }
131
132 size_t
133 getNTryComps() const
134 {
135 return m_nTryComps;
136 }
137
Yumin Xia55a7cc42017-05-14 18:43:34 -0700138private:
139 bool
140 isAbsentByDoe(const Data& data) const;
141
Shock Jiang698e6ed2014-11-09 11:22:24 -0800142protected:
Yumin Xia2c509c22017-02-09 14:37:36 -0800143 security::v2::Validator* m_validator;
Shock Jiang698e6ed2014-11-09 11:22:24 -0800144 /**
145 * @brief current query step
146 */
147 QueryStep m_step;
148
149 /*
150 * the number of label that has been resolved successfully
151 * This also define the next AuthZone prefix
152 * AuthZone = m_query.getRrLabel().getPrefix(m_nFinishedComps)
153 */
154 size_t m_nFinishedComps;
155
156 /*
157 * used when query the KSK (key signing key), e.g., /net/ndnsim/ksk-1
158 */
159 size_t m_nTryComps;
Yumin Xia4e561892016-10-21 10:48:01 -0700160
161private:
162 Block m_lastLink;
Yumin Xia55a7cc42017-05-14 18:43:34 -0700163 Data m_doe;
164 Name m_lastLabelType;
Yumin Xiad8b75fc2017-04-05 15:00:21 -0700165 ndn::InMemoryStorage* m_nsCache;
Shock Jiang698e6ed2014-11-09 11:22:24 -0800166};
167
168std::ostream&
169operator<<(std::ostream& os, const IterativeQueryController& iq);
170
171std::ostream&
172operator<<(std::ostream& os, const IterativeQueryController::QueryStep step);
173
Yumin Xiafa2bce72017-04-09 16:20:25 -0700174// Used if you want the controller's lifetime equals to other object inherited
175// from TagHost. For example, in the CertificateFetcher, the queryController's
176// lifetime is equal to ValidationState.
177using IterativeQueryTag = SimpleTag<shared_ptr<IterativeQueryController>, 1086>;
178
Shock Jiang698e6ed2014-11-09 11:22:24 -0800179} // namespace ndns
180} // namespace ndn
181
Yumin Xia2c509c22017-02-09 14:37:36 -0800182#endif // NDNS_CLIENTS_ITERATIVE_QUERY_CONTROLLER_HPP