blob: a5a9588086657f964d9693451b09fcc3ef236a2c [file] [log] [blame]
Yingdi Yu783ce4b2013-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"
Yingdi Yu76dd8002013-12-24 11:16:32 +080012#include "null-ptrs.h"
Yingdi Yu783ce4b2013-10-18 17:04:19 -070013#include "exception.h"
14
15#include <boost/filesystem.hpp>
Yingdi Yuec3d9a32013-10-18 18:35:09 -070016#include "logging.h"
Yingdi Yu783ce4b2013-10-18 17:04:19 -070017
18
19using namespace std;
20using namespace ndn;
Yingdi Yu76dd8002013-12-24 11:16:32 +080021using namespace ndn::ptr_lib;
Yingdi Yu783ce4b2013-10-18 17:04:19 -070022namespace fs = boost::filesystem;
23
Yingdi Yuec3d9a32013-10-18 18:35:09 -070024INIT_LOGGER("DnsStorage");
25
Yingdi Yu783ce4b2013-10-18 17:04:19 -070026const string INIT_DD_TABLE = "\
27CREATE TABLE IF NOT EXISTS \n \
28 DnsData( \n \
29 dns_identity BLOB NOT NULL, \n \
30 dns_name BLOB NOT NULL, \n \
31 dns_type BLOB NOT NULL, \n \
32 data_name BLOB NOT NULL, \n \
33 dns_value BLOB NOT NULL, \n \
34 \
35 PRIMARY KEY (dns_identity, dns_name, dns_type) \n \
36 ); \
37CREATE INDEX dd_index ON DnsData(dns_identity, dns_name, dns_type); \n \
38CREATE INDEX dd_index2 ON DnsData(data_name); \n \
39";
40
41DnsStorage::DnsStorage()
42{
43 fs::path chronosDir = fs::path(getenv("HOME")) / ".chronos";
44 fs::create_directories (chronosDir);
45
46 int res = sqlite3_open((chronosDir / "dns.db").c_str (), &m_db);
47 if (res != SQLITE_OK)
48 throw LnException("Chronos DNS DB cannot be open/created");
49
50 // Check if SelfProfile table exists
51 sqlite3_stmt *stmt;
52 sqlite3_prepare_v2 (m_db, "SELECT name FROM sqlite_master WHERE type='table' And name='DnsData'", -1, &stmt, 0);
53 res = sqlite3_step (stmt);
54
55 bool ddTableExist = false;
56 if (res == SQLITE_ROW)
57 ddTableExist = true;
58 sqlite3_finalize (stmt);
59
60 if(!ddTableExist)
61 {
62 char *errmsg = 0;
63 res = sqlite3_exec (m_db, INIT_DD_TABLE.c_str (), NULL, NULL, &errmsg);
64 if (res != SQLITE_OK && errmsg != 0)
65 throw LnException("Init \"error\" in DnsData");
66 }
67}
68
69DnsStorage::~DnsStorage()
70{
71 sqlite3_close(m_db);
72}
73
74void
75DnsStorage::updateDnsData(const ndn::Blob& data, const std::string& identity, const std::string& name, const std::string& type, const string& dataName)
76{
77 sqlite3_stmt *stmt;
78 sqlite3_prepare_v2 (m_db, "SELECT data_name FROM DnsData where dns_identity=? and dns_name=? and dns_type=?", -1, &stmt, 0);
79 sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size(), SQLITE_TRANSIENT);
80 sqlite3_bind_text(stmt, 2, name.c_str(), name.size(), SQLITE_TRANSIENT);
81 sqlite3_bind_text(stmt, 3, type.c_str(), type.size(), SQLITE_TRANSIENT);
82
83 if(sqlite3_step (stmt) != SQLITE_ROW)
84 {
85 sqlite3_finalize(stmt);
86 sqlite3_prepare_v2 (m_db, "INSERT INTO DnsData (dns_identity, dns_name, dns_type, dns_value, data_name) VALUES (?, ?, ?, ?, ?)", -1, &stmt, 0);
87 sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size(), SQLITE_TRANSIENT);
88 sqlite3_bind_text(stmt, 2, name.c_str(), name.size(), SQLITE_TRANSIENT);
89 sqlite3_bind_text(stmt, 3, type.c_str(), type.size(), SQLITE_TRANSIENT);
Yingdi Yu76dd8002013-12-24 11:16:32 +080090 sqlite3_bind_text(stmt, 4, (const char*)data.buf(), data.size(), SQLITE_TRANSIENT);
Yingdi Yu783ce4b2013-10-18 17:04:19 -070091 sqlite3_bind_text(stmt, 5, dataName.c_str(), dataName.size(), SQLITE_TRANSIENT);
92 sqlite3_step(stmt);
93 sqlite3_finalize(stmt);
94 }
95 else
96 {
Yingdi Yu783ce4b2013-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);
Yingdi Yu76dd8002013-12-24 11:16:32 +080099 sqlite3_bind_text(stmt, 1, (const char*)data.buf(), data.size(), SQLITE_TRANSIENT);
Yingdi Yu783ce4b2013-10-18 17:04:19 -0700100 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");
Yingdi Yu76dd8002013-12-24 11:16:32 +0800115 Blob dnsValue = data.wireEncode();
Yingdi Yu783ce4b2013-10-18 17:04:19 -0700116
Yingdi Yu76dd8002013-12-24 11:16:32 +0800117 updateDnsData(dnsValue, dnsIdentity, dnsName, dnsType, data.getName().toUri());
Yingdi Yu783ce4b2013-10-18 17:04:19 -0700118}
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 Yub2e747d2013-11-05 23:06:43 -0800125 string dnsType("ENDORSEE");
Yingdi Yu76dd8002013-12-24 11:16:32 +0800126 Blob dnsValue = data.wireEncode();
Yingdi Yu783ce4b2013-10-18 17:04:19 -0700127
Yingdi Yu76dd8002013-12-24 11:16:32 +0800128 updateDnsData(dnsValue, dnsIdentity, dnsName, dnsType, data.getName().toUri());
Yingdi Yu783ce4b2013-10-18 17:04:19 -0700129}
130
131void
Yingdi Yub2e747d2013-11-05 23:06:43 -0800132DnsStorage::updateDnsOthersEndorse(const Data& data, const Name& identity)
Yingdi Yu783ce4b2013-10-18 17:04:19 -0700133{
134 string dnsIdentity = identity.toUri();
Yingdi Yub2e747d2013-11-05 23:06:43 -0800135 string dnsName("N/A");
136 string dnsType("ENDORSED");
Yingdi Yu76dd8002013-12-24 11:16:32 +0800137 Blob dnsValue = data.wireEncode();
Yingdi Yu783ce4b2013-10-18 17:04:19 -0700138
Yingdi Yu76dd8002013-12-24 11:16:32 +0800139 updateDnsData(dnsValue, dnsIdentity, dnsName, dnsType, data.getName().toUri());
Yingdi Yu783ce4b2013-10-18 17:04:19 -0700140}
141
Yingdi Yu76dd8002013-12-24 11:16:32 +0800142shared_ptr<Data>
Yingdi Yu783ce4b2013-10-18 17:04:19 -0700143DnsStorage::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 {
Yingdi Yu76dd8002013-12-24 11:16:32 +0800151 shared_ptr<Data> data = make_shared<Data>();
152 data->wireDecode(reinterpret_cast<const uint8_t*>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0));
Yingdi Yue35bdb82013-11-07 11:32:40 -0800153 sqlite3_finalize(stmt);
Yingdi Yu76dd8002013-12-24 11:16:32 +0800154 return data;
Yingdi Yu783ce4b2013-10-18 17:04:19 -0700155 }
Yingdi Yue35bdb82013-11-07 11:32:40 -0800156 sqlite3_finalize(stmt);
157
Yingdi Yu76dd8002013-12-24 11:16:32 +0800158 return CHRONOCHAT_NULL_DATA_PTR;
Yingdi Yu783ce4b2013-10-18 17:04:19 -0700159}