LCOV - code coverage report
Current view: top level - src/jamidht - jami_contact.h (source / functions) Hit Total Coverage
Test: jami-coverage-filtered.info Lines: 58 63 92.1 %
Date: 2024-11-13 09:04:27 Functions: 21 21 100.0 %

          Line data    Source code
       1             : /*
       2             :  *  Copyright (C) 2004-2024 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 "string_utils.h"
      20             : 
      21             : #include <opendht/infohash.h>
      22             : #include <opendht/value.h>
      23             : #include <opendht/default_types.h>
      24             : 
      25             : #include <msgpack.hpp>
      26             : #include <json/json.h>
      27             : 
      28             : #include <map>
      29             : #include <string>
      30             : #include <ctime>
      31             : #include <ciso646>
      32             : 
      33             : namespace jami {
      34             : 
      35             : struct Contact
      36             : {
      37             :     /** Time of contact addition */
      38             :     time_t added {0};
      39             : 
      40             :     /** Time of contact removal */
      41             :     time_t removed {0};
      42             : 
      43             :     /** True if we got confirmation that this contact also added us */
      44             :     bool confirmed {false};
      45             : 
      46             :     /** True if the contact is banned (if not active) */
      47             :     bool banned {false};
      48             : 
      49             :     /** Non empty if a swarm is linked */
      50             :     std::string conversationId {};
      51             : 
      52             :     /** True if the contact is an active contact (not banned nor removed) */
      53        1428 :     bool isActive() const { return added > removed; }
      54         598 :     bool isBanned() const { return not isActive() and banned; }
      55             : 
      56         171 :     Contact() = default;
      57           3 :     Contact(const Json::Value& json)
      58           3 :     {
      59           3 :         added = json["added"].asLargestUInt();
      60           3 :         removed = json["removed"].asLargestUInt();
      61           3 :         confirmed = json["confirmed"].asBool();
      62           3 :         banned = json["banned"].asBool();
      63           3 :         conversationId = json["conversationId"].asString();
      64           3 :     }
      65             : 
      66             :     /**
      67             :      * Update this contact using other known contact information,
      68             :      * return true if contact state was changed.
      69             :      */
      70          54 :     bool update(const Contact& c)
      71             :     {
      72          54 :         const auto copy = *this;
      73          54 :         if (c.added > added) {
      74           2 :             added = c.added;
      75           2 :             conversationId = c.conversationId;
      76             :         }
      77          54 :         if (c.removed > removed) {
      78           3 :             removed = c.removed;
      79           3 :             banned = c.banned;
      80             :         }
      81          54 :         if (c.confirmed != confirmed) {
      82           7 :             confirmed = c.confirmed or confirmed;
      83             :         }
      84          54 :         if (isActive()) {
      85          42 :             removed = 0;
      86          42 :             banned = 0;
      87             :         }
      88          54 :         if (c.isActive() and conversationId.empty() and not c.conversationId.empty()) {
      89           0 :             conversationId = c.conversationId;
      90             :         }
      91         108 :         return hasDifferentState(copy);
      92          54 :     }
      93          54 :     bool hasDifferentState(const Contact& other) const
      94             :     {
      95         103 :         return other.isActive() != isActive() or other.isBanned() != isBanned()
      96         103 :                or other.confirmed != confirmed;
      97             :     }
      98             : 
      99           5 :     Json::Value toJson() const
     100             :     {
     101           5 :         Json::Value json;
     102           5 :         json["added"] = Json::Int64(added);
     103           5 :         if (removed) {
     104           0 :             json["removed"] = Json::Int64(removed);
     105             :         }
     106           5 :         if (confirmed)
     107           3 :             json["confirmed"] = confirmed;
     108           5 :         if (banned)
     109           0 :             json["banned"] = banned;
     110           5 :         json["conversationId"] = conversationId;
     111           5 :         return json;
     112           0 :     }
     113             : 
     114         440 :     std::map<std::string, std::string> toMap() const
     115             :     {
     116         440 :         std::map<std::string, std::string> result {{"added", std::to_string(added)},
     117         880 :                                                    {"removed", std::to_string(removed)},
     118        3078 :                                                    {"conversationId", conversationId}};
     119             : 
     120         440 :         if (isActive())
     121         385 :             result.emplace("confirmed", confirmed ? TRUE_STR : FALSE_STR);
     122         439 :         if (isBanned())
     123           9 :             result.emplace("banned", TRUE_STR);
     124             : 
     125         439 :         return result;
     126           0 :     }
     127             : 
     128         449 :     MSGPACK_DEFINE_MAP(added, removed, confirmed, banned, conversationId)
     129             : };
     130             : 
     131             : struct TrustRequest
     132             : {
     133             :     std::shared_ptr<dht::crypto::PublicKey> device;
     134             :     std::string conversationId;
     135             :     time_t received;
     136             :     std::vector<uint8_t> payload;
     137          94 :     MSGPACK_DEFINE_MAP(device, conversationId, received, payload)
     138             : };
     139             : 
     140             : struct DeviceAnnouncement : public dht::SignedValue<DeviceAnnouncement>
     141             : {
     142             : private:
     143             :     using BaseClass = dht::SignedValue<DeviceAnnouncement>;
     144             : 
     145             : public:
     146             :     static const constexpr dht::ValueType& TYPE = dht::ValueType::USER_DATA;
     147             :     dht::InfoHash dev;
     148             :     std::shared_ptr<dht::crypto::PublicKey> pk;
     149       18028 :     MSGPACK_DEFINE_MAP(dev, pk)
     150             : };
     151             : 
     152             : struct KnownDeviceSync {
     153             :     std::string name;
     154             :     dht::InfoHash sha1;
     155         862 :     MSGPACK_DEFINE_MAP(name, sha1)
     156             : };
     157             : 
     158             : struct DeviceSync : public dht::EncryptedValue<DeviceSync>
     159             : {
     160             :     static const constexpr dht::ValueType& TYPE = dht::ValueType::USER_DATA;
     161             :     uint64_t date;
     162             :     std::string device_name;
     163             :     std::map<dht::InfoHash, std::string> devices_known; // Legacy
     164             :     std::map<dht::PkId, KnownDeviceSync> devices;
     165             :     std::map<dht::InfoHash, Contact> peers;
     166             :     std::map<dht::InfoHash, TrustRequest> trust_requests;
     167         806 :     MSGPACK_DEFINE_MAP(date, device_name, devices_known, devices, peers, trust_requests)
     168             : };
     169             : 
     170             : struct KnownDevice
     171             : {
     172             :     using clock = std::chrono::system_clock;
     173             :     using time_point = clock::time_point;
     174             : 
     175             :     /** Device certificate */
     176             :     std::shared_ptr<dht::crypto::Certificate> certificate;
     177             : 
     178             :     /** Device name */
     179             :     std::string name {};
     180             : 
     181             :     /** Time of last received device sync */
     182             :     time_point last_sync {time_point::min()};
     183             : 
     184        4004 :     KnownDevice(const std::shared_ptr<dht::crypto::Certificate>& cert,
     185             :                 const std::string& n = {},
     186             :                 time_point sync = time_point::min())
     187        4004 :         : certificate(cert)
     188        4004 :         , name(n)
     189        4004 :         , last_sync(sync)
     190        4004 :     {}
     191             : };
     192             : 
     193             : } // namespace jami

Generated by: LCOV version 1.14