diff options
| author | murilo ijanc | 2026-03-24 15:04:03 -0300 |
|---|---|---|
| committer | murilo ijanc | 2026-03-24 15:04:03 -0300 |
| commit | 9821aabf0b50d2487b07502d3d2cd89e7d62bdbe (patch) | |
| tree | 53da095ff90cc755bac3d4bf699172b5e8cd07d6 /examples/join.rs | |
| download | tesseras-dht-9821aabf0b50d2487b07502d3d2cd89e7d62bdbe.tar.gz | |
Initial commitv0.1.0
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
Diffstat (limited to 'examples/join.rs')
| -rw-r--r-- | examples/join.rs | 65 |
1 files changed, 65 insertions, 0 deletions
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<String> = 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)); + } +} |