Line data Source code
1 : /* 2 : * Copyright (C) 2004-2024 Savoir-faire Linux Inc. 3 : * Author: Adrien BĂ©raud <adrien.beraud@savoirfairelinux.com> 4 : * Vsevolod Ivanov <vsevolod.ivanov@savoirfairelinux.com> 5 : * 6 : * This program is free software; you can redistribute it and/or modify 7 : * it under the terms of the GNU General Public License as published by 8 : * the Free Software Foundation; either version 3 of the License, or 9 : * (at your option) any later version. 10 : * 11 : * This library is distributed in the hope that it will be useful, 12 : * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 : * Lesser General Public License for more details. 15 : * 16 : * You should have received a copy of the GNU General Public License 17 : * along with this program. If not, see <http://www.gnu.org/licenses/>. 18 : */ 19 : #pragma once 20 : 21 : #include "noncopyable.h" 22 : 23 : #include <asio/io_context.hpp> 24 : 25 : #include <functional> 26 : #include <map> 27 : #include <set> 28 : #include <string> 29 : #include <mutex> 30 : #include <memory> 31 : #include <thread> 32 : #include <filesystem> 33 : 34 : namespace dht { 35 : class Executor; 36 : namespace crypto { 37 : struct PublicKey; 38 : } 39 : namespace http { 40 : class Request; 41 : struct Response; 42 : class Resolver; 43 : } // namespace http 44 : namespace log { 45 : struct Logger; 46 : } 47 : using Logger = log::Logger; 48 : } // namespace dht 49 : 50 : namespace jami { 51 : 52 : class Task; 53 : 54 : class NameDirectory 55 : { 56 : public: 57 : enum class Response : int { found = 0, invalidResponse, notFound, error }; 58 : enum class RegistrationResponse : int { 59 : success = 0, 60 : invalidName, 61 : invalidCredentials, 62 : alreadyTaken, 63 : error, 64 : incompleteRequest, 65 : signatureVerificationFailed, 66 : unsupported 67 : }; 68 : 69 : using LookupCallback = std::function<void(const std::string& result, Response response)>; 70 : using SearchResult = std::vector<std::map<std::string, std::string>>; 71 : using SearchCallback = std::function<void(const SearchResult& result, Response response)>; 72 : using RegistrationCallback = std::function<void(RegistrationResponse response)>; 73 : 74 : NameDirectory(const std::string& serverUrl, std::shared_ptr<dht::Logger> l = {}); 75 : ~NameDirectory(); 76 : void load(); 77 : 78 : static NameDirectory& instance(const std::string& serverUrl, 79 : std::shared_ptr<dht::Logger> l = {}); 80 : static NameDirectory& instance(); 81 : 82 0 : void setToken(std::string token) { serverToken_ = std::move(token); } 83 : 84 : static void lookupUri(std::string_view uri, 85 : const std::string& default_server, 86 : LookupCallback cb); 87 : 88 : void lookupAddress(const std::string& addr, LookupCallback cb); 89 : void lookupName(const std::string& name, LookupCallback cb); 90 : bool searchName(const std::string& /*name*/, SearchCallback /*cb*/) { return false; } 91 : 92 : void registerName(const std::string& addr, 93 : const std::string& name, 94 : const std::string& owner, 95 : RegistrationCallback cb, 96 : const std::string& signedname, 97 : const std::string& publickey); 98 : 99 : private: 100 : NON_COPYABLE(NameDirectory); 101 : NameDirectory(NameDirectory&&) = delete; 102 : NameDirectory& operator=(NameDirectory&&) = delete; 103 : 104 : std::string serverUrl_; 105 : std::string serverToken_; 106 : std::filesystem::path cachePath_; 107 : 108 : std::mutex cacheLock_ {}; 109 : std::shared_ptr<dht::Logger> logger_; 110 : 111 : /* 112 : * ASIO I/O Context for sockets in httpClient_. 113 : * Note: Each context is used in one thread only. 114 : */ 115 : std::shared_ptr<asio::io_context> httpContext_; 116 : std::shared_ptr<dht::http::Resolver> resolver_; 117 : std::mutex requestsMtx_ {}; 118 : std::set<std::shared_ptr<dht::http::Request>> requests_; 119 : 120 : std::map<std::string, std::string> nameCache_ {}; 121 : std::map<std::string, std::string> addrCache_ {}; 122 : 123 : std::weak_ptr<Task> saveTask_; 124 : 125 : void setHeaderFields(dht::http::Request& request); 126 : 127 782 : std::string nameCache(const std::string& addr) 128 : { 129 782 : std::lock_guard l(cacheLock_); 130 782 : auto cacheRes = nameCache_.find(addr); 131 1564 : return cacheRes != nameCache_.end() ? cacheRes->second : std::string {}; 132 782 : } 133 3 : std::string addrCache(const std::string& name) 134 : { 135 3 : std::lock_guard l(cacheLock_); 136 3 : auto cacheRes = addrCache_.find(name); 137 6 : return cacheRes != addrCache_.end() ? cacheRes->second : std::string {}; 138 3 : } 139 : 140 : bool validateName(const std::string& name) const; 141 : static bool verify(const std::string& name, 142 : const dht::crypto::PublicKey& publickey, 143 : const std::string& signature); 144 : 145 : void scheduleCacheSave(); 146 : void saveCache(); 147 : void loadCache(); 148 : }; 149 : } // namespace jami