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