From 9821aabf0b50d2487b07502d3d2cd89e7d62bdbe Mon Sep 17 00:00:00 2001 From: murilo ijanc Date: Tue, 24 Mar 2026 15:04:03 -0300 Subject: Initial commit NAT-aware Kademlia DHT library for peer-to-peer networks. Features: - Distributed key-value storage (iterative FIND_NODE, FIND_VALUE, STORE) - NAT traversal via DTUN hole-punching and proxy relay - Reliable Datagram Protocol (RDP) with 7-state connection machine - Datagram transport with automatic fragmentation/reassembly - Ed25519 packet authentication - 256-bit node IDs (Ed25519 public keys) - Rate limiting, ban list, and eclipse attack mitigation - Persistence and metrics - OpenBSD and Linux support --- examples/join.rs | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 examples/join.rs (limited to 'examples/join.rs') diff --git a/examples/join.rs b/examples/join.rs new file mode 100644 index 0000000..a478410 --- /dev/null +++ b/examples/join.rs @@ -0,0 +1,65 @@ +//! Basic bootstrap example (equivalent to example1.cpp). +//! +//! Usage: +//! cargo run --example join -- 10000 +//! cargo run --example join -- 10001 127.0.0.1 10000 +//! +//! The first invocation creates a standalone node. +//! The second joins via the first. + +use std::time::Duration; +use tesseras_dht::Node; +use tesseras_dht::nat::NatState; + +fn main() { + env_logger::Builder::from_env( + env_logger::Env::default().default_filter_or("info"), + ) + .format(|buf, record| { + use std::io::Write; + writeln!( + buf, + "{} [{}] {}", + record.level(), + record.target(), + record.args() + ) + }) + .init(); + + let args: Vec = std::env::args().collect(); + if args.len() < 2 { + eprintln!("usage: {} port [host port]", args[0]); + eprintln!(); + eprintln!("example:"); + eprintln!(" $ {} 10000 &", args[0]); + eprintln!(" $ {} 10001 127.0.0.1 10000", args[0]); + std::process::exit(1); + } + + let port: u16 = args[1].parse().expect("invalid port"); + + let mut node = Node::bind(port).expect("bind failed"); + node.set_nat_state(NatState::Global); + + println!("Node {} listening on port {port}", node.id_hex()); + + if args.len() >= 4 { + let dst_host = &args[2]; + let dst_port: u16 = args[3].parse().expect("invalid dst port"); + + match node.join(dst_host, dst_port) { + Ok(()) => println!("Join request sent"), + Err(e) => { + eprintln!("Join failed: {e}"); + std::process::exit(1); + } + } + } + + // Event loop + loop { + node.poll().ok(); + std::thread::sleep(Duration::from_millis(100)); + } +} -- cgit v1.2.3