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