Line data Source code
1 : /* 2 : * Copyright (C) 2004-2024 Savoir-faire Linux Inc. 3 : * Author : Adrien BĂ©raud <adrien.beraud@savoirfairelinux.com> 4 : * 5 : * This program is free software; you can redistribute it and/or modify 6 : * it under the terms of the GNU General Public License as published by 7 : * the Free Software Foundation; either version 3 of the License, or 8 : * (at your option) any later version. 9 : * 10 : * This program is distributed in the hope that it will be useful, 11 : * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 : * GNU General Public License for more details. 14 : * 15 : * You should have received a copy of the GNU General Public License 16 : * along with this program. If not, see <https://www.gnu.org/licenses/>. 17 : */ 18 : #pragma once 19 : 20 : #include "string_utils.h" 21 : 22 : #include <opendht/infohash.h> 23 : #include <opendht/value.h> 24 : #include <opendht/default_types.h> 25 : 26 : #include <msgpack.hpp> 27 : #include <json/json.h> 28 : 29 : #include <map> 30 : #include <string> 31 : #include <ctime> 32 : #include <ciso646> 33 : 34 : namespace jami { 35 : 36 : struct Contact 37 : { 38 : /** Time of contact addition */ 39 : time_t added {0}; 40 : 41 : /** Time of contact removal */ 42 : time_t removed {0}; 43 : 44 : /** True if we got confirmation that this contact also added us */ 45 : bool confirmed {false}; 46 : 47 : /** True if the contact is banned (if not active) */ 48 : bool banned {false}; 49 : 50 : /** Non empty if a swarm is linked */ 51 : std::string conversationId {}; 52 : 53 : /** True if the contact is an active contact (not banned nor removed) */ 54 1065 : bool isActive() const { return added > removed; } 55 441 : bool isBanned() const { return not isActive() and banned; } 56 : 57 140 : Contact() = default; 58 3 : Contact(const Json::Value& json) 59 3 : { 60 3 : added = json["added"].asLargestUInt(); 61 3 : removed = json["removed"].asLargestUInt(); 62 3 : confirmed = json["confirmed"].asBool(); 63 3 : banned = json["banned"].asBool(); 64 3 : conversationId = json["conversationId"].asString(); 65 3 : } 66 : 67 : /** 68 : * Update this contact using other known contact information, 69 : * return true if contact state was changed. 70 : */ 71 49 : bool update(const Contact& c) 72 : { 73 49 : const auto copy = *this; 74 49 : if (c.added > added) { 75 0 : added = c.added; 76 0 : conversationId = c.conversationId; 77 : } 78 49 : if (c.removed > removed) { 79 1 : removed = c.removed; 80 1 : banned = c.banned; 81 : } 82 49 : if (c.confirmed != confirmed) { 83 3 : confirmed = c.confirmed or confirmed; 84 : } 85 49 : if (isActive()) { 86 46 : removed = 0; 87 46 : banned = 0; 88 : } 89 49 : if (c.isActive() and conversationId.empty() and not c.conversationId.empty()) { 90 0 : conversationId = c.conversationId; 91 : } 92 98 : return hasDifferentState(copy); 93 49 : } 94 49 : bool hasDifferentState(const Contact& other) const 95 : { 96 97 : return other.isActive() != isActive() or other.isBanned() != isBanned() 97 97 : or other.confirmed != confirmed; 98 : } 99 : 100 5 : Json::Value toJson() const 101 : { 102 5 : Json::Value json; 103 5 : json["added"] = Json::Int64(added); 104 5 : if (removed) { 105 0 : json["removed"] = Json::Int64(removed); 106 : } 107 5 : if (confirmed) 108 3 : json["confirmed"] = confirmed; 109 5 : if (banned) 110 0 : json["banned"] = banned; 111 5 : json["conversationId"] = conversationId; 112 5 : return json; 113 0 : } 114 : 115 297 : std::map<std::string, std::string> toMap() const 116 : { 117 297 : std::map<std::string, std::string> result {{"added", std::to_string(added)}, 118 594 : {"removed", std::to_string(removed)}, 119 2079 : {"conversationId", conversationId}}; 120 : 121 297 : if (isActive()) 122 262 : result.emplace("confirmed", confirmed ? TRUE_STR : FALSE_STR); 123 297 : if (isBanned()) 124 3 : result.emplace("banned", TRUE_STR); 125 : 126 297 : return result; 127 0 : } 128 : 129 402 : MSGPACK_DEFINE_MAP(added, removed, confirmed, banned, conversationId) 130 : }; 131 : 132 : struct TrustRequest 133 : { 134 : std::shared_ptr<dht::crypto::PublicKey> device; 135 : std::string conversationId; 136 : time_t received; 137 : std::vector<uint8_t> payload; 138 72 : MSGPACK_DEFINE_MAP(device, conversationId, received, payload) 139 : }; 140 : 141 : struct DeviceAnnouncement : public dht::SignedValue<DeviceAnnouncement> 142 : { 143 : private: 144 : using BaseClass = dht::SignedValue<DeviceAnnouncement>; 145 : 146 : public: 147 : static const constexpr dht::ValueType& TYPE = dht::ValueType::USER_DATA; 148 : dht::InfoHash dev; 149 : std::shared_ptr<dht::crypto::PublicKey> pk; 150 5664 : MSGPACK_DEFINE_MAP(dev, pk) 151 : }; 152 : 153 : struct KnownDeviceSync { 154 : std::string name; 155 : dht::InfoHash sha1; 156 915 : MSGPACK_DEFINE_MAP(name, sha1) 157 : }; 158 : 159 : struct DeviceSync : public dht::EncryptedValue<DeviceSync> 160 : { 161 : static const constexpr dht::ValueType& TYPE = dht::ValueType::USER_DATA; 162 : uint64_t date; 163 : std::string device_name; 164 : std::map<dht::InfoHash, std::string> devices_known; // Legacy 165 : std::map<dht::PkId, KnownDeviceSync> devices; 166 : std::map<dht::InfoHash, Contact> peers; 167 : std::map<dht::InfoHash, TrustRequest> trust_requests; 168 872 : MSGPACK_DEFINE_MAP(date, device_name, devices_known, devices, peers, trust_requests) 169 : }; 170 : 171 : struct KnownDevice 172 : { 173 : using clock = std::chrono::system_clock; 174 : using time_point = clock::time_point; 175 : 176 : /** Device certificate */ 177 : std::shared_ptr<dht::crypto::Certificate> certificate; 178 : 179 : /** Device name */ 180 : std::string name {}; 181 : 182 : /** Time of last received device sync */ 183 : time_point last_sync {time_point::min()}; 184 : 185 3723 : KnownDevice(const std::shared_ptr<dht::crypto::Certificate>& cert, 186 : const std::string& n = {}, 187 : time_point sync = time_point::min()) 188 3723 : : certificate(cert) 189 3723 : , name(n) 190 3723 : , last_sync(sync) 191 3723 : {} 192 : }; 193 : 194 : } // namespace jami