diff --git a/rust/examples/dhtnode.rs b/rust/examples/dhtnode.rs index d81a3a5cf1726f11be4856b88f8d83ce41ff6a04..a6332ba27e944b2c95b288cf6297eeb1e8310f65 100644 --- a/rust/examples/dhtnode.rs +++ b/rust/examples/dhtnode.rs @@ -20,7 +20,7 @@ extern crate opendht; use std::{ thread, time }; use opendht::{ InfoHash, DhtRunner, DhtRunnerConfig, Value }; -use opendht::crypto::*; +// use opendht::crypto::*; fn main() { println!("{}", InfoHash::random()); @@ -31,7 +31,7 @@ fn main() { let mut dht = DhtRunner::new(); - let mut config = DhtRunnerConfig::new(); + let /*mut*/ config = DhtRunnerConfig::new(); //// If you want to inject a certificate, uncomment the following lines and previous mut. //// Note: you can generate a certificate with //// openssl req -x509 -newkey rsa:4096 -sha256 -days 3650 -nodes -keyout example.key -out example.crt -subj /CN=example.com @@ -46,6 +46,7 @@ fn main() { let mut get_cb = |v: Box<Value>| { //data += 1; println!("GET: VALUE CB - data: {} - v: {}", data, v); + true }; let mut done_cb = |ok: bool| { println!("GET: DONE CB - data: {} - ok: {}", data, ok); @@ -62,6 +63,7 @@ fn main() { println!("Start listening /foo"); let mut value_cb = |v, expired| { println!("LISTEN: DONE CB - data: {} - v: {} - expired: {}", data, v, expired); + true }; let token = dht.listen(&InfoHash::get("foo"), &mut value_cb); let one_min = time::Duration::from_secs(10); diff --git a/rust/src/crypto.rs b/rust/src/crypto.rs index 207ddd0bd60c77470e942e071f9977118617e576..01ec8fdcb522bb502951630524e9a6a7ec34b996 100644 --- a/rust/src/crypto.rs +++ b/rust/src/crypto.rs @@ -23,6 +23,7 @@ use std::ffi::CString; use std::io; use std::io::prelude::*; use std::fs::File; +use std::ptr; impl PublicKey { pub fn new() -> Box<PublicKey> { @@ -154,6 +155,12 @@ impl Drop for DhtCertificate { } impl DhtIdentity { + pub fn new(common_name: &str) -> DhtIdentity { + unsafe { + DhtIdentity::generate(common_name, Box::from_raw(ptr::null_mut())) + } + } + pub fn generate(common_name: &str, ca: Box<DhtIdentity>) -> DhtIdentity { let common_name = CString::new(common_name).unwrap(); unsafe { diff --git a/rust/src/dhtrunner.rs b/rust/src/dhtrunner.rs index 09736a07d33305c6c44c13d2cad3a853b72efae2..86ad5b244a7be97c7b8fbd90c7332c1d79e1b087 100644 --- a/rust/src/dhtrunner.rs +++ b/rust/src/dhtrunner.rs @@ -89,13 +89,13 @@ impl DhtNodeConfig struct GetHandler<'a> { - get_cb: &'a mut(dyn FnMut(Box<Value>)), + get_cb: &'a mut(dyn FnMut(Box<Value>) -> bool), done_cb: &'a mut(dyn FnMut(bool)) } impl<'a> GetHandler<'a> { - fn get_cb(&mut self, v: Box<Value>) { + fn get_cb(&mut self, v: Box<Value>) -> bool{ (self.get_cb)(v) } @@ -104,9 +104,9 @@ impl<'a> GetHandler<'a> } } -extern fn get_handler_cb(v: *mut Value, ptr: *mut c_void) { +extern fn get_handler_cb(v: *mut Value, ptr: *mut c_void) -> bool { if ptr.is_null() { - return; + return true; } unsafe { let handler = ptr as *mut GetHandler; @@ -142,17 +142,17 @@ extern fn put_handler_done(ok: bool, ptr: *mut c_void) { struct ListenHandler<'a> { - cb: &'a mut(dyn FnMut(Box<Value>, bool)) + cb: &'a mut(dyn FnMut(Box<Value>, bool) -> bool) } impl<'a> ListenHandler<'a> { - fn cb(&mut self, v: Box<Value>, expired: bool) { + fn cb(&mut self, v: Box<Value>, expired: bool) -> bool { (self.cb)(v, expired) } } -extern fn listen_handler(v: *mut Value, expired: bool, ptr: *mut c_void) { +extern fn listen_handler(v: *mut Value, expired: bool, ptr: *mut c_void) -> bool { unsafe { let handler = ptr as *mut ListenHandler; (*handler).cb((*v).boxed(), expired) @@ -205,7 +205,7 @@ impl DhtRunner { } pub fn get<'a>(&mut self, h: &InfoHash, - get_cb: &'a mut(dyn FnMut(Box<Value>)), + get_cb: &'a mut(dyn FnMut(Box<Value>) -> bool), done_cb: &'a mut(dyn FnMut(bool))) { let handler = Box::new(GetHandler { get_cb, @@ -246,7 +246,7 @@ impl DhtRunner { } pub fn listen<'a>(&mut self, h: &InfoHash, - cb: &'a mut(dyn FnMut(Box<Value>, bool))) -> Box<OpToken> { + cb: &'a mut(dyn FnMut(Box<Value>, bool) -> bool)) -> Box<OpToken> { let handler = Box::new(ListenHandler { cb, }); diff --git a/rust/src/ffi.rs b/rust/src/ffi.rs index 3c7e8e6d25f06f5089a3c48ec845c5980388081f..710196fbf04879b3c59d5dcac019040a9fccb304 100644 --- a/rust/src/ffi.rs +++ b/rust/src/ffi.rs @@ -185,7 +185,7 @@ extern { pub fn dht_runner_run_config(dht: *mut DhtRunner, port: in_port_t, config: *const DhtRunnerConfig); pub fn dht_runner_bootstrap(dht: *mut DhtRunner, host: *const c_char, service: *const c_char); pub fn dht_runner_get(dht: *mut DhtRunner, h: *const InfoHash, - get_cb: extern fn(*mut Value, *mut c_void), + get_cb: extern fn(*mut Value, *mut c_void) -> bool, done_cb: extern fn(bool, *mut c_void), cb_user_data: *mut c_void); pub fn dht_runner_put(dht: *mut DhtRunner, h: *const InfoHash, v: *const Value, @@ -196,7 +196,7 @@ extern { cb_user_data: *mut c_void); pub fn dht_runner_cancel_put(dht: *mut DhtRunner, h: *const InfoHash, vid: u64); pub fn dht_runner_listen(dht: *mut DhtRunner, h: *const InfoHash, - cb: extern fn(*mut Value, bool, *mut c_void), + cb: extern fn(*mut Value, bool, *mut c_void) -> bool, done_cb: extern fn(*mut c_void), cb_user_data: *mut c_void) -> *mut OpToken; pub fn dht_runner_cancel_listen(dht: *mut DhtRunner, h: *const InfoHash,