Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 2 | /* |
Alexander Afanasyev | 60514ec | 2020-06-03 14:18:53 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2020, Regents of the University of California. |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 4 | * |
| 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" |
Junxiao Shi | 15e51bc | 2018-12-12 13:48:56 -0700 | [diff] [blame] | 24 | #include "query-controller.hpp" |
| 25 | #include "response.hpp" |
Yumin Xia | 99c821a | 2017-04-07 11:01:08 -0700 | [diff] [blame] | 26 | #include "validator/validator.hpp" |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 27 | |
Yumin Xia | d8b75fc | 2017-04-05 15:00:21 -0700 | [diff] [blame] | 28 | #include <ndn-cxx/ims/in-memory-storage.hpp> |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame^] | 29 | #include <ndn-cxx/link.hpp> |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 30 | |
| 31 | namespace ndn { |
| 32 | namespace ndns { |
| 33 | |
| 34 | /** |
| 35 | * @brief controller which iteratively query a target label |
| 36 | */ |
| 37 | class IterativeQueryController : public QueryController |
| 38 | { |
| 39 | public: |
| 40 | /** |
| 41 | * @brief indicates a step in an iterative query process. |
| 42 | * Iterative Query contains multiple steps which are listed here |
| 43 | */ |
| 44 | enum QueryStep { |
| 45 | QUERY_STEP_QUERY_NS = 1, ///< to query the naming server, before querying NS & waiting for Data |
| 46 | QUERY_STEP_QUERY_RR, ///< to query RR, before querying RR & waiting for it Data |
| 47 | QUERY_STEP_ANSWER_STUB, ///< to answer to stub resolver, after getting final Response, |
| 48 | ///< or NACK or timeout |
| 49 | QUERY_STEP_ABORT, ///< to abort the resolver process, if unexpected behavior happens |
| 50 | QUERY_STEP_UNKNOWN = 255 |
| 51 | }; |
| 52 | |
| 53 | public: |
| 54 | explicit |
| 55 | IterativeQueryController(const Name& dstLabel, const name::Component& rrType, |
| 56 | const time::milliseconds& interestLifetime, |
| 57 | const QuerySucceedCallback& onSucceed, const QueryFailCallback& onFail, |
Alexander Afanasyev | 60514ec | 2020-06-03 14:18:53 -0400 | [diff] [blame] | 58 | Face& face, security::Validator* validator = nullptr, |
Yumin Xia | d8b75fc | 2017-04-05 15:00:21 -0700 | [diff] [blame] | 59 | ndn::InMemoryStorage* cache = nullptr); |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 60 | |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame^] | 61 | void |
| 62 | start() override; |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 63 | |
| 64 | /** |
| 65 | * @brief return false if the current status is not QUEYR_STEP_QUERY_NS |
| 66 | * or QUERY_STEP_QUERY_RR |
| 67 | */ |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame^] | 68 | bool |
| 69 | hasEnded() override; |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 70 | |
| 71 | NDNS_PUBLIC_WITH_TESTS_ELSE_PRIVATE: |
| 72 | void |
| 73 | onData(const ndn::Interest& interest, const Data& data); |
| 74 | |
Yumin Xia | 55a7cc4 | 2017-05-14 18:43:34 -0700 | [diff] [blame] | 75 | /** |
| 76 | * @brief called when any data are validated. |
| 77 | * It will unwrap the NACK record, |
| 78 | * so onSucceed callback will be called with only inner DoE |
| 79 | */ |
Shock Jiang | 5d5928c | 2014-12-03 13:41:22 -0800 | [diff] [blame] | 80 | void |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 81 | onDataValidated(const Data& data, NdnsContentType contentType); |
Shock Jiang | 5d5928c | 2014-12-03 13:41:22 -0800 | [diff] [blame] | 82 | |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 83 | /** |
| 84 | * @brief change the Controller state according to timeout. For current, |
| 85 | * abort the query when timeout |
| 86 | */ |
| 87 | void |
| 88 | onTimeout(const Interest& interest); |
| 89 | |
| 90 | void |
| 91 | abort(); |
| 92 | |
| 93 | /** |
| 94 | * @brief get the Interest according to current Controller state. |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 95 | * Only be valid on State QueryNS and QueryRR, or throw exception |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 96 | */ |
| 97 | const Interest |
| 98 | makeLatestInterest(); |
| 99 | |
| 100 | const Response |
| 101 | parseFinalResponse(const Data& data); |
| 102 | |
| 103 | void |
| 104 | express(const Interest& interest); |
| 105 | |
Shock Jiang | 5d5928c | 2014-12-03 13:41:22 -0800 | [diff] [blame] | 106 | public: |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 107 | void |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame^] | 108 | setStartComponentIndex(size_t finished) override |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 109 | { |
| 110 | m_nFinishedComps = finished; |
| 111 | } |
| 112 | |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 113 | QueryStep |
| 114 | getStep() const |
| 115 | { |
| 116 | return m_step; |
| 117 | } |
| 118 | |
| 119 | size_t |
| 120 | getNFinishedComps() const |
| 121 | { |
| 122 | return m_nFinishedComps; |
| 123 | } |
| 124 | |
| 125 | size_t |
| 126 | getNTryComps() const |
| 127 | { |
| 128 | return m_nTryComps; |
| 129 | } |
| 130 | |
Yumin Xia | 55a7cc4 | 2017-05-14 18:43:34 -0700 | [diff] [blame] | 131 | private: |
| 132 | bool |
| 133 | isAbsentByDoe(const Data& data) const; |
| 134 | |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 135 | protected: |
Alexander Afanasyev | 60514ec | 2020-06-03 14:18:53 -0400 | [diff] [blame] | 136 | security::Validator* m_validator; |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 137 | /** |
| 138 | * @brief current query step |
| 139 | */ |
| 140 | QueryStep m_step; |
| 141 | |
| 142 | /* |
| 143 | * the number of label that has been resolved successfully |
| 144 | * This also define the next AuthZone prefix |
| 145 | * AuthZone = m_query.getRrLabel().getPrefix(m_nFinishedComps) |
| 146 | */ |
| 147 | size_t m_nFinishedComps; |
| 148 | |
| 149 | /* |
| 150 | * used when query the KSK (key signing key), e.g., /net/ndnsim/ksk-1 |
| 151 | */ |
| 152 | size_t m_nTryComps; |
Yumin Xia | 4e56189 | 2016-10-21 10:48:01 -0700 | [diff] [blame] | 153 | |
| 154 | private: |
| 155 | Block m_lastLink; |
Yumin Xia | 55a7cc4 | 2017-05-14 18:43:34 -0700 | [diff] [blame] | 156 | Data m_doe; |
| 157 | Name m_lastLabelType; |
Yumin Xia | d8b75fc | 2017-04-05 15:00:21 -0700 | [diff] [blame] | 158 | ndn::InMemoryStorage* m_nsCache; |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 159 | }; |
| 160 | |
| 161 | std::ostream& |
| 162 | operator<<(std::ostream& os, const IterativeQueryController& iq); |
| 163 | |
| 164 | std::ostream& |
| 165 | operator<<(std::ostream& os, const IterativeQueryController::QueryStep step); |
| 166 | |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 167 | // Used if you want the controller's lifetime equals to other object inherited |
| 168 | // from TagHost. For example, in the CertificateFetcher, the queryController's |
| 169 | // lifetime is equal to ValidationState. |
| 170 | using IterativeQueryTag = SimpleTag<shared_ptr<IterativeQueryController>, 1086>; |
| 171 | |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 172 | } // namespace ndns |
| 173 | } // namespace ndn |
| 174 | |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 175 | #endif // NDNS_CLIENTS_ITERATIVE_QUERY_CONTROLLER_HPP |