blob: b3ee977a9aa55b35d46d52b800d16ac752ac20a0 [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 Yub2e747d2013-11-05 23:06:43 -080059CREATE INDEX contact_index ON Contact(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 Yub2e747d2013-11-05 23:06:43 -080065 id INTEGER PRIMARY KEY AUTOINCREMENT, \n \
Yingdi Yud04ed1a2013-10-14 14:07:03 -070066 contact_namespace BLOB NOT NULL, \n \
Yingdi Yub2e747d2013-11-05 23:06:43 -080067 trust_scope BLOB NOT NULL \n \
Yingdi Yud04ed1a2013-10-14 14:07:03 -070068 ); \n \
69 \
Yingdi Yub2e747d2013-11-05 23:06:43 -080070CREATE INDEX ts_index ON TrustScope(contact_namespace); \n \
71";
72
73const string INIT_CP_TABLE = "\
74CREATE 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 \
84CREATE INDEX cp_index ON ContactProfile(profile_identity); \n \
85";
86
87const string INIT_PE_TABLE = "\
88CREATE 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 \
96CREATE INDEX pe_index ON ProfileEndorse(identity); \n \
97";
98
99const string INIT_CE_TABLE = "\
100CREATE 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 \
110CREATE INDEX ce_index ON CollectEndorse(endorsee); \n \
Yingdi Yud04ed1a2013-10-14 14:07:03 -0700111";
112
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700113ContactStorage::ContactStorage()
Yingdi Yud04ed1a2013-10-14 14:07:03 -0700114{
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 Yu3b318c12013-10-15 17:54:00 -0700122 // Check if SelfProfile table exists
Yingdi Yud04ed1a2013-10-14 14:07:03 -0700123 sqlite3_stmt *stmt;
Yingdi Yu3b318c12013-10-15 17:54:00 -0700124 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 Yuec3d9a32013-10-18 18:35:09 -0700140 // 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 Yu3b318c12013-10-15 17:54:00 -0700142 res = sqlite3_step (stmt);
143
Yingdi Yuec3d9a32013-10-18 18:35:09 -0700144 bool seTableExist = false;
Yingdi Yu3b318c12013-10-15 17:54:00 -0700145 if (res == SQLITE_ROW)
Yingdi Yuec3d9a32013-10-18 18:35:09 -0700146 seTableExist = true;
Yingdi Yu3b318c12013-10-15 17:54:00 -0700147 sqlite3_finalize (stmt);
148
Yingdi Yuec3d9a32013-10-18 18:35:09 -0700149 if(!seTableExist)
Yingdi Yu3b318c12013-10-15 17:54:00 -0700150 {
151 char *errmsg = 0;
Yingdi Yuec3d9a32013-10-18 18:35:09 -0700152 res = sqlite3_exec (m_db, INIT_SE_TABLE.c_str (), NULL, NULL, &errmsg);
Yingdi Yu3b318c12013-10-15 17:54:00 -0700153 if (res != SQLITE_OK && errmsg != 0)
Yingdi Yuec3d9a32013-10-18 18:35:09 -0700154 throw LnException("Init \"error\" in SelfEndorse");
Yingdi Yu3b318c12013-10-15 17:54:00 -0700155 }
156
157
Yingdi Yub2e747d2013-11-05 23:06:43 -0800158 // Check if Contact table exists
Yingdi Yu71c01872013-11-03 16:22:05 -0800159 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 -0700160 res = sqlite3_step (stmt);
161
Yingdi Yu71c01872013-11-03 16:22:05 -0800162 bool contactTableExist = false;
Yingdi Yud04ed1a2013-10-14 14:07:03 -0700163 if (res == SQLITE_ROW)
Yingdi Yu71c01872013-11-03 16:22:05 -0800164 contactTableExist = true;
Yingdi Yud04ed1a2013-10-14 14:07:03 -0700165 sqlite3_finalize (stmt);
166
Yingdi Yu71c01872013-11-03 16:22:05 -0800167 if(!contactTableExist)
Yingdi Yud04ed1a2013-10-14 14:07:03 -0700168 {
169 char *errmsg = 0;
Yingdi Yu71c01872013-11-03 16:22:05 -0800170 res = sqlite3_exec (m_db, INIT_CONTACT_TABLE.c_str (), NULL, NULL, &errmsg);
Yingdi Yud04ed1a2013-10-14 14:07:03 -0700171 if (res != SQLITE_OK && errmsg != 0)
Yingdi Yu71c01872013-11-03 16:22:05 -0800172 throw LnException("Init \"error\" in Contact");
Yingdi Yud04ed1a2013-10-14 14:07:03 -0700173 }
Yingdi Yu71c01872013-11-03 16:22:05 -0800174
Yingdi Yub2e747d2013-11-05 23:06:43 -0800175 // Check if TrustScope table exists
Yingdi Yu71c01872013-11-03 16:22:05 -0800176 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 -0700177 res = sqlite3_step (stmt);
178
Yingdi Yu71c01872013-11-03 16:22:05 -0800179 bool tsTableExist = false;
Yingdi Yud04ed1a2013-10-14 14:07:03 -0700180 if (res == SQLITE_ROW)
Yingdi Yu71c01872013-11-03 16:22:05 -0800181 tsTableExist = true;
Yingdi Yud04ed1a2013-10-14 14:07:03 -0700182 sqlite3_finalize (stmt);
183
Yingdi Yu71c01872013-11-03 16:22:05 -0800184 if(!tsTableExist)
Yingdi Yud04ed1a2013-10-14 14:07:03 -0700185 {
186 char *errmsg = 0;
Yingdi Yu71c01872013-11-03 16:22:05 -0800187 res = sqlite3_exec (m_db, INIT_TS_TABLE.c_str (), NULL, NULL, &errmsg);
Yingdi Yud04ed1a2013-10-14 14:07:03 -0700188 if (res != SQLITE_OK && errmsg != 0)
Yingdi Yu71c01872013-11-03 16:22:05 -0800189 throw LnException("Init \"error\" in TrustScope");
Yingdi Yud04ed1a2013-10-14 14:07:03 -0700190 }
Yingdi Yub2e747d2013-11-05 23:06:43 -0800191
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 Yud04ed1a2013-10-14 14:07:03 -0700242}
243
Yingdi Yu3b318c12013-10-15 17:54:00 -0700244bool
Yingdi Yu54dcecc2013-10-17 15:07:17 -0700245ContactStorage::doesSelfEntryExist(const Name& identity, const string& profileType)
Yingdi Yu3b318c12013-10-15 17:54:00 -0700246{
247 bool result = false;
248
249 sqlite3_stmt *stmt;
Yingdi Yu54dcecc2013-10-17 15:07:17 -0700250 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 -0700251 sqlite3_bind_text(stmt, 1, profileType.c_str(), profileType.size(), SQLITE_TRANSIENT);
Yingdi Yu54dcecc2013-10-17 15:07:17 -0700252 sqlite3_bind_text(stmt, 2, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT);
Yingdi Yu3b318c12013-10-15 17:54:00 -0700253
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 Yu3b318c12013-10-15 17:54:00 -0700266void
Yingdi Yu54dcecc2013-10-17 15:07:17 -0700267ContactStorage::setSelfProfileEntry(const Name& identity, const string& profileType, const Blob& profileValue)
Yingdi Yu3b318c12013-10-15 17:54:00 -0700268{
Yingdi Yu3b318c12013-10-15 17:54:00 -0700269 sqlite3_stmt *stmt;
Yingdi Yu54dcecc2013-10-17 15:07:17 -0700270 if(doesSelfEntryExist(identity, profileType))
Yingdi Yu3b318c12013-10-15 17:54:00 -0700271 {
Yingdi Yu54dcecc2013-10-17 15:07:17 -0700272 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 -0700273 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 Yu54dcecc2013-10-17 15:07:17 -0700275 sqlite3_bind_text(stmt, 3, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT);
Yingdi Yu3b318c12013-10-15 17:54:00 -0700276 }
277 else
278 {
Yingdi Yu54dcecc2013-10-17 15:07:17 -0700279 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 Yu3b318c12013-10-15 17:54:00 -0700283 }
284 sqlite3_step (stmt);
285 sqlite3_finalize (stmt);
286}
287
288Ptr<Profile>
Yingdi Yu54dcecc2013-10-17 15:07:17 -0700289ContactStorage::getSelfProfile(const Name& identity)
Yingdi Yu3b318c12013-10-15 17:54:00 -0700290{
Yingdi Yu3b318c12013-10-15 17:54:00 -0700291 sqlite3_stmt *stmt;
Yingdi Yu54dcecc2013-10-17 15:07:17 -0700292 Ptr<Profile> profile = Ptr<Profile>(new Profile(identity));
Yingdi Yu3b318c12013-10-15 17:54:00 -0700293
Yingdi Yu2ac40fb2013-10-21 13:38:38 -0700294 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 -0700295 sqlite3_bind_text(stmt, 1, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT);
Yingdi Yu2ac40fb2013-10-21 13:38:38 -0700296
297 while(sqlite3_step (stmt) == SQLITE_ROW)
Yingdi Yu3b318c12013-10-15 17:54:00 -0700298 {
299 string profileType(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0));
300 Blob profileValue(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 1)), sqlite3_column_bytes (stmt, 1));
301
302 profile->setProfileEntry(profileType, profileValue );
303 }
304
Yingdi Yu72781e52013-11-06 23:00:21 -0800305 sqlite3_finalize(stmt);
306
Yingdi Yu3b318c12013-10-15 17:54:00 -0700307 return profile;
308}
Yingdi Yud04ed1a2013-10-14 14:07:03 -0700309
310void
Yingdi Yu71c01872013-11-03 16:22:05 -0800311ContactStorage::addContact(const ContactItem& contact)
Yingdi Yud04ed1a2013-10-14 14:07:03 -0700312{
Yingdi Yu71c01872013-11-03 16:22:05 -0800313 if(doesContactExist(contact.getNameSpace()))
Yingdi Yud04ed1a2013-10-14 14:07:03 -0700314 throw LnException("Normal Contact has already existed");
315
Yingdi Yu71c01872013-11-03 16:22:05 -0800316 bool isIntroducer = contact.isIntroducer();
317
Yingdi Yud04ed1a2013-10-14 14:07:03 -0700318 sqlite3_stmt *stmt;
319 sqlite3_prepare_v2 (m_db,
Yingdi Yu71c01872013-11-03 16:22:05 -0800320 "INSERT INTO Contact (contact_namespace, contact_alias, self_certificate, is_introducer) values (?, ?, ?, ?)",
Yingdi Yud04ed1a2013-10-14 14:07:03 -0700321 -1,
322 &stmt,
323 0);
324
Yingdi Yu71c01872013-11-03 16:22:05 -0800325 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 Yud04ed1a2013-10-14 14:07:03 -0700328 sqlite3_bind_text(stmt, 3, selfCertificateBlob->buf(), selfCertificateBlob->size(), SQLITE_TRANSIENT);
Yingdi Yu71c01872013-11-03 16:22:05 -0800329 sqlite3_bind_int(stmt, 4, (isIntroducer ? 1 : 0));
Yingdi Yud04ed1a2013-10-14 14:07:03 -0700330
331 int res = sqlite3_step (stmt);
332 sqlite3_finalize (stmt);
Yingdi Yu71c01872013-11-03 16:22:05 -0800333
Yingdi Yub2e747d2013-11-05 23:06:43 -0800334 Ptr<ProfileData> profileData = contact.getSelfEndorseCertificate().getProfileData();
335 const Profile& profile = profileData->getProfile();
336 Profile::const_iterator it = profile.begin();
337 string identity = contact.getNameSpace().toUri();
338 for(; it != profile.end(); it++)
339 {
340 sqlite3_prepare_v2 (m_db,
341 "INSERT INTO ContactProfile (profile_identity, profile_type, profile_value, endorse) values (?, ?, ?, 0)",
342 -1,
343 &stmt,
344 0);
345 sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size (), SQLITE_TRANSIENT);
346 sqlite3_bind_text(stmt, 2, it->first.c_str(), it->first.size(), SQLITE_TRANSIENT);
347 sqlite3_bind_text(stmt, 3, it->second.buf(), it->second.size(), SQLITE_TRANSIENT);
348 res = sqlite3_step (stmt);
349 sqlite3_finalize (stmt);
350 }
351
Yingdi Yu71c01872013-11-03 16:22:05 -0800352 if(isIntroducer)
353 {
354 const vector<Name>& scopeList = contact.getTrustScopeList();
355 vector<Name>::const_iterator it = scopeList.begin();
356 string nameSpace = contact.getNameSpace().toUri();
357
358 while(it != scopeList.end())
359 {
360 sqlite3_prepare_v2 (m_db,
361 "INSERT INTO TrustScope (contact_namespace, trust_scope) values (?, ?)",
362 -1,
363 &stmt,
364 0);
365 sqlite3_bind_text(stmt, 1, nameSpace.c_str(), nameSpace.size (), SQLITE_TRANSIENT);
366 sqlite3_bind_text(stmt, 2, it->toUri().c_str(), it->toUri().size(), SQLITE_TRANSIENT);
367 res = sqlite3_step (stmt);
368 sqlite3_finalize (stmt);
369 it++;
370 }
371 }
Yingdi Yud04ed1a2013-10-14 14:07:03 -0700372}
373
Yingdi Yub2e747d2013-11-05 23:06:43 -0800374void
375ContactStorage::updateIsIntroducer(const ndn::Name& identity, bool isIntroducer)
376{
377 sqlite3_stmt *stmt;
378 sqlite3_prepare_v2 (m_db, "UPDATE Contact SET is_introducer=? WHERE contact_namespace=?", -1, &stmt, 0);
379 sqlite3_bind_int(stmt, 1, (isIntroducer ? 1 : 0));
380 sqlite3_bind_text(stmt, 2, identity.toUri().c_str(), identity.toUri().size (), SQLITE_TRANSIENT);
381 int res = sqlite3_step (stmt);
382 sqlite3_finalize (stmt);
383 return;
384}
385
Yingdi Yu2ac40fb2013-10-21 13:38:38 -0700386void
387ContactStorage::updateAlias(const ndn::Name& identity, std::string alias)
388{
Yingdi Yu71c01872013-11-03 16:22:05 -0800389 sqlite3_stmt *stmt;
390 sqlite3_prepare_v2 (m_db, "UPDATE Contact SET contact_alias=? WHERE contact_namespace=?", -1, &stmt, 0);
391 sqlite3_bind_text(stmt, 1, alias.c_str(), alias.size(), SQLITE_TRANSIENT);
392 sqlite3_bind_text(stmt, 2, identity.toUri().c_str(), identity.toUri().size (), SQLITE_TRANSIENT);
393 int res = sqlite3_step (stmt);
394 sqlite3_finalize (stmt);
395 return;
Yingdi Yu2ac40fb2013-10-21 13:38:38 -0700396}
397
Yingdi Yud04ed1a2013-10-14 14:07:03 -0700398bool
Yingdi Yu71c01872013-11-03 16:22:05 -0800399ContactStorage::doesContactExist(const Name& name)
Yingdi Yud04ed1a2013-10-14 14:07:03 -0700400{
401 bool result = false;
402
403 sqlite3_stmt *stmt;
Yingdi Yu71c01872013-11-03 16:22:05 -0800404 sqlite3_prepare_v2 (m_db, "SELECT count(*) FROM Contact WHERE contact_namespace=?", -1, &stmt, 0);
Yingdi Yud04ed1a2013-10-14 14:07:03 -0700405 sqlite3_bind_text(stmt, 1, name.toUri().c_str(), name.toUri().size(), SQLITE_TRANSIENT);
406
407 int res = sqlite3_step (stmt);
408
409 if (res == SQLITE_ROW)
410 {
411 int countAll = sqlite3_column_int (stmt, 0);
412 if (countAll > 0)
413 result = true;
414 }
415 sqlite3_finalize (stmt);
416 return result;
417}
418
Yingdi Yud04ed1a2013-10-14 14:07:03 -0700419vector<Ptr<ContactItem> >
Yingdi Yu71c01872013-11-03 16:22:05 -0800420ContactStorage::getAllContacts() const
Yingdi Yud04ed1a2013-10-14 14:07:03 -0700421{
Yingdi Yu71c01872013-11-03 16:22:05 -0800422 vector<Ptr<ContactItem> > contacts;
Yingdi Yud04ed1a2013-10-14 14:07:03 -0700423
424 sqlite3_stmt *stmt;
Yingdi Yu71c01872013-11-03 16:22:05 -0800425 sqlite3_prepare_v2 (m_db, "SELECT contact_alias, self_certificate, is_introducer FROM Contact", -1, &stmt, 0);
Yingdi Yud04ed1a2013-10-14 14:07:03 -0700426
427 while( sqlite3_step (stmt) == SQLITE_ROW)
428 {
429 string alias(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0));
430 Ptr<Blob> certBlob = Ptr<Blob>(new Blob(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 1)), sqlite3_column_bytes (stmt, 1)));
431 Ptr<Data> certData = Data::decodeFromWire(certBlob);
432 EndorseCertificate endorseCertificate(*certData);
Yingdi Yu71c01872013-11-03 16:22:05 -0800433 int isIntroducer = sqlite3_column_int (stmt, 2);
Yingdi Yud04ed1a2013-10-14 14:07:03 -0700434
Yingdi Yu71c01872013-11-03 16:22:05 -0800435 contacts.push_back(Ptr<ContactItem>(new ContactItem(endorseCertificate, isIntroducer, alias)));
Yingdi Yud04ed1a2013-10-14 14:07:03 -0700436 }
Yingdi Yu71c01872013-11-03 16:22:05 -0800437 sqlite3_finalize (stmt);
438
439 vector<Ptr<ContactItem> >::iterator it = contacts.begin();
440 for(; it != contacts.end(); it++)
441 {
442 if((*it)->isIntroducer())
443 {
444 sqlite3_prepare_v2 (m_db, "SELECT trust_scope FROM TrustScope WHERE contact_namespace=?", -1, &stmt, 0);
445 sqlite3_bind_text(stmt, 1, (*it)->getNameSpace().toUri().c_str(), (*it)->getNameSpace().toUri().size(), SQLITE_TRANSIENT);
446
447 while( sqlite3_step (stmt) == SQLITE_ROW)
448 {
449 Name scope(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0)));
450 (*it)->addTrustScope(scope);
451 }
452 sqlite3_finalize (stmt);
453 }
454 }
455
456 return contacts;
Yingdi Yud04ed1a2013-10-14 14:07:03 -0700457}
Yingdi Yu3b318c12013-10-15 17:54:00 -0700458
Yingdi Yu4ef8cf62013-10-23 14:05:12 -0700459Ptr<ContactItem>
Yingdi Yu71c01872013-11-03 16:22:05 -0800460ContactStorage::getContact(const Name& name)
Yingdi Yu4ef8cf62013-10-23 14:05:12 -0700461{
462 sqlite3_stmt *stmt;
Yingdi Yu71c01872013-11-03 16:22:05 -0800463 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 -0700464 sqlite3_bind_text (stmt, 1, name.toUri().c_str(), name.toUri().size(), SQLITE_TRANSIENT);
465
466 if( sqlite3_step (stmt) == SQLITE_ROW)
467 {
468 string alias(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0));
469 Ptr<Blob> certBlob = Ptr<Blob>(new Blob(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 1)), sqlite3_column_bytes (stmt, 1)));
470 Ptr<Data> certData = Data::decodeFromWire(certBlob);
471 EndorseCertificate endorseCertificate(*certData);
Yingdi Yu71c01872013-11-03 16:22:05 -0800472 int isIntroducer = sqlite3_column_int (stmt, 2);
Yingdi Yu4ef8cf62013-10-23 14:05:12 -0700473
Yingdi Yub2e747d2013-11-05 23:06:43 -0800474 sqlite3_finalize (stmt);
475
476 Ptr<ContactItem> contact = Ptr<ContactItem>(new ContactItem(endorseCertificate, isIntroducer, alias));
477
478 if(contact->isIntroducer())
479 {
480 sqlite3_prepare_v2 (m_db, "SELECT trust_scope FROM TrustScope WHERE contact_namespace=?", -1, &stmt, 0);
481 sqlite3_bind_text(stmt, 1, name.toUri().c_str(), name.toUri().size(), SQLITE_TRANSIENT);
482
483 while( sqlite3_step (stmt) == SQLITE_ROW)
484 {
485 Name scope(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0)));
486 contact->addTrustScope(scope);
487 }
488 sqlite3_finalize (stmt);
489 }
490
491 return contact;
Yingdi Yu4ef8cf62013-10-23 14:05:12 -0700492 }
493 return NULL;
494}
495
Yingdi Yu3b318c12013-10-15 17:54:00 -0700496Ptr<Profile>
Yingdi Yu54dcecc2013-10-17 15:07:17 -0700497ContactStorage::getSelfProfile(const Name& identity) const
498{
Yingdi Yu3b318c12013-10-15 17:54:00 -0700499 Ptr<Profile> profile = Ptr<Profile>(new Profile(identity));
Yingdi Yu54dcecc2013-10-17 15:07:17 -0700500 sqlite3_stmt *stmt;
501 sqlite3_prepare_v2 (m_db, "SELECT profile_type, profile_value FROM SelfProfile WHERE profile_identity=?", -1, &stmt, 0);
502 sqlite3_bind_text (stmt, 1, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT);
Yingdi Yu3b318c12013-10-15 17:54:00 -0700503
504 while( sqlite3_step (stmt) == SQLITE_ROW)
505 {
506 string profileType(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0));
507 Blob profileValue(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 1)), sqlite3_column_bytes (stmt, 1));
508 profile->setProfileEntry(profileType, profileValue);
509 }
510
Yingdi Yu72781e52013-11-06 23:00:21 -0800511 sqlite3_finalize(stmt);
512
Yingdi Yu3b318c12013-10-15 17:54:00 -0700513 return profile;
514}
515
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700516Ptr<Blob>
517ContactStorage::getSelfEndorseCertificate(const Name& identity)
Yingdi Yu3b318c12013-10-15 17:54:00 -0700518{
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700519 sqlite3_stmt *stmt;
Yingdi Yuec3d9a32013-10-18 18:35:09 -0700520 sqlite3_prepare_v2 (m_db, "SELECT endorse_data FROM SelfEndorse where identity=?", -1, &stmt, 0);
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700521 sqlite3_bind_text(stmt, 1, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT);
Yingdi Yu54dcecc2013-10-17 15:07:17 -0700522
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700523 Ptr<Blob> result = NULL;
524 if(sqlite3_step (stmt) == SQLITE_ROW)
525 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 -0700526
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700527 sqlite3_finalize (stmt);
Yingdi Yu92e8e482013-10-17 21:13:03 -0700528
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700529 return result;
Yingdi Yu3b318c12013-10-15 17:54:00 -0700530}
531
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700532void
533ContactStorage::updateSelfEndorseCertificate(Ptr<EndorseCertificate> newEndorseCertificate, const Name& identity)
534{
535 Ptr<Blob> newEndorseCertificateBlob = newEndorseCertificate->encodeToWire();
536
537 sqlite3_stmt *stmt;
Yingdi Yuec3d9a32013-10-18 18:35:09 -0700538 sqlite3_prepare_v2 (m_db, "UPDATE SelfEndorse SET endorse_data=? WHERE identity=?", -1, &stmt, 0);
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700539 sqlite3_bind_text(stmt, 1, newEndorseCertificateBlob->buf(), newEndorseCertificateBlob->size(), SQLITE_TRANSIENT);
540 sqlite3_bind_text(stmt, 2, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT);
541 sqlite3_step(stmt);
Yingdi Yub2e747d2013-11-05 23:06:43 -0800542
543 sqlite3_finalize (stmt);
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700544}
545
546void
547ContactStorage::addSelfEndorseCertificate(Ptr<EndorseCertificate> newEndorseCertificate, const Name& identity)
548{
549 Ptr<Blob> newEndorseCertificateBlob = newEndorseCertificate->encodeToWire();
550
551 sqlite3_stmt *stmt;
Yingdi Yuec3d9a32013-10-18 18:35:09 -0700552 sqlite3_prepare_v2 (m_db, "INSERT INTO SelfEndorse (identity, endorse_data) values (?, ?)", -1, &stmt, 0);
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700553 sqlite3_bind_text(stmt, 1, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT);
554 sqlite3_bind_text(stmt, 2, newEndorseCertificateBlob->buf(), newEndorseCertificateBlob->size(), SQLITE_TRANSIENT);
555 sqlite3_step(stmt);
Yingdi Yub2e747d2013-11-05 23:06:43 -0800556
557 sqlite3_finalize (stmt);
558}
559
560Ptr<Blob>
561ContactStorage::getEndorseCertificate(const ndn::Name& identity)
562{
563 sqlite3_stmt *stmt;
564 sqlite3_prepare_v2 (m_db, "SELECT endorse_data FROM ProfileEndorse where identity=?", -1, &stmt, 0);
565 sqlite3_bind_text(stmt, 1, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT);
566
567 Ptr<Blob> result = NULL;
568 if(sqlite3_step (stmt) == SQLITE_ROW)
569 result = Ptr<Blob>(new Blob(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0)));
570
571 sqlite3_finalize (stmt);
572
573 return result;
574}
575
576void
577ContactStorage::updateEndorseCertificate(ndn::Ptr<EndorseCertificate> endorseCertificate, const ndn::Name& identity)
578{
579 Ptr<Blob> newEndorseCertificateBlob = endorseCertificate->encodeToWire();
580
581 sqlite3_stmt *stmt;
582 sqlite3_prepare_v2 (m_db, "UPDATE ProfileEndorse SET endorse_data=? WHERE identity=?", -1, &stmt, 0);
583 sqlite3_bind_text(stmt, 1, newEndorseCertificateBlob->buf(), newEndorseCertificateBlob->size(), SQLITE_TRANSIENT);
584 sqlite3_bind_text(stmt, 2, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT);
585 sqlite3_step(stmt);
586
587 sqlite3_finalize (stmt);
588}
589
590void
591ContactStorage::addEndorseCertificate(ndn::Ptr<EndorseCertificate> endorseCertificate, const ndn::Name& identity)
592{
593 Ptr<Blob> newEndorseCertificateBlob = endorseCertificate->encodeToWire();
594
595 sqlite3_stmt *stmt;
596 sqlite3_prepare_v2 (m_db, "INSERT INTO ProfileEndorse (identity, endorse_data) values (?, ?)", -1, &stmt, 0);
597 sqlite3_bind_text(stmt, 1, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT);
598 sqlite3_bind_text(stmt, 2, newEndorseCertificateBlob->buf(), newEndorseCertificateBlob->size(), SQLITE_TRANSIENT);
599 sqlite3_step(stmt);
600
601 sqlite3_finalize (stmt);
602}
603
604vector<string>
605ContactStorage::getEndorseList(const Name& identity)
606{
607 vector<string> endorseList;
608
609 sqlite3_stmt *stmt;
610 sqlite3_prepare_v2 (m_db, "SELECT profile_type FROM ContactProfile WHERE profile_identity=? AND endorse=1 ORDER BY profile_type", -1, &stmt, 0);
611 sqlite3_bind_text(stmt, 1, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT);
612
613 while( sqlite3_step (stmt) == SQLITE_ROW)
614 {
615 string profileType(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0));
616 endorseList.push_back(profileType);
617 }
618 sqlite3_finalize (stmt);
619
620 return endorseList;
621}
622
623void
624ContactStorage::updateCollectEndorse(const EndorseCertificate& endorseCertificate)
625{
626 Name endorserName = endorseCertificate.getSigner();
627 Name keyName = endorseCertificate.getPublicKeyName();
628 Name endorseeName = keyName.getPrefix(keyName.size()-1);
629 Name getCertName = endorseCertificate.getName();
630 Name oldCertName;
631 bool insert = true;
632 bool update = true;
633
634 sqlite3_stmt *stmt;
635 sqlite3_prepare_v2 (m_db, "SELECT endorse_name FROM CollectEndorse WHERE endorser=? AND endorsee=?", -1, &stmt, 0);
636 sqlite3_bind_text(stmt, 1, endorserName.toUri().c_str(), endorserName.toUri().size(), SQLITE_TRANSIENT);
637 sqlite3_bind_text(stmt, 2, endorseeName.toUri().c_str(), endorseeName.toUri().size(), SQLITE_TRANSIENT);
638
639 if(sqlite3_step (stmt) == SQLITE_ROW)
640 {
641 insert = false;
642 oldCertName = Name(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0)));
643 if(getCertName == oldCertName)
644 update = false;
645 }
646 sqlite3_finalize (stmt);
647
648 if(insert)
649 {
650 sqlite3_prepare_v2 (m_db, "INSERT INTO CollectEndorse (endorser, endorsee, endorse_name, endorse_data) VALUES (?, ?, ?, ?)", -1, &stmt, 0);
651 sqlite3_bind_text(stmt, 1, endorserName.toUri().c_str(), endorserName.toUri().size(), SQLITE_TRANSIENT);
652 sqlite3_bind_text(stmt, 2, endorseeName.toUri().c_str(), endorseeName.toUri().size(), SQLITE_TRANSIENT);
653 sqlite3_bind_text(stmt, 3, getCertName.toUri().c_str(), getCertName.toUri().size(), SQLITE_TRANSIENT);
654 Ptr<Blob> blob = endorseCertificate.encodeToWire();
655 sqlite3_bind_text(stmt, 4, blob->buf(), blob->size(), SQLITE_TRANSIENT);
656 int res = sqlite3_step (stmt);
Yingdi Yub2e747d2013-11-05 23:06:43 -0800657 sqlite3_finalize (stmt);
658 return;
659 }
660 if(update)
661 {
662 sqlite3_prepare_v2 (m_db, "UPDATE CollectEndorse SET endorse_name=?, endorse_data=? WHERE endorser=? AND endorsee=?", -1, &stmt, 0);
663 sqlite3_bind_text(stmt, 1, getCertName.toUri().c_str(), getCertName.toUri().size(), SQLITE_TRANSIENT);
664 Ptr<Blob> blob = endorseCertificate.encodeToWire();
665 sqlite3_bind_text(stmt, 2, blob->buf(), blob->size(), SQLITE_TRANSIENT);
666 sqlite3_bind_text(stmt, 3, endorserName.toUri().c_str(), endorserName.toUri().size(), SQLITE_TRANSIENT);
667 sqlite3_bind_text(stmt, 4, endorseeName.toUri().c_str(), endorseeName.toUri().size(), SQLITE_TRANSIENT);
668 int res = sqlite3_step (stmt);
Yingdi Yub2e747d2013-11-05 23:06:43 -0800669 sqlite3_finalize (stmt);
670 return;
671 }
672}
673
674Ptr<vector<Blob> >
675ContactStorage::getCollectEndorseList(const Name& name)
676{
677 Ptr<vector<Blob> > collectEndorseList = Ptr<vector<Blob> >::Create();
678
679 sqlite3_stmt *stmt;
680 sqlite3_prepare_v2 (m_db, "SELECT endorse_data FROM CollectEndorse WHERE endorsee=?", -1, &stmt, 0);
681 sqlite3_bind_text(stmt, 1, name.toUri().c_str(), name.toUri().size(), SQLITE_TRANSIENT);
682
683 while(sqlite3_step (stmt) == SQLITE_ROW)
684 {
685 Blob blob(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0));
686 collectEndorseList->push_back(blob);
687 }
688
Yingdi Yu72781e52013-11-06 23:00:21 -0800689 sqlite3_finalize (stmt);
690
Yingdi Yub2e747d2013-11-05 23:06:43 -0800691 return collectEndorseList;
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700692}