blob: 7ce92459cea2d14561f8e68a2def814721c375f0 [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"
Yingdi Yu64206112013-12-24 11:16:32 +080012#include "null-ptrs.h"
Yingdi Yu01d11352013-10-18 17:04:19 -070013
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;
Yingdi Yu64206112013-12-24 11:16:32 +080020using namespace ndn::ptr_lib;
Yingdi Yu01d11352013-10-18 17:04:19 -070021namespace fs = boost::filesystem;
22
Yingdi Yu590fa5d2013-10-18 18:35:09 -070023INIT_LOGGER("DnsStorage");
24
Yingdi Yu01d11352013-10-18 17:04:19 -070025const string INIT_DD_TABLE = "\
26CREATE TABLE IF NOT EXISTS \n \
27 DnsData( \n \
28 dns_identity BLOB NOT NULL, \n \
29 dns_name BLOB NOT NULL, \n \
30 dns_type BLOB NOT NULL, \n \
31 data_name BLOB NOT NULL, \n \
32 dns_value BLOB NOT NULL, \n \
33 \
34 PRIMARY KEY (dns_identity, dns_name, dns_type) \n \
35 ); \
36CREATE INDEX dd_index ON DnsData(dns_identity, dns_name, dns_type); \n \
37CREATE INDEX dd_index2 ON DnsData(data_name); \n \
38";
39
40DnsStorage::DnsStorage()
41{
42 fs::path chronosDir = fs::path(getenv("HOME")) / ".chronos";
43 fs::create_directories (chronosDir);
44
45 int res = sqlite3_open((chronosDir / "dns.db").c_str (), &m_db);
46 if (res != SQLITE_OK)
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -080047 throw Error("Chronos DNS DB cannot be open/created");
Yingdi Yu01d11352013-10-18 17:04:19 -070048
49 // Check if SelfProfile table exists
50 sqlite3_stmt *stmt;
51 sqlite3_prepare_v2 (m_db, "SELECT name FROM sqlite_master WHERE type='table' And name='DnsData'", -1, &stmt, 0);
52 res = sqlite3_step (stmt);
53
54 bool ddTableExist = false;
55 if (res == SQLITE_ROW)
56 ddTableExist = true;
57 sqlite3_finalize (stmt);
58
59 if(!ddTableExist)
60 {
61 char *errmsg = 0;
62 res = sqlite3_exec (m_db, INIT_DD_TABLE.c_str (), NULL, NULL, &errmsg);
63 if (res != SQLITE_OK && errmsg != 0)
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -080064 throw Error("Init \"error\" in DnsData");
Yingdi Yu01d11352013-10-18 17:04:19 -070065 }
66}
67
68DnsStorage::~DnsStorage()
69{
70 sqlite3_close(m_db);
71}
72
73void
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -080074DnsStorage::updateDnsData(const ndn::Block& data, const std::string& identity, const std::string& name, const std::string& type, const string& dataName)
Yingdi Yu01d11352013-10-18 17:04:19 -070075{
76 sqlite3_stmt *stmt;
77 sqlite3_prepare_v2 (m_db, "SELECT data_name FROM DnsData where dns_identity=? and dns_name=? and dns_type=?", -1, &stmt, 0);
78 sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size(), SQLITE_TRANSIENT);
79 sqlite3_bind_text(stmt, 2, name.c_str(), name.size(), SQLITE_TRANSIENT);
80 sqlite3_bind_text(stmt, 3, type.c_str(), type.size(), SQLITE_TRANSIENT);
81
82 if(sqlite3_step (stmt) != SQLITE_ROW)
83 {
84 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);
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -080089 sqlite3_bind_text(stmt, 4, (const char*)data.wire(), data.size(), SQLITE_TRANSIENT);
Yingdi Yu01d11352013-10-18 17:04:19 -070090 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 Yu01d11352013-10-18 17:04:19 -070096 sqlite3_finalize(stmt);
97 sqlite3_prepare_v2 (m_db, "UPDATE DnsData SET dns_value=?, data_name=? WHERE dns_identity=? and dns_name=?, dns_type=?", -1, &stmt, 0);
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -080098 sqlite3_bind_text(stmt, 1, (const char*)data.wire(), data.size(), SQLITE_TRANSIENT);
Yingdi Yu01d11352013-10-18 17:04:19 -070099 sqlite3_bind_text(stmt, 2, dataName.c_str(), dataName.size(), SQLITE_TRANSIENT);
100 sqlite3_bind_text(stmt, 3, identity.c_str(), identity.size(), SQLITE_TRANSIENT);
101 sqlite3_bind_text(stmt, 4, name.c_str(), name.size(), SQLITE_TRANSIENT);
102 sqlite3_bind_text(stmt, 5, type.c_str(), type.size(), SQLITE_TRANSIENT);
103 sqlite3_step(stmt);
104 sqlite3_finalize(stmt);
105 }
106}
107
108void
109DnsStorage::updateDnsSelfProfileData(const Data& data, const Name& identity)
110{
111 string dnsIdentity = identity.toUri();
112 string dnsName("N/A");
113 string dnsType("PROFILE");
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800114
Yingdi Yu01d11352013-10-18 17:04:19 -0700115
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800116 updateDnsData(data.wireEncode(), dnsIdentity, dnsName, dnsType, data.getName().toUri());
Yingdi Yu01d11352013-10-18 17:04:19 -0700117}
118
119void
120DnsStorage::updateDnsEndorseOthers(const Data& data, const Name& identity, const Name& endorsee)
121{
122 string dnsIdentity = identity.toUri();
123 string dnsName = endorsee.toUri();
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800124 string dnsType("ENDORSEE");
Yingdi Yu01d11352013-10-18 17:04:19 -0700125
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800126 updateDnsData(data.wireEncode(), dnsIdentity, dnsName, dnsType, data.getName().toUri());
Yingdi Yu01d11352013-10-18 17:04:19 -0700127}
128
129void
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800130DnsStorage::updateDnsOthersEndorse(const Data& data, const Name& identity)
Yingdi Yu01d11352013-10-18 17:04:19 -0700131{
132 string dnsIdentity = identity.toUri();
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800133 string dnsName("N/A");
134 string dnsType("ENDORSED");
Yingdi Yu01d11352013-10-18 17:04:19 -0700135
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800136 updateDnsData(data.wireEncode(), dnsIdentity, dnsName, dnsType, data.getName().toUri());
Yingdi Yu01d11352013-10-18 17:04:19 -0700137}
138
Yingdi Yu64206112013-12-24 11:16:32 +0800139shared_ptr<Data>
Yingdi Yu01d11352013-10-18 17:04:19 -0700140DnsStorage::getData(const Name& dataName)
141{
142 sqlite3_stmt *stmt;
143 sqlite3_prepare_v2 (m_db, "SELECT dns_value FROM DnsData where data_name=?", -1, &stmt, 0);
144 sqlite3_bind_text(stmt, 1, dataName.toUri().c_str(), dataName.toUri().size(), SQLITE_TRANSIENT);
145
146 if(sqlite3_step (stmt) == SQLITE_ROW)
147 {
Yingdi Yu64206112013-12-24 11:16:32 +0800148 shared_ptr<Data> data = make_shared<Data>();
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800149 data->wireDecode(Block(reinterpret_cast<const uint8_t*>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0)));
Yingdi Yub35b8652013-11-07 11:32:40 -0800150 sqlite3_finalize(stmt);
Yingdi Yu64206112013-12-24 11:16:32 +0800151 return data;
Yingdi Yu01d11352013-10-18 17:04:19 -0700152 }
Yingdi Yub35b8652013-11-07 11:32:40 -0800153 sqlite3_finalize(stmt);
154
Yingdi Yu64206112013-12-24 11:16:32 +0800155 return CHRONOCHAT_NULL_DATA_PTR;
Yingdi Yu01d11352013-10-18 17:04:19 -0700156}