blob: 70d04fe6c1590d9d04a130df9bdf997ae4a9569a [file] [log] [blame]
Yingdi Yu01d11352013-10-18 17:04:19 -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 "dns-storage.h"
12#include "exception.h"
13
14#include <boost/filesystem.hpp>
Yingdi Yu590fa5d2013-10-18 18:35:09 -070015#include "logging.h"
Yingdi Yu01d11352013-10-18 17:04:19 -070016
17
18using namespace std;
19using namespace ndn;
20namespace fs = boost::filesystem;
21
Yingdi Yu590fa5d2013-10-18 18:35:09 -070022INIT_LOGGER("DnsStorage");
23
Yingdi Yu01d11352013-10-18 17:04:19 -070024const string INIT_DD_TABLE = "\
25CREATE 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 ); \
35CREATE INDEX dd_index ON DnsData(dns_identity, dns_name, dns_type); \n \
36CREATE INDEX dd_index2 ON DnsData(data_name); \n \
37";
38
39DnsStorage::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)
46 throw LnException("Chronos DNS DB cannot be open/created");
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)
63 throw LnException("Init \"error\" in DnsData");
64 }
65}
66
67DnsStorage::~DnsStorage()
68{
69 sqlite3_close(m_db);
70}
71
72void
73DnsStorage::updateDnsData(const ndn::Blob& data, const std::string& identity, const std::string& name, const std::string& type, const string& dataName)
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 {
Yingdi Yu590fa5d2013-10-18 18:35:09 -070083 _LOG_DEBUG("INSERT");
Yingdi Yu01d11352013-10-18 17:04:19 -070084 sqlite3_finalize(stmt);
85 sqlite3_prepare_v2 (m_db, "INSERT INTO DnsData (dns_identity, dns_name, dns_type, dns_value, data_name) VALUES (?, ?, ?, ?, ?)", -1, &stmt, 0);
86 sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size(), SQLITE_TRANSIENT);
87 sqlite3_bind_text(stmt, 2, name.c_str(), name.size(), SQLITE_TRANSIENT);
88 sqlite3_bind_text(stmt, 3, type.c_str(), type.size(), SQLITE_TRANSIENT);
89 sqlite3_bind_text(stmt, 4, data.buf(), data.size(), SQLITE_TRANSIENT);
90 sqlite3_bind_text(stmt, 5, dataName.c_str(), dataName.size(), SQLITE_TRANSIENT);
91 sqlite3_step(stmt);
92 sqlite3_finalize(stmt);
93 }
94 else
95 {
Yingdi Yu590fa5d2013-10-18 18:35:09 -070096 _LOG_DEBUG("UPDATE");
Yingdi Yu01d11352013-10-18 17:04:19 -070097 sqlite3_finalize(stmt);
98 sqlite3_prepare_v2 (m_db, "UPDATE DnsData SET dns_value=?, data_name=? WHERE dns_identity=? and dns_name=?, dns_type=?", -1, &stmt, 0);
99 sqlite3_bind_text(stmt, 1, data.buf(), data.size(), SQLITE_TRANSIENT);
100 sqlite3_bind_text(stmt, 2, dataName.c_str(), dataName.size(), SQLITE_TRANSIENT);
101 sqlite3_bind_text(stmt, 3, identity.c_str(), identity.size(), SQLITE_TRANSIENT);
102 sqlite3_bind_text(stmt, 4, name.c_str(), name.size(), SQLITE_TRANSIENT);
103 sqlite3_bind_text(stmt, 5, type.c_str(), type.size(), SQLITE_TRANSIENT);
104 sqlite3_step(stmt);
105 sqlite3_finalize(stmt);
106 }
107}
108
109void
110DnsStorage::updateDnsSelfProfileData(const Data& data, const Name& identity)
111{
112 string dnsIdentity = identity.toUri();
113 string dnsName("N/A");
114 string dnsType("PROFILE");
115 Ptr<Blob> dnsValue = data.encodeToWire();
116
117 updateDnsData(*dnsValue, dnsIdentity, dnsName, dnsType, data.getName().toUri());
118}
119
120void
121DnsStorage::updateDnsEndorseOthers(const Data& data, const Name& identity, const Name& endorsee)
122{
123 string dnsIdentity = identity.toUri();
124 string dnsName = endorsee.toUri();
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800125 string dnsType("ENDORSEE");
Yingdi Yu01d11352013-10-18 17:04:19 -0700126 Ptr<Blob> dnsValue = data.encodeToWire();
127
128 updateDnsData(*dnsValue, dnsIdentity, dnsName, dnsType, data.getName().toUri());
129}
130
131void
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800132DnsStorage::updateDnsOthersEndorse(const Data& data, const Name& identity)
Yingdi Yu01d11352013-10-18 17:04:19 -0700133{
134 string dnsIdentity = identity.toUri();
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800135 string dnsName("N/A");
136 string dnsType("ENDORSED");
Yingdi Yu01d11352013-10-18 17:04:19 -0700137 Ptr<Blob> dnsValue = data.encodeToWire();
138
139 updateDnsData(*dnsValue, dnsIdentity, dnsName, dnsType, data.getName().toUri());
140}
141
142Ptr<Data>
143DnsStorage::getData(const Name& dataName)
144{
145 sqlite3_stmt *stmt;
146 sqlite3_prepare_v2 (m_db, "SELECT dns_value FROM DnsData where data_name=?", -1, &stmt, 0);
147 sqlite3_bind_text(stmt, 1, dataName.toUri().c_str(), dataName.toUri().size(), SQLITE_TRANSIENT);
148
149 if(sqlite3_step (stmt) == SQLITE_ROW)
150 {
151 Blob dnsDataBlob(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0));
152 boost::iostreams::stream
153 <boost::iostreams::array_source> is (dnsDataBlob.buf(), dnsDataBlob.size());
154 return Data::decodeFromWire(is);
155 }
156 return NULL;
157}