Zhiyi Zhang | 23564c8 | 2017-03-01 10:22:22 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2017, Regents of the University of California. |
| 4 | * |
| 5 | * This file is part of ndncert, a certificate management system based on NDN. |
| 6 | * |
| 7 | * ndncert is free software: you can redistribute it and/or modify it under the terms |
| 8 | * of the GNU General Public License as published by the Free Software Foundation, either |
| 9 | * version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndncert is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License along with |
| 16 | * ndncert, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 17 | * |
| 18 | * See AUTHORS.md for complete list of ndncert authors and contributors. |
| 19 | */ |
| 20 | |
| 21 | #ifndef NDNCERT_CLIENT_MODULE_HPP |
| 22 | #define NDNCERT_CLIENT_MODULE_HPP |
| 23 | |
| 24 | #include "client-config.hpp" |
| 25 | #include "certificate-request.hpp" |
| 26 | |
| 27 | namespace ndn { |
| 28 | namespace ndncert { |
| 29 | |
| 30 | class RequestState |
| 31 | { |
| 32 | public: |
| 33 | ClientCaItem m_ca; |
| 34 | security::Key m_key; |
| 35 | |
| 36 | std::string m_requestId; |
| 37 | std::string m_status; |
| 38 | std::string m_challengeType; |
| 39 | std::list<std::string> m_challengeList; |
| 40 | |
| 41 | bool m_isInstalled = false; |
| 42 | }; |
| 43 | |
| 44 | // TODO |
| 45 | // For each CA item in Client.Conf, create a validator instance and initialize it with CA's cert |
| 46 | // The validator instance should be in ClientCaItem |
| 47 | |
| 48 | class ClientModule : noncopyable |
| 49 | { |
| 50 | public: |
| 51 | /** |
| 52 | * @brief Error that can be thrown from ClientModule |
| 53 | */ |
| 54 | class Error : public std::runtime_error |
| 55 | { |
| 56 | public: |
| 57 | using std::runtime_error::runtime_error; |
| 58 | }; |
| 59 | |
| 60 | using RequestCallback = function<void (const shared_ptr<RequestState>&)>; |
| 61 | using ErrorCallback = function<void (const std::string&)>; |
| 62 | |
| 63 | public: |
| 64 | explicit |
| 65 | ClientModule(Face& face, security::v2::KeyChain& keyChain, size_t retryTimes = 2); |
| 66 | |
| 67 | ClientConfig& |
| 68 | getClientConf() |
| 69 | { |
| 70 | return m_config; |
| 71 | } |
| 72 | |
| 73 | void |
| 74 | sendProbe(const ClientCaItem& ca, const std::string& probeInfo, |
| 75 | const RequestCallback& requestCallback, const ErrorCallback& errorCallback); |
| 76 | |
| 77 | void |
| 78 | handleProbeResponse(const Interest& request, const Data& reply, const ClientCaItem& ca, |
| 79 | const RequestCallback& requestCallback, const ErrorCallback& errorCallback); |
| 80 | |
| 81 | void |
| 82 | sendNew(const ClientCaItem& ca, const Name& identityName, |
| 83 | const RequestCallback& requestCallback, const ErrorCallback& errorCallback); |
| 84 | |
| 85 | void |
| 86 | handleNewResponse(const Interest& request, const Data& reply, |
| 87 | const shared_ptr<RequestState>& state, |
| 88 | const RequestCallback& requestCallback, const ErrorCallback& errorCallback); |
| 89 | |
| 90 | void |
| 91 | sendSelect(const shared_ptr<RequestState>& state, const std::string& challengeType, |
| 92 | const JsonSection& selectParams, |
| 93 | const RequestCallback& requestCallback, const ErrorCallback& errorCallback); |
| 94 | |
| 95 | void |
| 96 | handleSelectResponse(const Interest& request, const Data& reply, |
| 97 | const shared_ptr<RequestState>& state, |
| 98 | const RequestCallback& requestCallback, const ErrorCallback& errorCallback); |
| 99 | |
| 100 | void |
| 101 | sendValidate(const shared_ptr<RequestState>& state, const JsonSection& validateParams, |
| 102 | const RequestCallback& requestCallback, const ErrorCallback& errorCallback); |
| 103 | |
| 104 | void |
| 105 | handleValidateResponse(const Interest& request, const Data& reply, |
| 106 | const shared_ptr<RequestState>& state, |
| 107 | const RequestCallback& requestCallback, const ErrorCallback& errorCallback); |
| 108 | |
| 109 | void |
| 110 | requestStatus(const shared_ptr<RequestState>& state, |
| 111 | const RequestCallback& requestCallback, const ErrorCallback& errorCallback); |
| 112 | |
| 113 | void |
| 114 | handleStatusResponse(const Interest& request, const Data& reply, |
| 115 | const shared_ptr<RequestState>& state, |
| 116 | const RequestCallback& requestCallback, const ErrorCallback& errorCallback); |
| 117 | |
| 118 | void |
| 119 | requestDownload(const shared_ptr<RequestState>& state, const RequestCallback& requestCallback, |
| 120 | const ErrorCallback& errorCallback); |
| 121 | |
| 122 | void |
| 123 | handleDownloadResponse(const Interest& request, const Data& reply, |
| 124 | const shared_ptr<RequestState>& state, |
| 125 | const RequestCallback& requestCallback, const ErrorCallback& errorCallback); |
| 126 | |
| 127 | // helper functions |
| 128 | static JsonSection |
| 129 | getJsonFromData(const Data& data); |
| 130 | |
| 131 | static Block |
| 132 | nameBlockFromJson(const JsonSection& json); |
| 133 | |
| 134 | static bool |
| 135 | checkStatus(const RequestState& state, const JsonSection& json, const ErrorCallback& errorCallback); |
| 136 | |
| 137 | protected: |
| 138 | virtual void |
| 139 | onTimeout(const Interest& interest, int nRetriesLeft, const DataCallback& dataCallback, |
| 140 | const RequestCallback& requestCallback, const ErrorCallback& errorCallback); |
| 141 | |
| 142 | virtual void |
| 143 | onNack(const Interest& interest, const lp::Nack& nack, const ErrorCallback& errorCallback); |
| 144 | |
| 145 | |
| 146 | protected: |
| 147 | ClientConfig m_config; |
| 148 | Face& m_face; |
| 149 | security::v2::KeyChain& m_keyChain; |
| 150 | size_t m_retryTimes; |
| 151 | }; |
| 152 | |
| 153 | } // namespace ndncert |
| 154 | } // namespace ndn |
| 155 | |
| 156 | #endif // NDNCERT_CLIENT_MODULE_HPP |