From fee7e5dd30a6e77fa8021c9e3d9626374acc751b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Blin?=
 <sebastien.blin@savoirfairelinux.com>
Date: Fri, 25 Oct 2019 12:05:36 -0400
Subject: [PATCH] rust: fix callback result and add DhtIdentity::new()

---
 rust/examples/dhtnode.rs |  6 ++++--
 rust/src/crypto.rs       |  7 +++++++
 rust/src/dhtrunner.rs    | 18 +++++++++---------
 rust/src/ffi.rs          |  4 ++--
 4 files changed, 22 insertions(+), 13 deletions(-)

diff --git a/rust/examples/dhtnode.rs b/rust/examples/dhtnode.rs
index d81a3a5c..a6332ba2 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 207ddd0b..01ec8fdc 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 09736a07..86ad5b24 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 3c7e8e6d..710196fb 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,
-- 
GitLab