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 | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame^] | 59 | CREATE INDEX contact_index ON Contact(contact_namespace); \n \ |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 60 | "; |
| 61 | |
Yingdi Yu | 71c0187 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 62 | const string INIT_TS_TABLE = "\ |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 63 | CREATE TABLE IF NOT EXISTS \n \ |
Yingdi Yu | 71c0187 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 64 | TrustScope( \n \ |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame^] | 65 | id INTEGER PRIMARY KEY AUTOINCREMENT, \n \ |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 66 | contact_namespace BLOB NOT NULL, \n \ |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame^] | 67 | trust_scope BLOB NOT NULL \n \ |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 68 | ); \n \ |
| 69 | \ |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame^] | 70 | CREATE INDEX ts_index ON TrustScope(contact_namespace); \n \ |
| 71 | "; |
| 72 | |
| 73 | const string INIT_CP_TABLE = "\ |
| 74 | CREATE TABLE IF NOT EXISTS \n \ |
| 75 | ContactProfile( \n \ |
| 76 | profile_identity BLOB NOT NULL, \n \ |
| 77 | profile_type BLOB NOT NULL, \n \ |
| 78 | profile_value BLOB NOT NULL, \n \ |
| 79 | endorse INTEGER NOT NULL, \n \ |
| 80 | \ |
| 81 | PRIMARY KEY (profile_identity, profile_type) \n \ |
| 82 | ); \n \ |
| 83 | \ |
| 84 | CREATE INDEX cp_index ON ContactProfile(profile_identity); \n \ |
| 85 | "; |
| 86 | |
| 87 | const string INIT_PE_TABLE = "\ |
| 88 | CREATE TABLE IF NOT EXISTS \n \ |
| 89 | ProfileEndorse( \n \ |
| 90 | identity BLOB NOT NULL UNIQUE, \n \ |
| 91 | endorse_data BLOB NOT NULL, \n \ |
| 92 | \ |
| 93 | PRIMARY KEY (identity) \n \ |
| 94 | ); \n \ |
| 95 | \ |
| 96 | CREATE INDEX pe_index ON ProfileEndorse(identity); \n \ |
| 97 | "; |
| 98 | |
| 99 | const string INIT_CE_TABLE = "\ |
| 100 | CREATE TABLE IF NOT EXISTS \n \ |
| 101 | CollectEndorse( \n \ |
| 102 | endorsee BLOB NOT NULL, \n \ |
| 103 | endorser BLOB NOT NULL, \n \ |
| 104 | endorse_name BLOB NOT NULL, \n \ |
| 105 | endorse_data BLOB NOT NULL, \n \ |
| 106 | \ |
| 107 | PRIMARY KEY (endorsee, endorser) \n \ |
| 108 | ); \n \ |
| 109 | \ |
| 110 | CREATE INDEX ce_index ON CollectEndorse(endorsee); \n \ |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 111 | "; |
| 112 | |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 113 | ContactStorage::ContactStorage() |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 114 | { |
| 115 | fs::path chronosDir = fs::path(getenv("HOME")) / ".chronos"; |
| 116 | fs::create_directories (chronosDir); |
| 117 | |
| 118 | int res = sqlite3_open((chronosDir / "chronos.db").c_str (), &m_db); |
| 119 | if (res != SQLITE_OK) |
| 120 | throw LnException("Chronos DB cannot be open/created"); |
| 121 | |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 122 | // Check if SelfProfile table exists |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 123 | sqlite3_stmt *stmt; |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 124 | sqlite3_prepare_v2 (m_db, "SELECT name FROM sqlite_master WHERE type='table' And name='SelfProfile'", -1, &stmt, 0); |
| 125 | res = sqlite3_step (stmt); |
| 126 | |
| 127 | bool spTableExist = false; |
| 128 | if (res == SQLITE_ROW) |
| 129 | spTableExist = true; |
| 130 | sqlite3_finalize (stmt); |
| 131 | |
| 132 | if(!spTableExist) |
| 133 | { |
| 134 | char *errmsg = 0; |
| 135 | res = sqlite3_exec (m_db, INIT_SP_TABLE.c_str (), NULL, NULL, &errmsg); |
| 136 | if (res != SQLITE_OK && errmsg != 0) |
| 137 | throw LnException("Init \"error\" in SelfProfile"); |
| 138 | } |
| 139 | |
Yingdi Yu | ec3d9a3 | 2013-10-18 18:35:09 -0700 | [diff] [blame] | 140 | // Check if SelfEndorse table exists |
| 141 | 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] | 142 | res = sqlite3_step (stmt); |
| 143 | |
Yingdi Yu | ec3d9a3 | 2013-10-18 18:35:09 -0700 | [diff] [blame] | 144 | bool seTableExist = false; |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 145 | if (res == SQLITE_ROW) |
Yingdi Yu | ec3d9a3 | 2013-10-18 18:35:09 -0700 | [diff] [blame] | 146 | seTableExist = true; |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 147 | sqlite3_finalize (stmt); |
| 148 | |
Yingdi Yu | ec3d9a3 | 2013-10-18 18:35:09 -0700 | [diff] [blame] | 149 | if(!seTableExist) |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 150 | { |
| 151 | char *errmsg = 0; |
Yingdi Yu | ec3d9a3 | 2013-10-18 18:35:09 -0700 | [diff] [blame] | 152 | 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] | 153 | if (res != SQLITE_OK && errmsg != 0) |
Yingdi Yu | ec3d9a3 | 2013-10-18 18:35:09 -0700 | [diff] [blame] | 154 | throw LnException("Init \"error\" in SelfEndorse"); |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame^] | 158 | // Check if Contact table exists |
Yingdi Yu | 71c0187 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 159 | 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] | 160 | res = sqlite3_step (stmt); |
| 161 | |
Yingdi Yu | 71c0187 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 162 | bool contactTableExist = false; |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 163 | if (res == SQLITE_ROW) |
Yingdi Yu | 71c0187 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 164 | contactTableExist = true; |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 165 | sqlite3_finalize (stmt); |
| 166 | |
Yingdi Yu | 71c0187 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 167 | if(!contactTableExist) |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 168 | { |
| 169 | char *errmsg = 0; |
Yingdi Yu | 71c0187 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 170 | 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] | 171 | if (res != SQLITE_OK && errmsg != 0) |
Yingdi Yu | 71c0187 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 172 | throw LnException("Init \"error\" in Contact"); |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 173 | } |
Yingdi Yu | 71c0187 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 174 | |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame^] | 175 | // Check if TrustScope table exists |
Yingdi Yu | 71c0187 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 176 | 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] | 177 | res = sqlite3_step (stmt); |
| 178 | |
Yingdi Yu | 71c0187 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 179 | bool tsTableExist = false; |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 180 | if (res == SQLITE_ROW) |
Yingdi Yu | 71c0187 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 181 | tsTableExist = true; |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 182 | sqlite3_finalize (stmt); |
| 183 | |
Yingdi Yu | 71c0187 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 184 | if(!tsTableExist) |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 185 | { |
| 186 | char *errmsg = 0; |
Yingdi Yu | 71c0187 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 187 | 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] | 188 | if (res != SQLITE_OK && errmsg != 0) |
Yingdi Yu | 71c0187 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 189 | throw LnException("Init \"error\" in TrustScope"); |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 190 | } |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame^] | 191 | |
| 192 | // Check if ContactProfile table exists |
| 193 | sqlite3_prepare_v2 (m_db, "SELECT name FROM sqlite_master WHERE type='table' And name='ContactProfile'", -1, &stmt, 0); |
| 194 | res = sqlite3_step (stmt); |
| 195 | |
| 196 | bool cpTableExist = false; |
| 197 | if (res == SQLITE_ROW) |
| 198 | cpTableExist = true; |
| 199 | sqlite3_finalize (stmt); |
| 200 | |
| 201 | if(!cpTableExist) |
| 202 | { |
| 203 | char *errmsg = 0; |
| 204 | res = sqlite3_exec (m_db, INIT_CP_TABLE.c_str (), NULL, NULL, &errmsg); |
| 205 | if (res != SQLITE_OK && errmsg != 0) |
| 206 | throw LnException("Init \"error\" in ContactProfile"); |
| 207 | } |
| 208 | |
| 209 | // Check if ProfileEndorse table exists |
| 210 | sqlite3_prepare_v2 (m_db, "SELECT name FROM sqlite_master WHERE type='table' And name='ProfileEndorse'", -1, &stmt, 0); |
| 211 | res = sqlite3_step (stmt); |
| 212 | |
| 213 | bool peTableExist = false; |
| 214 | if (res == SQLITE_ROW) |
| 215 | peTableExist = true; |
| 216 | sqlite3_finalize (stmt); |
| 217 | |
| 218 | if(!peTableExist) |
| 219 | { |
| 220 | char *errmsg = 0; |
| 221 | res = sqlite3_exec (m_db, INIT_PE_TABLE.c_str (), NULL, NULL, &errmsg); |
| 222 | if (res != SQLITE_OK && errmsg != 0) |
| 223 | throw LnException("Init \"error\" in ProfileEndorse"); |
| 224 | } |
| 225 | |
| 226 | // Check if CollectEndorse table exists |
| 227 | sqlite3_prepare_v2 (m_db, "SELECT name FROM sqlite_master WHERE type='table' And name='CollectEndorse'", -1, &stmt, 0); |
| 228 | res = sqlite3_step (stmt); |
| 229 | |
| 230 | bool ceTableExist = false; |
| 231 | if (res == SQLITE_ROW) |
| 232 | ceTableExist = true; |
| 233 | sqlite3_finalize (stmt); |
| 234 | |
| 235 | if(!ceTableExist) |
| 236 | { |
| 237 | char *errmsg = 0; |
| 238 | res = sqlite3_exec (m_db, INIT_CE_TABLE.c_str (), NULL, NULL, &errmsg); |
| 239 | if (res != SQLITE_OK && errmsg != 0) |
| 240 | throw LnException("Init \"error\" in CollectEndorse"); |
| 241 | } |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 242 | } |
| 243 | |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 244 | bool |
Yingdi Yu | 54dcecc | 2013-10-17 15:07:17 -0700 | [diff] [blame] | 245 | ContactStorage::doesSelfEntryExist(const Name& identity, const string& profileType) |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 246 | { |
| 247 | bool result = false; |
| 248 | |
| 249 | sqlite3_stmt *stmt; |
Yingdi Yu | 54dcecc | 2013-10-17 15:07:17 -0700 | [diff] [blame] | 250 | 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] | 251 | sqlite3_bind_text(stmt, 1, profileType.c_str(), profileType.size(), SQLITE_TRANSIENT); |
Yingdi Yu | 54dcecc | 2013-10-17 15:07:17 -0700 | [diff] [blame] | 252 | 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] | 253 | |
| 254 | int res = sqlite3_step (stmt); |
| 255 | |
| 256 | if (res == SQLITE_ROW) |
| 257 | { |
| 258 | int countAll = sqlite3_column_int (stmt, 0); |
| 259 | if (countAll > 0) |
| 260 | result = true; |
| 261 | } |
| 262 | sqlite3_finalize (stmt); |
| 263 | return result; |
| 264 | } |
| 265 | |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 266 | void |
Yingdi Yu | 54dcecc | 2013-10-17 15:07:17 -0700 | [diff] [blame] | 267 | ContactStorage::setSelfProfileEntry(const Name& identity, const string& profileType, const Blob& profileValue) |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 268 | { |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 269 | sqlite3_stmt *stmt; |
Yingdi Yu | 54dcecc | 2013-10-17 15:07:17 -0700 | [diff] [blame] | 270 | if(doesSelfEntryExist(identity, profileType)) |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 271 | { |
Yingdi Yu | 54dcecc | 2013-10-17 15:07:17 -0700 | [diff] [blame] | 272 | 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] | 273 | sqlite3_bind_text(stmt, 1, profileValue.buf(), profileValue.size(), SQLITE_TRANSIENT); |
| 274 | sqlite3_bind_text(stmt, 2, profileType.c_str(), profileType.size(), SQLITE_TRANSIENT); |
Yingdi Yu | 54dcecc | 2013-10-17 15:07:17 -0700 | [diff] [blame] | 275 | 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] | 276 | } |
| 277 | else |
| 278 | { |
Yingdi Yu | 54dcecc | 2013-10-17 15:07:17 -0700 | [diff] [blame] | 279 | sqlite3_prepare_v2 (m_db, "INSERT INTO SelfProfile (profile_identity, profile_type, profile_value) values (?, ?, ?)", -1, &stmt, 0); |
| 280 | sqlite3_bind_text(stmt, 1, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT); |
| 281 | sqlite3_bind_text(stmt, 2, profileType.c_str(), profileType.size(), SQLITE_TRANSIENT); |
| 282 | sqlite3_bind_text(stmt, 3, profileValue.buf(), profileValue.size(), SQLITE_TRANSIENT); |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 283 | } |
| 284 | sqlite3_step (stmt); |
| 285 | sqlite3_finalize (stmt); |
| 286 | } |
| 287 | |
| 288 | Ptr<Profile> |
Yingdi Yu | 54dcecc | 2013-10-17 15:07:17 -0700 | [diff] [blame] | 289 | ContactStorage::getSelfProfile(const Name& identity) |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 290 | { |
Yingdi Yu | 2ac40fb | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 291 | _LOG_DEBUG("getSelfProfile " << identity.toUri()); |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 292 | sqlite3_stmt *stmt; |
Yingdi Yu | 54dcecc | 2013-10-17 15:07:17 -0700 | [diff] [blame] | 293 | Ptr<Profile> profile = Ptr<Profile>(new Profile(identity)); |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 294 | |
Yingdi Yu | 2ac40fb | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 295 | 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] | 296 | 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] | 297 | |
| 298 | while(sqlite3_step (stmt) == SQLITE_ROW) |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 299 | { |
Yingdi Yu | 2ac40fb | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 300 | _LOG_DEBUG("entry"); |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 301 | string profileType(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0)); |
| 302 | Blob profileValue(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 1)), sqlite3_column_bytes (stmt, 1)); |
| 303 | |
| 304 | profile->setProfileEntry(profileType, profileValue ); |
| 305 | } |
| 306 | |
| 307 | return profile; |
| 308 | } |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 309 | |
| 310 | void |
Yingdi Yu | 71c0187 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 311 | ContactStorage::addContact(const ContactItem& contact) |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 312 | { |
Yingdi Yu | 71c0187 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 313 | if(doesContactExist(contact.getNameSpace())) |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 314 | throw LnException("Normal Contact has already existed"); |
| 315 | |
Yingdi Yu | 71c0187 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 316 | bool isIntroducer = contact.isIntroducer(); |
| 317 | |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 318 | sqlite3_stmt *stmt; |
| 319 | sqlite3_prepare_v2 (m_db, |
Yingdi Yu | 71c0187 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 320 | "INSERT INTO Contact (contact_namespace, contact_alias, self_certificate, is_introducer) values (?, ?, ?, ?)", |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 321 | -1, |
| 322 | &stmt, |
| 323 | 0); |
| 324 | |
Yingdi Yu | 71c0187 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 325 | sqlite3_bind_text(stmt, 1, contact.getNameSpace().toUri().c_str(), contact.getNameSpace().toUri().size (), SQLITE_TRANSIENT); |
| 326 | sqlite3_bind_text(stmt, 2, contact.getAlias().c_str(), contact.getAlias().size(), SQLITE_TRANSIENT); |
| 327 | Ptr<Blob> selfCertificateBlob = contact.getSelfEndorseCertificate().encodeToWire(); |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 328 | sqlite3_bind_text(stmt, 3, selfCertificateBlob->buf(), selfCertificateBlob->size(), SQLITE_TRANSIENT); |
Yingdi Yu | 71c0187 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 329 | sqlite3_bind_int(stmt, 4, (isIntroducer ? 1 : 0)); |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 330 | |
| 331 | int res = sqlite3_step (stmt); |
Yingdi Yu | 2ac40fb | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 332 | // _LOG_DEBUG("res " << res); |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 333 | sqlite3_finalize (stmt); |
Yingdi Yu | 71c0187 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 334 | |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame^] | 335 | Ptr<ProfileData> profileData = contact.getSelfEndorseCertificate().getProfileData(); |
| 336 | const Profile& profile = profileData->getProfile(); |
| 337 | Profile::const_iterator it = profile.begin(); |
| 338 | string identity = contact.getNameSpace().toUri(); |
| 339 | for(; it != profile.end(); it++) |
| 340 | { |
| 341 | sqlite3_prepare_v2 (m_db, |
| 342 | "INSERT INTO ContactProfile (profile_identity, profile_type, profile_value, endorse) values (?, ?, ?, 0)", |
| 343 | -1, |
| 344 | &stmt, |
| 345 | 0); |
| 346 | sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size (), SQLITE_TRANSIENT); |
| 347 | sqlite3_bind_text(stmt, 2, it->first.c_str(), it->first.size(), SQLITE_TRANSIENT); |
| 348 | sqlite3_bind_text(stmt, 3, it->second.buf(), it->second.size(), SQLITE_TRANSIENT); |
| 349 | res = sqlite3_step (stmt); |
| 350 | sqlite3_finalize (stmt); |
| 351 | } |
| 352 | |
Yingdi Yu | 71c0187 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 353 | if(isIntroducer) |
| 354 | { |
| 355 | const vector<Name>& scopeList = contact.getTrustScopeList(); |
| 356 | vector<Name>::const_iterator it = scopeList.begin(); |
| 357 | string nameSpace = contact.getNameSpace().toUri(); |
| 358 | |
| 359 | while(it != scopeList.end()) |
| 360 | { |
| 361 | sqlite3_prepare_v2 (m_db, |
| 362 | "INSERT INTO TrustScope (contact_namespace, trust_scope) values (?, ?)", |
| 363 | -1, |
| 364 | &stmt, |
| 365 | 0); |
| 366 | sqlite3_bind_text(stmt, 1, nameSpace.c_str(), nameSpace.size (), SQLITE_TRANSIENT); |
| 367 | sqlite3_bind_text(stmt, 2, it->toUri().c_str(), it->toUri().size(), SQLITE_TRANSIENT); |
| 368 | res = sqlite3_step (stmt); |
| 369 | sqlite3_finalize (stmt); |
| 370 | it++; |
| 371 | } |
| 372 | } |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 373 | } |
| 374 | |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame^] | 375 | void |
| 376 | ContactStorage::updateIsIntroducer(const ndn::Name& identity, bool isIntroducer) |
| 377 | { |
| 378 | sqlite3_stmt *stmt; |
| 379 | sqlite3_prepare_v2 (m_db, "UPDATE Contact SET is_introducer=? WHERE contact_namespace=?", -1, &stmt, 0); |
| 380 | sqlite3_bind_int(stmt, 1, (isIntroducer ? 1 : 0)); |
| 381 | sqlite3_bind_text(stmt, 2, identity.toUri().c_str(), identity.toUri().size (), SQLITE_TRANSIENT); |
| 382 | int res = sqlite3_step (stmt); |
| 383 | sqlite3_finalize (stmt); |
| 384 | return; |
| 385 | } |
| 386 | |
Yingdi Yu | 2ac40fb | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 387 | void |
| 388 | ContactStorage::updateAlias(const ndn::Name& identity, std::string alias) |
| 389 | { |
Yingdi Yu | 71c0187 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 390 | sqlite3_stmt *stmt; |
| 391 | sqlite3_prepare_v2 (m_db, "UPDATE Contact SET contact_alias=? WHERE contact_namespace=?", -1, &stmt, 0); |
| 392 | sqlite3_bind_text(stmt, 1, alias.c_str(), alias.size(), SQLITE_TRANSIENT); |
| 393 | sqlite3_bind_text(stmt, 2, identity.toUri().c_str(), identity.toUri().size (), SQLITE_TRANSIENT); |
| 394 | int res = sqlite3_step (stmt); |
| 395 | sqlite3_finalize (stmt); |
| 396 | return; |
Yingdi Yu | 2ac40fb | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 397 | } |
| 398 | |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 399 | bool |
Yingdi Yu | 71c0187 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 400 | ContactStorage::doesContactExist(const Name& name) |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 401 | { |
| 402 | bool result = false; |
| 403 | |
| 404 | sqlite3_stmt *stmt; |
Yingdi Yu | 71c0187 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 405 | 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] | 406 | sqlite3_bind_text(stmt, 1, name.toUri().c_str(), name.toUri().size(), SQLITE_TRANSIENT); |
| 407 | |
| 408 | int res = sqlite3_step (stmt); |
| 409 | |
| 410 | if (res == SQLITE_ROW) |
| 411 | { |
| 412 | int countAll = sqlite3_column_int (stmt, 0); |
| 413 | if (countAll > 0) |
| 414 | result = true; |
| 415 | } |
| 416 | sqlite3_finalize (stmt); |
| 417 | return result; |
| 418 | } |
| 419 | |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 420 | vector<Ptr<ContactItem> > |
Yingdi Yu | 71c0187 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 421 | ContactStorage::getAllContacts() const |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 422 | { |
Yingdi Yu | 71c0187 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 423 | vector<Ptr<ContactItem> > contacts; |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 424 | |
| 425 | sqlite3_stmt *stmt; |
Yingdi Yu | 71c0187 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 426 | 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] | 427 | |
| 428 | while( sqlite3_step (stmt) == SQLITE_ROW) |
| 429 | { |
| 430 | string alias(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0)); |
| 431 | Ptr<Blob> certBlob = Ptr<Blob>(new Blob(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 1)), sqlite3_column_bytes (stmt, 1))); |
| 432 | Ptr<Data> certData = Data::decodeFromWire(certBlob); |
| 433 | EndorseCertificate endorseCertificate(*certData); |
Yingdi Yu | 71c0187 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 434 | int isIntroducer = sqlite3_column_int (stmt, 2); |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 435 | |
Yingdi Yu | 71c0187 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 436 | contacts.push_back(Ptr<ContactItem>(new ContactItem(endorseCertificate, isIntroducer, alias))); |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 437 | } |
Yingdi Yu | 71c0187 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 438 | sqlite3_finalize (stmt); |
| 439 | |
| 440 | vector<Ptr<ContactItem> >::iterator it = contacts.begin(); |
| 441 | for(; it != contacts.end(); it++) |
| 442 | { |
| 443 | if((*it)->isIntroducer()) |
| 444 | { |
| 445 | sqlite3_prepare_v2 (m_db, "SELECT trust_scope FROM TrustScope WHERE contact_namespace=?", -1, &stmt, 0); |
| 446 | sqlite3_bind_text(stmt, 1, (*it)->getNameSpace().toUri().c_str(), (*it)->getNameSpace().toUri().size(), SQLITE_TRANSIENT); |
| 447 | |
| 448 | while( sqlite3_step (stmt) == SQLITE_ROW) |
| 449 | { |
| 450 | Name scope(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0))); |
| 451 | (*it)->addTrustScope(scope); |
| 452 | } |
| 453 | sqlite3_finalize (stmt); |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | return contacts; |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 458 | } |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 459 | |
Yingdi Yu | 4ef8cf6 | 2013-10-23 14:05:12 -0700 | [diff] [blame] | 460 | Ptr<ContactItem> |
Yingdi Yu | 71c0187 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 461 | ContactStorage::getContact(const Name& name) |
Yingdi Yu | 4ef8cf6 | 2013-10-23 14:05:12 -0700 | [diff] [blame] | 462 | { |
| 463 | sqlite3_stmt *stmt; |
Yingdi Yu | 71c0187 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 464 | 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] | 465 | sqlite3_bind_text (stmt, 1, name.toUri().c_str(), name.toUri().size(), SQLITE_TRANSIENT); |
| 466 | |
| 467 | if( sqlite3_step (stmt) == SQLITE_ROW) |
| 468 | { |
| 469 | string alias(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0)); |
| 470 | Ptr<Blob> certBlob = Ptr<Blob>(new Blob(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 1)), sqlite3_column_bytes (stmt, 1))); |
| 471 | Ptr<Data> certData = Data::decodeFromWire(certBlob); |
| 472 | EndorseCertificate endorseCertificate(*certData); |
Yingdi Yu | 71c0187 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 473 | int isIntroducer = sqlite3_column_int (stmt, 2); |
Yingdi Yu | 4ef8cf6 | 2013-10-23 14:05:12 -0700 | [diff] [blame] | 474 | |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame^] | 475 | sqlite3_finalize (stmt); |
| 476 | |
| 477 | Ptr<ContactItem> contact = Ptr<ContactItem>(new ContactItem(endorseCertificate, isIntroducer, alias)); |
| 478 | |
| 479 | if(contact->isIntroducer()) |
| 480 | { |
| 481 | sqlite3_prepare_v2 (m_db, "SELECT trust_scope FROM TrustScope WHERE contact_namespace=?", -1, &stmt, 0); |
| 482 | sqlite3_bind_text(stmt, 1, name.toUri().c_str(), name.toUri().size(), SQLITE_TRANSIENT); |
| 483 | |
| 484 | while( sqlite3_step (stmt) == SQLITE_ROW) |
| 485 | { |
| 486 | Name scope(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0))); |
| 487 | contact->addTrustScope(scope); |
| 488 | } |
| 489 | sqlite3_finalize (stmt); |
| 490 | } |
| 491 | |
| 492 | return contact; |
Yingdi Yu | 4ef8cf6 | 2013-10-23 14:05:12 -0700 | [diff] [blame] | 493 | } |
| 494 | return NULL; |
| 495 | } |
| 496 | |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 497 | Ptr<Profile> |
Yingdi Yu | 54dcecc | 2013-10-17 15:07:17 -0700 | [diff] [blame] | 498 | ContactStorage::getSelfProfile(const Name& identity) const |
| 499 | { |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 500 | Ptr<Profile> profile = Ptr<Profile>(new Profile(identity)); |
Yingdi Yu | 54dcecc | 2013-10-17 15:07:17 -0700 | [diff] [blame] | 501 | sqlite3_stmt *stmt; |
| 502 | sqlite3_prepare_v2 (m_db, "SELECT profile_type, profile_value FROM SelfProfile WHERE profile_identity=?", -1, &stmt, 0); |
| 503 | 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] | 504 | |
| 505 | while( sqlite3_step (stmt) == SQLITE_ROW) |
| 506 | { |
| 507 | string profileType(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0)); |
| 508 | Blob profileValue(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 1)), sqlite3_column_bytes (stmt, 1)); |
| 509 | profile->setProfileEntry(profileType, profileValue); |
| 510 | } |
| 511 | |
| 512 | return profile; |
| 513 | } |
| 514 | |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 515 | Ptr<Blob> |
| 516 | ContactStorage::getSelfEndorseCertificate(const Name& identity) |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 517 | { |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 518 | sqlite3_stmt *stmt; |
Yingdi Yu | ec3d9a3 | 2013-10-18 18:35:09 -0700 | [diff] [blame] | 519 | 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] | 520 | 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] | 521 | |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 522 | Ptr<Blob> result = NULL; |
| 523 | if(sqlite3_step (stmt) == SQLITE_ROW) |
| 524 | 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] | 525 | |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 526 | sqlite3_finalize (stmt); |
Yingdi Yu | 92e8e48 | 2013-10-17 21:13:03 -0700 | [diff] [blame] | 527 | |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 528 | return result; |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 529 | } |
| 530 | |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 531 | void |
| 532 | ContactStorage::updateSelfEndorseCertificate(Ptr<EndorseCertificate> newEndorseCertificate, const Name& identity) |
| 533 | { |
| 534 | Ptr<Blob> newEndorseCertificateBlob = newEndorseCertificate->encodeToWire(); |
| 535 | |
| 536 | sqlite3_stmt *stmt; |
Yingdi Yu | ec3d9a3 | 2013-10-18 18:35:09 -0700 | [diff] [blame] | 537 | 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] | 538 | sqlite3_bind_text(stmt, 1, newEndorseCertificateBlob->buf(), newEndorseCertificateBlob->size(), SQLITE_TRANSIENT); |
| 539 | sqlite3_bind_text(stmt, 2, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT); |
| 540 | sqlite3_step(stmt); |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame^] | 541 | |
| 542 | sqlite3_finalize (stmt); |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | void |
| 546 | ContactStorage::addSelfEndorseCertificate(Ptr<EndorseCertificate> newEndorseCertificate, const Name& identity) |
| 547 | { |
| 548 | Ptr<Blob> newEndorseCertificateBlob = newEndorseCertificate->encodeToWire(); |
| 549 | |
| 550 | sqlite3_stmt *stmt; |
Yingdi Yu | ec3d9a3 | 2013-10-18 18:35:09 -0700 | [diff] [blame] | 551 | 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] | 552 | sqlite3_bind_text(stmt, 1, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT); |
| 553 | sqlite3_bind_text(stmt, 2, newEndorseCertificateBlob->buf(), newEndorseCertificateBlob->size(), SQLITE_TRANSIENT); |
| 554 | sqlite3_step(stmt); |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame^] | 555 | |
| 556 | sqlite3_finalize (stmt); |
| 557 | } |
| 558 | |
| 559 | Ptr<Blob> |
| 560 | ContactStorage::getEndorseCertificate(const ndn::Name& identity) |
| 561 | { |
| 562 | sqlite3_stmt *stmt; |
| 563 | sqlite3_prepare_v2 (m_db, "SELECT endorse_data FROM ProfileEndorse where identity=?", -1, &stmt, 0); |
| 564 | sqlite3_bind_text(stmt, 1, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT); |
| 565 | |
| 566 | Ptr<Blob> result = NULL; |
| 567 | if(sqlite3_step (stmt) == SQLITE_ROW) |
| 568 | result = Ptr<Blob>(new Blob(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0))); |
| 569 | |
| 570 | sqlite3_finalize (stmt); |
| 571 | |
| 572 | return result; |
| 573 | } |
| 574 | |
| 575 | void |
| 576 | ContactStorage::updateEndorseCertificate(ndn::Ptr<EndorseCertificate> endorseCertificate, const ndn::Name& identity) |
| 577 | { |
| 578 | Ptr<Blob> newEndorseCertificateBlob = endorseCertificate->encodeToWire(); |
| 579 | |
| 580 | sqlite3_stmt *stmt; |
| 581 | sqlite3_prepare_v2 (m_db, "UPDATE ProfileEndorse SET endorse_data=? WHERE identity=?", -1, &stmt, 0); |
| 582 | sqlite3_bind_text(stmt, 1, newEndorseCertificateBlob->buf(), newEndorseCertificateBlob->size(), SQLITE_TRANSIENT); |
| 583 | sqlite3_bind_text(stmt, 2, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT); |
| 584 | sqlite3_step(stmt); |
| 585 | |
| 586 | sqlite3_finalize (stmt); |
| 587 | } |
| 588 | |
| 589 | void |
| 590 | ContactStorage::addEndorseCertificate(ndn::Ptr<EndorseCertificate> endorseCertificate, const ndn::Name& identity) |
| 591 | { |
| 592 | Ptr<Blob> newEndorseCertificateBlob = endorseCertificate->encodeToWire(); |
| 593 | |
| 594 | sqlite3_stmt *stmt; |
| 595 | sqlite3_prepare_v2 (m_db, "INSERT INTO ProfileEndorse (identity, endorse_data) values (?, ?)", -1, &stmt, 0); |
| 596 | sqlite3_bind_text(stmt, 1, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT); |
| 597 | sqlite3_bind_text(stmt, 2, newEndorseCertificateBlob->buf(), newEndorseCertificateBlob->size(), SQLITE_TRANSIENT); |
| 598 | sqlite3_step(stmt); |
| 599 | |
| 600 | sqlite3_finalize (stmt); |
| 601 | } |
| 602 | |
| 603 | vector<string> |
| 604 | ContactStorage::getEndorseList(const Name& identity) |
| 605 | { |
| 606 | vector<string> endorseList; |
| 607 | |
| 608 | sqlite3_stmt *stmt; |
| 609 | sqlite3_prepare_v2 (m_db, "SELECT profile_type FROM ContactProfile WHERE profile_identity=? AND endorse=1 ORDER BY profile_type", -1, &stmt, 0); |
| 610 | sqlite3_bind_text(stmt, 1, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT); |
| 611 | |
| 612 | while( sqlite3_step (stmt) == SQLITE_ROW) |
| 613 | { |
| 614 | string profileType(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0)); |
| 615 | endorseList.push_back(profileType); |
| 616 | } |
| 617 | sqlite3_finalize (stmt); |
| 618 | |
| 619 | return endorseList; |
| 620 | } |
| 621 | |
| 622 | void |
| 623 | ContactStorage::updateCollectEndorse(const EndorseCertificate& endorseCertificate) |
| 624 | { |
| 625 | Name endorserName = endorseCertificate.getSigner(); |
| 626 | Name keyName = endorseCertificate.getPublicKeyName(); |
| 627 | Name endorseeName = keyName.getPrefix(keyName.size()-1); |
| 628 | Name getCertName = endorseCertificate.getName(); |
| 629 | Name oldCertName; |
| 630 | bool insert = true; |
| 631 | bool update = true; |
| 632 | |
| 633 | sqlite3_stmt *stmt; |
| 634 | sqlite3_prepare_v2 (m_db, "SELECT endorse_name FROM CollectEndorse WHERE endorser=? AND endorsee=?", -1, &stmt, 0); |
| 635 | sqlite3_bind_text(stmt, 1, endorserName.toUri().c_str(), endorserName.toUri().size(), SQLITE_TRANSIENT); |
| 636 | sqlite3_bind_text(stmt, 2, endorseeName.toUri().c_str(), endorseeName.toUri().size(), SQLITE_TRANSIENT); |
| 637 | |
| 638 | if(sqlite3_step (stmt) == SQLITE_ROW) |
| 639 | { |
| 640 | insert = false; |
| 641 | oldCertName = Name(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0))); |
| 642 | if(getCertName == oldCertName) |
| 643 | update = false; |
| 644 | } |
| 645 | sqlite3_finalize (stmt); |
| 646 | |
| 647 | if(insert) |
| 648 | { |
| 649 | sqlite3_prepare_v2 (m_db, "INSERT INTO CollectEndorse (endorser, endorsee, endorse_name, endorse_data) VALUES (?, ?, ?, ?)", -1, &stmt, 0); |
| 650 | sqlite3_bind_text(stmt, 1, endorserName.toUri().c_str(), endorserName.toUri().size(), SQLITE_TRANSIENT); |
| 651 | sqlite3_bind_text(stmt, 2, endorseeName.toUri().c_str(), endorseeName.toUri().size(), SQLITE_TRANSIENT); |
| 652 | sqlite3_bind_text(stmt, 3, getCertName.toUri().c_str(), getCertName.toUri().size(), SQLITE_TRANSIENT); |
| 653 | Ptr<Blob> blob = endorseCertificate.encodeToWire(); |
| 654 | sqlite3_bind_text(stmt, 4, blob->buf(), blob->size(), SQLITE_TRANSIENT); |
| 655 | int res = sqlite3_step (stmt); |
| 656 | // if(res != SQLITE_OK) |
| 657 | // _LOG_DEBUG("Insert CollectEndorse Failure: " << getCertName.toUri()); |
| 658 | sqlite3_finalize (stmt); |
| 659 | return; |
| 660 | } |
| 661 | if(update) |
| 662 | { |
| 663 | sqlite3_prepare_v2 (m_db, "UPDATE CollectEndorse SET endorse_name=?, endorse_data=? WHERE endorser=? AND endorsee=?", -1, &stmt, 0); |
| 664 | sqlite3_bind_text(stmt, 1, getCertName.toUri().c_str(), getCertName.toUri().size(), SQLITE_TRANSIENT); |
| 665 | Ptr<Blob> blob = endorseCertificate.encodeToWire(); |
| 666 | sqlite3_bind_text(stmt, 2, blob->buf(), blob->size(), SQLITE_TRANSIENT); |
| 667 | sqlite3_bind_text(stmt, 3, endorserName.toUri().c_str(), endorserName.toUri().size(), SQLITE_TRANSIENT); |
| 668 | sqlite3_bind_text(stmt, 4, endorseeName.toUri().c_str(), endorseeName.toUri().size(), SQLITE_TRANSIENT); |
| 669 | int res = sqlite3_step (stmt); |
| 670 | // if(res != SQLITE_OK) |
| 671 | // _LOG_DEBUG("Insert CollectEndorse Failure: " << getCertName.toUri()); |
| 672 | sqlite3_finalize (stmt); |
| 673 | return; |
| 674 | } |
| 675 | } |
| 676 | |
| 677 | Ptr<vector<Blob> > |
| 678 | ContactStorage::getCollectEndorseList(const Name& name) |
| 679 | { |
| 680 | Ptr<vector<Blob> > collectEndorseList = Ptr<vector<Blob> >::Create(); |
| 681 | |
| 682 | sqlite3_stmt *stmt; |
| 683 | sqlite3_prepare_v2 (m_db, "SELECT endorse_data FROM CollectEndorse WHERE endorsee=?", -1, &stmt, 0); |
| 684 | sqlite3_bind_text(stmt, 1, name.toUri().c_str(), name.toUri().size(), SQLITE_TRANSIENT); |
| 685 | |
| 686 | while(sqlite3_step (stmt) == SQLITE_ROW) |
| 687 | { |
| 688 | Blob blob(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0)); |
| 689 | collectEndorseList->push_back(blob); |
| 690 | } |
| 691 | |
| 692 | return collectEndorseList; |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 693 | } |