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 | |
| 11 | #include "contact-storage.h" |
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 | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 14 | #include "logging.h" |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 15 | |
| 16 | using namespace std; |
| 17 | using namespace ndn; |
| 18 | namespace fs = boost::filesystem; |
| 19 | |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 20 | INIT_LOGGER ("ContactStorage"); |
| 21 | |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 22 | namespace chronos{ |
| 23 | |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 24 | const string INIT_SP_TABLE = "\ |
| 25 | CREATE TABLE IF NOT EXISTS \n \ |
| 26 | SelfProfile( \n \ |
Yingdi Yu | 54dcecc | 2013-10-17 15:07:17 -0700 | [diff] [blame] | 27 | profile_identity BLOB NOT NULL, \n \ |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 28 | profile_type BLOB NOT NULL, \n \ |
Yingdi Yu | 54dcecc | 2013-10-17 15:07:17 -0700 | [diff] [blame] | 29 | profile_value BLOB NOT NULL, \n \ |
| 30 | \ |
| 31 | PRIMARY KEY (profile_identity, profile_type) \n \ |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 32 | ); \n \ |
| 33 | \ |
Yingdi Yu | 54dcecc | 2013-10-17 15:07:17 -0700 | [diff] [blame] | 34 | CREATE INDEX sp_index ON SelfProfile(profile_identity,profile_type); \n \ |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 35 | "; |
| 36 | |
Yingdi Yu | ec3d9a3 | 2013-10-18 18:35:09 -0700 | [diff] [blame] | 37 | const string INIT_SE_TABLE = "\ |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 38 | CREATE TABLE IF NOT EXISTS \n \ |
Yingdi Yu | ec3d9a3 | 2013-10-18 18:35:09 -0700 | [diff] [blame] | 39 | SelfEndorse( \n \ |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 40 | identity BLOB NOT NULL UNIQUE, \n \ |
Yingdi Yu | ec3d9a3 | 2013-10-18 18:35:09 -0700 | [diff] [blame] | 41 | endorse_data BLOB NOT NULL, \n \ |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 42 | \ |
| 43 | PRIMARY KEY (identity) \n \ |
| 44 | ); \n \ |
Yingdi Yu | ec3d9a3 | 2013-10-18 18:35:09 -0700 | [diff] [blame] | 45 | CREATE INDEX se_index ON SelfEndorse(identity); \n \ |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 46 | "; |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 47 | |
Yingdi Yu | 71c0187 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 48 | const string INIT_CONTACT_TABLE = "\ |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 49 | CREATE TABLE IF NOT EXISTS \n \ |
Yingdi Yu | 71c0187 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 50 | Contact( \n \ |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 51 | contact_namespace BLOB NOT NULL, \n \ |
| 52 | contact_alias BLOB NOT NULL, \n \ |
| 53 | self_certificate BLOB NOT NULL, \n \ |
Yingdi Yu | 71c0187 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 54 | is_introducer INTEGER DEFAULT 0, \n \ |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 55 | \ |
| 56 | PRIMARY KEY (contact_namespace) \n \ |
| 57 | ); \n \ |
| 58 | \ |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 59 | CREATE INDEX contact_index ON Contact(contact_namespace); \n \ |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 60 | "; |
| 61 | |
Yingdi Yu | 71c0187 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 62 | const string INIT_TS_TABLE = "\ |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 63 | CREATE TABLE IF NOT EXISTS \n \ |
Yingdi Yu | 71c0187 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 64 | TrustScope( \n \ |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 65 | id INTEGER PRIMARY KEY AUTOINCREMENT, \n \ |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 66 | contact_namespace BLOB NOT NULL, \n \ |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 67 | trust_scope BLOB NOT NULL \n \ |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 68 | ); \n \ |
| 69 | \ |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 70 | CREATE INDEX ts_index ON TrustScope(contact_namespace); \n \ |
| 71 | "; |
| 72 | |
| 73 | const string INIT_CP_TABLE = "\ |
| 74 | CREATE TABLE IF NOT EXISTS \n \ |
| 75 | ContactProfile( \n \ |
| 76 | profile_identity BLOB NOT NULL, \n \ |
| 77 | profile_type BLOB NOT NULL, \n \ |
| 78 | profile_value BLOB NOT NULL, \n \ |
| 79 | endorse INTEGER NOT NULL, \n \ |
| 80 | \ |
| 81 | PRIMARY KEY (profile_identity, profile_type) \n \ |
| 82 | ); \n \ |
| 83 | \ |
| 84 | CREATE INDEX cp_index ON ContactProfile(profile_identity); \n \ |
| 85 | "; |
| 86 | |
| 87 | const string INIT_PE_TABLE = "\ |
| 88 | CREATE TABLE IF NOT EXISTS \n \ |
| 89 | ProfileEndorse( \n \ |
| 90 | identity BLOB NOT NULL UNIQUE, \n \ |
| 91 | endorse_data BLOB NOT NULL, \n \ |
| 92 | \ |
| 93 | PRIMARY KEY (identity) \n \ |
| 94 | ); \n \ |
| 95 | \ |
| 96 | CREATE INDEX pe_index ON ProfileEndorse(identity); \n \ |
| 97 | "; |
| 98 | |
| 99 | const string INIT_CE_TABLE = "\ |
| 100 | CREATE TABLE IF NOT EXISTS \n \ |
| 101 | CollectEndorse( \n \ |
| 102 | endorsee BLOB NOT NULL, \n \ |
| 103 | endorser BLOB NOT NULL, \n \ |
| 104 | endorse_name BLOB NOT NULL, \n \ |
| 105 | endorse_data BLOB NOT NULL, \n \ |
| 106 | \ |
| 107 | PRIMARY KEY (endorsee, endorser) \n \ |
| 108 | ); \n \ |
| 109 | \ |
| 110 | CREATE INDEX ce_index ON CollectEndorse(endorsee); \n \ |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 111 | "; |
| 112 | |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 113 | ContactStorage::ContactStorage() |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 114 | { |
| 115 | fs::path chronosDir = fs::path(getenv("HOME")) / ".chronos"; |
| 116 | fs::create_directories (chronosDir); |
| 117 | |
| 118 | int res = sqlite3_open((chronosDir / "chronos.db").c_str (), &m_db); |
| 119 | if (res != SQLITE_OK) |
Yingdi Yu | f8f572d | 2014-01-13 11:19:47 -0800 | [diff] [blame] | 120 | throw Error("Chronos DB cannot be open/created"); |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 121 | |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 122 | initializeTable("SelfProfile", INIT_SP_TABLE); |
| 123 | initializeTable("SelfEndorse", INIT_SE_TABLE); |
| 124 | initializeTable("Contact", INIT_CONTACT_TABLE); |
| 125 | initializeTable("TrustScope", INIT_TS_TABLE); |
| 126 | initializeTable("ContactProfile", INIT_CP_TABLE); |
| 127 | initializeTable("ProfileEndorse", INIT_PE_TABLE); |
| 128 | initializeTable("CollectEndorse", INIT_CE_TABLE); |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 129 | |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 132 | void |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 133 | ContactStorage::initializeTable(const string& tableName, const string& sqlCreateStmt) |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 134 | { |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 135 | sqlite3_stmt *stmt; |
| 136 | sqlite3_prepare_v2 (m_db, "SELECT name FROM sqlite_master WHERE type='table' And name=?", -1, &stmt, 0); |
| 137 | sqlite3_bind_text(stmt, 1, tableName.c_str(), tableName.size(), SQLITE_TRANSIENT); |
| 138 | int res = sqlite3_step (stmt); |
| 139 | |
| 140 | bool tableExist = false; |
| 141 | if (res == SQLITE_ROW) |
| 142 | tableExist = true; |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 143 | sqlite3_finalize (stmt); |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 144 | |
| 145 | if(!tableExist) |
| 146 | { |
| 147 | char *errmsg = 0; |
| 148 | res = sqlite3_exec (m_db, sqlCreateStmt.c_str (), NULL, NULL, &errmsg); |
| 149 | if (res != SQLITE_OK && errmsg != 0) |
| 150 | throw Error("Init \"error\" in " + tableName); |
| 151 | } |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 152 | } |
| 153 | |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 154 | shared_ptr<Profile> |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 155 | ContactStorage::getSelfProfile(const Name& identity) const |
| 156 | { |
| 157 | shared_ptr<Profile> profile; |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 158 | sqlite3_stmt *stmt; |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 159 | sqlite3_prepare_v2 (m_db, "SELECT profile_type, profile_value FROM SelfProfile WHERE profile_identity=?", -1, &stmt, 0); |
| 160 | sqlite3_bind_text (stmt, 1, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT); |
Yingdi Yu | 2ac40fb | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 161 | |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 162 | while( sqlite3_step (stmt) == SQLITE_ROW) |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 163 | { |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 164 | profile = make_shared<Profile>(identity); |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 165 | string profileType(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0)); |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 166 | string profileValue(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 1)), sqlite3_column_bytes (stmt, 1)); |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 167 | (*profile)[profileType] = profileValue; |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 168 | } |
| 169 | |
Yingdi Yu | 72781e5 | 2013-11-06 23:00:21 -0800 | [diff] [blame] | 170 | sqlite3_finalize(stmt); |
| 171 | |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 172 | return profile; |
| 173 | } |
Yingdi Yu | ae8217c | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 174 | |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 175 | // Block |
| 176 | // ContactStorage::getSelfEndorseCertificate(const Name& identity) |
| 177 | // { |
| 178 | // sqlite3_stmt *stmt; |
| 179 | // sqlite3_prepare_v2 (m_db, "SELECT endorse_data FROM SelfEndorse where identity=?", -1, &stmt, 0); |
| 180 | // sqlite3_bind_text(stmt, 1, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT); |
| 181 | |
| 182 | // if(sqlite3_step (stmt) == SQLITE_ROW) |
| 183 | // { |
| 184 | // Block result(reinterpret_cast<const uint8_t*>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0)); |
| 185 | // sqlite3_finalize (stmt); |
| 186 | // return result; |
| 187 | // } |
| 188 | |
| 189 | // sqlite3_finalize (stmt); |
| 190 | |
| 191 | // throw Error("ContactStorage: No self-endorse certificate found!"); |
| 192 | // } |
| 193 | |
| 194 | void |
| 195 | ContactStorage::addSelfEndorseCertificate(const EndorseCertificate& newEndorseCertificate, const Name& identity) |
| 196 | { |
| 197 | const Block& newEndorseCertificateBlock = newEndorseCertificate.wireEncode(); |
| 198 | |
| 199 | sqlite3_stmt *stmt; |
| 200 | sqlite3_prepare_v2 (m_db, "INSERT OR REPLACE INTO SelfEndorse (identity, endorse_data) values (?, ?)", -1, &stmt, 0); |
| 201 | sqlite3_bind_text(stmt, 1, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT); |
| 202 | sqlite3_bind_text(stmt, 2, reinterpret_cast<const char*>(newEndorseCertificateBlock.wire()), newEndorseCertificateBlock.size(), SQLITE_TRANSIENT); |
| 203 | sqlite3_step(stmt); |
| 204 | |
| 205 | sqlite3_finalize (stmt); |
| 206 | } |
| 207 | |
| 208 | // Block |
| 209 | // ContactStorage::getEndorseCertificate(const Name& identity) |
| 210 | // { |
| 211 | // sqlite3_stmt *stmt; |
| 212 | // sqlite3_prepare_v2 (m_db, "SELECT endorse_data FROM ProfileEndorse where identity=?", -1, &stmt, 0); |
| 213 | // sqlite3_bind_text(stmt, 1, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT); |
| 214 | |
| 215 | |
| 216 | // if(sqlite3_step (stmt) == SQLITE_ROW) |
| 217 | // { |
| 218 | // Block result(reinterpret_cast<const uint8_t*>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0)); |
| 219 | // sqlite3_finalize (stmt); |
| 220 | // return result; |
| 221 | // } |
| 222 | |
| 223 | // sqlite3_finalize (stmt); |
| 224 | |
| 225 | // throw Error("ContactStorage: No endorse certificate found!"); |
| 226 | |
| 227 | // return Block(); |
| 228 | // } |
| 229 | |
| 230 | void |
| 231 | ContactStorage::addEndorseCertificate(const EndorseCertificate& endorseCertificate, const Name& identity) |
| 232 | { |
| 233 | const Block& newEndorseCertificateBlock = endorseCertificate.wireEncode(); |
| 234 | |
| 235 | sqlite3_stmt *stmt; |
| 236 | sqlite3_prepare_v2 (m_db, "INSERT OR REPLACE INTO ProfileEndorse (identity, endorse_data) values (?, ?)", -1, &stmt, 0); |
| 237 | sqlite3_bind_text(stmt, 1, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT); |
| 238 | sqlite3_bind_text(stmt, 2, reinterpret_cast<const char*>(newEndorseCertificateBlock.value()), newEndorseCertificateBlock.size(), SQLITE_TRANSIENT); |
| 239 | sqlite3_step(stmt); |
| 240 | |
| 241 | sqlite3_finalize (stmt); |
| 242 | } |
| 243 | |
| 244 | void |
| 245 | ContactStorage::updateCollectEndorse(const EndorseCertificate& endorseCertificate) |
| 246 | { |
| 247 | Name endorserName = endorseCertificate.getSigner(); |
| 248 | Name keyName = endorseCertificate.getPublicKeyName(); |
| 249 | Name endorseeName = keyName.getPrefix(keyName.size()-1); |
| 250 | Name getCertName = endorseCertificate.getName(); |
| 251 | |
| 252 | sqlite3_stmt *stmt; |
| 253 | sqlite3_prepare_v2 (m_db, "INSERT OR REPLACE INTO CollectEndorse (endorser, endorsee, endorse_name, endorse_data) VALUES (?, ?, ?, ?)", -1, &stmt, 0); |
| 254 | sqlite3_bind_text(stmt, 1, endorserName.toUri().c_str(), endorserName.toUri().size(), SQLITE_TRANSIENT); |
| 255 | sqlite3_bind_text(stmt, 2, endorseeName.toUri().c_str(), endorseeName.toUri().size(), SQLITE_TRANSIENT); |
| 256 | sqlite3_bind_text(stmt, 3, getCertName.toUri().c_str(), getCertName.toUri().size(), SQLITE_TRANSIENT); |
| 257 | const Block &block = endorseCertificate.wireEncode(); |
| 258 | sqlite3_bind_text(stmt, 4, reinterpret_cast<const char*>(block.wire()), block.size(), SQLITE_TRANSIENT); |
| 259 | int res = sqlite3_step (stmt); |
| 260 | sqlite3_finalize (stmt); |
| 261 | return; |
| 262 | } |
| 263 | |
| 264 | void |
| 265 | ContactStorage::getCollectEndorseList(const Name& name, vector<Buffer>& endorseList) |
| 266 | { |
| 267 | sqlite3_stmt *stmt; |
| 268 | sqlite3_prepare_v2 (m_db, "SELECT endorse_data FROM CollectEndorse WHERE endorsee=?", -1, &stmt, 0); |
| 269 | sqlite3_bind_text(stmt, 1, name.toUri().c_str(), name.toUri().size(), SQLITE_TRANSIENT); |
| 270 | |
| 271 | while(sqlite3_step (stmt) == SQLITE_ROW) |
| 272 | { |
| 273 | Buffer blob(reinterpret_cast<const uint8_t*>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0)); |
| 274 | endorseList.push_back(blob); |
| 275 | } |
| 276 | |
| 277 | sqlite3_finalize (stmt); |
| 278 | } |
| 279 | |
| 280 | void |
| 281 | ContactStorage::getEndorseList(const Name& identity, vector<string>& endorseList) |
| 282 | { |
| 283 | sqlite3_stmt *stmt; |
| 284 | sqlite3_prepare_v2 (m_db, "SELECT profile_type FROM ContactProfile WHERE profile_identity=? AND endorse=1 ORDER BY profile_type", -1, &stmt, 0); |
| 285 | sqlite3_bind_text(stmt, 1, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT); |
| 286 | |
| 287 | while( sqlite3_step (stmt) == SQLITE_ROW) |
| 288 | { |
| 289 | string profileType(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0)); |
| 290 | endorseList.push_back(profileType); |
| 291 | } |
| 292 | sqlite3_finalize (stmt); |
| 293 | } |
| 294 | |
| 295 | |
Yingdi Yu | ae8217c | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 296 | void |
| 297 | ContactStorage::removeContact(const Name& contactNameSpace) |
| 298 | { |
Yingdi Yu | ae8217c | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 299 | string identity = contactNameSpace.toUri(); |
| 300 | |
Yingdi Yu | ae8217c | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 301 | sqlite3_stmt *stmt; |
| 302 | sqlite3_prepare_v2 (m_db, "DELETE FROM Contact WHERE contact_namespace=?", -1, &stmt, 0); |
| 303 | sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size (), SQLITE_TRANSIENT); |
| 304 | int res = sqlite3_step (stmt); |
| 305 | sqlite3_finalize (stmt); |
| 306 | |
| 307 | sqlite3_prepare_v2 (m_db, "DELETE FROM ContactProfile WHERE profile_identity=?", -1, &stmt, 0); |
| 308 | sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size (), SQLITE_TRANSIENT); |
| 309 | res = sqlite3_step (stmt); |
| 310 | sqlite3_finalize (stmt); |
| 311 | |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 312 | sqlite3_prepare_v2 (m_db, "DELETE FROM TrustScope WHERE contact_namespace=?", -1, &stmt, 0); |
| 313 | sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size (), SQLITE_TRANSIENT); |
| 314 | res = sqlite3_step (stmt); |
| 315 | sqlite3_finalize (stmt); |
Yingdi Yu | ae8217c | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 316 | } |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 317 | |
| 318 | void |
Yingdi Yu | 71c0187 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 319 | ContactStorage::addContact(const ContactItem& contact) |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 320 | { |
Yingdi Yu | 71c0187 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 321 | if(doesContactExist(contact.getNameSpace())) |
Yingdi Yu | f8f572d | 2014-01-13 11:19:47 -0800 | [diff] [blame] | 322 | throw Error("Normal Contact has already existed"); |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 323 | |
Yingdi Yu | 71c0187 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 324 | bool isIntroducer = contact.isIntroducer(); |
| 325 | |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 326 | sqlite3_stmt *stmt; |
| 327 | sqlite3_prepare_v2 (m_db, |
Yingdi Yu | 71c0187 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 328 | "INSERT INTO Contact (contact_namespace, contact_alias, self_certificate, is_introducer) values (?, ?, ?, ?)", |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 329 | -1, |
| 330 | &stmt, |
| 331 | 0); |
| 332 | |
Yingdi Yu | 71c0187 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 333 | sqlite3_bind_text(stmt, 1, contact.getNameSpace().toUri().c_str(), contact.getNameSpace().toUri().size (), SQLITE_TRANSIENT); |
| 334 | sqlite3_bind_text(stmt, 2, contact.getAlias().c_str(), contact.getAlias().size(), SQLITE_TRANSIENT); |
Yingdi Yu | f8f572d | 2014-01-13 11:19:47 -0800 | [diff] [blame] | 335 | const Block& selfCertificateBlock = contact.getSelfEndorseCertificate().wireEncode(); |
| 336 | sqlite3_bind_text(stmt, 3, reinterpret_cast<const char*>(selfCertificateBlock.wire()), selfCertificateBlock.size(), SQLITE_TRANSIENT); |
Yingdi Yu | 71c0187 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 337 | sqlite3_bind_int(stmt, 4, (isIntroducer ? 1 : 0)); |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 338 | |
| 339 | int res = sqlite3_step (stmt); |
| 340 | sqlite3_finalize (stmt); |
Yingdi Yu | 71c0187 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 341 | |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 342 | const Profile& profile = contact.getSelfEndorseCertificate().getProfile(); |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 343 | Profile::const_iterator it = profile.begin(); |
| 344 | string identity = contact.getNameSpace().toUri(); |
| 345 | for(; it != profile.end(); it++) |
| 346 | { |
| 347 | sqlite3_prepare_v2 (m_db, |
| 348 | "INSERT INTO ContactProfile (profile_identity, profile_type, profile_value, endorse) values (?, ?, ?, 0)", |
| 349 | -1, |
| 350 | &stmt, |
| 351 | 0); |
| 352 | sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size (), SQLITE_TRANSIENT); |
| 353 | sqlite3_bind_text(stmt, 2, it->first.c_str(), it->first.size(), SQLITE_TRANSIENT); |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 354 | sqlite3_bind_text(stmt, 3, it->second.c_str(), it->second.size(), SQLITE_TRANSIENT); |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 355 | res = sqlite3_step (stmt); |
| 356 | sqlite3_finalize (stmt); |
| 357 | } |
| 358 | |
Yingdi Yu | 71c0187 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 359 | if(isIntroducer) |
| 360 | { |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 361 | ContactItem::const_iterator it = contact.trustScopeBegin(); |
Yingdi Yu | 71c0187 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 362 | string nameSpace = contact.getNameSpace().toUri(); |
| 363 | |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 364 | while(it != contact.trustScopeEnd()) |
Yingdi Yu | 71c0187 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 365 | { |
| 366 | sqlite3_prepare_v2 (m_db, |
| 367 | "INSERT INTO TrustScope (contact_namespace, trust_scope) values (?, ?)", |
| 368 | -1, |
| 369 | &stmt, |
| 370 | 0); |
| 371 | sqlite3_bind_text(stmt, 1, nameSpace.c_str(), nameSpace.size (), SQLITE_TRANSIENT); |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 372 | sqlite3_bind_text(stmt, 2, it->first.toUri().c_str(), it->first.toUri().size(), SQLITE_TRANSIENT); |
Yingdi Yu | 71c0187 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 373 | res = sqlite3_step (stmt); |
| 374 | sqlite3_finalize (stmt); |
| 375 | it++; |
| 376 | } |
| 377 | } |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 378 | } |
| 379 | |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 380 | |
| 381 | shared_ptr<ContactItem> |
| 382 | ContactStorage::getContact(const Name& name) |
| 383 | { |
| 384 | sqlite3_stmt *stmt; |
| 385 | sqlite3_prepare_v2 (m_db, "SELECT contact_alias, self_certificate, is_introducer FROM Contact where contact_namespace=?", -1, &stmt, 0); |
| 386 | sqlite3_bind_text (stmt, 1, name.toUri().c_str(), name.toUri().size(), SQLITE_TRANSIENT); |
| 387 | |
| 388 | if( sqlite3_step (stmt) == SQLITE_ROW) |
| 389 | { |
| 390 | string alias(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0)); |
| 391 | |
| 392 | Data certData; |
| 393 | certData.wireDecode(Block(reinterpret_cast<const uint8_t*>(sqlite3_column_text(stmt, 1)), sqlite3_column_bytes (stmt, 1))); |
| 394 | EndorseCertificate endorseCertificate(certData); |
| 395 | |
| 396 | int isIntroducer = sqlite3_column_int (stmt, 2); |
| 397 | |
| 398 | sqlite3_finalize (stmt); |
| 399 | |
| 400 | shared_ptr<ContactItem> contact = make_shared<ContactItem>(endorseCertificate, isIntroducer, alias); |
| 401 | |
| 402 | if(contact->isIntroducer()) |
| 403 | { |
| 404 | sqlite3_prepare_v2 (m_db, "SELECT trust_scope FROM TrustScope WHERE contact_namespace=?", -1, &stmt, 0); |
| 405 | sqlite3_bind_text(stmt, 1, name.toUri().c_str(), name.toUri().size(), SQLITE_TRANSIENT); |
| 406 | |
| 407 | while( sqlite3_step (stmt) == SQLITE_ROW) |
| 408 | { |
| 409 | Name scope(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0))); |
| 410 | contact->addTrustScope(scope); |
| 411 | } |
| 412 | sqlite3_finalize (stmt); |
| 413 | } |
| 414 | |
| 415 | return contact; |
| 416 | } |
| 417 | return shared_ptr<ContactItem>(); |
| 418 | } |
| 419 | |
| 420 | |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 421 | void |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 422 | ContactStorage::updateIsIntroducer(const Name& identity, bool isIntroducer) |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 423 | { |
| 424 | sqlite3_stmt *stmt; |
| 425 | sqlite3_prepare_v2 (m_db, "UPDATE Contact SET is_introducer=? WHERE contact_namespace=?", -1, &stmt, 0); |
| 426 | sqlite3_bind_int(stmt, 1, (isIntroducer ? 1 : 0)); |
| 427 | sqlite3_bind_text(stmt, 2, identity.toUri().c_str(), identity.toUri().size (), SQLITE_TRANSIENT); |
| 428 | int res = sqlite3_step (stmt); |
| 429 | sqlite3_finalize (stmt); |
| 430 | return; |
| 431 | } |
| 432 | |
Yingdi Yu | 2ac40fb | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 433 | void |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 434 | ContactStorage::updateAlias(const Name& identity, string alias) |
Yingdi Yu | 2ac40fb | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 435 | { |
Yingdi Yu | 71c0187 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 436 | sqlite3_stmt *stmt; |
| 437 | sqlite3_prepare_v2 (m_db, "UPDATE Contact SET contact_alias=? WHERE contact_namespace=?", -1, &stmt, 0); |
| 438 | sqlite3_bind_text(stmt, 1, alias.c_str(), alias.size(), SQLITE_TRANSIENT); |
| 439 | sqlite3_bind_text(stmt, 2, identity.toUri().c_str(), identity.toUri().size (), SQLITE_TRANSIENT); |
| 440 | int res = sqlite3_step (stmt); |
| 441 | sqlite3_finalize (stmt); |
| 442 | return; |
Yingdi Yu | 2ac40fb | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 443 | } |
| 444 | |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 445 | bool |
Yingdi Yu | 71c0187 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 446 | ContactStorage::doesContactExist(const Name& name) |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 447 | { |
| 448 | bool result = false; |
| 449 | |
| 450 | sqlite3_stmt *stmt; |
Yingdi Yu | 71c0187 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 451 | sqlite3_prepare_v2 (m_db, "SELECT count(*) FROM Contact WHERE contact_namespace=?", -1, &stmt, 0); |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 452 | sqlite3_bind_text(stmt, 1, name.toUri().c_str(), name.toUri().size(), SQLITE_TRANSIENT); |
| 453 | |
| 454 | int res = sqlite3_step (stmt); |
| 455 | |
| 456 | if (res == SQLITE_ROW) |
| 457 | { |
| 458 | int countAll = sqlite3_column_int (stmt, 0); |
| 459 | if (countAll > 0) |
| 460 | result = true; |
| 461 | } |
| 462 | sqlite3_finalize (stmt); |
| 463 | return result; |
| 464 | } |
| 465 | |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 466 | void |
| 467 | ContactStorage::getAllContacts(vector<shared_ptr<ContactItem> >& contacts) const |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 468 | { |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 469 | sqlite3_stmt *stmt; |
Yingdi Yu | 71c0187 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 470 | sqlite3_prepare_v2 (m_db, "SELECT contact_alias, self_certificate, is_introducer FROM Contact", -1, &stmt, 0); |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 471 | |
| 472 | while( sqlite3_step (stmt) == SQLITE_ROW) |
| 473 | { |
| 474 | string alias(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0)); |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 475 | |
| 476 | Data certData; |
Yingdi Yu | f8f572d | 2014-01-13 11:19:47 -0800 | [diff] [blame] | 477 | certData.wireDecode(Block(reinterpret_cast<const uint8_t*>(sqlite3_column_text(stmt, 1)), sqlite3_column_bytes (stmt, 1))); |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 478 | EndorseCertificate endorseCertificate(certData); |
| 479 | |
Yingdi Yu | 71c0187 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 480 | int isIntroducer = sqlite3_column_int (stmt, 2); |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 481 | |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 482 | contacts.push_back(make_shared<ContactItem>(endorseCertificate, isIntroducer, alias)); |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 483 | } |
Yingdi Yu | 71c0187 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 484 | sqlite3_finalize (stmt); |
| 485 | |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 486 | vector<shared_ptr<ContactItem> >::iterator it = contacts.begin(); |
Yingdi Yu | 71c0187 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 487 | for(; it != contacts.end(); it++) |
| 488 | { |
| 489 | if((*it)->isIntroducer()) |
| 490 | { |
| 491 | sqlite3_prepare_v2 (m_db, "SELECT trust_scope FROM TrustScope WHERE contact_namespace=?", -1, &stmt, 0); |
| 492 | sqlite3_bind_text(stmt, 1, (*it)->getNameSpace().toUri().c_str(), (*it)->getNameSpace().toUri().size(), SQLITE_TRANSIENT); |
| 493 | |
| 494 | while( sqlite3_step (stmt) == SQLITE_ROW) |
| 495 | { |
| 496 | Name scope(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0))); |
| 497 | (*it)->addTrustScope(scope); |
| 498 | } |
| 499 | sqlite3_finalize (stmt); |
| 500 | } |
| 501 | } |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 502 | } |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 503 | |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 504 | }//chronos |