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" |
| 12 | #include "exception.h" |
| 13 | |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 14 | #include <boost/filesystem.hpp> |
Yingdi Yu | 92e8e48 | 2013-10-17 21:13:03 -0700 | [diff] [blame] | 15 | #include <ndn.cxx/fields/signature-sha256-with-rsa.h> |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 16 | #include "logging.h" |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 17 | |
| 18 | using namespace std; |
| 19 | using namespace ndn; |
| 20 | namespace fs = boost::filesystem; |
| 21 | |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 22 | INIT_LOGGER ("ContactStorage"); |
| 23 | |
| 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 | |
| 48 | const string INIT_TC_TABLE = "\ |
| 49 | CREATE TABLE IF NOT EXISTS \n \ |
| 50 | TrustedContact( \n \ |
| 51 | contact_namespace BLOB NOT NULL, \n \ |
| 52 | contact_alias BLOB NOT NULL, \n \ |
| 53 | self_certificate BLOB NOT NULL, \n \ |
| 54 | trust_scope BLOB NOT NULL, \n \ |
| 55 | \ |
| 56 | PRIMARY KEY (contact_namespace) \n \ |
| 57 | ); \n \ |
| 58 | \ |
| 59 | CREATE INDEX tc_index ON TrustedContact(contact_namespace); \n \ |
| 60 | "; |
| 61 | |
| 62 | const string INIT_NC_TABLE = "\ |
| 63 | CREATE TABLE IF NOT EXISTS \n \ |
| 64 | NormalContact( \n \ |
| 65 | contact_namespace BLOB NOT NULL, \n \ |
| 66 | contact_alias BLOB NOT NULL, \n \ |
| 67 | self_certificate BLOB NOT NULL, \n \ |
| 68 | \ |
| 69 | PRIMARY KEY (contact_namespace) \n \ |
| 70 | ); \n \ |
| 71 | \ |
| 72 | CREATE INDEX nc_index ON NormalContact(contact_namespace); \n \ |
| 73 | "; |
| 74 | |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 75 | ContactStorage::ContactStorage() |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 76 | { |
| 77 | fs::path chronosDir = fs::path(getenv("HOME")) / ".chronos"; |
| 78 | fs::create_directories (chronosDir); |
| 79 | |
| 80 | int res = sqlite3_open((chronosDir / "chronos.db").c_str (), &m_db); |
| 81 | if (res != SQLITE_OK) |
| 82 | throw LnException("Chronos DB cannot be open/created"); |
| 83 | |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 84 | // Check if SelfProfile table exists |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 85 | sqlite3_stmt *stmt; |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 86 | sqlite3_prepare_v2 (m_db, "SELECT name FROM sqlite_master WHERE type='table' And name='SelfProfile'", -1, &stmt, 0); |
| 87 | res = sqlite3_step (stmt); |
| 88 | |
| 89 | bool spTableExist = false; |
| 90 | if (res == SQLITE_ROW) |
| 91 | spTableExist = true; |
| 92 | sqlite3_finalize (stmt); |
| 93 | |
| 94 | if(!spTableExist) |
| 95 | { |
| 96 | char *errmsg = 0; |
| 97 | res = sqlite3_exec (m_db, INIT_SP_TABLE.c_str (), NULL, NULL, &errmsg); |
| 98 | if (res != SQLITE_OK && errmsg != 0) |
| 99 | throw LnException("Init \"error\" in SelfProfile"); |
| 100 | } |
| 101 | |
Yingdi Yu | ec3d9a3 | 2013-10-18 18:35:09 -0700 | [diff] [blame] | 102 | // Check if SelfEndorse table exists |
| 103 | sqlite3_prepare_v2 (m_db, "SELECT name FROM sqlite_master WHERE type='table' And name='SelfEndorse'", -1, &stmt, 0); |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 104 | res = sqlite3_step (stmt); |
| 105 | |
Yingdi Yu | ec3d9a3 | 2013-10-18 18:35:09 -0700 | [diff] [blame] | 106 | bool seTableExist = false; |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 107 | if (res == SQLITE_ROW) |
Yingdi Yu | ec3d9a3 | 2013-10-18 18:35:09 -0700 | [diff] [blame] | 108 | seTableExist = true; |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 109 | sqlite3_finalize (stmt); |
| 110 | |
Yingdi Yu | ec3d9a3 | 2013-10-18 18:35:09 -0700 | [diff] [blame] | 111 | if(!seTableExist) |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 112 | { |
| 113 | char *errmsg = 0; |
Yingdi Yu | ec3d9a3 | 2013-10-18 18:35:09 -0700 | [diff] [blame] | 114 | res = sqlite3_exec (m_db, INIT_SE_TABLE.c_str (), NULL, NULL, &errmsg); |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 115 | if (res != SQLITE_OK && errmsg != 0) |
Yingdi Yu | ec3d9a3 | 2013-10-18 18:35:09 -0700 | [diff] [blame] | 116 | throw LnException("Init \"error\" in SelfEndorse"); |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | |
| 120 | // Check if TrustedContact table exists |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 121 | sqlite3_prepare_v2 (m_db, "SELECT name FROM sqlite_master WHERE type='table' And name='TrustedContact'", -1, &stmt, 0); |
| 122 | res = sqlite3_step (stmt); |
| 123 | |
| 124 | bool tcTableExist = false; |
| 125 | if (res == SQLITE_ROW) |
| 126 | tcTableExist = true; |
| 127 | sqlite3_finalize (stmt); |
| 128 | |
| 129 | if(!tcTableExist) |
| 130 | { |
| 131 | char *errmsg = 0; |
| 132 | res = sqlite3_exec (m_db, INIT_TC_TABLE.c_str (), NULL, NULL, &errmsg); |
| 133 | if (res != SQLITE_OK && errmsg != 0) |
| 134 | throw LnException("Init \"error\" in TrustedContact"); |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | // Check if NormalContact table exists |
| 138 | sqlite3_prepare_v2 (m_db, "SELECT name FROM sqlite_master WHERE type='table' And name='NormalContact'", -1, &stmt, 0); |
| 139 | res = sqlite3_step (stmt); |
| 140 | |
| 141 | bool ncTableExist = false; |
| 142 | if (res == SQLITE_ROW) |
| 143 | ncTableExist = true; |
| 144 | sqlite3_finalize (stmt); |
| 145 | |
| 146 | if(!ncTableExist) |
| 147 | { |
| 148 | char *errmsg = 0; |
| 149 | res = sqlite3_exec (m_db, INIT_NC_TABLE.c_str (), NULL, NULL, &errmsg); |
| 150 | |
| 151 | if (res != SQLITE_OK && errmsg != 0) |
| 152 | throw LnException("Init \"error\" in NormalContact"); |
| 153 | } |
| 154 | } |
| 155 | |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 156 | bool |
Yingdi Yu | 54dcecc | 2013-10-17 15:07:17 -0700 | [diff] [blame] | 157 | ContactStorage::doesSelfEntryExist(const Name& identity, const string& profileType) |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 158 | { |
| 159 | bool result = false; |
| 160 | |
| 161 | sqlite3_stmt *stmt; |
Yingdi Yu | 54dcecc | 2013-10-17 15:07:17 -0700 | [diff] [blame] | 162 | sqlite3_prepare_v2 (m_db, "SELECT count(*) FROM SelfProfile WHERE profile_type=? and profile_identity=?", -1, &stmt, 0); |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 163 | sqlite3_bind_text(stmt, 1, profileType.c_str(), profileType.size(), SQLITE_TRANSIENT); |
Yingdi Yu | 54dcecc | 2013-10-17 15:07:17 -0700 | [diff] [blame] | 164 | sqlite3_bind_text(stmt, 2, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT); |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 165 | |
| 166 | int res = sqlite3_step (stmt); |
| 167 | |
| 168 | if (res == SQLITE_ROW) |
| 169 | { |
| 170 | int countAll = sqlite3_column_int (stmt, 0); |
| 171 | if (countAll > 0) |
| 172 | result = true; |
| 173 | } |
| 174 | sqlite3_finalize (stmt); |
| 175 | return result; |
| 176 | } |
| 177 | |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 178 | void |
Yingdi Yu | 54dcecc | 2013-10-17 15:07:17 -0700 | [diff] [blame] | 179 | ContactStorage::setSelfProfileEntry(const Name& identity, const string& profileType, const Blob& profileValue) |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 180 | { |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 181 | sqlite3_stmt *stmt; |
Yingdi Yu | 54dcecc | 2013-10-17 15:07:17 -0700 | [diff] [blame] | 182 | if(doesSelfEntryExist(identity, profileType)) |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 183 | { |
Yingdi Yu | 54dcecc | 2013-10-17 15:07:17 -0700 | [diff] [blame] | 184 | sqlite3_prepare_v2 (m_db, "UPDATE SelfProfile SET profile_value=? WHERE profile_type=? and profile_identity=?", -1, &stmt, 0); |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 185 | sqlite3_bind_text(stmt, 1, profileValue.buf(), profileValue.size(), SQLITE_TRANSIENT); |
| 186 | sqlite3_bind_text(stmt, 2, profileType.c_str(), profileType.size(), SQLITE_TRANSIENT); |
Yingdi Yu | 54dcecc | 2013-10-17 15:07:17 -0700 | [diff] [blame] | 187 | sqlite3_bind_text(stmt, 3, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT); |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 188 | } |
| 189 | else |
| 190 | { |
Yingdi Yu | 54dcecc | 2013-10-17 15:07:17 -0700 | [diff] [blame] | 191 | sqlite3_prepare_v2 (m_db, "INSERT INTO SelfProfile (profile_identity, profile_type, profile_value) values (?, ?, ?)", -1, &stmt, 0); |
| 192 | sqlite3_bind_text(stmt, 1, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT); |
| 193 | sqlite3_bind_text(stmt, 2, profileType.c_str(), profileType.size(), SQLITE_TRANSIENT); |
| 194 | sqlite3_bind_text(stmt, 3, profileValue.buf(), profileValue.size(), SQLITE_TRANSIENT); |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 195 | } |
| 196 | sqlite3_step (stmt); |
| 197 | sqlite3_finalize (stmt); |
| 198 | } |
| 199 | |
| 200 | Ptr<Profile> |
Yingdi Yu | 54dcecc | 2013-10-17 15:07:17 -0700 | [diff] [blame] | 201 | ContactStorage::getSelfProfile(const Name& identity) |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 202 | { |
Yingdi Yu | 2ac40fb | 2013-10-21 13:38:38 -0700 | [diff] [blame^] | 203 | _LOG_DEBUG("getSelfProfile " << identity.toUri()); |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 204 | sqlite3_stmt *stmt; |
Yingdi Yu | 54dcecc | 2013-10-17 15:07:17 -0700 | [diff] [blame] | 205 | Ptr<Profile> profile = Ptr<Profile>(new Profile(identity)); |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 206 | |
Yingdi Yu | 2ac40fb | 2013-10-21 13:38:38 -0700 | [diff] [blame^] | 207 | sqlite3_prepare_v2(m_db, "SELECT profile_type, profile_value FROM SelfProfile WHERE profile_identity=?", -1, &stmt, 0); |
Yingdi Yu | 54dcecc | 2013-10-17 15:07:17 -0700 | [diff] [blame] | 208 | 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^] | 209 | |
| 210 | while(sqlite3_step (stmt) == SQLITE_ROW) |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 211 | { |
Yingdi Yu | 2ac40fb | 2013-10-21 13:38:38 -0700 | [diff] [blame^] | 212 | _LOG_DEBUG("entry"); |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 213 | string profileType(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0)); |
| 214 | Blob profileValue(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 1)), sqlite3_column_bytes (stmt, 1)); |
| 215 | |
| 216 | profile->setProfileEntry(profileType, profileValue ); |
| 217 | } |
| 218 | |
| 219 | return profile; |
| 220 | } |
| 221 | |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 222 | void |
| 223 | ContactStorage::addTrustedContact(const TrustedContact& trustedContact) |
| 224 | { |
| 225 | if(doesTrustedContactExist(trustedContact.getNameSpace())) |
| 226 | throw LnException("Trusted Contact has already existed"); |
| 227 | |
| 228 | sqlite3_stmt *stmt; |
| 229 | sqlite3_prepare_v2 (m_db, |
| 230 | "INSERT INTO TrustedContact (contact_namespace, contact_alias, self_certificate, trust_scope) values (?, ?, ?, ?)", |
| 231 | -1, |
| 232 | &stmt, |
| 233 | 0); |
| 234 | |
| 235 | sqlite3_bind_text(stmt, 1, trustedContact.getNameSpace().toUri().c_str(), trustedContact.getNameSpace().toUri().size (), SQLITE_TRANSIENT); |
| 236 | sqlite3_bind_text(stmt, 2, trustedContact.getAlias().c_str(), trustedContact.getAlias().size(), SQLITE_TRANSIENT); |
| 237 | Ptr<Blob> selfCertificateBlob = trustedContact.getSelfEndorseCertificate().encodeToWire(); |
| 238 | sqlite3_bind_text(stmt, 3, selfCertificateBlob->buf(), selfCertificateBlob->size(), SQLITE_TRANSIENT); |
| 239 | Ptr<Blob> trustScopeBlob = trustedContact.getTrustScopeBlob(); |
| 240 | sqlite3_bind_text(stmt, 4, trustScopeBlob->buf(), trustScopeBlob->size(),SQLITE_TRANSIENT); |
| 241 | |
| 242 | int res = sqlite3_step (stmt); |
| 243 | sqlite3_finalize (stmt); |
| 244 | } |
| 245 | |
| 246 | void |
| 247 | ContactStorage::addNormalContact(const ContactItem& normalContact) |
| 248 | { |
| 249 | if(doesNormalContactExist(normalContact.getNameSpace())) |
| 250 | throw LnException("Normal Contact has already existed"); |
| 251 | |
| 252 | sqlite3_stmt *stmt; |
| 253 | sqlite3_prepare_v2 (m_db, |
| 254 | "INSERT INTO NormalContact (contact_namespace, contact_alias, self_certificate) values (?, ?, ?)", |
| 255 | -1, |
| 256 | &stmt, |
| 257 | 0); |
| 258 | |
| 259 | sqlite3_bind_text(stmt, 1, normalContact.getNameSpace().toUri().c_str(), normalContact.getNameSpace().toUri().size (), SQLITE_TRANSIENT); |
| 260 | sqlite3_bind_text(stmt, 2, normalContact.getAlias().c_str(), normalContact.getAlias().size(), SQLITE_TRANSIENT); |
| 261 | Ptr<Blob> selfCertificateBlob = normalContact.getSelfEndorseCertificate().encodeToWire(); |
| 262 | sqlite3_bind_text(stmt, 3, selfCertificateBlob->buf(), selfCertificateBlob->size(), SQLITE_TRANSIENT); |
| 263 | |
| 264 | int res = sqlite3_step (stmt); |
Yingdi Yu | 2ac40fb | 2013-10-21 13:38:38 -0700 | [diff] [blame^] | 265 | // _LOG_DEBUG("res " << res); |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 266 | sqlite3_finalize (stmt); |
| 267 | } |
| 268 | |
Yingdi Yu | 2ac40fb | 2013-10-21 13:38:38 -0700 | [diff] [blame^] | 269 | void |
| 270 | ContactStorage::updateAlias(const ndn::Name& identity, std::string alias) |
| 271 | { |
| 272 | if(doesNormalContactExist(identity)) |
| 273 | { |
| 274 | sqlite3_stmt *stmt; |
| 275 | sqlite3_prepare_v2 (m_db, "UPDATE NormalContact SET contact_alias=? WHERE contact_namespace=?", -1, &stmt, 0); |
| 276 | sqlite3_bind_text(stmt, 1, alias.c_str(), alias.size(), SQLITE_TRANSIENT); |
| 277 | sqlite3_bind_text(stmt, 2, identity.toUri().c_str(), identity.toUri().size (), SQLITE_TRANSIENT); |
| 278 | int res = sqlite3_step (stmt); |
| 279 | sqlite3_finalize (stmt); |
| 280 | return; |
| 281 | } |
| 282 | if(doesTrustedContactExist(identity)) |
| 283 | { |
| 284 | sqlite3_stmt *stmt; |
| 285 | sqlite3_prepare_v2 (m_db, "UPDATE TrustedContact SET contact_alias=? WHERE contact_namespace=?", -1, &stmt, 0); |
| 286 | sqlite3_bind_text(stmt, 1, alias.c_str(), alias.size(), SQLITE_TRANSIENT); |
| 287 | sqlite3_bind_text(stmt, 2, identity.toUri().c_str(), identity.toUri().size (), SQLITE_TRANSIENT); |
| 288 | int res = sqlite3_step (stmt); |
| 289 | sqlite3_finalize (stmt); |
| 290 | return; |
| 291 | } |
| 292 | } |
| 293 | |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 294 | bool |
| 295 | ContactStorage::doesContactExist(const Name& name, bool normal) |
| 296 | { |
| 297 | bool result = false; |
| 298 | |
| 299 | sqlite3_stmt *stmt; |
| 300 | if(normal) |
| 301 | sqlite3_prepare_v2 (m_db, "SELECT count(*) FROM NormalContact WHERE contact_namespace=?", -1, &stmt, 0); |
| 302 | else |
| 303 | sqlite3_prepare_v2 (m_db, "SELECT count(*) FROM TrustedContact WHERE contact_namespace=?", -1, &stmt, 0); |
| 304 | sqlite3_bind_text(stmt, 1, name.toUri().c_str(), name.toUri().size(), SQLITE_TRANSIENT); |
| 305 | |
| 306 | int res = sqlite3_step (stmt); |
| 307 | |
| 308 | if (res == SQLITE_ROW) |
| 309 | { |
| 310 | int countAll = sqlite3_column_int (stmt, 0); |
| 311 | if (countAll > 0) |
| 312 | result = true; |
| 313 | } |
| 314 | sqlite3_finalize (stmt); |
| 315 | return result; |
| 316 | } |
| 317 | |
| 318 | vector<Ptr<TrustedContact> > |
| 319 | ContactStorage::getAllTrustedContacts() const |
| 320 | { |
| 321 | vector<Ptr<TrustedContact> > trustedContacts; |
| 322 | |
| 323 | sqlite3_stmt *stmt; |
| 324 | sqlite3_prepare_v2 (m_db, |
| 325 | "SELECT contact_alias, self_certificate, trust_scope FROM TrustedContact", |
| 326 | -1, |
| 327 | &stmt, |
| 328 | 0); |
| 329 | |
| 330 | while( sqlite3_step (stmt) == SQLITE_ROW) |
| 331 | { |
| 332 | string alias(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0)); |
| 333 | Ptr<Blob> certBlob = Ptr<Blob>(new Blob(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 1)), sqlite3_column_bytes (stmt, 1))); |
| 334 | Ptr<Data> certData = Data::decodeFromWire(certBlob); |
| 335 | EndorseCertificate endorseCertificate(*certData); |
| 336 | string trustScope(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 2)), sqlite3_column_bytes (stmt, 2)); |
| 337 | |
| 338 | trustedContacts.push_back(Ptr<TrustedContact>(new TrustedContact(endorseCertificate, trustScope, alias))); |
| 339 | } |
| 340 | |
| 341 | return trustedContacts; |
| 342 | } |
| 343 | |
| 344 | vector<Ptr<ContactItem> > |
| 345 | ContactStorage::getAllNormalContacts() const |
| 346 | { |
| 347 | vector<Ptr<ContactItem> > normalContacts; |
| 348 | |
| 349 | sqlite3_stmt *stmt; |
| 350 | sqlite3_prepare_v2 (m_db, |
| 351 | "SELECT contact_alias, self_certificate FROM NormalContact", |
| 352 | -1, |
| 353 | &stmt, |
| 354 | 0); |
| 355 | |
| 356 | while( sqlite3_step (stmt) == SQLITE_ROW) |
| 357 | { |
| 358 | string alias(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0)); |
| 359 | Ptr<Blob> certBlob = Ptr<Blob>(new Blob(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 1)), sqlite3_column_bytes (stmt, 1))); |
| 360 | Ptr<Data> certData = Data::decodeFromWire(certBlob); |
| 361 | EndorseCertificate endorseCertificate(*certData); |
| 362 | |
| 363 | normalContacts.push_back(Ptr<ContactItem>(new ContactItem(endorseCertificate, alias))); |
| 364 | } |
| 365 | |
| 366 | return normalContacts; |
| 367 | } |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 368 | |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 369 | Ptr<Profile> |
Yingdi Yu | 54dcecc | 2013-10-17 15:07:17 -0700 | [diff] [blame] | 370 | ContactStorage::getSelfProfile(const Name& identity) const |
| 371 | { |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 372 | Ptr<Profile> profile = Ptr<Profile>(new Profile(identity)); |
Yingdi Yu | 54dcecc | 2013-10-17 15:07:17 -0700 | [diff] [blame] | 373 | sqlite3_stmt *stmt; |
| 374 | sqlite3_prepare_v2 (m_db, "SELECT profile_type, profile_value FROM SelfProfile WHERE profile_identity=?", -1, &stmt, 0); |
| 375 | sqlite3_bind_text (stmt, 1, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT); |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 376 | |
| 377 | while( sqlite3_step (stmt) == SQLITE_ROW) |
| 378 | { |
| 379 | string profileType(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0)); |
| 380 | Blob profileValue(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 1)), sqlite3_column_bytes (stmt, 1)); |
| 381 | profile->setProfileEntry(profileType, profileValue); |
| 382 | } |
| 383 | |
| 384 | return profile; |
| 385 | } |
| 386 | |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 387 | Ptr<Blob> |
| 388 | ContactStorage::getSelfEndorseCertificate(const Name& identity) |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 389 | { |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 390 | sqlite3_stmt *stmt; |
Yingdi Yu | ec3d9a3 | 2013-10-18 18:35:09 -0700 | [diff] [blame] | 391 | sqlite3_prepare_v2 (m_db, "SELECT endorse_data FROM SelfEndorse where identity=?", -1, &stmt, 0); |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 392 | sqlite3_bind_text(stmt, 1, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT); |
Yingdi Yu | 54dcecc | 2013-10-17 15:07:17 -0700 | [diff] [blame] | 393 | |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 394 | Ptr<Blob> result = NULL; |
| 395 | if(sqlite3_step (stmt) == SQLITE_ROW) |
| 396 | result = Ptr<Blob>(new Blob(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0))); |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 397 | |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 398 | sqlite3_finalize (stmt); |
Yingdi Yu | 92e8e48 | 2013-10-17 21:13:03 -0700 | [diff] [blame] | 399 | |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 400 | return result; |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 401 | } |
| 402 | |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 403 | void |
| 404 | ContactStorage::updateSelfEndorseCertificate(Ptr<EndorseCertificate> newEndorseCertificate, const Name& identity) |
| 405 | { |
| 406 | Ptr<Blob> newEndorseCertificateBlob = newEndorseCertificate->encodeToWire(); |
| 407 | |
| 408 | sqlite3_stmt *stmt; |
Yingdi Yu | ec3d9a3 | 2013-10-18 18:35:09 -0700 | [diff] [blame] | 409 | sqlite3_prepare_v2 (m_db, "UPDATE SelfEndorse SET endorse_data=? WHERE identity=?", -1, &stmt, 0); |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 410 | sqlite3_bind_text(stmt, 1, newEndorseCertificateBlob->buf(), newEndorseCertificateBlob->size(), SQLITE_TRANSIENT); |
| 411 | sqlite3_bind_text(stmt, 2, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT); |
| 412 | sqlite3_step(stmt); |
| 413 | } |
| 414 | |
| 415 | void |
| 416 | ContactStorage::addSelfEndorseCertificate(Ptr<EndorseCertificate> newEndorseCertificate, const Name& identity) |
| 417 | { |
| 418 | Ptr<Blob> newEndorseCertificateBlob = newEndorseCertificate->encodeToWire(); |
| 419 | |
| 420 | sqlite3_stmt *stmt; |
Yingdi Yu | ec3d9a3 | 2013-10-18 18:35:09 -0700 | [diff] [blame] | 421 | sqlite3_prepare_v2 (m_db, "INSERT INTO SelfEndorse (identity, endorse_data) values (?, ?)", -1, &stmt, 0); |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 422 | sqlite3_bind_text(stmt, 1, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT); |
| 423 | sqlite3_bind_text(stmt, 2, newEndorseCertificateBlob->buf(), newEndorseCertificateBlob->size(), SQLITE_TRANSIENT); |
| 424 | sqlite3_step(stmt); |
| 425 | } |