Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
Varun Patil | b3d03b9 | 2021-01-08 17:09:32 +0530 | [diff] [blame] | 3 | * Copyright (c) 2013-2021, Regents of the University of California |
| 4 | * Yingdi Yu |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 5 | * |
| 6 | * BSD license, See the LICENSE file for more information |
| 7 | * |
| 8 | * Author: Yingdi Yu <yingdi@cs.ucla.edu> |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 9 | * Author: Qiuhan Ding <qiuhanding@cs.ucla.edu> |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 10 | */ |
| 11 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 12 | #include "contact-storage.hpp" |
Davide Pesavento | 7676b56 | 2020-12-14 00:41:26 -0500 | [diff] [blame] | 13 | |
| 14 | #include <ndn-cxx/security/transform/buffer-source.hpp> |
| 15 | #include <ndn-cxx/security/transform/digest-filter.hpp> |
| 16 | #include <ndn-cxx/security/transform/hex-encode.hpp> |
| 17 | #include <ndn-cxx/security/transform/stream-sink.hpp> |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 18 | |
Varun Patil | a24bd3e | 2020-11-24 10:08:33 +0530 | [diff] [blame] | 19 | #include <boost/filesystem.hpp> |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 20 | |
Yingdi Yu | eb692ac | 2015-02-10 18:46:18 -0800 | [diff] [blame] | 21 | namespace chronochat { |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 22 | |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 23 | namespace fs = boost::filesystem; |
| 24 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 25 | using std::string; |
| 26 | using std::vector; |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 27 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 28 | // user's own profile; |
| 29 | const string INIT_SP_TABLE = |
| 30 | "CREATE TABLE IF NOT EXISTS " |
| 31 | " SelfProfile( " |
| 32 | " profile_type BLOB NOT NULL, " |
| 33 | " profile_value BLOB NOT NULL, " |
| 34 | " PRIMARY KEY (profile_type) " |
| 35 | " ); " |
| 36 | "CREATE INDEX sp_index ON SelfProfile(profile_type); "; |
Yingdi Yu | 17032f8 | 2014-03-25 15:48:23 -0700 | [diff] [blame] | 37 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 38 | // user's self endorse cert; |
| 39 | const string INIT_SE_TABLE = |
| 40 | "CREATE TABLE IF NOT EXISTS " |
| 41 | " SelfEndorse( " |
| 42 | " identity BLOB NOT NULL UNIQUE, " |
| 43 | " endorse_data BLOB NOT NULL, " |
| 44 | " PRIMARY KEY (identity) " |
| 45 | " ); " |
| 46 | "CREATE INDEX se_index ON SelfEndorse(identity); "; |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 47 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 48 | // contact's basic info |
| 49 | const string INIT_CONTACT_TABLE = |
| 50 | "CREATE TABLE IF NOT EXISTS " |
| 51 | " Contact( " |
| 52 | " contact_namespace BLOB NOT NULL, " |
| 53 | " contact_alias BLOB NOT NULL, " |
| 54 | " contact_keyName BLOB NOT NULL, " |
| 55 | " contact_key BLOB NOT NULL, " |
| 56 | " notBefore INTEGER DEFAULT 0, " |
| 57 | " notAfter INTEGER DEFAULT 0, " |
| 58 | " is_introducer INTEGER DEFAULT 0, " |
| 59 | " PRIMARY KEY (contact_namespace) " |
| 60 | " ); " |
| 61 | "CREATE INDEX contact_index ON Contact(contact_namespace); "; |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 62 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 63 | // contact's trust scope; |
| 64 | const string INIT_TS_TABLE = |
| 65 | "CREATE TABLE IF NOT EXISTS " |
| 66 | " TrustScope( " |
| 67 | " id INTEGER PRIMARY KEY AUTOINCREMENT, " |
| 68 | " contact_namespace BLOB NOT NULL, " |
| 69 | " trust_scope BLOB NOT NULL " |
| 70 | " ); " |
| 71 | "CREATE INDEX ts_index ON TrustScope(contact_namespace); "; |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 72 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 73 | // contact's profile |
| 74 | const string INIT_CP_TABLE = |
| 75 | "CREATE TABLE IF NOT EXISTS " |
| 76 | " ContactProfile( " |
| 77 | " profile_identity BLOB NOT NULL, " |
| 78 | " profile_type BLOB NOT NULL, " |
| 79 | " profile_value BLOB NOT NULL, " |
| 80 | " endorse INTEGER NOT NULL, " |
| 81 | " PRIMARY KEY (profile_identity, profile_type) " |
| 82 | " ); " |
| 83 | "CREATE INDEX cp_index ON ContactProfile(profile_identity); "; |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 84 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 85 | // user's endorsement on contacts |
| 86 | const string INIT_PE_TABLE = |
| 87 | "CREATE TABLE IF NOT EXISTS " |
| 88 | " ProfileEndorse( " |
| 89 | " identity BLOB NOT NULL UNIQUE, " |
| 90 | " endorse_data BLOB NOT NULL, " |
| 91 | " PRIMARY KEY (identity) " |
| 92 | " ); " |
| 93 | "CREATE INDEX pe_index ON ProfileEndorse(identity); "; |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 94 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 95 | // contact's endorsements on the user |
| 96 | const string INIT_CE_TABLE = |
| 97 | "CREATE TABLE IF NOT EXISTS " |
| 98 | " CollectEndorse( " |
| 99 | " endorser BLOB NOT NULL, " |
| 100 | " endorse_name BLOB NOT NULL, " |
| 101 | " endorse_data BLOB NOT NULL, " |
| 102 | " PRIMARY KEY (endorser) " |
| 103 | " ); "; |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 104 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 105 | // dns data; |
| 106 | const string INIT_DD_TABLE = |
| 107 | "CREATE TABLE IF NOT EXISTS " |
| 108 | " DnsData( " |
| 109 | " dns_name BLOB NOT NULL, " |
| 110 | " dns_type BLOB NOT NULL, " |
| 111 | " data_name BLOB NOT NULL, " |
| 112 | " dns_value BLOB NOT NULL, " |
| 113 | " PRIMARY KEY (dns_name, dns_type) " |
| 114 | " ); " |
| 115 | "CREATE INDEX dd_index ON DnsData(dns_name, dns_type); " |
| 116 | "CREATE INDEX dd_index2 ON DnsData(data_name); "; |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 117 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 118 | /** |
| 119 | * A utility function to call the normal sqlite3_bind_text where the value and length are |
| 120 | * value.c_str() and value.size(). |
| 121 | */ |
| 122 | static int |
| 123 | sqlite3_bind_string(sqlite3_stmt* statement, |
| 124 | int index, |
| 125 | const string& value, |
| 126 | void(*destructor)(void*)) |
| 127 | { |
| 128 | return sqlite3_bind_text(statement, index, value.c_str(), value.size(), destructor); |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * A utility function to call the normal sqlite3_bind_blob where the value and length are |
| 133 | * block.wire() and block.size(). |
| 134 | */ |
| 135 | static int |
| 136 | sqlite3_bind_block(sqlite3_stmt* statement, |
| 137 | int index, |
| 138 | const Block& block, |
| 139 | void(*destructor)(void*)) |
| 140 | { |
| 141 | return sqlite3_bind_blob(statement, index, block.wire(), block.size(), destructor); |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * A utility function to generate string by calling the normal sqlite3_column_text. |
| 146 | */ |
| 147 | static string |
| 148 | sqlite3_column_string(sqlite3_stmt* statement, int column) |
| 149 | { |
| 150 | return string(reinterpret_cast<const char*>(sqlite3_column_text(statement, column)), |
| 151 | sqlite3_column_bytes(statement, column)); |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * A utility function to generate block by calling the normal sqlite3_column_text. |
| 156 | */ |
| 157 | static Block |
| 158 | sqlite3_column_block(sqlite3_stmt* statement, int column) |
| 159 | { |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 160 | return Block(reinterpret_cast<const uint8_t*>(sqlite3_column_blob(statement, column)), |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 161 | sqlite3_column_bytes(statement, column)); |
| 162 | } |
| 163 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 164 | |
| 165 | ContactStorage::ContactStorage(const Name& identity) |
| 166 | : m_identity(identity) |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 167 | { |
| 168 | fs::path chronosDir = fs::path(getenv("HOME")) / ".chronos"; |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 169 | fs::create_directories(chronosDir); |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 170 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 171 | int res = sqlite3_open((chronosDir / getDBName()).c_str(), &m_db); |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 172 | if (res != SQLITE_OK) |
Varun Patil | a24bd3e | 2020-11-24 10:08:33 +0530 | [diff] [blame] | 173 | NDN_THROW(Error("chronochat DB cannot be open/created")); |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 174 | |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 175 | initializeTable("SelfProfile", INIT_SP_TABLE); |
| 176 | initializeTable("SelfEndorse", INIT_SE_TABLE); |
| 177 | initializeTable("Contact", INIT_CONTACT_TABLE); |
| 178 | initializeTable("TrustScope", INIT_TS_TABLE); |
| 179 | initializeTable("ContactProfile", INIT_CP_TABLE); |
| 180 | initializeTable("ProfileEndorse", INIT_PE_TABLE); |
| 181 | initializeTable("CollectEndorse", INIT_CE_TABLE); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 182 | initializeTable("DnsData", INIT_DD_TABLE); |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 183 | |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 184 | } |
| 185 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 186 | string |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 187 | ContactStorage::getDBName() |
| 188 | { |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 189 | string dbName("chronos-"); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 190 | |
Davide Pesavento | 7676b56 | 2020-12-14 00:41:26 -0500 | [diff] [blame] | 191 | std::ostringstream ss; |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 192 | { |
Davide Pesavento | 7676b56 | 2020-12-14 00:41:26 -0500 | [diff] [blame] | 193 | using namespace ndn::security::transform; |
| 194 | bufferSource(m_identity.wireEncode().wire(), m_identity.wireEncode().size()) |
| 195 | >> digestFilter(ndn::DigestAlgorithm::SHA256) |
| 196 | >> hexEncode(false) |
| 197 | >> streamSink(ss); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 198 | } |
| 199 | dbName.append(ss.str()).append(".db"); |
| 200 | |
| 201 | return dbName; |
| 202 | } |
| 203 | |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 204 | void |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 205 | ContactStorage::initializeTable(const string& tableName, const string& sqlCreateStmt) |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 206 | { |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 207 | sqlite3_stmt *stmt; |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 208 | sqlite3_prepare_v2(m_db, |
| 209 | "SELECT name FROM sqlite_master WHERE type='table' And name=?", |
Davide Pesavento | 7676b56 | 2020-12-14 00:41:26 -0500 | [diff] [blame] | 210 | -1, &stmt, nullptr); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 211 | sqlite3_bind_string(stmt, 1, tableName, SQLITE_TRANSIENT); |
| 212 | int res = sqlite3_step(stmt); |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 213 | |
| 214 | bool tableExist = false; |
| 215 | if (res == SQLITE_ROW) |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 216 | tableExist = true; |
| 217 | sqlite3_finalize(stmt); |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 218 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 219 | if (!tableExist) { |
Davide Pesavento | 7676b56 | 2020-12-14 00:41:26 -0500 | [diff] [blame] | 220 | char *errmsg = nullptr; |
Varun Patil | a24bd3e | 2020-11-24 10:08:33 +0530 | [diff] [blame] | 221 | res = sqlite3_exec(m_db, sqlCreateStmt.c_str (), nullptr, nullptr, &errmsg); |
Davide Pesavento | 7676b56 | 2020-12-14 00:41:26 -0500 | [diff] [blame] | 222 | if (res != SQLITE_OK && errmsg != nullptr) |
Varun Patil | a24bd3e | 2020-11-24 10:08:33 +0530 | [diff] [blame] | 223 | NDN_THROW(Error("Init \"error\" in " + tableName)); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 224 | } |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 225 | } |
| 226 | |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 227 | shared_ptr<Profile> |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 228 | ContactStorage::getSelfProfile() |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 229 | { |
Varun Patil | b3d03b9 | 2021-01-08 17:09:32 +0530 | [diff] [blame] | 230 | auto profile = std::make_shared<Profile>(m_identity); |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 231 | sqlite3_stmt *stmt; |
Davide Pesavento | 7676b56 | 2020-12-14 00:41:26 -0500 | [diff] [blame] | 232 | sqlite3_prepare_v2(m_db, "SELECT profile_type, profile_value FROM SelfProfile", |
| 233 | -1, &stmt, nullptr); |
Yingdi Yu | 2ac40fb | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 234 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 235 | while (sqlite3_step(stmt) == SQLITE_ROW) { |
| 236 | string profileType = sqlite3_column_string(stmt, 0); |
| 237 | string profileValue = sqlite3_column_string (stmt, 1); |
| 238 | (*profile)[profileType] = profileValue; |
| 239 | } |
Yingdi Yu | 72781e5 | 2013-11-06 23:00:21 -0800 | [diff] [blame] | 240 | sqlite3_finalize(stmt); |
| 241 | |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 242 | return profile; |
| 243 | } |
Yingdi Yu | ae8217c | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 244 | |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 245 | void |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 246 | ContactStorage::addSelfEndorseCertificate(const EndorseCertificate& newEndorseCertificate) |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 247 | { |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 248 | sqlite3_stmt *stmt; |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 249 | sqlite3_prepare_v2(m_db, |
| 250 | "INSERT OR REPLACE INTO SelfEndorse (identity, endorse_data) values (?, ?)", |
Davide Pesavento | 7676b56 | 2020-12-14 00:41:26 -0500 | [diff] [blame] | 251 | -1, &stmt, nullptr); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 252 | sqlite3_bind_string(stmt, 1, m_identity.toUri(), SQLITE_TRANSIENT); |
| 253 | sqlite3_bind_block(stmt, 2, newEndorseCertificate.wireEncode(), SQLITE_TRANSIENT); |
Yingdi Yu | 4212586 | 2014-08-07 17:04:28 -0700 | [diff] [blame] | 254 | sqlite3_step(stmt); |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 255 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 256 | sqlite3_finalize(stmt); |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 257 | } |
| 258 | |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 259 | shared_ptr<EndorseCertificate> |
| 260 | ContactStorage::getSelfEndorseCertificate() |
| 261 | { |
| 262 | shared_ptr<EndorseCertificate> cert; |
| 263 | |
| 264 | sqlite3_stmt *stmt; |
| 265 | sqlite3_prepare_v2(m_db, |
| 266 | "SELECT endorse_data FROM SelfEndorse where identity=?", |
Davide Pesavento | 7676b56 | 2020-12-14 00:41:26 -0500 | [diff] [blame] | 267 | -1, &stmt, nullptr); |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 268 | sqlite3_bind_string(stmt, 1, m_identity.toUri(), SQLITE_TRANSIENT); |
| 269 | |
| 270 | if (sqlite3_step(stmt) == SQLITE_ROW) { |
Varun Patil | b3d03b9 | 2021-01-08 17:09:32 +0530 | [diff] [blame] | 271 | cert = std::make_shared<EndorseCertificate>(); |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 272 | cert->wireDecode(sqlite3_column_block(stmt, 0)); |
| 273 | } |
| 274 | |
| 275 | sqlite3_finalize(stmt); |
| 276 | |
| 277 | return cert; |
| 278 | } |
| 279 | |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 280 | void |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 281 | ContactStorage::addEndorseCertificate(const EndorseCertificate& endorseCertificate, |
| 282 | const Name& identity) |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 283 | { |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 284 | sqlite3_stmt *stmt; |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 285 | sqlite3_prepare_v2(m_db, |
| 286 | "INSERT OR REPLACE INTO ProfileEndorse \ |
| 287 | (identity, endorse_data) values (?, ?)", |
Davide Pesavento | 7676b56 | 2020-12-14 00:41:26 -0500 | [diff] [blame] | 288 | -1, &stmt, nullptr); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 289 | sqlite3_bind_string(stmt, 1, identity.toUri(), SQLITE_TRANSIENT); |
| 290 | sqlite3_bind_block(stmt, 2, endorseCertificate.wireEncode(), SQLITE_TRANSIENT); |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 291 | sqlite3_step(stmt); |
| 292 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 293 | sqlite3_finalize(stmt); |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | void |
| 297 | ContactStorage::updateCollectEndorse(const EndorseCertificate& endorseCertificate) |
| 298 | { |
| 299 | Name endorserName = endorseCertificate.getSigner(); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 300 | Name certName = endorseCertificate.getName(); |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 301 | |
| 302 | sqlite3_stmt *stmt; |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 303 | sqlite3_prepare_v2(m_db, |
| 304 | "INSERT OR REPLACE INTO CollectEndorse \ |
| 305 | (endorser, endorse_name, endorse_data) \ |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 306 | VALUES (?, ?, ?)", |
Davide Pesavento | 7676b56 | 2020-12-14 00:41:26 -0500 | [diff] [blame] | 307 | -1, &stmt, nullptr); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 308 | sqlite3_bind_string(stmt, 1, endorserName.toUri(), SQLITE_TRANSIENT); |
| 309 | sqlite3_bind_string(stmt, 2, certName.toUri(), SQLITE_TRANSIENT); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 310 | sqlite3_bind_block(stmt, 3, endorseCertificate.wireEncode(), SQLITE_TRANSIENT); |
Yingdi Yu | 4212586 | 2014-08-07 17:04:28 -0700 | [diff] [blame] | 311 | sqlite3_step(stmt); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 312 | sqlite3_finalize(stmt); |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 313 | return; |
| 314 | } |
| 315 | |
| 316 | void |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 317 | ContactStorage::getCollectEndorse(EndorseCollection& endorseCollection) |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 318 | { |
| 319 | sqlite3_stmt *stmt; |
Davide Pesavento | 7676b56 | 2020-12-14 00:41:26 -0500 | [diff] [blame] | 320 | sqlite3_prepare_v2(m_db, "SELECT endorse_name, endorse_data FROM CollectEndorse", |
| 321 | -1, &stmt, nullptr); |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 322 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 323 | while (sqlite3_step(stmt) == SQLITE_ROW) { |
| 324 | string certName = sqlite3_column_string(stmt, 0); |
Davide Pesavento | 7676b56 | 2020-12-14 00:41:26 -0500 | [diff] [blame] | 325 | std::ostringstream ss; |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 326 | { |
Davide Pesavento | 7676b56 | 2020-12-14 00:41:26 -0500 | [diff] [blame] | 327 | using namespace ndn::security::transform; |
| 328 | bufferSource(sqlite3_column_text(stmt, 1), sqlite3_column_bytes(stmt, 1)) |
| 329 | >> digestFilter(ndn::DigestAlgorithm::SHA256) |
| 330 | >> streamSink(ss); |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 331 | } |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 332 | endorseCollection.addCollectionEntry(Name(certName), ss.str()); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 333 | } |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 334 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 335 | sqlite3_finalize(stmt); |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 336 | } |
| 337 | |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 338 | shared_ptr<EndorseCertificate> |
| 339 | ContactStorage::getCollectEndorseByName(const Name& name) |
| 340 | { |
| 341 | shared_ptr<EndorseCertificate> cert; |
| 342 | |
| 343 | sqlite3_stmt *stmt; |
| 344 | sqlite3_prepare_v2(m_db, |
| 345 | "SELECT endorse_name, endorse_data FROM CollectEndorse where endorse_name=?", |
Davide Pesavento | 7676b56 | 2020-12-14 00:41:26 -0500 | [diff] [blame] | 346 | -1, &stmt, nullptr); |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 347 | sqlite3_bind_string(stmt, 1, name.toUri(), SQLITE_TRANSIENT); |
| 348 | |
| 349 | if (sqlite3_step(stmt) == SQLITE_ROW) { |
Varun Patil | b3d03b9 | 2021-01-08 17:09:32 +0530 | [diff] [blame] | 350 | cert = std::make_shared<EndorseCertificate>(); |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 351 | cert->wireDecode(sqlite3_column_block(stmt, 1)); |
| 352 | } |
| 353 | |
| 354 | sqlite3_finalize(stmt); |
| 355 | |
| 356 | return cert; |
| 357 | } |
| 358 | |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 359 | void |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 360 | ContactStorage::getEndorseList(const Name& identity, vector<string>& endorseList) |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 361 | { |
| 362 | sqlite3_stmt *stmt; |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 363 | sqlite3_prepare_v2(m_db, |
| 364 | "SELECT profile_type FROM ContactProfile \ |
| 365 | WHERE profile_identity=? AND endorse=1 ORDER BY profile_type", |
Davide Pesavento | 7676b56 | 2020-12-14 00:41:26 -0500 | [diff] [blame] | 366 | -1, &stmt, nullptr); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 367 | sqlite3_bind_string(stmt, 1, identity.toUri(), SQLITE_TRANSIENT); |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 368 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 369 | while (sqlite3_step(stmt) == SQLITE_ROW) { |
| 370 | string profileType = sqlite3_column_string(stmt, 0); |
| 371 | endorseList.push_back(profileType); |
| 372 | } |
| 373 | sqlite3_finalize(stmt); |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 377 | void |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 378 | ContactStorage::removeContact(const Name& identityName) |
Yingdi Yu | ae8217c | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 379 | { |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 380 | string identity = identityName.toUri(); |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 381 | |
| 382 | sqlite3_stmt *stmt; |
Davide Pesavento | 7676b56 | 2020-12-14 00:41:26 -0500 | [diff] [blame] | 383 | sqlite3_prepare_v2(m_db, "DELETE FROM Contact WHERE contact_namespace=?", -1, &stmt, nullptr); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 384 | sqlite3_bind_string(stmt, 1, identity, SQLITE_TRANSIENT); |
Yingdi Yu | 4212586 | 2014-08-07 17:04:28 -0700 | [diff] [blame] | 385 | sqlite3_step(stmt); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 386 | sqlite3_finalize(stmt); |
Yingdi Yu | ae8217c | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 387 | |
Davide Pesavento | 7676b56 | 2020-12-14 00:41:26 -0500 | [diff] [blame] | 388 | sqlite3_prepare_v2(m_db, "DELETE FROM ContactProfile WHERE profile_identity=?", -1, &stmt, nullptr); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 389 | sqlite3_bind_string(stmt, 1, identity, SQLITE_TRANSIENT); |
Yingdi Yu | 4212586 | 2014-08-07 17:04:28 -0700 | [diff] [blame] | 390 | sqlite3_step(stmt); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 391 | sqlite3_finalize(stmt); |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 392 | |
Davide Pesavento | 7676b56 | 2020-12-14 00:41:26 -0500 | [diff] [blame] | 393 | sqlite3_prepare_v2(m_db, "DELETE FROM TrustScope WHERE contact_namespace=?", -1, &stmt, nullptr); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 394 | sqlite3_bind_string(stmt, 1, identity, SQLITE_TRANSIENT); |
Yingdi Yu | 4212586 | 2014-08-07 17:04:28 -0700 | [diff] [blame] | 395 | sqlite3_step(stmt); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 396 | sqlite3_finalize(stmt); |
Yingdi Yu | ae8217c | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 397 | } |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 398 | |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 399 | void |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 400 | ContactStorage::addContact(const Contact& contact) |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 401 | { |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 402 | if (doesContactExist(contact.getNameSpace())) |
Varun Patil | a24bd3e | 2020-11-24 10:08:33 +0530 | [diff] [blame] | 403 | NDN_THROW(Error("Normal Contact has already existed")); |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 404 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 405 | string identity = contact.getNameSpace().toUri(); |
Yingdi Yu | 71c0187 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 406 | bool isIntroducer = contact.isIntroducer(); |
| 407 | |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 408 | sqlite3_stmt *stmt; |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 409 | sqlite3_prepare_v2(m_db, |
| 410 | "INSERT INTO Contact (contact_namespace, contact_alias, contact_keyName, \ |
| 411 | contact_key, notBefore, notAfter, is_introducer) \ |
| 412 | values (?, ?, ?, ?, ?, ?, ?)", |
Davide Pesavento | 7676b56 | 2020-12-14 00:41:26 -0500 | [diff] [blame] | 413 | -1, &stmt, nullptr); |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 414 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 415 | sqlite3_bind_string(stmt, 1, identity, SQLITE_TRANSIENT); |
| 416 | sqlite3_bind_string(stmt, 2, contact.getAlias(), SQLITE_TRANSIENT); |
| 417 | sqlite3_bind_string(stmt, 3, contact.getPublicKeyName().toUri(), SQLITE_TRANSIENT); |
| 418 | sqlite3_bind_text(stmt, 4, |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 419 | reinterpret_cast<const char*>(contact.getPublicKey().data()), |
| 420 | contact.getPublicKey().size(), SQLITE_TRANSIENT); |
Yingdi Yu | a787672 | 2014-03-25 14:46:55 -0700 | [diff] [blame] | 421 | sqlite3_bind_int64(stmt, 5, time::toUnixTimestamp(contact.getNotBefore()).count()); |
| 422 | sqlite3_bind_int64(stmt, 6, time::toUnixTimestamp(contact.getNotAfter()).count()); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 423 | sqlite3_bind_int(stmt, 7, (isIntroducer ? 1 : 0)); |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 424 | |
Yingdi Yu | 1cc45d9 | 2015-02-09 14:19:54 -0800 | [diff] [blame] | 425 | sqlite3_step(stmt); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 426 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 427 | sqlite3_finalize(stmt); |
Yingdi Yu | 71c0187 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 428 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 429 | const Profile& profile = contact.getProfile(); |
Davide Pesavento | 7676b56 | 2020-12-14 00:41:26 -0500 | [diff] [blame] | 430 | for (auto it = profile.begin(); it != profile.end(); it++) { |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 431 | sqlite3_prepare_v2(m_db, |
| 432 | "INSERT INTO ContactProfile \ |
| 433 | (profile_identity, profile_type, profile_value, endorse) \ |
| 434 | values (?, ?, ?, 0)", |
Davide Pesavento | 7676b56 | 2020-12-14 00:41:26 -0500 | [diff] [blame] | 435 | -1, &stmt, nullptr); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 436 | sqlite3_bind_string(stmt, 1, identity, SQLITE_TRANSIENT); |
| 437 | sqlite3_bind_string(stmt, 2, it->first, SQLITE_TRANSIENT); |
| 438 | sqlite3_bind_string(stmt, 3, it->second, SQLITE_TRANSIENT); |
Yingdi Yu | 1cc45d9 | 2015-02-09 14:19:54 -0800 | [diff] [blame] | 439 | sqlite3_step(stmt); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 440 | sqlite3_finalize(stmt); |
| 441 | } |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 442 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 443 | if (isIntroducer) { |
Davide Pesavento | 7676b56 | 2020-12-14 00:41:26 -0500 | [diff] [blame] | 444 | auto it = contact.trustScopeBegin(); |
| 445 | auto end = contact.trustScopeEnd(); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 446 | |
| 447 | while (it != end) { |
| 448 | sqlite3_prepare_v2(m_db, |
| 449 | "INSERT INTO TrustScope (contact_namespace, trust_scope) values (?, ?)", |
Davide Pesavento | 7676b56 | 2020-12-14 00:41:26 -0500 | [diff] [blame] | 450 | -1, &stmt, nullptr); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 451 | sqlite3_bind_string(stmt, 1, identity, SQLITE_TRANSIENT); |
| 452 | sqlite3_bind_string(stmt, 2, it->first.toUri(), SQLITE_TRANSIENT); |
Yingdi Yu | 1cc45d9 | 2015-02-09 14:19:54 -0800 | [diff] [blame] | 453 | sqlite3_step(stmt); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 454 | sqlite3_finalize(stmt); |
| 455 | it++; |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 456 | } |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 457 | } |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 458 | } |
| 459 | |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 460 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 461 | shared_ptr<Contact> |
| 462 | ContactStorage::getContact(const Name& identity) const |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 463 | { |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 464 | shared_ptr<Contact> contact; |
| 465 | Profile profile; |
| 466 | |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 467 | sqlite3_stmt *stmt; |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 468 | sqlite3_prepare_v2(m_db, |
| 469 | "SELECT contact_alias, contact_keyName, contact_key, notBefore, notAfter, \ |
| 470 | is_introducer FROM Contact where contact_namespace=?", |
Davide Pesavento | 7676b56 | 2020-12-14 00:41:26 -0500 | [diff] [blame] | 471 | -1, &stmt, nullptr); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 472 | sqlite3_bind_string(stmt, 1, identity.toUri(), SQLITE_TRANSIENT); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 473 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 474 | if (sqlite3_step(stmt) == SQLITE_ROW) { |
| 475 | string alias = sqlite3_column_string(stmt, 0); |
| 476 | string keyName = sqlite3_column_string(stmt, 1); |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 477 | ndn::Buffer key(sqlite3_column_text(stmt, 2), sqlite3_column_bytes (stmt, 2)); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 478 | time::system_clock::TimePoint notBefore = |
| 479 | time::fromUnixTimestamp(time::milliseconds(sqlite3_column_int64 (stmt, 3))); |
| 480 | time::system_clock::TimePoint notAfter = |
| 481 | time::fromUnixTimestamp(time::milliseconds(sqlite3_column_int64 (stmt, 4))); |
| 482 | int isIntroducer = sqlite3_column_int (stmt, 5); |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 483 | |
Varun Patil | b3d03b9 | 2021-01-08 17:09:32 +0530 | [diff] [blame] | 484 | contact = std::make_shared<Contact>(identity, alias, Name(keyName), |
| 485 | notBefore, notAfter, key, isIntroducer); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 486 | } |
| 487 | sqlite3_finalize(stmt); |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 488 | |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 489 | if (!static_cast<bool>(contact)) |
| 490 | return contact; |
| 491 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 492 | sqlite3_prepare_v2(m_db, |
| 493 | "SELECT profile_type, profile_value FROM ContactProfile \ |
| 494 | where profile_identity=?", |
Davide Pesavento | 7676b56 | 2020-12-14 00:41:26 -0500 | [diff] [blame] | 495 | -1, &stmt, nullptr); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 496 | sqlite3_bind_string(stmt, 1, identity.toUri(), SQLITE_TRANSIENT); |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 497 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 498 | while (sqlite3_step(stmt) == SQLITE_ROW) { |
| 499 | string type = sqlite3_column_string(stmt, 0); |
| 500 | string value = sqlite3_column_string(stmt, 1); |
| 501 | profile[type] = value; |
| 502 | } |
| 503 | sqlite3_finalize(stmt); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 504 | contact->setProfile(profile); |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 505 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 506 | if (contact->isIntroducer()) { |
| 507 | sqlite3_prepare_v2(m_db, |
| 508 | "SELECT trust_scope FROM TrustScope WHERE contact_namespace=?", |
Davide Pesavento | 7676b56 | 2020-12-14 00:41:26 -0500 | [diff] [blame] | 509 | -1, &stmt, nullptr); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 510 | sqlite3_bind_string(stmt, 1, identity.toUri(), SQLITE_TRANSIENT); |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 511 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 512 | while (sqlite3_step(stmt) == SQLITE_ROW) { |
| 513 | Name scope(sqlite3_column_string(stmt, 0)); |
| 514 | contact->addTrustScope(scope); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 515 | } |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 516 | sqlite3_finalize(stmt); |
| 517 | } |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 518 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 519 | return contact; |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 523 | void |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 524 | ContactStorage::updateIsIntroducer(const Name& identity, bool isIntroducer) |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 525 | { |
| 526 | sqlite3_stmt *stmt; |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 527 | sqlite3_prepare_v2(m_db, "UPDATE Contact SET is_introducer=? WHERE contact_namespace=?", |
Davide Pesavento | 7676b56 | 2020-12-14 00:41:26 -0500 | [diff] [blame] | 528 | -1, &stmt, nullptr); |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 529 | sqlite3_bind_int(stmt, 1, (isIntroducer ? 1 : 0)); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 530 | sqlite3_bind_string(stmt, 2, identity.toUri(), SQLITE_TRANSIENT); |
Yingdi Yu | 4212586 | 2014-08-07 17:04:28 -0700 | [diff] [blame] | 531 | sqlite3_step(stmt); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 532 | sqlite3_finalize(stmt); |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 533 | return; |
| 534 | } |
| 535 | |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 536 | void |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 537 | ContactStorage::updateAlias(const Name& identity, const string& alias) |
Yingdi Yu | 2ac40fb | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 538 | { |
Yingdi Yu | 71c0187 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 539 | sqlite3_stmt *stmt; |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 540 | sqlite3_prepare_v2(m_db, "UPDATE Contact SET contact_alias=? WHERE contact_namespace=?", |
Davide Pesavento | 7676b56 | 2020-12-14 00:41:26 -0500 | [diff] [blame] | 541 | -1, &stmt, nullptr); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 542 | sqlite3_bind_string(stmt, 1, alias, SQLITE_TRANSIENT); |
| 543 | sqlite3_bind_string(stmt, 2, identity.toUri(), SQLITE_TRANSIENT); |
Yingdi Yu | 4212586 | 2014-08-07 17:04:28 -0700 | [diff] [blame] | 544 | sqlite3_step(stmt); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 545 | sqlite3_finalize(stmt); |
Yingdi Yu | 71c0187 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 546 | return; |
Yingdi Yu | 2ac40fb | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 547 | } |
| 548 | |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 549 | bool |
Yingdi Yu | 71c0187 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 550 | ContactStorage::doesContactExist(const Name& name) |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 551 | { |
| 552 | bool result = false; |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 553 | |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 554 | sqlite3_stmt *stmt; |
Davide Pesavento | 7676b56 | 2020-12-14 00:41:26 -0500 | [diff] [blame] | 555 | sqlite3_prepare_v2(m_db, "SELECT count(*) FROM Contact WHERE contact_namespace=?", |
| 556 | -1, &stmt, nullptr); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 557 | sqlite3_bind_string(stmt, 1, name.toUri(), SQLITE_TRANSIENT); |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 558 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 559 | int res = sqlite3_step(stmt); |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 560 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 561 | if (res == SQLITE_ROW) { |
Davide Pesavento | 7676b56 | 2020-12-14 00:41:26 -0500 | [diff] [blame] | 562 | int countAll = sqlite3_column_int(stmt, 0); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 563 | if (countAll > 0) |
| 564 | result = true; |
| 565 | } |
| 566 | |
| 567 | sqlite3_finalize(stmt); |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 568 | return result; |
| 569 | } |
| 570 | |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 571 | void |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 572 | ContactStorage::getAllContacts(vector<shared_ptr<Contact> >& contacts) const |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 573 | { |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 574 | vector<Name> contactNames; |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 575 | |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 576 | sqlite3_stmt *stmt; |
Davide Pesavento | 7676b56 | 2020-12-14 00:41:26 -0500 | [diff] [blame] | 577 | sqlite3_prepare_v2(m_db, "SELECT contact_namespace FROM Contact", -1, &stmt, nullptr); |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 578 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 579 | while (sqlite3_step(stmt) == SQLITE_ROW) { |
| 580 | string identity = sqlite3_column_string(stmt, 0); |
| 581 | contactNames.push_back(Name(identity)); |
| 582 | } |
| 583 | sqlite3_finalize(stmt); |
Yingdi Yu | 71c0187 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 584 | |
Davide Pesavento | 7676b56 | 2020-12-14 00:41:26 -0500 | [diff] [blame] | 585 | for (auto it = contactNames.begin(); it != contactNames.end(); it++) { |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 586 | shared_ptr<Contact> contact = getContact(*it); |
| 587 | if (static_cast<bool>(contact)) |
| 588 | contacts.push_back(contact); |
| 589 | } |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 590 | } |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 591 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 592 | void |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 593 | ContactStorage::updateDnsData(const Block& data, const string& name, |
| 594 | const string& type, const string& dataName) |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 595 | { |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 596 | sqlite3_stmt *stmt; |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 597 | sqlite3_prepare_v2(m_db, |
| 598 | "INSERT OR REPLACE INTO DnsData (dns_name, dns_type, dns_value, data_name) \ |
| 599 | VALUES (?, ?, ?, ?)", |
Davide Pesavento | 7676b56 | 2020-12-14 00:41:26 -0500 | [diff] [blame] | 600 | -1, &stmt, nullptr); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 601 | sqlite3_bind_string(stmt, 1, name, SQLITE_TRANSIENT); |
| 602 | sqlite3_bind_string(stmt, 2, type, SQLITE_TRANSIENT); |
| 603 | sqlite3_bind_block(stmt, 3, data, SQLITE_TRANSIENT); |
| 604 | sqlite3_bind_string(stmt, 4, dataName, SQLITE_TRANSIENT); |
Yingdi Yu | 4212586 | 2014-08-07 17:04:28 -0700 | [diff] [blame] | 605 | sqlite3_step(stmt); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 606 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 607 | sqlite3_finalize(stmt); |
| 608 | } |
| 609 | |
| 610 | shared_ptr<Data> |
| 611 | ContactStorage::getDnsData(const Name& dataName) |
| 612 | { |
| 613 | shared_ptr<Data> data; |
| 614 | |
| 615 | sqlite3_stmt *stmt; |
Davide Pesavento | 7676b56 | 2020-12-14 00:41:26 -0500 | [diff] [blame] | 616 | sqlite3_prepare_v2(m_db, "SELECT dns_value FROM DnsData where data_name=?", -1, &stmt, nullptr); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 617 | sqlite3_bind_string(stmt, 1, dataName.toUri(), SQLITE_TRANSIENT); |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 618 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 619 | if (sqlite3_step(stmt) == SQLITE_ROW) { |
Varun Patil | b3d03b9 | 2021-01-08 17:09:32 +0530 | [diff] [blame] | 620 | data = std::make_shared<Data>(); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 621 | data->wireDecode(sqlite3_column_block(stmt, 0)); |
| 622 | } |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 623 | sqlite3_finalize(stmt); |
| 624 | |
| 625 | return data; |
| 626 | } |
| 627 | |
| 628 | shared_ptr<Data> |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 629 | ContactStorage::getDnsData(const string& name, const string& type) |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 630 | { |
| 631 | shared_ptr<Data> data; |
| 632 | |
| 633 | sqlite3_stmt *stmt; |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 634 | sqlite3_prepare_v2(m_db, "SELECT dns_value FROM DnsData where dns_name=? and dns_type=?", |
Davide Pesavento | 7676b56 | 2020-12-14 00:41:26 -0500 | [diff] [blame] | 635 | -1, &stmt, nullptr); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 636 | sqlite3_bind_string(stmt, 1, name, SQLITE_TRANSIENT); |
| 637 | sqlite3_bind_string(stmt, 2, type, SQLITE_TRANSIENT); |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 638 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 639 | if (sqlite3_step(stmt) == SQLITE_ROW) { |
Varun Patil | b3d03b9 | 2021-01-08 17:09:32 +0530 | [diff] [blame] | 640 | data = std::make_shared<Data>(); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 641 | data->wireDecode(sqlite3_column_block(stmt, 0)); |
| 642 | } |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 643 | sqlite3_finalize(stmt); |
| 644 | |
| 645 | return data; |
| 646 | } |
| 647 | |
Yingdi Yu | eb692ac | 2015-02-10 18:46:18 -0800 | [diff] [blame] | 648 | } // namespace chronochat |