Skip to content

Commit

Permalink
Updated list of dht nodes, reworked how dht nodes are stored, started…
Browse files Browse the repository at this point in the history
… working on storing dht nodes in the sqlite db
  • Loading branch information
naxuroqa committed May 21, 2014
1 parent a4dcf4f commit ededc28
Show file tree
Hide file tree
Showing 4 changed files with 205 additions and 20 deletions.
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ SET( VENOM_SRC
core/Contact.vala
core/ContactStorage.vala
core/DhtNode.vala
core/DhtNodeStorage.vala
core/FileTransfer.vala
core/GroupChat.vala
core/GroupChatContact.vala
Expand Down
12 changes: 8 additions & 4 deletions src/core/DhtNode.vala
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,21 @@ using Tox;
namespace Venom {
public class DhtNode : GLib.Object {
public string host {get; set;}
public uint16 port {get; set;}
public uint8[] pub_key {get; set;}
public bool is_ipv6 {get; set;}
public uint16 port {get; set;}
public string maintainer {get; set;}
public string location {get; set;}
public bool is_blocked {get; set;}

public bool is_ipv6 {get; private set;}

public DhtNode.ipv4(string host, string pub_key, uint16 port = 33445) {
public DhtNode.ipv4(string host, string pub_key, uint16 port = 33445, bool is_blocked = false, string maintainer = "", string location = "") {
this.host = host;
this.port = port;
this.pub_key = Tools.hexstring_to_bin(pub_key);
this.is_ipv6 = false;
}
public DhtNode.ipv6(string host, string pub_key, uint16 port = 33445) {
public DhtNode.ipv6(string host, string pub_key, uint16 port = 33445, bool is_blocked = false, string maintainer = "", string location = "") {
this.host = host;
this.port = port;
this.pub_key = Tools.hexstring_to_bin(pub_key);
Expand Down
186 changes: 186 additions & 0 deletions src/core/DhtNodeStorage.vala
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
/*
* DhtNodeStorage.vala
*
* Copyright (C) 2013-2014 Venom authors and contributors
*
* This file is part of Venom.
*
* Venom is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Venom is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Venom. If not, see <http://www.gnu.org/licenses/>.
*/

namespace Venom {

public interface IDhtNodeStorage : GLib.Object {
public abstract DhtNode[] get_dht_nodes();
public abstract void save_dht_node(DhtNode node);
}

public class DummyDhtNodeStorage : IDhtNodeStorage, GLib.Object {
public DhtNode[] get_dht_nodes() {
DhtNode[] nodes = {};
nodes += new DhtNode.ipv4(
"192.254.75.98",
"951C88B7E75C867418ACDB5D273821372BB5BD652740BCDF623A4FA293E75D2F",
33445,
false,
"stqism",
"US"
);
nodes += new DhtNode.ipv6(
"2607:5600:284::2",
"951C88B7E75C867418ACDB5D273821372BB5BD652740BCDF623A4FA293E75D2F",
33445,
false,
"stqism",
"US"
);
nodes += new DhtNode.ipv4(
"37.187.46.132",
"A9D98212B3F972BD11DA52BEB0658C326FCCC1BFD49F347F9C2D3D8B61E1B927",
33445,
false,
"mouseym",
"FR"
);
nodes += new DhtNode.ipv6(
"2001:41d0:0052:0300::0507",
"A9D98212B3F972BD11DA52BEB0658C326FCCC1BFD49F347F9C2D3D8B61E1B927",
33445,
false,
"mouseym",
"FR"
);
nodes += new DhtNode.ipv4(
"54.199.139.199",
"7F9C31FE850E97CEFD4C4591DF93FC757C7C12549DDD55F8EEAECC34FE76C029",
33445,
false,
"aitjcize",
"JP"
);
nodes += new DhtNode.ipv4(
"109.169.46.133",
"7F31BFC93B8E4016A902144D0B110C3EA97CB7D43F1C4D21BCAE998A7C838821",
33445,
false,
"astonex",
"UK"
);
return nodes;
}
public void save_dht_node(DhtNode node) {}
}

public class SqliteDhtNodeStorage : IDhtNodeStorage, GLib.Object {
private unowned Sqlite.Database db;
private Sqlite.Statement insert_statement;
private Sqlite.Statement select_statement;
private static string QUERY_TABLE_NODES = """
CREATE TABLE IF NOT EXISTS Nodes (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
key TEXT NOT NULL,
host TEXT NOT NULL,
isipv6 INTEGER,
port INTEGER,
maintainer TEXT NOT NULL,
location TEXT NOT NULL,
isblocked INTEGER DEFAULT 0
);
""";
private enum NodeColumn {
ID,
KEY,
HOST,
ISIPV6,
PORT,
MAINTAINER,
LOCATION,
ISBLOCKED
}
private static string TABLE_KEY = "$KEY";
private static string TABLE_HOST = "$HOST";
private static string TABLE_ISIPV6 = "$ISIPV6";
private static string TABLE_PORT = "$PORT";
private static string TABLE_MAINTAINER = "$MAINTAINER";
private static string TABLE_LOCATION = "$LOCATION";
private static string TABLE_ISBLOCKED = "$ISBLOCKED";

private static string STATEMENT_INSERT_NODES = "INSERT INTO Nodes (key, host, isipv6, port, maintainer, location, isblocked) VALUES (%s, %s, %s, %s, %s, %s, %s);".printf(TABLE_KEY, TABLE_HOST, TABLE_ISIPV6, TABLE_PORT, TABLE_MAINTAINER, TABLE_LOCATION, TABLE_ISBLOCKED);
private static string STATEMENT_SELECT_NODES = "SELECT * FROM Nodes";

public DhtNode[] get_dht_nodes() {
DhtNode[] nodes = {};

while(select_statement.step () == Sqlite.ROW) {
string key = select_statement.column_text(NodeColumn.KEY);
string host = select_statement.column_text(NodeColumn.HOST);
bool is_ipv6 = select_statement.column_int(NodeColumn.ISIPV6) != 0;
uint16 port = (uint16) select_statement.column_int(NodeColumn.PORT);
string maintainer = select_statement.column_text(NodeColumn.MAINTAINER);
string location = select_statement.column_text(NodeColumn.LOCATION);
bool is_blocked = select_statement.column_int(NodeColumn.ISBLOCKED) != 0;

if(is_ipv6) {
nodes += new DhtNode.ipv6(host, key, port, is_blocked, maintainer, location);
} else {
nodes += new DhtNode.ipv4(host, key, port, is_blocked, maintainer, location);
}
}

select_statement.reset ();
return nodes;
}

public void save_dht_node(DhtNode node) {
string key = Tools.bin_to_hexstring(node.pub_key);

try {
SqliteTools.put_text(insert_statement, TABLE_KEY, key);
SqliteTools.put_text(insert_statement, TABLE_HOST, node.host);
SqliteTools.put_int (insert_statement, TABLE_ISIPV6, node.is_ipv6 ? 1 : 0);
SqliteTools.put_int (insert_statement, TABLE_PORT, node.port);
SqliteTools.put_text(insert_statement, TABLE_MAINTAINER, node.maintainer);
SqliteTools.put_text(insert_statement, TABLE_LOCATION, node.location);
SqliteTools.put_int (insert_statement, TABLE_ISBLOCKED, node.is_blocked ? 1 : 0);
} catch (SqliteStatementError e) {
stderr.printf("Error writing dht node to sqlite database: %s\n", e.message);
return;
}

insert_statement.step();
insert_statement.reset();
}

public SqliteDhtNodeStorage( Sqlite.Database db ) throws SqliteDbError {
this.db = db;

string errmsg;

//create table and index if needed
if(db.exec (QUERY_TABLE_NODES, null, out errmsg) != Sqlite.OK) {
throw new SqliteDbError.QUERY("Error creating dht nodes table: %s\n", errmsg);
}

//prepare insert statement for adding new history messages
if(db.prepare_v2 (STATEMENT_INSERT_NODES, STATEMENT_INSERT_NODES.length, out insert_statement) != Sqlite.OK) {
throw new SqliteDbError.QUERY("Error creating dht nodes insert statement: %d: %s\n", db.errcode (), db.errmsg());
}

//prepare select statement to get history. Will execute on indexed data
if(db.prepare_v2 (STATEMENT_SELECT_NODES, STATEMENT_SELECT_NODES.length, out select_statement) != Sqlite.OK) {
throw new SqliteDbError.QUERY("Error creating dht nodes select statement: %d: %s\n", db.errcode (), db.errmsg());
}
}
}
}
26 changes: 10 additions & 16 deletions src/core/ToxSession.vala
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ namespace Venom {
private Tox.Tox handle;
private IMessageLog message_log;
private IContactStorage contact_storage;
private IDhtNodeStorage dht_node_storage;
private Sqlite.Database db;

private DhtNode[] dht_nodes = {};
Expand Down Expand Up @@ -104,9 +105,13 @@ namespace Venom {
message_log = new DummyMessageLog();
}
contact_storage = new SqliteContactStorage(db);
dht_node_storage = new DummyDhtNodeStorage();
//unfinished, so replaced with dummy storage
//dht_node_storage = new SqliteDhtNodeStorage(db);
} catch (Error e) {
stderr.printf("Error opening database: %s\n", e.message);
message_log = new DummyMessageLog();
dht_node_storage = new DummyDhtNodeStorage();
}

init_dht_nodes();
Expand All @@ -119,22 +124,11 @@ namespace Venom {
}

private void init_dht_nodes() {
dht_nodes += new DhtNode.ipv4(
"192.254.75.98",
"951C88B7E75C867418ACDB5D273821372BB5BD652740BCDF623A4FA293E75D2F"
);
dht_nodes += new DhtNode.ipv6(
"2607:5600:284::2",
"951C88B7E75C867418ACDB5D273821372BB5BD652740BCDF623A4FA293E75D2F"
);
dht_nodes += new DhtNode.ipv4(
"66.175.223.88",
"B24E2FB924AE66D023FE1E42A2EE3B432010206F751A2FFD3E297383ACF1572E"
);
dht_nodes += new DhtNode.ipv4(
"192.210.149.121",
"F404ABAA1C99A9D37D61AB54898F56793E1DEF8BD46B1038B9D822E8460FAB67"
);
dht_nodes = dht_node_storage.get_dht_nodes();
if(dht_nodes.length == 0) {
DummyDhtNodeStorage dummy_storage = new DummyDhtNodeStorage();
dht_nodes = dummy_storage.get_dht_nodes();
}
}

private void init_callbacks() {
Expand Down

0 comments on commit ededc28

Please sign in to comment.