Ring Daemon 16.0.0
Loading...
Searching...
No Matches
namedirectory.h
Go to the documentation of this file.
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
32namespace dht {
33class Executor;
34namespace crypto {
35struct PublicKey;
36}
37namespace http {
38class Request;
39struct Response;
40class Resolver;
41} // namespace http
42namespace log {
43struct Logger;
44}
45using Logger = log::Logger;
46} // namespace dht
47
48namespace jami {
49
50class Task;
51
53{
54public:
55 enum class Response : int { found = 0, invalidResponse, notFound, error };
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 = {});
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 void setToken(std::string token) { serverToken_ = std::move(token); }
81
82 static void lookupUri(std::string_view uri,
83 const std::string& default_server,
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,
94 const std::string& signedname,
95 const std::string& publickey);
96
97private:
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::pair<std::string, std::string>> nameCache_ {};
121 std::map<std::string, std::pair<std::string, std::string>> addrCache_ {};
122
123 std::weak_ptr<Task> saveTask_;
124
125 void setHeaderFields(dht::http::Request& request);
126
127 std::pair<std::string, std::string> nameCache(const std::string& addr)
128 {
129 std::lock_guard l(cacheLock_);
130 auto cacheRes = nameCache_.find(addr);
131 return cacheRes != nameCache_.end() ? cacheRes->second : std::pair<std::string, std::string> {};
132 }
133 std::pair<std::string, std::string> addrCache(const std::string& name)
134 {
135 std::lock_guard l(cacheLock_);
136 auto cacheRes = addrCache_.find(name);
137 return cacheRes != addrCache_.end() ? cacheRes->second : std::pair<std::string, std::string> {};
138 }
139
140 static bool verify(const std::string& name,
141 const dht::crypto::PublicKey& publickey,
142 const std::string& signature);
143
144 void scheduleCacheSave();
145 void saveCache();
146 void loadCache();
147};
148} // namespace jami
std::function< void(RegistrationResponse response, const std::string &name)> RegistrationCallback
void lookupName(const std::string &name, LookupCallback cb)
std::function< void(const SearchResult &result, Response response)> SearchCallback
void lookupAddress(const std::string &addr, LookupCallback cb)
void registerName(const std::string &addr, const std::string &name, const std::string &owner, RegistrationCallback cb, const std::string &signedname, const std::string &publickey)
static void lookupUri(std::string_view uri, const std::string &default_server, LookupCallback cb)
void setToken(std::string token)
std::function< void(const std::string &name, const std::string &address, Response response)> LookupCallback
static NameDirectory & instance()
std::vector< std::map< std::string, std::string > > SearchResult
bool searchName(const std::string &, SearchCallback)
Definition account.h:55
log::Logger Logger
void emitSignal(Args... args)
Definition ring_signal.h:64
Simple macro to hide class' copy constructor and assignment operator.
#define NON_COPYABLE(ClassName)
Definition noncopyable.h:30