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