LCOV - code coverage report
Current view: top level - src/jamidht - jamiaccount.h (source / functions) Coverage Total Hit
Test: jami-coverage-filtered.info Lines: 87.2 % 39 34
Test Date: 2026-06-13 09:18:46 Functions: 87.0 % 23 20

            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 "def.h"
      20              : #ifdef HAVE_CONFIG_H
      21              : #include "config.h"
      22              : #endif
      23              : 
      24              : #include "sip/sipaccountbase.h"
      25              : #include "jami/datatransfer_interface.h"
      26              : #include "jamidht/conversation.h"
      27              : #include "data_transfer.h"
      28              : #include "uri.h"
      29              : #include "jamiaccount_config.h"
      30              : 
      31              : #include "noncopyable.h"
      32              : #include "gitserver.h"
      33              : #include "channel_handler.h"
      34              : #include "conversation_module.h"
      35              : #include "sync_module.h"
      36              : #include "conversationrepository.h"
      37              : #include "namedirectory.h"
      38              : 
      39              : #include <dhtnet/diffie-hellman.h>
      40              : #include <dhtnet/tls_session.h>
      41              : #include <dhtnet/multiplexed_socket.h>
      42              : #include <dhtnet/certstore.h>
      43              : #include <dhtnet/connectionmanager.h>
      44              : #include <dhtnet/upnp/mapping.h>
      45              : #include <dhtnet/ip_utils.h>
      46              : #include <dhtnet/fileutils.h>
      47              : 
      48              : #include <opendht/dhtrunner.h>
      49              : #include <opendht/default_types.h>
      50              : #include <opendht/dht_proxy_server.h>
      51              : 
      52              : #include <pjsip/sip_types.h>
      53              : #include <json/json.h>
      54              : 
      55              : #include <chrono>
      56              : #include <functional>
      57              : #include <future>
      58              : #include <list>
      59              : #include <map>
      60              : #include <optional>
      61              : #include <vector>
      62              : #include <filesystem>
      63              : #include <shared_mutex>
      64              : 
      65              : namespace dev {
      66              : template<unsigned N>
      67              : class FixedHash;
      68              : using h160 = FixedHash<20>;
      69              : using Address = h160;
      70              : } // namespace dev
      71              : 
      72              : namespace jami {
      73              : 
      74              : class IceTransport;
      75              : struct Contact;
      76              : struct AccountArchive;
      77              : class DhtPeerConnector;
      78              : class AccountManager;
      79              : struct AccountInfo;
      80              : class SipTransport;
      81              : class ChanneledOutgoingTransfer;
      82              : class SyncModule;
      83              : class PresenceManager;
      84              : 
      85              : using SipConnectionKey = std::pair<std::string /* uri */, DeviceId>;
      86              : 
      87              : static constexpr const char MIME_TYPE_IM_COMPOSING[] {"application/im-iscomposing+xml"};
      88              : 
      89              : /**
      90              :  * @brief Ring Account is build on top of SIPAccountBase and uses DHT to handle call connectivity.
      91              :  */
      92              : class JamiAccount : public SIPAccountBase
      93              : {
      94              : public:
      95              :     constexpr static auto ACCOUNT_TYPE = ACCOUNT_TYPE_JAMI;
      96              :     constexpr static const std::pair<uint16_t, uint16_t> DHT_PORT_RANGE {4000, 8888};
      97              :     constexpr static int ICE_STREAMS_COUNT {1};
      98              :     constexpr static int ICE_COMP_COUNT_PER_STREAM {1};
      99              : 
     100         3486 :     std::string_view getAccountType() const override { return ACCOUNT_TYPE; }
     101              : 
     102         6546 :     std::shared_ptr<JamiAccount> shared() { return std::static_pointer_cast<JamiAccount>(shared_from_this()); }
     103              :     std::shared_ptr<JamiAccount const> shared() const
     104              :     {
     105              :         return std::static_pointer_cast<JamiAccount const>(shared_from_this());
     106              :     }
     107        72231 :     std::weak_ptr<JamiAccount> weak() { return std::static_pointer_cast<JamiAccount>(shared_from_this()); }
     108              :     std::weak_ptr<JamiAccount const> weak() const
     109              :     {
     110              :         return std::static_pointer_cast<JamiAccount const>(shared_from_this());
     111              :     }
     112              : 
     113        22587 :     const JamiAccountConfig& config() const { return *static_cast<const JamiAccountConfig*>(&Account::config()); }
     114              : 
     115          793 :     JamiAccountConfig::Credentials consumeConfigCredentials()
     116              :     {
     117          793 :         auto* conf = static_cast<JamiAccountConfig*>(config_.get());
     118          793 :         return std::move(conf->credentials);
     119              :     }
     120              : 
     121              :     void loadConfig() override;
     122              : 
     123              :     /**
     124              :      * Constructor
     125              :      * @param accountID The account identifier
     126              :      */
     127              :     JamiAccount(const std::string& accountId);
     128              : 
     129              :     ~JamiAccount() noexcept;
     130              : 
     131              :     /**
     132              :      * Retrieve volatile details such as recent registration errors
     133              :      * @return std::map< std::string, std::string > The account volatile details
     134              :      */
     135              :     virtual std::map<std::string, std::string> getVolatileAccountDetails() const override;
     136              : 
     137          773 :     std::unique_ptr<AccountConfig> buildConfig() const override
     138              :     {
     139          773 :         return std::make_unique<JamiAccountConfig>(getAccountID(), idPath_);
     140              :     }
     141              : 
     142              :     /**
     143              :      * Adds an account id to the list of accounts to track on the DHT for
     144              :      * buddy presence.
     145              :      *
     146              :      * @param buddy_id  The buddy id.
     147              :      */
     148              :     void trackBuddyPresence(const std::string& buddy_id, bool track);
     149              : 
     150              :     /**
     151              :      * Tells for each tracked account id if it has been seen online so far
     152              :      * in the last DeviceAnnouncement::TYPE.expiration minutes.
     153              :      *
     154              :      * @return map of buddy_uri to bool (online or not)
     155              :      */
     156              :     std::map<std::string, bool> getTrackedBuddyPresence() const;
     157              : 
     158              :     void setActiveCodecs(const std::vector<unsigned>& list) override;
     159              : 
     160              :     /**
     161              :      * Connect to the DHT.
     162              :      */
     163              :     void doRegister() override;
     164              : 
     165              :     /**
     166              :      * Disconnect from the DHT.
     167              :      */
     168              :     void doUnregister(bool forceShutdownConnections = false) override;
     169              : 
     170              :     /**
     171              :      * Set the registration state of the specified link
     172              :      * @param state The registration state of underlying VoIPLink
     173              :      */
     174              :     void setRegistrationState(RegistrationState state, int detail_code = 0, const std::string& detail_str = {}) override;
     175              : 
     176              :     /**
     177              :      * @return pj_str_t "From" uri based on account information.
     178              :      * From RFC3261: "The To header field first and foremost specifies the desired
     179              :      * logical" recipient of the request, or the address-of-record of the
     180              :      * user or resource that is the target of this request. [...]  As such, it is
     181              :      * very important that the From URI not contain IP addresses or the FQDN
     182              :      * of the host on which the UA is running, since these are not logical
     183              :      * names."
     184              :      */
     185              :     std::string getFromUri() const override;
     186              : 
     187              :     /**
     188              :      * This method adds the correct scheme, hostname and append
     189              :      * the ;transport= parameter at the end of the uri, in accordance with RFC3261.
     190              :      * It is expected that "port" is present in the internal hostname_.
     191              :      *
     192              :      * @return pj_str_t "To" uri based on @param username
     193              :      * @param username A string formatted as : "username"
     194              :      */
     195              :     std::string getToUri(const std::string& username) const override;
     196              : 
     197              :     /**
     198              :      * In the current version, "srv" uri is obtained in the preformated
     199              :      * way: hostname:port. This method adds the correct scheme and append
     200              :      * the ;transport= parameter at the end of the uri, in accordance with RFC3261.
     201              :      *
     202              :      * @return pj_str_t "server" uri based on @param hostPort
     203              :      * @param hostPort A string formatted as : "hostname:port"
     204              :      */
     205              :     std::string getServerUri() const { return ""; };
     206              : 
     207              :     void setIsComposing(const std::string& conversationUri, bool isWriting) override;
     208              : 
     209              :     bool setMessageDisplayed(const std::string& conversationUri, const std::string& messageId, int status) override;
     210              : 
     211              :     /**
     212              :      * Get the contact header for
     213              :      * @return The contact header based on account information
     214              :      */
     215              :     std::string getContactHeader(const std::shared_ptr<SipTransport>& sipTransport);
     216              : 
     217              :     /* Returns true if the username and/or hostname match this account */
     218              :     MatchRank matches(std::string_view username, std::string_view hostname) const override;
     219              : 
     220              :     /**
     221              :      * Create outgoing SIPCall.
     222              :      * @note Accepts several urls:
     223              :      *          + jami:uri for calling someone
     224              :      *          + swarm:id for calling a group (will host or join if an active call is detected)
     225              :      *          + rdv:id/uri/device/confId to join a specific conference hosted on (uri, device)
     226              :      * @param[in] toUrl The address to call
     227              :      * @param[in] mediaList list of medias
     228              :      * @return A shared pointer on the created call.
     229              :      */
     230              :     std::shared_ptr<Call> newOutgoingCall(std::string_view toUrl,
     231              :                                           const std::vector<libjami::MediaMap>& mediaList) override;
     232              : 
     233              :     /**
     234              :      * Create incoming SIPCall.
     235              :      * @param[in] from The origin of the call
     236              :      * @param mediaList A list of media
     237              :      * @param sipTr: SIP Transport
     238              :      * @return A shared pointer on the created call.
     239              :      */
     240              :     std::shared_ptr<SIPCall> newIncomingCall(const std::string& from,
     241              :                                              const std::vector<libjami::MediaMap>& mediaList,
     242              :                                              const std::shared_ptr<SipTransport>& sipTr = {}) override;
     243              : 
     244              :     void onTextMessage(const std::string& id,
     245              :                        const std::string& from,
     246              :                        const std::shared_ptr<dht::crypto::Certificate>& peerCert,
     247              :                        const std::map<std::string, std::string>& payloads) override;
     248              :     void loadConversation(const std::string& convId);
     249              : 
     250            0 :     virtual bool isTlsEnabled() const override { return true; }
     251          426 :     bool isSrtpEnabled() const override { return true; }
     252              : 
     253              :     bool setCertificateStatus(const std::string& cert_id, dhtnet::tls::TrustStore::PermissionStatus status);
     254              :     bool setCertificateStatus(const std::shared_ptr<crypto::Certificate>& cert,
     255              :                               dhtnet::tls::TrustStore::PermissionStatus status,
     256              :                               bool local = true);
     257              :     std::vector<std::string> getCertificatesByStatus(dhtnet::tls::TrustStore::PermissionStatus status);
     258              : 
     259              :     bool findCertificate(const std::string& id);
     260              :     bool findCertificate(const dht::InfoHash& h,
     261              :                          std::function<void(const std::shared_ptr<dht::crypto::Certificate>&)>&& cb = {});
     262              :     bool findCertificate(const dht::PkId& h,
     263              :                          std::function<void(const std::shared_ptr<dht::crypto::Certificate>&)>&& cb = {});
     264              : 
     265              :     /* contact requests */
     266              :     std::vector<std::map<std::string, std::string>> getTrustRequests() const;
     267              :     // Note: includeConversation used for compatibility test. Do not change
     268              :     bool acceptTrustRequest(const std::string& from, bool includeConversation = true);
     269              :     bool discardTrustRequest(const std::string& from);
     270              :     void declineConversationRequest(const std::string& conversationId);
     271              : 
     272              :     /**
     273              :      * Add contact to the account contact list.
     274              :      * Set confirmed if we know the contact also added us.
     275              :      */
     276              :     void addContact(const std::string& uri, bool confirmed = false);
     277              :     void removeContact(const std::string& uri, bool banned = true);
     278              :     std::vector<std::map<std::string, std::string>> getContacts(bool includeRemoved = false) const;
     279              : 
     280              :     ///
     281              :     /// Obtain details about one account contact in serializable form.
     282              :     ///
     283              :     std::map<std::string, std::string> getContactDetails(const std::string& uri) const;
     284              :     std::optional<Contact> getContactInfo(const std::string& uri) const;
     285              : 
     286              :     void sendTrustRequest(const std::string& to, const std::vector<uint8_t>& payload);
     287              :     void sendMessage(const std::string& to,
     288              :                      const std::string& deviceId,
     289              :                      const std::map<std::string, std::string>& payloads,
     290              :                      uint64_t id,
     291              :                      bool retryOnTimeout = true,
     292              :                      bool onlyConnected = false) override;
     293              : 
     294              :     uint64_t sendTextMessage(const std::string& to,
     295              :                              const std::string& deviceId,
     296              :                              const std::map<std::string, std::string>& payloads,
     297              :                              uint64_t refreshToken = 0,
     298              :                              bool onlyConnected = false) override;
     299              :     void sendInstantMessage(const std::string& convId, const std::map<std::string, std::string>& msg);
     300              : 
     301              :     /**
     302              :      * Create and return ICE options.
     303              :      */
     304              :     dhtnet::IceTransportOptions getIceOptions() const override;
     305              :     void getIceOptions(std::function<void(dhtnet::IceTransportOptions&&)> cb) const;
     306              :     dhtnet::IpAddr getPublishedIpAddress(uint16_t family = PF_UNSPEC) const override;
     307              : 
     308              :     /* Devices - existing device */
     309              :     /**
     310              :      * Initiates the process of adding a new device to this account
     311              :      * @param uriProvided The URI provided by the new device to be added
     312              :      * @return A positive operation ID if successful, or a negative value indicating an AddDeviceError:
     313              :      *         - INVALID_URI (-1): The provided URI is invalid
     314              :      *         - ALREADY_LINKING (-2): A device linking operation is already in progress
     315              :      *         - GENERIC (-3): A generic error occurred during the process
     316              :      */
     317              :     int32_t addDevice(const std::string& uriProvided);
     318              :     bool cancelAddDevice(uint32_t op_token);
     319              :     bool confirmAddDevice(uint32_t op_token);
     320              :     /* Devices - new device */
     321              :     bool provideAccountAuthentication(const std::string& credentialsFromUser, const std::string& scheme);
     322              : 
     323              :     /**
     324              :      * Export the archive to a file
     325              :      * @param destinationPath
     326              :      * @param (optional) password, if not provided, will update the contacts only if the archive
     327              :      * doesn't have a password
     328              :      * @return if the archive was exported
     329              :      */
     330              :     bool exportArchive(const std::string& destinationPath,
     331              :                        std::string_view scheme = {},
     332              :                        const std::string& password = {});
     333              :     bool revokeDevice(const std::string& device, std::string_view scheme = {}, const std::string& password = {});
     334              :     std::map<std::string, std::string> getKnownDevices() const;
     335              : 
     336              :     bool isPasswordValid(const std::string& password);
     337              :     std::vector<uint8_t> getPasswordKey(const std::string& password);
     338              : 
     339              :     bool changeArchivePassword(const std::string& password_old, const std::string& password_new);
     340              : 
     341              :     void connectivityChanged() override;
     342              : 
     343              :     // overloaded methods
     344              :     void flush() override;
     345              : 
     346              :     void lookupName(const std::string& name);
     347              :     void lookupAddress(const std::string& address);
     348              :     void registerName(const std::string& name, const std::string& scheme, const std::string& password);
     349              :     bool searchUser(const std::string& nameQuery);
     350              : 
     351              :     /// \return true if the given DHT message identifier has been treated
     352              :     /// \note if message has not been treated yet this method store this id and returns true at
     353              :     /// further calls
     354              :     bool isMessageTreated(dht::Value::Id id);
     355              : 
     356          674 :     std::shared_ptr<dht::DhtRunner> dht() { return dht_; }
     357              : 
     358         3389 :     const dht::crypto::Identity& identity() const { return id_; }
     359              : 
     360         2476 :     PresenceManager* presenceManager() const { return presenceManager_.get(); }
     361              : 
     362              :     void forEachDevice(const dht::InfoHash& to,
     363              :                        std::function<void(const std::shared_ptr<dht::crypto::PublicKey>&)>&& op,
     364              :                        std::function<void(bool)>&& end = {});
     365              : 
     366              :     bool setPushNotificationToken(const std::string& pushDeviceToken = "") override;
     367              :     bool setPushNotificationTopic(const std::string& topic) override;
     368              :     bool setPushNotificationConfig(const std::map<std::string, std::string>& data) override;
     369              : 
     370              :     /**
     371              :      * To be called by clients with relevant data when a push notification is received.
     372              :      */
     373              :     void pushNotificationReceived(const std::string& from, const std::map<std::string, std::string>& data);
     374              : 
     375              :     std::string getUserUri() const override;
     376              : 
     377              :     /**
     378              :      * Get last messages (should be used to retrieve messages when launching the client)
     379              :      * @param base_timestamp
     380              :      */
     381              :     std::vector<libjami::Message> getLastMessages(const uint64_t& base_timestamp) override;
     382              : 
     383              :     /**
     384              :      * Start Publish the Jami Account onto the Network
     385              :      */
     386              :     void startAccountPublish();
     387              : 
     388              :     /**
     389              :      * Start Discovery the Jami Account from the Network
     390              :      */
     391              :     void startAccountDiscovery();
     392              : 
     393              :     void saveConfig() const override;
     394              : 
     395          785 :     inline void editConfig(std::function<void(JamiAccountConfig& conf)>&& edit)
     396              :     {
     397         1570 :         Account::editConfig([&](AccountConfig& conf) { edit(*static_cast<JamiAccountConfig*>(&conf)); });
     398          785 :     }
     399              : 
     400              :     /**
     401              :      * Get current discovered peers account id and display name
     402              :      */
     403              :     std::map<std::string, std::string> getNearbyPeers() const override;
     404              : 
     405              :     void sendProfileToPeers();
     406              : 
     407              :     /**
     408              :      * Update the profile vcard and send it to peers
     409              :      * @param displayName Current or new display name
     410              :      * @param avatar Current or new avatar
     411              :      * @param flag  0 for path to avatar, 1 for base64 avatar
     412              :      */
     413              :     void updateProfile(const std::string& displayName,
     414              :                        const std::string& avatar,
     415              :                        const std::string& fileType,
     416              :                        int32_t flag) override;
     417              : 
     418              : #ifdef LIBJAMI_TEST
     419            1 :     dhtnet::ConnectionManager& connectionManager() { return *connectionManager_; }
     420              : 
     421              :     /**
     422              :      * Only used for tests, disable sha3sum verification for transfers.
     423              :      * @param newValue
     424              :      */
     425              :     void noSha3sumVerification(bool newValue);
     426              : 
     427            2 :     void publishPresence(bool newValue) { publishPresence_ = newValue; }
     428              : #endif
     429              : 
     430              :     /**
     431              :      * This should be called before flushing the account.
     432              :      * ConnectionManager needs the account to exists
     433              :      */
     434              :     void shutdownConnections();
     435              : 
     436              :     std::string_view currentDeviceId() const;
     437              : 
     438              :     // Received a new commit notification
     439              : 
     440              :     bool handleMessage(const std::shared_ptr<dht::crypto::Certificate>& cert,
     441              :                        const std::string& from,
     442              :                        const std::pair<std::string, std::string>& message) override;
     443              : 
     444              :     void monitor();
     445              :     // conversationId optional
     446              :     std::vector<std::map<std::string, std::string>> getConnectionList(const std::string& conversationId = "");
     447              :     std::vector<std::map<std::string, std::string>> getConversationConnectivity(const std::string& conversationId);
     448              :     std::vector<std::map<std::string, std::string>> getConversationTrackedMembers(const std::string& conversationId);
     449              :     std::vector<std::map<std::string, std::string>> getChannelList(const std::string& connectionId);
     450              : 
     451              :     // File transfer
     452              :     void sendFile(const std::string& conversationId,
     453              :                   const std::filesystem::path& path,
     454              :                   const std::string& name,
     455              :                   const std::string& replyTo);
     456              : 
     457              :     void transferFile(const std::string& conversationId,
     458              :                       const std::string& path,
     459              :                       const std::string& deviceId,
     460              :                       const std::string& fileId,
     461              :                       const std::string& interactionId,
     462              :                       size_t start = 0,
     463              :                       size_t end = 0,
     464              :                       const std::string& sha3Sum = "",
     465              :                       uint64_t lastWriteTime = 0,
     466              :                       std::function<void()> onFinished = {});
     467              : 
     468              :     void askForFileChannel(const std::string& conversationId,
     469              :                            const std::string& deviceId,
     470              :                            const std::string& interactionId,
     471              :                            const std::string& fileId,
     472              :                            size_t start = 0,
     473              :                            size_t end = 0);
     474              : 
     475              :     void askForProfile(const std::string& conversationId, const std::string& deviceId, const std::string& memberUri);
     476              : 
     477              :     /**
     478              :      * Retrieve linked transfer manager
     479              :      * @param id    conversationId or empty for fallback
     480              :      * @return linked transfer manager
     481              :      */
     482              :     std::shared_ptr<TransferManager> dataTransfer(const std::string& id = "");
     483              : 
     484              :     /**
     485              :      *   Used to get the instance of the ConversationModule class which is
     486              :      *  responsible for managing conversations and messages between users.
     487              :      * @param noCreate    whether or not to create a new instance
     488              :      * @return conversationModule instance
     489              :      */
     490              :     ConversationModule* convModule(bool noCreation = false);
     491              :     SyncModule* syncModule();
     492              : 
     493              :     /**
     494              :      * Check (via the cache) if we need to send our profile to a specific device
     495              :      * @param peerUri       Uri that will receive the profile
     496              :      * @param deviceId      Device that will receive the profile
     497              :      * @param sha3Sum       SHA3 hash of the profile
     498              :      */
     499              :     // Note: when swarm will be merged, this can be moved in transferManager
     500              :     bool needToSendProfile(const std::string& peerUri, const std::string& deviceId, const std::string& sha3Sum);
     501              :     /**
     502              :      * Send Profile via cached SIP connection
     503              :      * @param convId        Conversation's identifier (can be empty for self profile on sync)
     504              :      * @param peerUri       Uri that will receive the profile
     505              :      * @param deviceId      Device that will receive the profile
     506              :      */
     507              :     void sendProfile(const std::string& convId, const std::string& peerUri, const std::string& deviceId);
     508              :     /**
     509              :      * Send profile via cached SIP connection
     510              :      * @param peerUri       Uri that will receive the profile
     511              :      * @param deviceId      Device that will receive the profile
     512              :      */
     513              :     void sendProfile(const std::string& peerUri, const std::string& deviceId);
     514              :     /**
     515              :      * Clear sent profiles (because of a removed contact or new trust request)
     516              :      * @param peerUri       Uri used to clear cache
     517              :      */
     518              :     void clearProfileCache(const std::string& peerUri);
     519              : 
     520              :     std::filesystem::path profilePath() const;
     521              : 
     522        46604 :     const std::shared_ptr<AccountManager>& accountManager() { return accountManager_; }
     523              : 
     524              :     bool sha3SumVerify() const;
     525              : 
     526              :     /**
     527              :      * Change certificate's validity period
     528              :      * @param pwd       Password for the archive
     529              :      * @param id        Certificate to update ({} for updating the whole chain)
     530              :      * @param validity  New validity
     531              :      * @note forceReloadAccount may be necessary to retrigger the migration
     532              :      */
     533              :     bool setValidity(std::string_view scheme, const std::string& pwd, const dht::InfoHash& id, int64_t validity);
     534              :     /**
     535              :      * Try to reload the account to force the identity to be updated
     536              :      */
     537              :     void forceReloadAccount();
     538              : 
     539              :     void reloadContacts();
     540              : 
     541              :     /**
     542              :      * Make sure appdata/contacts.yml contains correct information
     543              :      * @param removedConv   The current removed conversations
     544              :      */
     545              :     void unlinkConversations(const std::set<std::string>& removedConv);
     546              : 
     547              :     bool isValidAccountDevice(const dht::crypto::Certificate& cert) const;
     548              : 
     549              :     /**
     550              :      * Join incoming call to hosted conference
     551              :      * @param callId        The call to join
     552              :      * @param destination   conversation/uri/device/confId to join
     553              :      */
     554              :     void handleIncomingConversationCall(const std::string& callId, const std::string& destination);
     555              : 
     556              :     /**
     557              :      * The DRT component is composed on some special nodes, that are usually present but not
     558              :      * connected. This kind of node corresponds to devices with push notifications & proxy and are
     559              :      * stored in the mobile nodes
     560              :      */
     561              :     bool isMobile() const { return config().proxyEnabled and not config().deviceKey.empty(); }
     562              : 
     563              : #ifdef LIBJAMI_TEST
     564            1 :     std::map<Uri::Scheme, std::unique_ptr<ChannelHandlerInterface>>& channelHandlers() { return channelHandlers_; };
     565              : #endif
     566              : 
     567        30236 :     dhtnet::tls::CertificateStore& certStore() const { return *certStore_; }
     568              : 
     569              :     /// Returns true if `peerAccountUri` is an active
     570              :     /// contact of this account.
     571              :     bool isContact(const std::string& peerAccountUri) const;
     572              : 
     573         1213 :     class ServiceManager& serviceManager() { return *serviceManager_; }
     574              :     const class ServiceManager& serviceManager() const { return *serviceManager_; }
     575            0 :     bool hasServiceManager() const { return serviceManager_ != nullptr; }
     576              : 
     577              :     /* Service-exposure high-level API. Implemented in jamiaccount.cpp. */
     578              : 
     579              :     /// Send a discovery query to every known device of `peerUri` and return a
     580              :     /// monotonically-increasing request id. Responses are delivered through the
     581              :     /// `libjami::ServiceSignal::PeerServicesReceived` signal with this id as
     582              :     /// first argument.
     583              :     uint32_t queryPeerServices(const std::string& peerUri);
     584              : 
     585              :     /// Open a TCP-tunnel listener on 127.0.0.1:`localPort` (0 = pick a free
     586              :     /// port) that forwards each accepted connection to `serviceId` on
     587              :     /// `peerUri`'s `deviceId`. Returns a tunnel id, or an empty string on
     588              :     /// failure. `serviceName` is purely informational.
     589              :     std::string openServiceTunnel(const std::string& peerUri,
     590              :                                   const std::string& deviceId,
     591              :                                   const std::string& serviceId,
     592              :                                   const std::string& serviceName,
     593              :                                   uint16_t localPort);
     594              : 
     595              :     /// Close a tunnel previously created with openServiceTunnel.
     596              :     bool closeServiceTunnel(const std::string& tunnelId);
     597              : 
     598              :     /// Server-side: shutdown every active inbound tunnel currently serving
     599              :     /// `serviceId`. Called when a local exposed service is removed or
     600              :     /// disabled so that already-established peer connections are torn down.
     601              :     void closeServerTunnelsForService(const std::string& serviceId);
     602              : 
     603              :     /// Snapshot of currently active client tunnels for this account.
     604              :     std::vector<std::map<std::string, std::string>> getActiveServiceTunnels() const;
     605              :     /**
     606              :      * Check if a Device is connected
     607              :      * @param deviceId
     608              :      * @return true if connected
     609              :      */
     610              :     bool isConnectedWith(const DeviceId& deviceId) const;
     611              : 
     612              :     /**
     613              :      * Send a presence note
     614              :      * @param note
     615              :      */
     616              :     void sendPresenceNote(const std::string& note);
     617              : 
     618              : private:
     619              :     NON_COPYABLE(JamiAccount);
     620              : 
     621              :     using clock = std::chrono::system_clock;
     622              :     using time_point = clock::time_point;
     623              : 
     624              :     /**
     625              :      * Private structures
     626              :      */
     627              :     struct PendingCall;
     628              :     struct PendingMessage;
     629              :     struct DiscoveredPeer;
     630              :     class SendMessageContext;
     631              : 
     632            0 :     inline std::string getProxyConfigKey() const
     633              :     {
     634            0 :         const auto& conf = config();
     635            0 :         return dht::InfoHash::get(conf.proxyServer + conf.proxyListUrl).toString();
     636              :     }
     637              : 
     638              :     void scheduleAccountReady() const;
     639              :     AccountManager::OnChangeCallback setupAccountCallbacks();
     640              : 
     641              :     void onContactAdded(const std::string& uri, bool confirmed);
     642              :     void onContactRemoved(const std::string& uri, bool banned);
     643              :     void onIncomingTrustRequest(const std::string& uri,
     644              :                                 const std::string& conversationId,
     645              :                                 const std::vector<uint8_t>& payload,
     646              :                                 time_t received);
     647              :     void onKnownDevicesChanged(const std::map<DeviceId, KnownDevice>& devices);
     648              :     void onConversationRequestAccepted(const std::string& conversationId, const std::string& deviceId);
     649              :     void onContactConfirmed(const std::string& uri, const std::string& convFromReq);
     650              : 
     651              :     void conversationNeedsSyncing(std::shared_ptr<SyncMsg>&& syncMsg);
     652              :     uint64_t conversationSendMessage(const std::string& uri,
     653              :                                      const DeviceId& device,
     654              :                                      const std::map<std::string, std::string>& msg,
     655              :                                      uint64_t token = 0);
     656              :     void onConversationNeedSocket(const std::string& convId,
     657              :                                   const std::string& deviceId,
     658              :                                   ChannelCb&& cb,
     659              :                                   const std::string& type,
     660              :                                   bool noNewSocket = false);
     661              :     void onConversationNeedSwarmSocket(const std::string& convId,
     662              :                                        const std::string& deviceId,
     663              :                                        ChannelCb&& cb,
     664              :                                        const std::string& type,
     665              :                                        bool noNewSocket = false);
     666              :     void conversationOneToOneReceive(const std::string& convId, const std::string& from);
     667              : 
     668              :     std::unique_ptr<AccountManager::AccountCredentials> buildAccountCredentials(
     669              :         const JamiAccountConfig& conf,
     670              :         const dht::crypto::Identity& id,
     671              :         const std::string& archive_password_scheme,
     672              :         const std::string& archive_password,
     673              :         const std::string& archive_path,
     674              :         bool& migrating,
     675              :         bool& hasPassword);
     676              : 
     677              :     void onAuthenticationSuccess(bool migrating,
     678              :                                  bool hasPassword,
     679              :                                  const AccountInfo& info,
     680              :                                  const std::map<std::string, std::string>& configMap,
     681              :                                  std::string&& receipt,
     682              :                                  std::vector<uint8_t>&& receiptSignature);
     683              : 
     684              :     static void onAuthenticationError(const std::weak_ptr<JamiAccount>& w,
     685              :                                       bool hadIdentity,
     686              :                                       bool migrating,
     687              :                                       std::string accountId,
     688              :                                       AccountManager::AuthError error,
     689              :                                       const std::string& message);
     690              : 
     691              :     void onPeerConnected(const std::string& peerId, bool connected);
     692              : 
     693              :     void doRegister_();
     694              : 
     695              :     void lookupRegisteredName(const std::string& regName, const NameDirectory::Response& response);
     696              :     dht::DhtRunner::Config initDhtConfig(const JamiAccountConfig& conf);
     697              :     dht::DhtRunner::Context initDhtContext();
     698              :     void onAccountDeviceFound(const std::shared_ptr<dht::crypto::Certificate>& crt);
     699              :     void onAccountDeviceAnnounced();
     700              :     bool onICERequest(const DeviceId& deviceId);
     701              :     bool onChannelRequest(const std::shared_ptr<dht::crypto::Certificate>& cert, const std::string& name);
     702              :     void onNewDeviceConnection(const std::shared_ptr<dht::crypto::Certificate>& cert);
     703              :     void onConnectionReady(const DeviceId& deviceId,
     704              :                            const std::string& name,
     705              :                            std::shared_ptr<dhtnet::ChannelSocket> channel);
     706              : 
     707          778 :     const dht::ValueType USER_PROFILE_TYPE = {9, "User profile", std::chrono::hours(24 * 7)};
     708              : 
     709              :     void startOutgoingCall(const std::shared_ptr<SIPCall>& call, const std::string& toUri);
     710              : 
     711              :     void onConnectedOutgoingCall(const std::shared_ptr<SIPCall>& call, const std::string& to_id, dhtnet::IpAddr target);
     712              : 
     713              :     /**
     714              :      * Start a SIP Call
     715              :      * @param call  The current call
     716              :      * @return true if all is correct
     717              :      */
     718              :     bool SIPStartCall(SIPCall& call, const dhtnet::IpAddr& target);
     719              : 
     720              :     /**
     721              :      * Update tracking info when buddy appears offline.
     722              :      */
     723              :     void onTrackedBuddyOffline(const std::string&);
     724              : 
     725              :     /**
     726              :      * Update tracking info when buddy appears offline.
     727              :      */
     728              :     void onTrackedBuddyOnline(const std::string&);
     729              : 
     730              :     /**
     731              :      * Maps require port via UPnP and other async ops
     732              :      */
     733              :     void registerAsyncOps();
     734              :     /**
     735              :      * Add port mapping callback function.
     736              :      */
     737              :     void onPortMappingAdded(uint16_t port_used, bool success);
     738              :     void forEachPendingCall(const DeviceId& deviceId, const std::function<void(const std::shared_ptr<SIPCall>&)>& cb);
     739              : 
     740              :     void loadAccount(const std::string& archive_password_scheme = {},
     741              :                      const std::string& archive_password = {},
     742              :                      const std::string& archive_path = {});
     743              : 
     744              :     std::vector<std::string> loadBootstrap() const;
     745              : 
     746              :     static std::pair<std::string, std::string> saveIdentity(const dht::crypto::Identity& id,
     747              :                                                             const std::filesystem::path& path,
     748              :                                                             const std::string& name);
     749              : 
     750              :     void replyToIncomingIceMsg(const std::shared_ptr<SIPCall>&,
     751              :                                const std::shared_ptr<IceTransport>&,
     752              :                                const std::shared_ptr<IceTransport>&,
     753              :                                const dht::IceCandidates&,
     754              :                                const std::shared_ptr<dht::crypto::Certificate>& from_cert,
     755              :                                const dht::InfoHash& from);
     756              : 
     757              :     void loadCachedUrl(const std::string& url,
     758              :                        const std::filesystem::path& cachePath,
     759              :                        const std::chrono::seconds& cacheDuration,
     760              :                        const std::function<void(const dht::http::Response& response)>& cb);
     761              : 
     762              :     std::string getDhtProxyServer(const std::string& serverList);
     763              :     void loadCachedProxyServer(std::function<void(const std::string&)> cb);
     764              : 
     765              :     void newOutgoingCallHelper(const std::shared_ptr<SIPCall>& call, const Uri& uri);
     766              :     std::shared_ptr<SIPCall> newSwarmOutgoingCallHelper(const Uri& uri, const std::vector<libjami::MediaMap>& mediaList);
     767              :     std::shared_ptr<SIPCall> createSubCall(const std::shared_ptr<SIPCall>& mainCall);
     768              : 
     769              :     std::filesystem::path cachePath_ {};
     770              :     std::filesystem::path dataPath_ {};
     771              : 
     772              :     mutable std::mutex registeredNameMutex_;
     773              :     std::string registeredName_;
     774              : 
     775          691 :     bool setRegisteredName(const std::string& name)
     776              :     {
     777          691 :         std::lock_guard<std::mutex> lock(registeredNameMutex_);
     778          691 :         if (registeredName_ != name) {
     779            1 :             registeredName_ = name;
     780            1 :             return true;
     781              :         }
     782          690 :         return false;
     783          691 :     }
     784         4604 :     std::string getRegisteredName() const
     785              :     {
     786         4604 :         std::lock_guard<std::mutex> lock(registeredNameMutex_);
     787         9208 :         return registeredName_;
     788         4604 :     }
     789              : 
     790              :     std::shared_ptr<dht::Logger> logger_;
     791              :     std::shared_ptr<dhtnet::tls::CertificateStore> certStore_;
     792              : 
     793              :     std::unique_ptr<class ServiceManager> serviceManager_;
     794              : 
     795              :     /// Per-request state for an in-flight queryPeerServices() call. Defined
     796              :     /// out-of-line in jamiaccount.cpp so callers don't need to pull in asio
     797              :     /// just to compile this header.
     798              :     struct PendingSvcQuery;
     799              : 
     800              :     /// Emit the terminal `PeerServicesReceived` signal for `requestId` with
     801              :     /// the given status (matching `libjami::ServiceSignal::PeerServicesStatus`)
     802              :     /// and JSON payload, then drop the request from the pending tables.
     803              :     void finalizeSvcQuery(uint32_t requestId, int status, const std::string& servicesJson);
     804              : 
     805              :     /// Build the JSON array describing `peerUri`'s cached services, tagging each
     806              :     /// entry's "available" flag from the presence system (a device is available
     807              :     /// when it is currently announced online). `forceAvailableDevice`, when
     808              :     /// non-null, is always flagged available -- used right after a device
     809              :     /// answers a discovery query, before its DHT presence announcement has been
     810              :     /// observed. Returns an empty string when the peer has no cached services.
     811              :     std::string buildPeerServicesJson(const std::string& peerUri, const DeviceId* forceAvailableDevice = nullptr);
     812              : 
     813              :     mutable std::mutex pendingSvcQueriesMtx_;
     814              :     std::map<uint32_t, std::shared_ptr<PendingSvcQuery>> pendingSvcQueries_;
     815              : 
     816              :     std::shared_ptr<dht::DhtRunner> dht_ {};
     817              :     std::shared_ptr<AccountManager> accountManager_;
     818              :     dht::crypto::Identity id_ {};
     819              : 
     820              :     std::shared_ptr<dht::DhtProxyServer> dhtProxyServer_;
     821              : 
     822              :     mutable std::mutex messageMutex_ {};
     823              :     std::map<dht::Value::Id, PendingMessage> sentMessages_;
     824              :     dhtnet::fileutils::IdList treatedMessages_;
     825              : 
     826              :     /* tracked buddies presence */
     827              :     std::unique_ptr<PresenceManager> presenceManager_;
     828              :     uint64_t presenceListenerToken_ {0};
     829              :     uint64_t svcPresenceListenerToken_ {0};
     830              : 
     831              :     std::atomic_int syncCnt_ {0};
     832              : 
     833              :     /**
     834              :      * DHT port actually used.
     835              :      * This holds the actual DHT port, which might different from the port
     836              :      * set in the configuration. This can be the case if UPnP is used.
     837              :      */
     838              :     in_port_t dhtPortUsed()
     839              :     {
     840              :         return (upnpCtrl_ and dhtUpnpMapping_.isValid()) ? dhtUpnpMapping_.getExternalPort() : config().dhtPort;
     841              :     }
     842              : 
     843              :     /* Current UPNP mapping */
     844              :     dhtnet::upnp::Mapping dhtUpnpMapping_ {dhtnet::upnp::PortType::UDP};
     845              : 
     846              :     /**
     847              :      * Proxy
     848              :      */
     849              :     std::string proxyServerCached_ {};
     850              : 
     851              :     /**
     852              :      * Optional: via_addr construct from received parameters
     853              :      */
     854              :     pjsip_host_port via_addr_ {};
     855              : 
     856              :     pjsip_transport* via_tp_ {nullptr};
     857              : 
     858              :     /** ConnectionManager is thread-safe.
     859              :      * The shared mutex protects the pointer while allowing
     860              :      * multiple threads to access the ConnectionManager concurrently  */
     861              :     mutable std::shared_mutex connManagerMtx_ {};
     862              :     std::unique_ptr<dhtnet::ConnectionManager> connectionManager_;
     863              : 
     864              :     virtual void updateUpnpController() override;
     865              : 
     866              :     std::mutex discoveryMapMtx_;
     867              :     std::shared_ptr<dht::PeerDiscovery> peerDiscovery_;
     868              :     std::map<dht::InfoHash, DiscoveredPeer> discoveredPeers_;
     869              :     std::map<std::string, std::string> discoveredPeerMap_;
     870              : 
     871              :     std::set<std::shared_ptr<dht::http::Request>> requests_;
     872              : 
     873              :     mutable std::mutex sipConnsMtx_ {};
     874              :     struct SipConnection
     875              :     {
     876              :         std::shared_ptr<SipTransport> transport;
     877              :         // Needs to keep track of that channel to access underlying ICE
     878              :         // information, as the SipTransport use a generic transport
     879              :         std::shared_ptr<dhtnet::ChannelSocket> channel;
     880              :     };
     881              :     // NOTE: here we use a vector to avoid race conditions. In fact the contact
     882              :     // can ask for a SIP channel when we are creating a new SIP Channel with this
     883              :     // peer too.
     884              :     std::map<SipConnectionKey, std::vector<SipConnection>> sipConns_;
     885              : 
     886              :     std::mutex pendingCallsMutex_;
     887              :     std::map<DeviceId, std::vector<std::shared_ptr<SIPCall>>> pendingCalls_;
     888              : 
     889              :     std::mutex onConnectionClosedMtx_ {};
     890              :     std::map<DeviceId, std::function<void(const DeviceId&, bool)>> onConnectionClosed_ {};
     891              :     /**
     892              :      * onConnectionClosed contains callbacks that need to be called if a sub call is failing
     893              :      * @param deviceId      The device we are calling
     894              :      * @param eraseDummy    Erase the dummy call (a temporary subcall that must be stop when we will
     895              :      * not create new subcalls)
     896              :      */
     897              :     void callConnectionClosed(const DeviceId& deviceId, bool eraseDummy);
     898              : 
     899              :     /**
     900              :      * Ask a device to open a channeled SIP socket
     901              :      * @param peerId             The contact who owns the device
     902              :      * @param deviceId           The device to ask
     903              :      * @param forceNewConnection If we want a new SIP connection
     904              :      * @param pc                 A pending call to stop if the request fails
     905              :      * @note triggers cacheSIPConnection
     906              :      */
     907              :     void requestSIPConnection(const std::string& peerId,
     908              :                               const DeviceId& deviceId,
     909              :                               const std::string& connectionType,
     910              :                               bool forceNewConnection = false,
     911              :                               const std::shared_ptr<SIPCall>& pc = {});
     912              :     /**
     913              :      * Store a new SIP connection into sipConnections_
     914              :      * @param channel   The new sip channel
     915              :      * @param peerId    The contact who owns the device
     916              :      * @param deviceId  Device linked to that transport
     917              :      */
     918              :     void cacheSIPConnection(std::shared_ptr<dhtnet::ChannelSocket>&& channel,
     919              :                             const std::string& peerId,
     920              :                             const DeviceId& deviceId);
     921              :     /**
     922              :      * Shutdown a SIP connection
     923              :      * @param channel   The channel to close
     924              :      * @param peerId    The contact who owns the device
     925              :      * @param deviceId  Device linked to that transport
     926              :      */
     927              :     void shutdownSIPConnection(const std::shared_ptr<dhtnet::ChannelSocket>& channel,
     928              :                                const std::string& peerId,
     929              :                                const DeviceId& deviceId);
     930              : 
     931              :     void requestMessageConnection(const std::string& peerId,
     932              :                                   const DeviceId& deviceId,
     933              :                                   const std::string& connectionType);
     934              : 
     935              :     // File transfers
     936              :     std::mutex transfersMtx_ {};
     937              :     std::set<std::string> incomingFileTransfers_ {};
     938              : 
     939              :     void onMessageSent(
     940              :         const std::string& to, uint64_t id, const std::string& deviceId, bool success, bool onlyConnected, bool retry);
     941              : 
     942              :     std::mutex gitServersMtx_ {};
     943              :     std::map<dht::Value::Id, std::unique_ptr<GitServer>> gitServers_ {};
     944              : 
     945              :     //// File transfer (for profiles)
     946              :     std::shared_ptr<TransferManager> nonSwarmTransferManager_;
     947              : 
     948              :     std::atomic_bool deviceAnnounced_ {false};
     949              :     std::atomic_bool noSha3sumVerification_ {false};
     950              : 
     951              :     bool publishPresence_ {true};
     952              : 
     953              :     std::map<Uri::Scheme, std::unique_ptr<ChannelHandlerInterface>> channelHandlers_ {};
     954              : 
     955              :     std::unique_ptr<ConversationModule> convModule_;
     956              :     std::mutex moduleMtx_;
     957              :     std::unique_ptr<SyncModule> syncModule_;
     958              : 
     959              :     std::mutex rdvMtx_;
     960              : 
     961              :     int dhtBoundPort_ {0};
     962              : 
     963              :     void initConnectionManager();
     964              : 
     965              :     enum class PresenceState : int { DISCONNECTED = 0, AVAILABLE, CONNECTED };
     966              :     std::map<std::string, PresenceState> presenceState_;
     967              :     mutable std::mutex presenceStateMtx_;
     968              :     std::string presenceNote_;
     969              : };
     970              : 
     971              : static inline std::ostream&
     972              : operator<<(std::ostream& os, const JamiAccount& acc)
     973              : {
     974              :     os << "[Account " << acc.getAccountID() << "] ";
     975              :     return os;
     976              : }
     977              : 
     978              : } // namespace jami
        

Generated by: LCOV version 2.0-1