LCOV - code coverage report
Current view: top level - src/jamidht - namedirectory.h (source / functions) Coverage Total Hit
Test: jami-coverage-filtered.info Lines: 90.9 % 11 10
Test Date: 2026-06-13 09:18:46 Functions: 66.7 % 3 2

            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              :     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         1557 :     std::pair<std::string, std::string> nameCache(const std::string& addr)
     124              :     {
     125         1557 :         std::lock_guard l(cacheLock_);
     126         1557 :         auto cacheRes = nameCache_.find(addr);
     127         3114 :         return cacheRes != nameCache_.end() ? cacheRes->second : std::pair<std::string, std::string> {};
     128         1557 :     }
     129            4 :     std::pair<std::string, std::string> addrCache(const std::string& name)
     130              :     {
     131            4 :         std::lock_guard l(cacheLock_);
     132            4 :         auto cacheRes = addrCache_.find(name);
     133            8 :         return cacheRes != addrCache_.end() ? cacheRes->second : std::pair<std::string, std::string> {};
     134            4 :     }
     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
        

Generated by: LCOV version 2.0-1