blob: 7a24fdc9ca2f0ad78636a0d38668b5ba7a7e7e04 [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
48const string INIT_TC_TABLE = "\
49CREATE TABLE IF NOT EXISTS \n \
50 TrustedContact( \n \
51 contact_namespace BLOB NOT NULL, \n \
52 contact_alias BLOB NOT NULL, \n \
53 self_certificate BLOB NOT NULL, \n \
54 trust_scope BLOB NOT NULL, \n \
55 \
56 PRIMARY KEY (contact_namespace) \n \
57 ); \n \
58 \
59CREATE INDEX tc_index ON TrustedContact(contact_namespace); \n \
60";
61
62const string INIT_NC_TABLE = "\
63CREATE TABLE IF NOT EXISTS \n \
64 NormalContact( \n \
65 contact_namespace BLOB NOT NULL, \n \
66 contact_alias BLOB NOT NULL, \n \
67 self_certificate BLOB NOT NULL, \n \
68 \
69 PRIMARY KEY (contact_namespace) \n \
70 ); \n \
71 \
72CREATE INDEX nc_index ON NormalContact(contact_namespace); \n \
73";
74
Yingdi Yu4685b1b2013-10-18 17:05:02 -070075ContactStorage::ContactStorage()
Yingdi Yud04ed1a2013-10-14 14:07:03 -070076{
77 fs::path chronosDir = fs::path(getenv("HOME")) / ".chronos";
78 fs::create_directories (chronosDir);
79
80 int res = sqlite3_open((chronosDir / "chronos.db").c_str (), &m_db);
81 if (res != SQLITE_OK)
82 throw LnException("Chronos DB cannot be open/created");
83
Yingdi Yu3b318c12013-10-15 17:54:00 -070084 // Check if SelfProfile table exists
Yingdi Yud04ed1a2013-10-14 14:07:03 -070085 sqlite3_stmt *stmt;
Yingdi Yu3b318c12013-10-15 17:54:00 -070086 sqlite3_prepare_v2 (m_db, "SELECT name FROM sqlite_master WHERE type='table' And name='SelfProfile'", -1, &stmt, 0);
87 res = sqlite3_step (stmt);
88
89 bool spTableExist = false;
90 if (res == SQLITE_ROW)
91 spTableExist = true;
92 sqlite3_finalize (stmt);
93
94 if(!spTableExist)
95 {
96 char *errmsg = 0;
97 res = sqlite3_exec (m_db, INIT_SP_TABLE.c_str (), NULL, NULL, &errmsg);
98 if (res != SQLITE_OK && errmsg != 0)
99 throw LnException("Init \"error\" in SelfProfile");
100 }
101
Yingdi Yuec3d9a32013-10-18 18:35:09 -0700102 // Check if SelfEndorse table exists
103 sqlite3_prepare_v2 (m_db, "SELECT name FROM sqlite_master WHERE type='table' And name='SelfEndorse'", -1, &stmt, 0);
Yingdi Yu3b318c12013-10-15 17:54:00 -0700104 res = sqlite3_step (stmt);
105
Yingdi Yuec3d9a32013-10-18 18:35:09 -0700106 bool seTableExist = false;
Yingdi Yu3b318c12013-10-15 17:54:00 -0700107 if (res == SQLITE_ROW)
Yingdi Yuec3d9a32013-10-18 18:35:09 -0700108 seTableExist = true;
Yingdi Yu3b318c12013-10-15 17:54:00 -0700109 sqlite3_finalize (stmt);
110
Yingdi Yuec3d9a32013-10-18 18:35:09 -0700111 if(!seTableExist)
Yingdi Yu3b318c12013-10-15 17:54:00 -0700112 {
113 char *errmsg = 0;
Yingdi Yuec3d9a32013-10-18 18:35:09 -0700114 res = sqlite3_exec (m_db, INIT_SE_TABLE.c_str (), NULL, NULL, &errmsg);
Yingdi Yu3b318c12013-10-15 17:54:00 -0700115 if (res != SQLITE_OK && errmsg != 0)
Yingdi Yuec3d9a32013-10-18 18:35:09 -0700116 throw LnException("Init \"error\" in SelfEndorse");
Yingdi Yu3b318c12013-10-15 17:54:00 -0700117 }
118
119
120 // Check if TrustedContact table exists
Yingdi Yud04ed1a2013-10-14 14:07:03 -0700121 sqlite3_prepare_v2 (m_db, "SELECT name FROM sqlite_master WHERE type='table' And name='TrustedContact'", -1, &stmt, 0);
122 res = sqlite3_step (stmt);
123
124 bool tcTableExist = false;
125 if (res == SQLITE_ROW)
126 tcTableExist = true;
127 sqlite3_finalize (stmt);
128
129 if(!tcTableExist)
130 {
131 char *errmsg = 0;
132 res = sqlite3_exec (m_db, INIT_TC_TABLE.c_str (), NULL, NULL, &errmsg);
133 if (res != SQLITE_OK && errmsg != 0)
134 throw LnException("Init \"error\" in TrustedContact");
Yingdi Yud04ed1a2013-10-14 14:07:03 -0700135 }
136
137 // Check if NormalContact table exists
138 sqlite3_prepare_v2 (m_db, "SELECT name FROM sqlite_master WHERE type='table' And name='NormalContact'", -1, &stmt, 0);
139 res = sqlite3_step (stmt);
140
141 bool ncTableExist = false;
142 if (res == SQLITE_ROW)
143 ncTableExist = true;
144 sqlite3_finalize (stmt);
145
146 if(!ncTableExist)
147 {
148 char *errmsg = 0;
149 res = sqlite3_exec (m_db, INIT_NC_TABLE.c_str (), NULL, NULL, &errmsg);
150
151 if (res != SQLITE_OK && errmsg != 0)
152 throw LnException("Init \"error\" in NormalContact");
153 }
154}
155
Yingdi Yu3b318c12013-10-15 17:54:00 -0700156bool
Yingdi Yu54dcecc2013-10-17 15:07:17 -0700157ContactStorage::doesSelfEntryExist(const Name& identity, const string& profileType)
Yingdi Yu3b318c12013-10-15 17:54:00 -0700158{
159 bool result = false;
160
161 sqlite3_stmt *stmt;
Yingdi Yu54dcecc2013-10-17 15:07:17 -0700162 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 -0700163 sqlite3_bind_text(stmt, 1, profileType.c_str(), profileType.size(), SQLITE_TRANSIENT);
Yingdi Yu54dcecc2013-10-17 15:07:17 -0700164 sqlite3_bind_text(stmt, 2, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT);
Yingdi Yu3b318c12013-10-15 17:54:00 -0700165
166 int res = sqlite3_step (stmt);
167
168 if (res == SQLITE_ROW)
169 {
170 int countAll = sqlite3_column_int (stmt, 0);
171 if (countAll > 0)
172 result = true;
173 }
174 sqlite3_finalize (stmt);
175 return result;
176}
177
Yingdi Yu3b318c12013-10-15 17:54:00 -0700178void
Yingdi Yu54dcecc2013-10-17 15:07:17 -0700179ContactStorage::setSelfProfileEntry(const Name& identity, const string& profileType, const Blob& profileValue)
Yingdi Yu3b318c12013-10-15 17:54:00 -0700180{
Yingdi Yu3b318c12013-10-15 17:54:00 -0700181 sqlite3_stmt *stmt;
Yingdi Yu54dcecc2013-10-17 15:07:17 -0700182 if(doesSelfEntryExist(identity, profileType))
Yingdi Yu3b318c12013-10-15 17:54:00 -0700183 {
Yingdi Yu54dcecc2013-10-17 15:07:17 -0700184 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 -0700185 sqlite3_bind_text(stmt, 1, profileValue.buf(), profileValue.size(), SQLITE_TRANSIENT);
186 sqlite3_bind_text(stmt, 2, profileType.c_str(), profileType.size(), SQLITE_TRANSIENT);
Yingdi Yu54dcecc2013-10-17 15:07:17 -0700187 sqlite3_bind_text(stmt, 3, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT);
Yingdi Yu3b318c12013-10-15 17:54:00 -0700188 }
189 else
190 {
Yingdi Yu54dcecc2013-10-17 15:07:17 -0700191 sqlite3_prepare_v2 (m_db, "INSERT INTO SelfProfile (profile_identity, profile_type, profile_value) values (?, ?, ?)", -1, &stmt, 0);
192 sqlite3_bind_text(stmt, 1, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT);
193 sqlite3_bind_text(stmt, 2, profileType.c_str(), profileType.size(), SQLITE_TRANSIENT);
194 sqlite3_bind_text(stmt, 3, profileValue.buf(), profileValue.size(), SQLITE_TRANSIENT);
Yingdi Yu3b318c12013-10-15 17:54:00 -0700195 }
196 sqlite3_step (stmt);
197 sqlite3_finalize (stmt);
198}
199
200Ptr<Profile>
Yingdi Yu54dcecc2013-10-17 15:07:17 -0700201ContactStorage::getSelfProfile(const Name& identity)
Yingdi Yu3b318c12013-10-15 17:54:00 -0700202{
Yingdi Yu2ac40fb2013-10-21 13:38:38 -0700203 _LOG_DEBUG("getSelfProfile " << identity.toUri());
Yingdi Yu3b318c12013-10-15 17:54:00 -0700204 sqlite3_stmt *stmt;
Yingdi Yu54dcecc2013-10-17 15:07:17 -0700205 Ptr<Profile> profile = Ptr<Profile>(new Profile(identity));
Yingdi Yu3b318c12013-10-15 17:54:00 -0700206
Yingdi Yu2ac40fb2013-10-21 13:38:38 -0700207 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 -0700208 sqlite3_bind_text(stmt, 1, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT);
Yingdi Yu2ac40fb2013-10-21 13:38:38 -0700209
210 while(sqlite3_step (stmt) == SQLITE_ROW)
Yingdi Yu3b318c12013-10-15 17:54:00 -0700211 {
Yingdi Yu2ac40fb2013-10-21 13:38:38 -0700212 _LOG_DEBUG("entry");
Yingdi Yu3b318c12013-10-15 17:54:00 -0700213 string profileType(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0));
214 Blob profileValue(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 1)), sqlite3_column_bytes (stmt, 1));
215
216 profile->setProfileEntry(profileType, profileValue );
217 }
218
219 return profile;
220}
221
Yingdi Yud04ed1a2013-10-14 14:07:03 -0700222void
223ContactStorage::addTrustedContact(const TrustedContact& trustedContact)
224{
225 if(doesTrustedContactExist(trustedContact.getNameSpace()))
226 throw LnException("Trusted Contact has already existed");
227
228 sqlite3_stmt *stmt;
229 sqlite3_prepare_v2 (m_db,
230 "INSERT INTO TrustedContact (contact_namespace, contact_alias, self_certificate, trust_scope) values (?, ?, ?, ?)",
231 -1,
232 &stmt,
233 0);
234
235 sqlite3_bind_text(stmt, 1, trustedContact.getNameSpace().toUri().c_str(), trustedContact.getNameSpace().toUri().size (), SQLITE_TRANSIENT);
236 sqlite3_bind_text(stmt, 2, trustedContact.getAlias().c_str(), trustedContact.getAlias().size(), SQLITE_TRANSIENT);
237 Ptr<Blob> selfCertificateBlob = trustedContact.getSelfEndorseCertificate().encodeToWire();
238 sqlite3_bind_text(stmt, 3, selfCertificateBlob->buf(), selfCertificateBlob->size(), SQLITE_TRANSIENT);
239 Ptr<Blob> trustScopeBlob = trustedContact.getTrustScopeBlob();
240 sqlite3_bind_text(stmt, 4, trustScopeBlob->buf(), trustScopeBlob->size(),SQLITE_TRANSIENT);
241
242 int res = sqlite3_step (stmt);
243 sqlite3_finalize (stmt);
244}
245
246void
247ContactStorage::addNormalContact(const ContactItem& normalContact)
248{
249 if(doesNormalContactExist(normalContact.getNameSpace()))
250 throw LnException("Normal Contact has already existed");
251
252 sqlite3_stmt *stmt;
253 sqlite3_prepare_v2 (m_db,
254 "INSERT INTO NormalContact (contact_namespace, contact_alias, self_certificate) values (?, ?, ?)",
255 -1,
256 &stmt,
257 0);
258
259 sqlite3_bind_text(stmt, 1, normalContact.getNameSpace().toUri().c_str(), normalContact.getNameSpace().toUri().size (), SQLITE_TRANSIENT);
260 sqlite3_bind_text(stmt, 2, normalContact.getAlias().c_str(), normalContact.getAlias().size(), SQLITE_TRANSIENT);
261 Ptr<Blob> selfCertificateBlob = normalContact.getSelfEndorseCertificate().encodeToWire();
262 sqlite3_bind_text(stmt, 3, selfCertificateBlob->buf(), selfCertificateBlob->size(), SQLITE_TRANSIENT);
263
264 int res = sqlite3_step (stmt);
Yingdi Yu2ac40fb2013-10-21 13:38:38 -0700265 // _LOG_DEBUG("res " << res);
Yingdi Yud04ed1a2013-10-14 14:07:03 -0700266 sqlite3_finalize (stmt);
267}
268
Yingdi Yu2ac40fb2013-10-21 13:38:38 -0700269void
270ContactStorage::updateAlias(const ndn::Name& identity, std::string alias)
271{
272 if(doesNormalContactExist(identity))
273 {
274 sqlite3_stmt *stmt;
275 sqlite3_prepare_v2 (m_db, "UPDATE NormalContact SET contact_alias=? WHERE contact_namespace=?", -1, &stmt, 0);
276 sqlite3_bind_text(stmt, 1, alias.c_str(), alias.size(), SQLITE_TRANSIENT);
277 sqlite3_bind_text(stmt, 2, identity.toUri().c_str(), identity.toUri().size (), SQLITE_TRANSIENT);
278 int res = sqlite3_step (stmt);
279 sqlite3_finalize (stmt);
280 return;
281 }
282 if(doesTrustedContactExist(identity))
283 {
284 sqlite3_stmt *stmt;
285 sqlite3_prepare_v2 (m_db, "UPDATE TrustedContact SET contact_alias=? WHERE contact_namespace=?", -1, &stmt, 0);
286 sqlite3_bind_text(stmt, 1, alias.c_str(), alias.size(), SQLITE_TRANSIENT);
287 sqlite3_bind_text(stmt, 2, identity.toUri().c_str(), identity.toUri().size (), SQLITE_TRANSIENT);
288 int res = sqlite3_step (stmt);
289 sqlite3_finalize (stmt);
290 return;
291 }
292}
293
Yingdi Yud04ed1a2013-10-14 14:07:03 -0700294bool
295ContactStorage::doesContactExist(const Name& name, bool normal)
296{
297 bool result = false;
298
299 sqlite3_stmt *stmt;
300 if(normal)
301 sqlite3_prepare_v2 (m_db, "SELECT count(*) FROM NormalContact WHERE contact_namespace=?", -1, &stmt, 0);
302 else
303 sqlite3_prepare_v2 (m_db, "SELECT count(*) FROM TrustedContact WHERE contact_namespace=?", -1, &stmt, 0);
304 sqlite3_bind_text(stmt, 1, name.toUri().c_str(), name.toUri().size(), SQLITE_TRANSIENT);
305
306 int res = sqlite3_step (stmt);
307
308 if (res == SQLITE_ROW)
309 {
310 int countAll = sqlite3_column_int (stmt, 0);
311 if (countAll > 0)
312 result = true;
313 }
314 sqlite3_finalize (stmt);
315 return result;
316}
317
318vector<Ptr<TrustedContact> >
319ContactStorage::getAllTrustedContacts() const
320{
321 vector<Ptr<TrustedContact> > trustedContacts;
322
323 sqlite3_stmt *stmt;
324 sqlite3_prepare_v2 (m_db,
325 "SELECT contact_alias, self_certificate, trust_scope FROM TrustedContact",
326 -1,
327 &stmt,
328 0);
329
330 while( sqlite3_step (stmt) == SQLITE_ROW)
331 {
332 string alias(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0));
333 Ptr<Blob> certBlob = Ptr<Blob>(new Blob(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 1)), sqlite3_column_bytes (stmt, 1)));
334 Ptr<Data> certData = Data::decodeFromWire(certBlob);
335 EndorseCertificate endorseCertificate(*certData);
336 string trustScope(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 2)), sqlite3_column_bytes (stmt, 2));
337
338 trustedContacts.push_back(Ptr<TrustedContact>(new TrustedContact(endorseCertificate, trustScope, alias)));
339 }
340
341 return trustedContacts;
342}
343
344vector<Ptr<ContactItem> >
345ContactStorage::getAllNormalContacts() const
346{
347 vector<Ptr<ContactItem> > normalContacts;
348
349 sqlite3_stmt *stmt;
350 sqlite3_prepare_v2 (m_db,
351 "SELECT contact_alias, self_certificate FROM NormalContact",
352 -1,
353 &stmt,
354 0);
355
356 while( sqlite3_step (stmt) == SQLITE_ROW)
357 {
358 string alias(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0));
359 Ptr<Blob> certBlob = Ptr<Blob>(new Blob(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 1)), sqlite3_column_bytes (stmt, 1)));
360 Ptr<Data> certData = Data::decodeFromWire(certBlob);
361 EndorseCertificate endorseCertificate(*certData);
362
363 normalContacts.push_back(Ptr<ContactItem>(new ContactItem(endorseCertificate, alias)));
364 }
365
366 return normalContacts;
367}
Yingdi Yu3b318c12013-10-15 17:54:00 -0700368
Yingdi Yu4ef8cf62013-10-23 14:05:12 -0700369Ptr<ContactItem>
370ContactStorage::getNormalContact(const Name& name)
371{
372 sqlite3_stmt *stmt;
373 sqlite3_prepare_v2 (m_db,
374 "SELECT contact_alias, self_certificate FROM NormalContact where contact_namespace=?",
375 -1,
376 &stmt,
377 0);
378 sqlite3_bind_text (stmt, 1, name.toUri().c_str(), name.toUri().size(), SQLITE_TRANSIENT);
379
380 if( sqlite3_step (stmt) == SQLITE_ROW)
381 {
382 string alias(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0));
383 Ptr<Blob> certBlob = Ptr<Blob>(new Blob(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 1)), sqlite3_column_bytes (stmt, 1)));
384 Ptr<Data> certData = Data::decodeFromWire(certBlob);
385 EndorseCertificate endorseCertificate(*certData);
386
387 return Ptr<ContactItem>(new ContactItem(endorseCertificate, alias));
388 }
389 return NULL;
390}
391
392Ptr<TrustedContact>
393ContactStorage::getTrustedContact(const Name& name)
394{
395 sqlite3_stmt *stmt;
396 sqlite3_prepare_v2 (m_db,
397 "SELECT contact_alias, self_certificate, trust_scope FROM TrustedContact where contact_namespace=?",
398 -1,
399 &stmt,
400 0);
401 sqlite3_bind_text (stmt, 1, name.toUri().c_str(), name.toUri().size(), SQLITE_TRANSIENT);
402
403 if( sqlite3_step (stmt) == SQLITE_ROW)
404 {
405 string alias(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0));
406 Ptr<Blob> certBlob = Ptr<Blob>(new Blob(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 1)), sqlite3_column_bytes (stmt, 1)));
407 Ptr<Data> certData = Data::decodeFromWire(certBlob);
408 EndorseCertificate endorseCertificate(*certData);
409 string trustScope(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 2)), sqlite3_column_bytes (stmt, 2));
410
411 return Ptr<TrustedContact>(new TrustedContact(endorseCertificate, trustScope, alias));
412 }
413 return NULL;
414}
415
Yingdi Yu3b318c12013-10-15 17:54:00 -0700416Ptr<Profile>
Yingdi Yu54dcecc2013-10-17 15:07:17 -0700417ContactStorage::getSelfProfile(const Name& identity) const
418{
Yingdi Yu3b318c12013-10-15 17:54:00 -0700419 Ptr<Profile> profile = Ptr<Profile>(new Profile(identity));
Yingdi Yu54dcecc2013-10-17 15:07:17 -0700420 sqlite3_stmt *stmt;
421 sqlite3_prepare_v2 (m_db, "SELECT profile_type, profile_value FROM SelfProfile WHERE profile_identity=?", -1, &stmt, 0);
422 sqlite3_bind_text (stmt, 1, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT);
Yingdi Yu3b318c12013-10-15 17:54:00 -0700423
424 while( sqlite3_step (stmt) == SQLITE_ROW)
425 {
426 string profileType(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0));
427 Blob profileValue(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 1)), sqlite3_column_bytes (stmt, 1));
428 profile->setProfileEntry(profileType, profileValue);
429 }
430
431 return profile;
432}
433
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700434Ptr<Blob>
435ContactStorage::getSelfEndorseCertificate(const Name& identity)
Yingdi Yu3b318c12013-10-15 17:54:00 -0700436{
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700437 sqlite3_stmt *stmt;
Yingdi Yuec3d9a32013-10-18 18:35:09 -0700438 sqlite3_prepare_v2 (m_db, "SELECT endorse_data FROM SelfEndorse where identity=?", -1, &stmt, 0);
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700439 sqlite3_bind_text(stmt, 1, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT);
Yingdi Yu54dcecc2013-10-17 15:07:17 -0700440
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700441 Ptr<Blob> result = NULL;
442 if(sqlite3_step (stmt) == SQLITE_ROW)
443 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 -0700444
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700445 sqlite3_finalize (stmt);
Yingdi Yu92e8e482013-10-17 21:13:03 -0700446
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700447 return result;
Yingdi Yu3b318c12013-10-15 17:54:00 -0700448}
449
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700450void
451ContactStorage::updateSelfEndorseCertificate(Ptr<EndorseCertificate> newEndorseCertificate, const Name& identity)
452{
453 Ptr<Blob> newEndorseCertificateBlob = newEndorseCertificate->encodeToWire();
454
455 sqlite3_stmt *stmt;
Yingdi Yuec3d9a32013-10-18 18:35:09 -0700456 sqlite3_prepare_v2 (m_db, "UPDATE SelfEndorse SET endorse_data=? WHERE identity=?", -1, &stmt, 0);
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700457 sqlite3_bind_text(stmt, 1, newEndorseCertificateBlob->buf(), newEndorseCertificateBlob->size(), SQLITE_TRANSIENT);
458 sqlite3_bind_text(stmt, 2, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT);
459 sqlite3_step(stmt);
460}
461
462void
463ContactStorage::addSelfEndorseCertificate(Ptr<EndorseCertificate> newEndorseCertificate, const Name& identity)
464{
465 Ptr<Blob> newEndorseCertificateBlob = newEndorseCertificate->encodeToWire();
466
467 sqlite3_stmt *stmt;
Yingdi Yuec3d9a32013-10-18 18:35:09 -0700468 sqlite3_prepare_v2 (m_db, "INSERT INTO SelfEndorse (identity, endorse_data) values (?, ?)", -1, &stmt, 0);
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700469 sqlite3_bind_text(stmt, 1, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT);
470 sqlite3_bind_text(stmt, 2, newEndorseCertificateBlob->buf(), newEndorseCertificateBlob->size(), SQLITE_TRANSIENT);
471 sqlite3_step(stmt);
472}