Yingdi Yu | 01d1135 | 2013-10-18 17:04:19 -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 "dns-storage.h" |
Yingdi Yu | 01d1135 | 2013-10-18 17:04:19 -0700 | [diff] [blame] | 12 | |
| 13 | #include <boost/filesystem.hpp> |
Yingdi Yu | 590fa5d | 2013-10-18 18:35:09 -0700 | [diff] [blame] | 14 | #include "logging.h" |
Yingdi Yu | 01d1135 | 2013-10-18 17:04:19 -0700 | [diff] [blame] | 15 | |
| 16 | |
| 17 | using namespace std; |
| 18 | using namespace ndn; |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 19 | using namespace ndn::ptr_lib; |
Yingdi Yu | 01d1135 | 2013-10-18 17:04:19 -0700 | [diff] [blame] | 20 | namespace fs = boost::filesystem; |
| 21 | |
Yingdi Yu | 590fa5d | 2013-10-18 18:35:09 -0700 | [diff] [blame] | 22 | INIT_LOGGER("DnsStorage"); |
| 23 | |
Yingdi Yu | 01d1135 | 2013-10-18 17:04:19 -0700 | [diff] [blame] | 24 | const string INIT_DD_TABLE = "\ |
| 25 | CREATE TABLE IF NOT EXISTS \n \ |
| 26 | DnsData( \n \ |
| 27 | dns_identity BLOB NOT NULL, \n \ |
| 28 | dns_name BLOB NOT NULL, \n \ |
| 29 | dns_type BLOB NOT NULL, \n \ |
| 30 | data_name BLOB NOT NULL, \n \ |
| 31 | dns_value BLOB NOT NULL, \n \ |
| 32 | \ |
| 33 | PRIMARY KEY (dns_identity, dns_name, dns_type) \n \ |
| 34 | ); \ |
| 35 | CREATE INDEX dd_index ON DnsData(dns_identity, dns_name, dns_type); \n \ |
| 36 | CREATE INDEX dd_index2 ON DnsData(data_name); \n \ |
| 37 | "; |
| 38 | |
| 39 | DnsStorage::DnsStorage() |
| 40 | { |
| 41 | fs::path chronosDir = fs::path(getenv("HOME")) / ".chronos"; |
| 42 | fs::create_directories (chronosDir); |
| 43 | |
| 44 | int res = sqlite3_open((chronosDir / "dns.db").c_str (), &m_db); |
| 45 | if (res != SQLITE_OK) |
Yingdi Yu | c9ffa9f | 2014-01-13 11:19:47 -0800 | [diff] [blame] | 46 | throw Error("Chronos DNS DB cannot be open/created"); |
Yingdi Yu | 01d1135 | 2013-10-18 17:04:19 -0700 | [diff] [blame] | 47 | |
| 48 | // Check if SelfProfile table exists |
| 49 | sqlite3_stmt *stmt; |
| 50 | sqlite3_prepare_v2 (m_db, "SELECT name FROM sqlite_master WHERE type='table' And name='DnsData'", -1, &stmt, 0); |
| 51 | res = sqlite3_step (stmt); |
| 52 | |
| 53 | bool ddTableExist = false; |
| 54 | if (res == SQLITE_ROW) |
| 55 | ddTableExist = true; |
| 56 | sqlite3_finalize (stmt); |
| 57 | |
| 58 | if(!ddTableExist) |
| 59 | { |
| 60 | char *errmsg = 0; |
| 61 | res = sqlite3_exec (m_db, INIT_DD_TABLE.c_str (), NULL, NULL, &errmsg); |
| 62 | if (res != SQLITE_OK && errmsg != 0) |
Yingdi Yu | c9ffa9f | 2014-01-13 11:19:47 -0800 | [diff] [blame] | 63 | throw Error("Init \"error\" in DnsData"); |
Yingdi Yu | 01d1135 | 2013-10-18 17:04:19 -0700 | [diff] [blame] | 64 | } |
| 65 | } |
| 66 | |
| 67 | DnsStorage::~DnsStorage() |
| 68 | { |
| 69 | sqlite3_close(m_db); |
| 70 | } |
| 71 | |
| 72 | void |
Yingdi Yu | c9ffa9f | 2014-01-13 11:19:47 -0800 | [diff] [blame] | 73 | DnsStorage::updateDnsData(const ndn::Block& data, const std::string& identity, const std::string& name, const std::string& type, const string& dataName) |
Yingdi Yu | 01d1135 | 2013-10-18 17:04:19 -0700 | [diff] [blame] | 74 | { |
| 75 | sqlite3_stmt *stmt; |
| 76 | sqlite3_prepare_v2 (m_db, "SELECT data_name FROM DnsData where dns_identity=? and dns_name=? and dns_type=?", -1, &stmt, 0); |
| 77 | sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size(), SQLITE_TRANSIENT); |
| 78 | sqlite3_bind_text(stmt, 2, name.c_str(), name.size(), SQLITE_TRANSIENT); |
| 79 | sqlite3_bind_text(stmt, 3, type.c_str(), type.size(), SQLITE_TRANSIENT); |
| 80 | |
| 81 | if(sqlite3_step (stmt) != SQLITE_ROW) |
| 82 | { |
| 83 | sqlite3_finalize(stmt); |
| 84 | sqlite3_prepare_v2 (m_db, "INSERT INTO DnsData (dns_identity, dns_name, dns_type, dns_value, data_name) VALUES (?, ?, ?, ?, ?)", -1, &stmt, 0); |
| 85 | sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size(), SQLITE_TRANSIENT); |
| 86 | sqlite3_bind_text(stmt, 2, name.c_str(), name.size(), SQLITE_TRANSIENT); |
| 87 | sqlite3_bind_text(stmt, 3, type.c_str(), type.size(), SQLITE_TRANSIENT); |
Yingdi Yu | c9ffa9f | 2014-01-13 11:19:47 -0800 | [diff] [blame] | 88 | sqlite3_bind_text(stmt, 4, (const char*)data.wire(), data.size(), SQLITE_TRANSIENT); |
Yingdi Yu | 01d1135 | 2013-10-18 17:04:19 -0700 | [diff] [blame] | 89 | sqlite3_bind_text(stmt, 5, dataName.c_str(), dataName.size(), SQLITE_TRANSIENT); |
| 90 | sqlite3_step(stmt); |
| 91 | sqlite3_finalize(stmt); |
| 92 | } |
| 93 | else |
| 94 | { |
Yingdi Yu | 01d1135 | 2013-10-18 17:04:19 -0700 | [diff] [blame] | 95 | sqlite3_finalize(stmt); |
| 96 | sqlite3_prepare_v2 (m_db, "UPDATE DnsData SET dns_value=?, data_name=? WHERE dns_identity=? and dns_name=?, dns_type=?", -1, &stmt, 0); |
Yingdi Yu | c9ffa9f | 2014-01-13 11:19:47 -0800 | [diff] [blame] | 97 | sqlite3_bind_text(stmt, 1, (const char*)data.wire(), data.size(), SQLITE_TRANSIENT); |
Yingdi Yu | 01d1135 | 2013-10-18 17:04:19 -0700 | [diff] [blame] | 98 | sqlite3_bind_text(stmt, 2, dataName.c_str(), dataName.size(), SQLITE_TRANSIENT); |
| 99 | sqlite3_bind_text(stmt, 3, identity.c_str(), identity.size(), SQLITE_TRANSIENT); |
| 100 | sqlite3_bind_text(stmt, 4, name.c_str(), name.size(), SQLITE_TRANSIENT); |
| 101 | sqlite3_bind_text(stmt, 5, type.c_str(), type.size(), SQLITE_TRANSIENT); |
| 102 | sqlite3_step(stmt); |
| 103 | sqlite3_finalize(stmt); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | void |
| 108 | DnsStorage::updateDnsSelfProfileData(const Data& data, const Name& identity) |
| 109 | { |
| 110 | string dnsIdentity = identity.toUri(); |
| 111 | string dnsName("N/A"); |
| 112 | string dnsType("PROFILE"); |
Yingdi Yu | c9ffa9f | 2014-01-13 11:19:47 -0800 | [diff] [blame] | 113 | |
Yingdi Yu | 01d1135 | 2013-10-18 17:04:19 -0700 | [diff] [blame] | 114 | |
Yingdi Yu | c9ffa9f | 2014-01-13 11:19:47 -0800 | [diff] [blame] | 115 | updateDnsData(data.wireEncode(), dnsIdentity, dnsName, dnsType, data.getName().toUri()); |
Yingdi Yu | 01d1135 | 2013-10-18 17:04:19 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | void |
| 119 | DnsStorage::updateDnsEndorseOthers(const Data& data, const Name& identity, const Name& endorsee) |
| 120 | { |
| 121 | string dnsIdentity = identity.toUri(); |
| 122 | string dnsName = endorsee.toUri(); |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 123 | string dnsType("ENDORSEE"); |
Yingdi Yu | 01d1135 | 2013-10-18 17:04:19 -0700 | [diff] [blame] | 124 | |
Yingdi Yu | c9ffa9f | 2014-01-13 11:19:47 -0800 | [diff] [blame] | 125 | updateDnsData(data.wireEncode(), dnsIdentity, dnsName, dnsType, data.getName().toUri()); |
Yingdi Yu | 01d1135 | 2013-10-18 17:04:19 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | void |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 129 | DnsStorage::updateDnsOthersEndorse(const Data& data, const Name& identity) |
Yingdi Yu | 01d1135 | 2013-10-18 17:04:19 -0700 | [diff] [blame] | 130 | { |
| 131 | string dnsIdentity = identity.toUri(); |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 132 | string dnsName("N/A"); |
| 133 | string dnsType("ENDORSED"); |
Yingdi Yu | 01d1135 | 2013-10-18 17:04:19 -0700 | [diff] [blame] | 134 | |
Yingdi Yu | c9ffa9f | 2014-01-13 11:19:47 -0800 | [diff] [blame] | 135 | updateDnsData(data.wireEncode(), dnsIdentity, dnsName, dnsType, data.getName().toUri()); |
Yingdi Yu | 01d1135 | 2013-10-18 17:04:19 -0700 | [diff] [blame] | 136 | } |
| 137 | |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 138 | shared_ptr<Data> |
Yingdi Yu | 01d1135 | 2013-10-18 17:04:19 -0700 | [diff] [blame] | 139 | DnsStorage::getData(const Name& dataName) |
| 140 | { |
| 141 | sqlite3_stmt *stmt; |
| 142 | sqlite3_prepare_v2 (m_db, "SELECT dns_value FROM DnsData where data_name=?", -1, &stmt, 0); |
| 143 | sqlite3_bind_text(stmt, 1, dataName.toUri().c_str(), dataName.toUri().size(), SQLITE_TRANSIENT); |
| 144 | |
| 145 | if(sqlite3_step (stmt) == SQLITE_ROW) |
| 146 | { |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 147 | shared_ptr<Data> data = make_shared<Data>(); |
Yingdi Yu | c9ffa9f | 2014-01-13 11:19:47 -0800 | [diff] [blame] | 148 | data->wireDecode(Block(reinterpret_cast<const uint8_t*>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0))); |
Yingdi Yu | b35b865 | 2013-11-07 11:32:40 -0800 | [diff] [blame] | 149 | sqlite3_finalize(stmt); |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 150 | return data; |
Yingdi Yu | 01d1135 | 2013-10-18 17:04:19 -0700 | [diff] [blame] | 151 | } |
Yingdi Yu | b35b865 | 2013-11-07 11:32:40 -0800 | [diff] [blame] | 152 | sqlite3_finalize(stmt); |
| 153 | |
Yingdi Yu | eaa84e2 | 2014-01-16 10:30:26 -0800 | [diff] [blame^] | 154 | return shared_ptr<Data>(); |
Yingdi Yu | 01d1135 | 2013-10-18 17:04:19 -0700 | [diff] [blame] | 155 | } |