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