Line data Source code
1 : /*
2 : * Copyright (C) 2004-2026 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 : #include <asio/steady_timer.hpp>
23 :
24 : #include <functional>
25 : #include <map>
26 : #include <set>
27 : #include <string>
28 : #include <mutex>
29 : #include <memory>
30 : #include <thread>
31 : #include <filesystem>
32 :
33 : namespace dht {
34 : class Executor;
35 : namespace crypto {
36 : struct PublicKey;
37 : }
38 : namespace http {
39 : class Request;
40 : struct Response;
41 : class Resolver;
42 : } // namespace http
43 : namespace log {
44 : struct Logger;
45 : }
46 : using Logger = log::Logger;
47 : } // namespace dht
48 :
49 : namespace jami {
50 :
51 : class NameDirectory
52 : {
53 : public:
54 : enum class Response : int { found = 0, invalidResponse, notFound, error };
55 : enum class RegistrationResponse : int {
56 : success = 0,
57 : invalidName,
58 : invalidCredentials,
59 : alreadyTaken,
60 : error,
61 : incompleteRequest,
62 : signatureVerificationFailed,
63 : unsupported
64 : };
65 :
66 : using LookupCallback = std::function<void(const std::string& name, const std::string& address, Response response)>;
67 : using SearchResult = std::vector<std::map<std::string, std::string>>;
68 : using SearchCallback = std::function<void(const SearchResult& result, Response response)>;
69 : using RegistrationCallback = std::function<void(RegistrationResponse response, const std::string& name)>;
70 :
71 : NameDirectory(const std::string& serverUrl, std::shared_ptr<dht::Logger> l = {});
72 : ~NameDirectory();
73 : void load();
74 :
75 : static NameDirectory& instance(const std::string& serverUrl, std::shared_ptr<dht::Logger> l = {});
76 : static NameDirectory& instance();
77 :
78 0 : void setToken(std::string token) { serverToken_ = std::move(token); }
79 :
80 : static void lookupUri(std::string_view uri, const std::string& default_server, LookupCallback cb);
81 :
82 : void lookupAddress(const std::string& addr, LookupCallback cb);
83 : void lookupName(const std::string& name, LookupCallback cb);
84 : bool searchName(const std::string& /*name*/, SearchCallback /*cb*/) { return false; }
85 :
86 : void registerName(const std::string& addr,
87 : const std::string& name,
88 : const std::string& owner,
89 : RegistrationCallback cb,
90 : const std::string& signedname,
91 : const std::string& publickey);
92 :
93 : private:
94 : NON_COPYABLE(NameDirectory);
95 : NameDirectory(NameDirectory&&) = delete;
96 : NameDirectory& operator=(NameDirectory&&) = delete;
97 :
98 : std::string serverUrl_;
99 : std::string serverToken_;
100 : std::filesystem::path cachePath_;
101 :
102 : std::mutex cacheLock_ {};
103 : std::shared_ptr<dht::Logger> logger_;
104 :
105 : /*
106 : * ASIO I/O Context for sockets in httpClient_.
107 : * Note: Each context is used in one thread only.
108 : */
109 : std::shared_ptr<asio::io_context> httpContext_;
110 : std::shared_ptr<dht::http::Resolver> resolver_;
111 : // A Resolver resolves its host once and replays that result (or that error) to every later
112 : // request. Requests take it through resolver(), and a request that got no HTTP response at all
113 : // drops it, so the next request resolves the host again.
114 : std::mutex resolverMtx_ {};
115 : std::shared_ptr<dht::http::Resolver> resolver();
116 : void dropResolver(const std::shared_ptr<dht::http::Resolver>& failed);
117 : std::mutex requestsMtx_ {};
118 : std::set<std::shared_ptr<dht::http::Request>> requests_;
119 :
120 : std::map<std::string, std::string> pendingRegistrations_ {};
121 :
122 : std::map<std::string, std::pair<std::string, std::string>> nameCache_ {};
123 : std::map<std::string, std::pair<std::string, std::string>> addrCache_ {};
124 :
125 : asio::steady_timer saveTask_;
126 :
127 : void setHeaderFields(dht::http::Request& request);
128 :
129 2025 : std::pair<std::string, std::string> nameCache(const std::string& addr)
130 : {
131 2025 : std::lock_guard l(cacheLock_);
132 2025 : auto cacheRes = nameCache_.find(addr);
133 4050 : return cacheRes != nameCache_.end() ? cacheRes->second : std::pair<std::string, std::string> {};
134 2025 : }
135 4 : std::pair<std::string, std::string> addrCache(const std::string& name)
136 : {
137 4 : std::lock_guard l(cacheLock_);
138 4 : auto cacheRes = addrCache_.find(name);
139 8 : return cacheRes != addrCache_.end() ? cacheRes->second : std::pair<std::string, std::string> {};
140 4 : }
141 :
142 : static bool verify(const std::string& name, const dht::crypto::PublicKey& publickey, const std::string& signature);
143 :
144 : void scheduleCacheSave();
145 : void saveCache();
146 : void loadCache();
147 : };
148 : } // namespace jami
|