blob: 073eaa170d61cdaaf265da24b4a58065fb40ef8d [file] [log] [blame]
Yingdi Yud04ed1a2013-10-14 14:07:03 -07001/* -*- 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 Yud04ed1a2013-10-14 14:07:03 -070014#include <boost/filesystem.hpp>
Yingdi Yu92e8e482013-10-17 21:13:03 -070015#include <ndn.cxx/fields/signature-sha256-with-rsa.h>
Yingdi Yu3b318c12013-10-15 17:54:00 -070016#include "logging.h"
Yingdi Yud04ed1a2013-10-14 14:07:03 -070017
18using namespace std;
19using namespace ndn;
20namespace fs = boost::filesystem;
21
Yingdi Yu3b318c12013-10-15 17:54:00 -070022INIT_LOGGER ("ContactStorage");
23
24const string INIT_SP_TABLE = "\
25CREATE TABLE IF NOT EXISTS \n \
26 SelfProfile( \n \
Yingdi Yu54dcecc2013-10-17 15:07:17 -070027 profile_identity BLOB NOT NULL, \n \
Yingdi Yu3b318c12013-10-15 17:54:00 -070028 profile_type BLOB NOT NULL, \n \
Yingdi Yu54dcecc2013-10-17 15:07:17 -070029 profile_value BLOB NOT NULL, \n \
30 \
31 PRIMARY KEY (profile_identity, profile_type) \n \
Yingdi Yu3b318c12013-10-15 17:54:00 -070032 ); \n \
33 \
Yingdi Yu54dcecc2013-10-17 15:07:17 -070034CREATE INDEX sp_index ON SelfProfile(profile_identity,profile_type); \n \
Yingdi Yu3b318c12013-10-15 17:54:00 -070035";
36
Yingdi Yuec3d9a32013-10-18 18:35:09 -070037const string INIT_SE_TABLE = "\
Yingdi Yu3b318c12013-10-15 17:54:00 -070038CREATE TABLE IF NOT EXISTS \n \
Yingdi Yuec3d9a32013-10-18 18:35:09 -070039 SelfEndorse( \n \
Yingdi Yu3b318c12013-10-15 17:54:00 -070040 identity BLOB NOT NULL UNIQUE, \n \
Yingdi Yuec3d9a32013-10-18 18:35:09 -070041 endorse_data BLOB NOT NULL, \n \
Yingdi Yu3b318c12013-10-15 17:54:00 -070042 \
43 PRIMARY KEY (identity) \n \
44 ); \n \
Yingdi Yuec3d9a32013-10-18 18:35:09 -070045CREATE INDEX se_index ON SelfEndorse(identity); \n \
Yingdi Yu3b318c12013-10-15 17:54:00 -070046";
Yingdi Yud04ed1a2013-10-14 14:07:03 -070047
Yingdi Yu71c01872013-11-03 16:22:05 -080048const string INIT_CONTACT_TABLE = "\
Yingdi Yud04ed1a2013-10-14 14:07:03 -070049CREATE TABLE IF NOT EXISTS \n \
Yingdi Yu71c01872013-11-03 16:22:05 -080050 Contact( \n \
Yingdi Yud04ed1a2013-10-14 14:07:03 -070051 contact_namespace BLOB NOT NULL, \n \
52 contact_alias BLOB NOT NULL, \n \
53 self_certificate BLOB NOT NULL, \n \
Yingdi Yu71c01872013-11-03 16:22:05 -080054 is_introducer INTEGER DEFAULT 0, \n \
Yingdi Yud04ed1a2013-10-14 14:07:03 -070055 \
56 PRIMARY KEY (contact_namespace) \n \
57 ); \n \
58 \
Yingdi Yu71c01872013-11-03 16:22:05 -080059CREATE INDEX contact_index ON TrustedContact(contact_namespace); \n \
Yingdi Yud04ed1a2013-10-14 14:07:03 -070060";
61
Yingdi Yu71c01872013-11-03 16:22:05 -080062const string INIT_TS_TABLE = "\
Yingdi Yud04ed1a2013-10-14 14:07:03 -070063CREATE TABLE IF NOT EXISTS \n \
Yingdi Yu71c01872013-11-03 16:22:05 -080064 TrustScope( \n \
Yingdi Yud04ed1a2013-10-14 14:07:03 -070065 contact_namespace BLOB NOT NULL, \n \
Yingdi Yu71c01872013-11-03 16:22:05 -080066 trust_scope BLOB NOT NULL, \n \
Yingdi Yud04ed1a2013-10-14 14:07:03 -070067 \
Yingdi Yu71c01872013-11-03 16:22:05 -080068 PRIMARY KEY (contact_namespace, trust_scope) \n \
Yingdi Yud04ed1a2013-10-14 14:07:03 -070069 ); \n \
70 \
Yingdi Yu71c01872013-11-03 16:22:05 -080071CREATE INDEX ts_index ON TrustedContact(contact_namespace); \n \
Yingdi Yud04ed1a2013-10-14 14:07:03 -070072";
73
Yingdi Yu4685b1b2013-10-18 17:05:02 -070074ContactStorage::ContactStorage()
Yingdi Yud04ed1a2013-10-14 14:07:03 -070075{
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 Yu3b318c12013-10-15 17:54:00 -070083 // Check if SelfProfile table exists
Yingdi Yud04ed1a2013-10-14 14:07:03 -070084 sqlite3_stmt *stmt;
Yingdi Yu3b318c12013-10-15 17:54:00 -070085 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 Yuec3d9a32013-10-18 18:35:09 -0700101 // 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 Yu3b318c12013-10-15 17:54:00 -0700103 res = sqlite3_step (stmt);
104
Yingdi Yuec3d9a32013-10-18 18:35:09 -0700105 bool seTableExist = false;
Yingdi Yu3b318c12013-10-15 17:54:00 -0700106 if (res == SQLITE_ROW)
Yingdi Yuec3d9a32013-10-18 18:35:09 -0700107 seTableExist = true;
Yingdi Yu3b318c12013-10-15 17:54:00 -0700108 sqlite3_finalize (stmt);
109
Yingdi Yuec3d9a32013-10-18 18:35:09 -0700110 if(!seTableExist)
Yingdi Yu3b318c12013-10-15 17:54:00 -0700111 {
112 char *errmsg = 0;
Yingdi Yuec3d9a32013-10-18 18:35:09 -0700113 res = sqlite3_exec (m_db, INIT_SE_TABLE.c_str (), NULL, NULL, &errmsg);
Yingdi Yu3b318c12013-10-15 17:54:00 -0700114 if (res != SQLITE_OK && errmsg != 0)
Yingdi Yuec3d9a32013-10-18 18:35:09 -0700115 throw LnException("Init \"error\" in SelfEndorse");
Yingdi Yu3b318c12013-10-15 17:54:00 -0700116 }
117
118
119 // Check if TrustedContact table exists
Yingdi Yu71c01872013-11-03 16:22:05 -0800120 sqlite3_prepare_v2 (m_db, "SELECT name FROM sqlite_master WHERE type='table' And name='Contact'", -1, &stmt, 0);
Yingdi Yud04ed1a2013-10-14 14:07:03 -0700121 res = sqlite3_step (stmt);
122
Yingdi Yu71c01872013-11-03 16:22:05 -0800123 bool contactTableExist = false;
Yingdi Yud04ed1a2013-10-14 14:07:03 -0700124 if (res == SQLITE_ROW)
Yingdi Yu71c01872013-11-03 16:22:05 -0800125 contactTableExist = true;
Yingdi Yud04ed1a2013-10-14 14:07:03 -0700126 sqlite3_finalize (stmt);
127
Yingdi Yu71c01872013-11-03 16:22:05 -0800128 if(!contactTableExist)
Yingdi Yud04ed1a2013-10-14 14:07:03 -0700129 {
130 char *errmsg = 0;
Yingdi Yu71c01872013-11-03 16:22:05 -0800131 res = sqlite3_exec (m_db, INIT_CONTACT_TABLE.c_str (), NULL, NULL, &errmsg);
Yingdi Yud04ed1a2013-10-14 14:07:03 -0700132 if (res != SQLITE_OK && errmsg != 0)
Yingdi Yu71c01872013-11-03 16:22:05 -0800133 throw LnException("Init \"error\" in Contact");
Yingdi Yud04ed1a2013-10-14 14:07:03 -0700134 }
Yingdi Yu71c01872013-11-03 16:22:05 -0800135
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 Yud04ed1a2013-10-14 14:07:03 -0700138 res = sqlite3_step (stmt);
139
Yingdi Yu71c01872013-11-03 16:22:05 -0800140 bool tsTableExist = false;
Yingdi Yud04ed1a2013-10-14 14:07:03 -0700141 if (res == SQLITE_ROW)
Yingdi Yu71c01872013-11-03 16:22:05 -0800142 tsTableExist = true;
Yingdi Yud04ed1a2013-10-14 14:07:03 -0700143 sqlite3_finalize (stmt);
144
Yingdi Yu71c01872013-11-03 16:22:05 -0800145 if(!tsTableExist)
Yingdi Yud04ed1a2013-10-14 14:07:03 -0700146 {
147 char *errmsg = 0;
Yingdi Yu71c01872013-11-03 16:22:05 -0800148 res = sqlite3_exec (m_db, INIT_TS_TABLE.c_str (), NULL, NULL, &errmsg);
Yingdi Yud04ed1a2013-10-14 14:07:03 -0700149 if (res != SQLITE_OK && errmsg != 0)
Yingdi Yu71c01872013-11-03 16:22:05 -0800150 throw LnException("Init \"error\" in TrustScope");
Yingdi Yud04ed1a2013-10-14 14:07:03 -0700151 }
152}
153
Yingdi Yu3b318c12013-10-15 17:54:00 -0700154bool
Yingdi Yu54dcecc2013-10-17 15:07:17 -0700155ContactStorage::doesSelfEntryExist(const Name& identity, const string& profileType)
Yingdi Yu3b318c12013-10-15 17:54:00 -0700156{
157 bool result = false;
158
159 sqlite3_stmt *stmt;
Yingdi Yu54dcecc2013-10-17 15:07:17 -0700160 sqlite3_prepare_v2 (m_db, "SELECT count(*) FROM SelfProfile WHERE profile_type=? and profile_identity=?", -1, &stmt, 0);
Yingdi Yu3b318c12013-10-15 17:54:00 -0700161 sqlite3_bind_text(stmt, 1, profileType.c_str(), profileType.size(), SQLITE_TRANSIENT);
Yingdi Yu54dcecc2013-10-17 15:07:17 -0700162 sqlite3_bind_text(stmt, 2, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT);
Yingdi Yu3b318c12013-10-15 17:54:00 -0700163
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 Yu3b318c12013-10-15 17:54:00 -0700176void
Yingdi Yu54dcecc2013-10-17 15:07:17 -0700177ContactStorage::setSelfProfileEntry(const Name& identity, const string& profileType, const Blob& profileValue)
Yingdi Yu3b318c12013-10-15 17:54:00 -0700178{
Yingdi Yu3b318c12013-10-15 17:54:00 -0700179 sqlite3_stmt *stmt;
Yingdi Yu54dcecc2013-10-17 15:07:17 -0700180 if(doesSelfEntryExist(identity, profileType))
Yingdi Yu3b318c12013-10-15 17:54:00 -0700181 {
Yingdi Yu54dcecc2013-10-17 15:07:17 -0700182 sqlite3_prepare_v2 (m_db, "UPDATE SelfProfile SET profile_value=? WHERE profile_type=? and profile_identity=?", -1, &stmt, 0);
Yingdi Yu3b318c12013-10-15 17:54:00 -0700183 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 Yu54dcecc2013-10-17 15:07:17 -0700185 sqlite3_bind_text(stmt, 3, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT);
Yingdi Yu3b318c12013-10-15 17:54:00 -0700186 }
187 else
188 {
Yingdi Yu54dcecc2013-10-17 15:07:17 -0700189 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 Yu3b318c12013-10-15 17:54:00 -0700193 }
194 sqlite3_step (stmt);
195 sqlite3_finalize (stmt);
196}
197
198Ptr<Profile>
Yingdi Yu54dcecc2013-10-17 15:07:17 -0700199ContactStorage::getSelfProfile(const Name& identity)
Yingdi Yu3b318c12013-10-15 17:54:00 -0700200{
Yingdi Yu2ac40fb2013-10-21 13:38:38 -0700201 _LOG_DEBUG("getSelfProfile " << identity.toUri());
Yingdi Yu3b318c12013-10-15 17:54:00 -0700202 sqlite3_stmt *stmt;
Yingdi Yu54dcecc2013-10-17 15:07:17 -0700203 Ptr<Profile> profile = Ptr<Profile>(new Profile(identity));
Yingdi Yu3b318c12013-10-15 17:54:00 -0700204
Yingdi Yu2ac40fb2013-10-21 13:38:38 -0700205 sqlite3_prepare_v2(m_db, "SELECT profile_type, profile_value FROM SelfProfile WHERE profile_identity=?", -1, &stmt, 0);
Yingdi Yu54dcecc2013-10-17 15:07:17 -0700206 sqlite3_bind_text(stmt, 1, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT);
Yingdi Yu2ac40fb2013-10-21 13:38:38 -0700207
208 while(sqlite3_step (stmt) == SQLITE_ROW)
Yingdi Yu3b318c12013-10-15 17:54:00 -0700209 {
Yingdi Yu2ac40fb2013-10-21 13:38:38 -0700210 _LOG_DEBUG("entry");
Yingdi Yu3b318c12013-10-15 17:54:00 -0700211 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 Yud04ed1a2013-10-14 14:07:03 -0700219
220void
Yingdi Yu71c01872013-11-03 16:22:05 -0800221ContactStorage::addContact(const ContactItem& contact)
Yingdi Yud04ed1a2013-10-14 14:07:03 -0700222{
Yingdi Yu71c01872013-11-03 16:22:05 -0800223 if(doesContactExist(contact.getNameSpace()))
Yingdi Yud04ed1a2013-10-14 14:07:03 -0700224 throw LnException("Normal Contact has already existed");
225
Yingdi Yu71c01872013-11-03 16:22:05 -0800226 bool isIntroducer = contact.isIntroducer();
227
Yingdi Yud04ed1a2013-10-14 14:07:03 -0700228 sqlite3_stmt *stmt;
229 sqlite3_prepare_v2 (m_db,
Yingdi Yu71c01872013-11-03 16:22:05 -0800230 "INSERT INTO Contact (contact_namespace, contact_alias, self_certificate, is_introducer) values (?, ?, ?, ?)",
Yingdi Yud04ed1a2013-10-14 14:07:03 -0700231 -1,
232 &stmt,
233 0);
234
Yingdi Yu71c01872013-11-03 16:22:05 -0800235 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 Yud04ed1a2013-10-14 14:07:03 -0700238 sqlite3_bind_text(stmt, 3, selfCertificateBlob->buf(), selfCertificateBlob->size(), SQLITE_TRANSIENT);
Yingdi Yu71c01872013-11-03 16:22:05 -0800239 sqlite3_bind_int(stmt, 4, (isIntroducer ? 1 : 0));
Yingdi Yud04ed1a2013-10-14 14:07:03 -0700240
241 int res = sqlite3_step (stmt);
Yingdi Yu2ac40fb2013-10-21 13:38:38 -0700242 // _LOG_DEBUG("res " << res);
Yingdi Yud04ed1a2013-10-14 14:07:03 -0700243 sqlite3_finalize (stmt);
Yingdi Yu71c01872013-11-03 16:22:05 -0800244
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 Yud04ed1a2013-10-14 14:07:03 -0700265}
266
Yingdi Yu2ac40fb2013-10-21 13:38:38 -0700267void
268ContactStorage::updateAlias(const ndn::Name& identity, std::string alias)
269{
Yingdi Yu71c01872013-11-03 16:22:05 -0800270 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 Yu2ac40fb2013-10-21 13:38:38 -0700277}
278
Yingdi Yud04ed1a2013-10-14 14:07:03 -0700279bool
Yingdi Yu71c01872013-11-03 16:22:05 -0800280ContactStorage::doesContactExist(const Name& name)
Yingdi Yud04ed1a2013-10-14 14:07:03 -0700281{
282 bool result = false;
283
284 sqlite3_stmt *stmt;
Yingdi Yu71c01872013-11-03 16:22:05 -0800285 sqlite3_prepare_v2 (m_db, "SELECT count(*) FROM Contact WHERE contact_namespace=?", -1, &stmt, 0);
Yingdi Yud04ed1a2013-10-14 14:07:03 -0700286 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 Yud04ed1a2013-10-14 14:07:03 -0700300vector<Ptr<ContactItem> >
Yingdi Yu71c01872013-11-03 16:22:05 -0800301ContactStorage::getAllContacts() const
Yingdi Yud04ed1a2013-10-14 14:07:03 -0700302{
Yingdi Yu71c01872013-11-03 16:22:05 -0800303 vector<Ptr<ContactItem> > contacts;
Yingdi Yud04ed1a2013-10-14 14:07:03 -0700304
305 sqlite3_stmt *stmt;
Yingdi Yu71c01872013-11-03 16:22:05 -0800306 sqlite3_prepare_v2 (m_db, "SELECT contact_alias, self_certificate, is_introducer FROM Contact", -1, &stmt, 0);
Yingdi Yud04ed1a2013-10-14 14:07:03 -0700307
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 Yu71c01872013-11-03 16:22:05 -0800314 int isIntroducer = sqlite3_column_int (stmt, 2);
Yingdi Yud04ed1a2013-10-14 14:07:03 -0700315
Yingdi Yu71c01872013-11-03 16:22:05 -0800316 contacts.push_back(Ptr<ContactItem>(new ContactItem(endorseCertificate, isIntroducer, alias)));
Yingdi Yud04ed1a2013-10-14 14:07:03 -0700317 }
Yingdi Yu71c01872013-11-03 16:22:05 -0800318 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 Yud04ed1a2013-10-14 14:07:03 -0700338}
Yingdi Yu3b318c12013-10-15 17:54:00 -0700339
Yingdi Yu4ef8cf62013-10-23 14:05:12 -0700340Ptr<ContactItem>
Yingdi Yu71c01872013-11-03 16:22:05 -0800341ContactStorage::getContact(const Name& name)
Yingdi Yu4ef8cf62013-10-23 14:05:12 -0700342{
343 sqlite3_stmt *stmt;
Yingdi Yu71c01872013-11-03 16:22:05 -0800344 sqlite3_prepare_v2 (m_db, "SELECT contact_alias, self_certificate, is_introducer FROM Contact where contact_namespace=?", -1, &stmt, 0);
Yingdi Yu4ef8cf62013-10-23 14:05:12 -0700345 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 Yu71c01872013-11-03 16:22:05 -0800353 int isIntroducer = sqlite3_column_int (stmt, 2);
Yingdi Yu4ef8cf62013-10-23 14:05:12 -0700354
Yingdi Yu71c01872013-11-03 16:22:05 -0800355 return Ptr<ContactItem>(new ContactItem(endorseCertificate, isIntroducer, alias));
Yingdi Yu4ef8cf62013-10-23 14:05:12 -0700356 }
357 return NULL;
358}
359
Yingdi Yu3b318c12013-10-15 17:54:00 -0700360Ptr<Profile>
Yingdi Yu54dcecc2013-10-17 15:07:17 -0700361ContactStorage::getSelfProfile(const Name& identity) const
362{
Yingdi Yu3b318c12013-10-15 17:54:00 -0700363 Ptr<Profile> profile = Ptr<Profile>(new Profile(identity));
Yingdi Yu54dcecc2013-10-17 15:07:17 -0700364 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 Yu3b318c12013-10-15 17:54:00 -0700367
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 Yu4685b1b2013-10-18 17:05:02 -0700378Ptr<Blob>
379ContactStorage::getSelfEndorseCertificate(const Name& identity)
Yingdi Yu3b318c12013-10-15 17:54:00 -0700380{
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700381 sqlite3_stmt *stmt;
Yingdi Yuec3d9a32013-10-18 18:35:09 -0700382 sqlite3_prepare_v2 (m_db, "SELECT endorse_data FROM SelfEndorse where identity=?", -1, &stmt, 0);
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700383 sqlite3_bind_text(stmt, 1, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT);
Yingdi Yu54dcecc2013-10-17 15:07:17 -0700384
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700385 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 Yu3b318c12013-10-15 17:54:00 -0700388
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700389 sqlite3_finalize (stmt);
Yingdi Yu92e8e482013-10-17 21:13:03 -0700390
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700391 return result;
Yingdi Yu3b318c12013-10-15 17:54:00 -0700392}
393
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700394void
395ContactStorage::updateSelfEndorseCertificate(Ptr<EndorseCertificate> newEndorseCertificate, const Name& identity)
396{
397 Ptr<Blob> newEndorseCertificateBlob = newEndorseCertificate->encodeToWire();
398
399 sqlite3_stmt *stmt;
Yingdi Yuec3d9a32013-10-18 18:35:09 -0700400 sqlite3_prepare_v2 (m_db, "UPDATE SelfEndorse SET endorse_data=? WHERE identity=?", -1, &stmt, 0);
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700401 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
406void
407ContactStorage::addSelfEndorseCertificate(Ptr<EndorseCertificate> newEndorseCertificate, const Name& identity)
408{
409 Ptr<Blob> newEndorseCertificateBlob = newEndorseCertificate->encodeToWire();
410
411 sqlite3_stmt *stmt;
Yingdi Yuec3d9a32013-10-18 18:35:09 -0700412 sqlite3_prepare_v2 (m_db, "INSERT INTO SelfEndorse (identity, endorse_data) values (?, ?)", -1, &stmt, 0);
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700413 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}