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