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