Ring Daemon
Loading...
Searching...
No Matches
namedirectory.h
Go to the documentation of this file.
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
33namespace dht {
34class Executor;
35namespace crypto {
36struct PublicKey;
37}
38namespace http {
39class Request;
40struct Response;
41class Resolver;
42} // namespace http
43namespace log {
44struct Logger;
45}
46using Logger = log::Logger;
47} // namespace dht
48
49namespace jami {
50
52{
53public:
54 enum class Response : int { found = 0, invalidResponse, notFound, error };
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 = {});
73 void load();
74
75 static NameDirectory& instance(const std::string& serverUrl, std::shared_ptr<dht::Logger> l = {});
76 static NameDirectory& instance();
77
78 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,
90 const std::string& signedname,
91 const std::string& publickey);
92
93private:
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 std::mutex requestsMtx_ {};
112 std::set<std::shared_ptr<dht::http::Request>> requests_;
113
114 std::map<std::string, std::string> pendingRegistrations_ {};
115
116 std::map<std::string, std::pair<std::string, std::string>> nameCache_ {};
117 std::map<std::string, std::pair<std::string, std::string>> addrCache_ {};
118
119 asio::steady_timer saveTask_;
120
121 void setHeaderFields(dht::http::Request& request);
122
123 std::pair<std::string, std::string> nameCache(const std::string& addr)
124 {
125 std::lock_guard l(cacheLock_);
126 auto cacheRes = nameCache_.find(addr);
127 return cacheRes != nameCache_.end() ? cacheRes->second : std::pair<std::string, std::string> {};
128 }
129 std::pair<std::string, std::string> addrCache(const std::string& name)
130 {
131 std::lock_guard l(cacheLock_);
132 auto cacheRes = addrCache_.find(name);
133 return cacheRes != addrCache_.end() ? cacheRes->second : std::pair<std::string, std::string> {};
134 }
135
136 static bool verify(const std::string& name, const dht::crypto::PublicKey& publickey, const std::string& signature);
137
138 void scheduleCacheSave();
139 void saveCache();
140 void loadCache();
141};
142} // 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:50
log::Logger Logger
void emitSignal(Args... args)
Definition jami_signal.h:64
Simple macro to hide class' copy constructor and assignment operator.
#define NON_COPYABLE(ClassName)
Definition noncopyable.h:30