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