|
NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
|
API Documentation
|
Go to the documentation of this file.
29 #include <boost/algorithm/string.hpp>
30 #include <boost/filesystem.hpp>
36 using util::Sqlite3Statement;
39 CREATE TABLE IF NOT EXISTS
44 CREATE TABLE IF NOT EXISTS
46 id INTEGER PRIMARY KEY,
47 identity BLOB NOT NULL,
48 is_default INTEGER DEFAULT 0
51 CREATE UNIQUE INDEX IF NOT EXISTS
52 identityIndex ON identities(identity);
54 CREATE TRIGGER IF NOT EXISTS
55 identity_default_before_insert_trigger
56 BEFORE INSERT ON identities
60 UPDATE identities SET is_default=0;
63 CREATE TRIGGER IF NOT EXISTS
64 identity_default_after_insert_trigger
65 AFTER INSERT ON identities
74 WHERE identity=NEW.identity;
77 CREATE TRIGGER IF NOT EXISTS
78 identity_default_update_trigger
79 BEFORE UPDATE ON identities
81 WHEN NEW.is_default=1 AND OLD.is_default=0
83 UPDATE identities SET is_default=0;
86 CREATE TABLE IF NOT EXISTS
88 id INTEGER PRIMARY KEY,
89 identity_id INTEGER NOT NULL,
90 key_name BLOB NOT NULL,
91 key_bits BLOB NOT NULL,
92 is_default INTEGER DEFAULT 0,
93 FOREIGN KEY(identity_id)
94 REFERENCES identities(id)
99 CREATE UNIQUE INDEX IF NOT EXISTS
100 keyIndex ON keys(key_name);
102 CREATE TRIGGER IF NOT EXISTS
103 key_default_before_insert_trigger
104 BEFORE INSERT ON keys
106 WHEN NEW.is_default=1
110 WHERE identity_id=NEW.identity_id;
113 CREATE TRIGGER IF NOT EXISTS
114 key_default_after_insert_trigger
121 AND identity_id=NEW.identity_id)
125 WHERE key_name=NEW.key_name;
128 CREATE TRIGGER IF NOT EXISTS
129 key_default_update_trigger
130 BEFORE UPDATE ON keys
132 WHEN NEW.is_default=1 AND OLD.is_default=0
136 WHERE identity_id=NEW.identity_id;
140 CREATE TABLE IF NOT EXISTS
142 id INTEGER PRIMARY KEY,
143 key_id INTEGER NOT NULL,
144 certificate_name BLOB NOT NULL,
145 certificate_data BLOB NOT NULL,
146 is_default INTEGER DEFAULT 0,
153 CREATE UNIQUE INDEX IF NOT EXISTS
154 certIndex ON certificates(certificate_name);
156 CREATE TRIGGER IF NOT EXISTS
157 cert_default_before_insert_trigger
158 BEFORE INSERT ON certificates
160 WHEN NEW.is_default=1
164 WHERE key_id=NEW.key_id;
167 CREATE TRIGGER IF NOT EXISTS
168 cert_default_after_insert_trigger
169 AFTER INSERT ON certificates
175 AND key_id=NEW.key_id)
179 WHERE certificate_name=NEW.certificate_name;
182 CREATE TRIGGER IF NOT EXISTS
183 cert_default_update_trigger
184 BEFORE UPDATE ON certificates
186 WHEN NEW.is_default=1 AND OLD.is_default=0
190 WHERE key_id=NEW.key_id;
197 boost::filesystem::path dbDir;
198 if (!location.empty()) {
199 dbDir = boost::filesystem::path(location);
201 #ifdef NDN_CXX_HAVE_TESTS
202 else if (getenv(
"TEST_HOME") !=
nullptr) {
203 dbDir = boost::filesystem::path(getenv(
"TEST_HOME")) /
".ndn";
205 #endif // NDN_CXX_HAVE_TESTS
206 else if (getenv(
"HOME") !=
nullptr) {
207 dbDir = boost::filesystem::path(getenv(
"HOME")) /
".ndn";
210 dbDir = boost::filesystem::current_path() /
".ndn";
212 boost::filesystem::create_directories(dbDir);
215 int result = sqlite3_open_v2((dbDir /
"pib.db").c_str(), &m_database,
216 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE,
217 #ifdef NDN_CXX_DISABLE_SQLITE3_FS_LOCKING
224 if (result != SQLITE_OK) {
229 sqlite3_exec(m_database,
"PRAGMA foreign_keys=ON",
nullptr,
nullptr,
nullptr);
232 char* errmsg =
nullptr;
233 result = sqlite3_exec(m_database,
INITIALIZATION.c_str(),
nullptr,
nullptr, &errmsg);
234 if (result != SQLITE_OK && errmsg !=
nullptr) {
235 std::string what =
"PIB database cannot be initialized: "s + errmsg;
236 sqlite3_free(errmsg);
243 sqlite3_close(m_database);
249 static std::string scheme =
"pib-sqlite3";
257 statement.
bind(1, tpmLocator, SQLITE_TRANSIENT);
260 if (sqlite3_changes(m_database) == 0) {
262 Sqlite3Statement insertStatement(m_database,
"INSERT INTO tpmInfo (tpm_locator) values (?)");
263 insertStatement.
bind(1, tpmLocator, SQLITE_TRANSIENT);
264 insertStatement.
step();
272 int res = statement.
step();
273 if (res == SQLITE_ROW)
282 Sqlite3Statement statement(m_database,
"SELECT id FROM identities WHERE identity=?");
284 return statement.
step() == SQLITE_ROW;
291 Sqlite3Statement statement(m_database,
"INSERT INTO identities (identity) values (?)");
296 if (!hasDefaultIdentity()) {
304 Sqlite3Statement statement(m_database,
"DELETE FROM identities WHERE identity=?");
319 std::set<Name> identities;
322 while (statement.
step() == SQLITE_ROW)
331 Sqlite3Statement statement(m_database,
"UPDATE identities SET is_default=1 WHERE identity=?");
339 Sqlite3Statement statement(m_database,
"SELECT identity FROM identities WHERE is_default=1");
341 if (statement.
step() == SQLITE_ROW)
348 PibSqlite3::hasDefaultIdentity()
const
350 Sqlite3Statement statement(m_database,
"SELECT identity FROM identities WHERE is_default=1");
351 return (statement.step() == SQLITE_ROW);
357 Sqlite3Statement statement(m_database,
"SELECT id FROM keys WHERE key_name=?");
360 return (statement.
step() == SQLITE_ROW);
365 const uint8_t* key,
size_t keyLen)
372 "INSERT INTO keys (identity_id, key_name, key_bits) "
373 "VALUES ((SELECT id FROM identities WHERE identity=?), ?, ?)");
376 statement.
bind(3, key, keyLen, SQLITE_STATIC);
381 "UPDATE keys SET key_bits=? WHERE key_name=?");
382 statement.
bind(1, key, keyLen, SQLITE_STATIC);
387 if (!hasDefaultKeyOfIdentity(identity)) {
395 Sqlite3Statement statement(m_database,
"DELETE FROM keys WHERE key_name=?");
403 Sqlite3Statement statement(m_database,
"SELECT key_bits FROM keys WHERE key_name=?");
406 if (statement.
step() == SQLITE_ROW)
415 std::set<Name> keyNames;
419 "FROM keys JOIN identities ON keys.identity_id=identities.id "
420 "WHERE identities.identity=?");
423 while (statement.
step() == SQLITE_ROW) {
437 Sqlite3Statement statement(m_database,
"UPDATE keys SET is_default=1 WHERE key_name=?");
451 "FROM keys JOIN identities ON keys.identity_id=identities.id "
452 "WHERE identities.identity=? AND keys.is_default=1");
455 if (statement.
step() == SQLITE_ROW) {
463 PibSqlite3::hasDefaultKeyOfIdentity(
const Name& identity)
const
467 "FROM keys JOIN identities ON keys.identity_id=identities.id "
468 "WHERE identities.identity=? AND keys.is_default=1");
469 statement.bind(1, identity.
wireEncode(), SQLITE_TRANSIENT);
471 return (statement.step() == SQLITE_ROW);
477 Sqlite3Statement statement(m_database,
"SELECT id FROM certificates WHERE certificate_name=?");
479 return (statement.
step() == SQLITE_ROW);
491 "INSERT INTO certificates "
492 "(key_id, certificate_name, certificate_data) "
493 "VALUES ((SELECT id FROM keys WHERE key_name=?), ?, ?)");
501 "UPDATE certificates SET certificate_data=? WHERE certificate_name=?");
507 if (!hasDefaultCertificateOfKey(certificate.
getKeyName())) {
515 Sqlite3Statement statement(m_database,
"DELETE FROM certificates WHERE certificate_name=?");
524 "SELECT certificate_data FROM certificates WHERE certificate_name=?");
527 if (statement.
step() == SQLITE_ROW)
536 std::set<Name> certNames;
539 "SELECT certificate_name "
540 "FROM certificates JOIN keys ON certificates.key_id=keys.id "
541 "WHERE keys.key_name=?");
544 while (statement.
step() == SQLITE_ROW)
558 "UPDATE certificates SET is_default=1 WHERE certificate_name=?");
567 "SELECT certificate_data "
568 "FROM certificates JOIN keys ON certificates.key_id=keys.id "
569 "WHERE certificates.is_default=1 AND keys.key_name=?");
572 if (statement.
step() == SQLITE_ROW)
579 PibSqlite3::hasDefaultCertificateOfKey(
const Name& keyName)
const
582 "SELECT certificate_data "
583 "FROM certificates JOIN keys ON certificates.key_id=keys.id "
584 "WHERE certificates.is_default=1 AND keys.key_name=?");
585 statement.bind(1, keyName.
wireEncode(), SQLITE_TRANSIENT);
587 return statement.step() == SQLITE_ROW;
Buffer getKeyBits(const Name &keyName) const final
Get the key bits of a key with name keyName.
int step()
wrapper of sqlite3_step
bool hasIdentity(const Name &identity) const final
Check the existence of an identity.
const uint8_t * getBlob(int column)
get a pointer of byte blob from column.
void removeCertificate(const Name &certName) final
Remove a certificate with name certName.
Name getIdentity() const
Get identity name.
Name getDefaultKeyOfIdentity(const Name &identity) const final
size_t value_size() const noexcept
Return the size of TLV-VALUE, aka TLV-LENGTH.
void setDefaultCertificateOfKey(const Name &keyName, const Name &certName) final
Set a cert with name certName as the default of a key with keyName.
represents a non-semantic error
General-purpose automatically managed/resized buffer.
void setDefaultKeyOfIdentity(const Name &identity, const Name &keyName) final
Set an key with keyName as the default key of an identity with name identity.
void addIdentity(const Name &identity) final
Add an identity.
static const std::string & getScheme()
Name getKeyName() const
Get key name.
const Block & getContent() const
Get Content.
size_t wireEncode(EncodingImpl< TAG > &encoder, bool wantUnsignedPortionOnly=false) const
Prepend wire encoding to encoder in NDN Packet Format v0.2.
const Name & getName() const
Get name.
void setTpmLocator(const std::string &tpmLocator) final
Set the corresponding TPM information to tpmLocator.
static const std::string INITIALIZATION
wrap an SQLite3 prepared statement
Represents an absolute name.
v2::Certificate getCertificate(const Name &certName) const final
Get a certificate with name certName.
int getSize(int column)
get the size of column.
The certificate following the certificate format naming convention.
std::set< Name > getIdentities() const final
Get the name of all the identities.
size_t wireEncode(EncodingImpl< TAG > &encoder) const
Fast encoding or block size estimation.
represents a semantic error
~PibSqlite3()
Destruct and cleanup internal state.
void setDefaultIdentity(const Name &identityName) final
Set an identity with name identityName as the default identity.
void addCertificate(const v2::Certificate &certificate) final
Add a certificate.
void toUri(std::ostream &os, name::UriFormat format=name::UriFormat::DEFAULT) const
Write URI representation of the name to the output stream.
std::string getTpmLocator() const final
Get TPM Locator.
bool hasKey(const Name &keyName) const final
Check the existence of a key with keyName.
Name getDefaultIdentity() const final
Get the default identity.
void clearIdentities() final
Erasing all certificates, keys, and identities.
const uint8_t * value() const noexcept
Return a raw pointer to the beginning of TLV-VALUE.
Represents a TLV element of NDN packet format.
bool hasCertificate(const Name &certName) const final
Check the existence of a certificate with name certName.
void addKey(const Name &identity, const Name &keyName, const uint8_t *key, size_t keyLen) final
Add a key.
std::set< Name > getKeysOfIdentity(const Name &identity) const final
Get all the key names of an identity with name identity.
std::string getString(int column)
get a string from column.
std::set< Name > getCertificatesOfKey(const Name &keyName) const final
Get a list of certificate names of a key with id keyName.
PibSqlite3(const std::string &location="")
Create sqlite3-based PIB backed.
void removeKey(const Name &keyName) final
Remove a key with keyName and related certificates.
int bind(int index, const char *value, size_t size, void(*destructor)(void *))
bind a string to the statement
Block getBlock(int column)
get a block from column.
Copyright (c) 2011-2015 Regents of the University of California.
void removeIdentity(const Name &identity) final
Remove an identity and related keys and certificates.
v2::Certificate getDefaultCertificateOfKey(const Name &keyName) const final