Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
opendht
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Deploy
Model registry
Analyze
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
savoirfairelinux
opendht
Commits
09818d77
Commit
09818d77
authored
Oct 16, 2019
by
Sébastien Blin
Committed by
Adrien Béraud
Oct 22, 2019
Browse files
Options
Downloads
Patches
Plain Diff
rust: start dhtnode example
parent
934e36ba
No related branches found
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
rust/README.md
+6
-0
6 additions, 0 deletions
rust/README.md
rust/examples/dhtnode.rs
+13
-0
13 additions, 0 deletions
rust/examples/dhtnode.rs
rust/src/lib.rs
+87
-9
87 additions, 9 deletions
rust/src/lib.rs
with
106 additions
and
9 deletions
rust/README.md
+
6
−
0
View file @
09818d77
...
@@ -13,3 +13,9 @@ cargo build
...
@@ -13,3 +13,9 @@ cargo build
```
```
cargo test
cargo test
```
```
## Run dht node
```
cargo run --example dhtnode
```
\ No newline at end of file
This diff is collapsed.
Click to expand it.
rust/examples/dhtnode.rs
0 → 100644
+
13
−
0
View file @
09818d77
extern
crate
opendht
;
use
opendht
::
*
;
fn
main
()
{
println!
(
"{}"
,
InfoHash
::
random
());
println!
(
"{}"
,
InfoHash
::
new
());
//
//let mut dht = DhtRunner::new();
//dht.run(1412);
//let bootstrap_address = CString::new("bootstrap.jami.net").unwrap();
//dht.ping(&bootstrap_address, bootstrap_address.len());
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
rust/src/lib.rs
+
87
−
9
View file @
09818d77
extern
crate
libc
;
extern
crate
libc
;
use
std
::
fmt
;
use
std
::
ffi
::
CStr
;
use
std
::
ffi
::
CString
;
use
std
::
ffi
::
CString
;
use
libc
::
c_char
;
use
libc
::{
c_char
,
c_void
,
in_port_t
,
sockaddr
,
socklen_t
};
const
HASH_LEN
:
usize
=
20
;
#[repr(C)]
pub
struct
InfoHash
{
d
:
[
u8
;
HASH_LEN
],
}
#[repr(C)]
pub
struct
DhtRunner
;
#[repr(C)]
#[repr(C)]
struct
InfoHash
{}
pub
struct
Value
;
// TODO remove Box?
// TODO callbacks
// TODO dhtnode-rust
#[link(name
=
"opendht-c"
)]
#[link(name
=
"opendht-c"
)]
extern
{
extern
{
fn
dht_infohash_print
(
h
:
*
const
InfoHash
)
->
*
mut
c_char
;
fn
dht_infohash_print
(
h
:
*
const
InfoHash
)
->
*
mut
c_char
;
fn
dht_infohash_random
(
h
:
*
mut
InfoHash
)
->
();
fn
dht_infohash_random
(
h
:
*
mut
InfoHash
);
fn
dht_runner_new
()
->
*
mut
DhtRunner
;
fn
dht_runner_delete
(
dht
:
*
mut
DhtRunner
);
fn
dht_runner_run
(
dht
:
*
mut
DhtRunner
,
port
:
in_port_t
);
fn
dht_runner_ping
(
dht
:
*
mut
DhtRunner
,
addr
:
*
mut
sockaddr
,
addr_len
:
socklen_t
);
fn
dht_runner_get
(
dht
:
*
mut
DhtRunner
,
h
:
*
const
InfoHash
,
get_cb
:
extern
fn
(
*
mut
Value
,
i32
,
*
mut
c_void
),
done_cb
:
extern
fn
(
bool
,
*
mut
c_void
),
cb_user_data
:
*
mut
c_void
);
}
}
impl
InfoHash
{
impl
InfoHash
{
pub
fn
new
()
->
Box
<
InfoHash
>
{
pub
fn
new
()
->
InfoHash
{
let
mut
h
=
Box
::
new
(
InfoHash
{});
InfoHash
{
d
:
[
0
;
20
]
}
}
pub
fn
random
()
->
InfoHash
{
let
mut
h
=
InfoHash
::
new
();
unsafe
{
unsafe
{
dht_infohash_random
(
&
mut
*
h
);
dht_infohash_random
(
&
mut
h
);
}
}
h
h
}
}
pub
fn
print
(
&
self
)
->
CString
{
}
impl
fmt
::
Display
for
InfoHash
{
fn
fmt
(
&
self
,
f
:
&
mut
fmt
::
Formatter
<
'_
>
)
->
fmt
::
Result
{
unsafe
{
unsafe
{
CString
::
from_raw
(
dht_infohash_print
(
&*
self
))
let
self_str
=
CStr
::
from_ptr
(
dht_infohash_print
(
self
)
)
.to_str
()
.unwrap_or
(
""
);
write!
(
f
,
"{}"
,
self_str
)
}
}
}
}
}
}
impl
DhtRunner
{
pub
fn
new
()
->
Box
<
DhtRunner
>
{
unsafe
{
Box
::
from_raw
(
dht_runner_new
())
}
}
pub
fn
run
(
&
mut
self
,
port
:
u16
)
{
unsafe
{
dht_runner_run
(
&
mut
*
self
,
port
)
}
}
pub
fn
ping
(
&
mut
self
,
addr
:
&
CString
,
addr_len
:
u32
)
{
unsafe
{
let
mut
s
=
sockaddr
{
sa_family
:
0
/* TODO AF_UNSPEC */
,
sa_data
:
[
0
;
14
]
/* TODO */
,
};
dht_runner_ping
(
&
mut
*
self
,
&
mut
s
,
addr_len
)
}
}
}
impl
Drop
for
DhtRunner
{
fn
drop
(
&
mut
self
)
{
unsafe
{
dht_runner_delete
(
&
mut
*
self
)
}
}
}
#[cfg(test)]
#[cfg(test)]
mod
tests
{
mod
tests
{
...
@@ -33,6 +107,10 @@ mod tests {
...
@@ -33,6 +107,10 @@ mod tests {
#[test]
#[test]
fn
print_random_infohash
()
{
fn
print_random_infohash
()
{
println!
(
"{:?}"
,
InfoHash
::
print
(
&*
InfoHash
::
new
()));
unsafe
{
let
h
=
InfoHash
{};
println!
(
"{:?}"
,
dht_infohash_print
(
&
h
));
}
//println!("{:?}", InfoHash::print(&*InfoHash::new()));
}
}
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
sign in
to comment