Line data Source code
1 : /*
2 : * Copyright (C) 2004-2025 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& name, const std::string& address, 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, std::shared_ptr<dht::Logger> l = {});
77 : static NameDirectory& instance();
78 :
79 0 : void setToken(std::string token) { serverToken_ = std::move(token); }
80 :
81 : static void lookupUri(std::string_view uri, const std::string& default_server, LookupCallback cb);
82 :
83 : void lookupAddress(const std::string& addr, LookupCallback cb);
84 : void lookupName(const std::string& name, LookupCallback cb);
85 : bool searchName(const std::string& /*name*/, SearchCallback /*cb*/) { return false; }
86 :
87 : void registerName(const std::string& addr,
88 : const std::string& name,
89 : const std::string& owner,
90 : RegistrationCallback cb,
91 : const std::string& signedname,
92 : const std::string& publickey);
93 :
94 : private:
95 : NON_COPYABLE(NameDirectory);
96 : NameDirectory(NameDirectory&&) = delete;
97 : NameDirectory& operator=(NameDirectory&&) = delete;
98 :
99 : std::string serverUrl_;
100 : std::string serverToken_;
101 : std::filesystem::path cachePath_;
102 :
103 : std::mutex cacheLock_ {};
104 : std::shared_ptr<dht::Logger> logger_;
105 :
106 : /*
107 : * ASIO I/O Context for sockets in httpClient_.
108 : * Note: Each context is used in one thread only.
109 : */
110 : std::shared_ptr<asio::io_context> httpContext_;
111 : std::shared_ptr<dht::http::Resolver> resolver_;
112 : std::mutex requestsMtx_ {};
113 : std::set<std::shared_ptr<dht::http::Request>> requests_;
114 :
115 : std::map<std::string, std::string> pendingRegistrations_ {};
116 :
117 : std::map<std::string, std::pair<std::string, std::string>> nameCache_ {};
118 : std::map<std::string, std::pair<std::string, std::string>> addrCache_ {};
119 :
120 : std::weak_ptr<Task> saveTask_;
121 :
122 : void setHeaderFields(dht::http::Request& request);
123 :
124 1551 : std::pair<std::string, std::string> nameCache(const std::string& addr)
125 : {
126 1551 : std::lock_guard l(cacheLock_);
127 1551 : auto cacheRes = nameCache_.find(addr);
128 3102 : return cacheRes != nameCache_.end() ? cacheRes->second : std::pair<std::string, std::string> {};
129 1551 : }
130 4 : std::pair<std::string, std::string> addrCache(const std::string& name)
131 : {
132 4 : std::lock_guard l(cacheLock_);
133 4 : auto cacheRes = addrCache_.find(name);
134 8 : return cacheRes != addrCache_.end() ? cacheRes->second : std::pair<std::string, std::string> {};
135 4 : }
136 :
137 : static bool verify(const std::string& name, const dht::crypto::PublicKey& publickey, const std::string& signature);
138 :
139 : void scheduleCacheSave();
140 : void saveCache();
141 : void loadCache();
142 : };
143 : } // namespace jami
|