Browse Source

Update Rust crate

pull/8/head
Joseph Henry 5 years ago
parent
commit
d5454040f0
No known key found for this signature in database
GPG Key ID: C45B33FF5EBC9344
  1. 2
      README.md
  2. 14
      examples/rust/README.md
  3. 10
      pkg/crate/libzt/Cargo.lock
  4. 7
      pkg/crate/libzt/Cargo.toml
  5. 22
      pkg/crate/libzt/README.md
  6. 21
      pkg/crate/libzt/build.rs
  7. 36
      pkg/crate/libzt/src/examples/libzt-test-app.rs
  8. 3136
      pkg/crate/libzt/src/include/ZeroTierSockets.h
  9. 2
      pkg/crate/libzt/wrapper.h

2
README.md

@ -28,7 +28,7 @@ Peer-to-peer and cross-platform encrypted connections built right into your app
| C/C++ | [Build from source](#build-from-source) | <img alt="version" src="https://img.shields.io/github/v/tag/zerotier/libzt?label="/></a>| [C/C++](./examples/c) |
| C# | `Install-Package ZeroTier.Sockets` |<a href="https://www.nuget.org/packages/ZeroTier.Sockets/"><img src="https://img.shields.io/github/v/tag/zerotier/libzt?label=NuGet"/></a> |[C#](./examples/csharp) |
| Python | `pip install libzt`|<a href="https://pypi.org/project/libzt/"><img src="https://img.shields.io/pypi/v/libzt?label=PyPI"/></a> |[Python](./examples/python) |
| Rust | Coming *very* soon | <img alt="version" src="https://img.shields.io/github/v/tag/zerotier/libzt?label="/>|[Rust](./examples/rust) |
| Rust | See: [crates.io/crates/libzt](https://crates.io/crates/libzt) | <img alt="version" src="https://img.shields.io/crates/v/libzt?color=blue"/>|[Rust](./examples/rust) |
| Java | `./build.sh host-jar` |<img src="https://img.shields.io/github/v/tag/zerotier/libzt?label="/> |[Java](./examples/java) |
| Linux | `brew install zerotier/tap/libzt` | <img alt="version" src="https://img.shields.io/github/v/tag/zerotier/libzt?label="/></a>| [C/C++](./examples/c) |
| macOS | `brew install zerotier/tap/libzt`| <img alt="version" src="https://img.shields.io/github/v/tag/zerotier/libzt?label=Homebrew"/></a>| [C/C++](./examples/c) |

14
examples/rust/README.md

@ -1,11 +1,5 @@
# Rust example
# Rust
```
cargo install libzt
```
Examples: [pkg/crate/libzt/src/examples](pkg/crate/libzt/src/examples)
## Links
- Getting Started: [docs.zerotier.com/sockets](https://docs.zerotier.com/sockets/tutorial.html)
- Crate: [crates.io/crates/libzt](https://crates.io/crates/libzt)
- Example: [pkg/crate/libzt/src/examples](./../../pkg/crate/libzt/src/examples)
- Docs: [docs.zerotier.com/sockets](https://docs.zerotier.com/sockets/tutorial.html)

10
pkg/crate/libzt/Cargo.lock generated

@ -31,9 +31,9 @@ dependencies = [
[[package]]
name = "bindgen"
version = "0.57.0"
version = "0.58.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fd4865004a46a0aafb2a0a5eb19d3c9fc46ee5f063a6cfc605c69ac9ecf5263d"
checksum = "0f8523b410d7187a43085e7e064416ea32ded16bd0a4e6fc025e21616d01258f"
dependencies = [
"bitflags",
"cexpr",
@ -178,7 +178,7 @@ dependencies = [
[[package]]
name = "libzt"
version = "0.1.0"
version = "0.1.2"
dependencies = [
"bindgen",
"cmake",
@ -259,9 +259,9 @@ checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2"
[[package]]
name = "shlex"
version = "0.1.1"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7fdf1b9db47230893d76faad238fd6097fd6d6a9245cd7a4d90dbd639536bbd2"
checksum = "42a568c8f2cd051a4d283bd6eb0343ac214c1b0f1ac19f93e1175b2dee38c73d"
[[package]]
name = "strsim"

7
pkg/crate/libzt/Cargo.toml

@ -1,10 +1,11 @@
[package]
name = "libzt"
version = "0.1.0"
version = "0.1.2"
authors = ["Joseph Henry <joseph.henry@zerotier.com>"]
edition = "2018"
description = "Encrypted P2P SD-WAN library by ZeroTier"
documentation = "https://github.com/zerotier/libzt"
documentation = "https://docs.zerotier.com"
repository = "https://github.com/zerotier/libzt"
readme = "README.md"
homepage = "https://www.zerotier.com"
license-file = "LICENSE.txt"
@ -14,7 +15,7 @@ categories = ["network-programming", "cryptography"]
[dependencies]
[build-dependencies]
bindgen = "0.57"
bindgen = "0.58"
libc = "0.2"
cmake = "0.1"

22
pkg/crate/libzt/README.md

@ -1,8 +1,18 @@
# libzt - Sockets over ZeroTier
`libzt` replicates the functionality of [std::net](https://doc.rust-lang.org/std/net/index.html) but uses [ZeroTier](https://www.zerotier.com) as its transport layer.
`libzt` replicates the functionality of [std::net](https://doc.rust-lang.org/std/net/index.html) but uses [ZeroTier](https://www.zerotier.com) as its P2P transport layer.
Securely connect application instances, physical devices, and virtual devices as if everything is on a single LAN. ZeroTier brings your network into user-space. No root, and no host configuration requirements.
Securely connect application instances, physical devices, and virtual devices as if everything is on a single LAN.
## Dependencies
The `libzt` crate is a binding around a C/C++ native library. You must have this library installed on your system in order for this crate to work. Currently the best way to do this is to install from our Homebrew tap:
```
brew install zerotier/tap/libzt
```
*Note: Windows is untested but support is planned.*
## Usage
@ -10,11 +20,9 @@ Add the following to your `Cargo.toml`:
```toml
[dependencies]
libzt = "0.1.0"
libzt = "0.1.2"
```
## Resources
## Docs
- Docs: [docs.zerotier.com](https://docs.zerotier.com/sockets/tutorial.html)
- Repo: [github.com/zerotier/libzt](https://github.com/zerotier/libzt)
- Website: [zerotier.com](https://www.zerotier.com/)
- See: [docs.zerotier.com/sockets](https://docs.zerotier.com/sockets/tutorial.html)

21
pkg/crate/libzt/build.rs

@ -1,30 +1,13 @@
extern crate bindgen;
use cmake::Config;
use std::env;
use std::path::PathBuf;
fn main() {
Config::new("src/native").build_target("zt-static").define("ZTS_ENABLE_RUST", "1").out_dir("target").build();
println!("cargo:rustc-link-search=target/build/lib");
println!("cargo:rustc-link-lib=static=zt");
// See here for reasoning: https://flames-of-code.netlify.app/blog/rust-and-cmake-cplusplus/
let target = env::var("TARGET").unwrap();
if target.contains("apple") {
println!("cargo:rustc-link-lib=dylib=c++");
} else if target.contains("linux") {
println!("cargo:rustc-link-lib=dylib=stdc++");
} else {
unimplemented!();
}
//println!("cargo:rustc-env=LLVM_CONFIG_PATH=/usr/local/opt/llvm/bin/llvm-config");
println!("cargo:rustc-link-lib=zt");
let bindings = bindgen::Builder::default()
.header("src/native/include/ZeroTierSockets.h")
.header("src/include/ZeroTierSockets.h")
.parse_callbacks(Box::new(bindgen::CargoCallbacks))
.generate()
.expect("Unable to generate bindings");

36
pkg/crate/libzt/src/examples/libzt-test-app.rs

@ -38,8 +38,9 @@ fn main() -> std::io::Result<()> {
if args.len() != 5 && args.len() != 6 {
println!("Incorrect number of arguments.");
println!(" Usage: server <storage_path> <net_id> <local_ip> <local_port>");
println!(" Usage: client <storage_path> <net_id> <remote_ip> <remote_port>");
println!(" Usage: libzt-test-app server <storage_path> <net_id> <local_ip> <local_port>");
println!(" Usage: libzt-test-app client <storage_path> <net_id> <remote_ip> <remote_port>");
return Ok(())
}
let storage_path = &args[2];
@ -50,25 +51,25 @@ fn main() -> std::io::Result<()> {
// SET UP ZEROTIER
let nn = libzt::node::ZeroTierNode {};
let node = libzt::node::ZeroTierNode {};
// (Optional) initialization
nn.init_set_port(0);
nn.init_set_event_handler(user_event_handler);
nn.init_from_storage(&storage_path);
node.init_set_port(0);
node.init_set_event_handler(user_event_handler);
node.init_from_storage(&storage_path);
// Start the node
nn.start();
node.start();
println!("Waiting for node to come online...");
while !nn.is_online() {
nn.delay(50);
while !node.is_online() {
node.delay(50);
}
println!("Node ID = {:#06x}", nn.id());
println!("Node ID = {:#06x}", node.id());
println!("Joining network");
nn.net_join(net_id);
node.net_join(net_id);
println!("Waiting for network to assign addresses...");
while !nn.net_transport_is_ready(net_id) {
nn.delay(50);
while !node.net_transport_is_ready(net_id) {
node.delay(50);
}
let addr = nn.addr_get(net_id).unwrap();
let addr = node.addr_get(net_id).unwrap();
println!("Assigned addr = {}", addr);
// Server
@ -114,12 +115,11 @@ fn main() -> std::io::Result<()> {
Ok(mut stream) => {
println!("Successfully connected to server");
let msg = b"Hello!";
let msg = b"Hello, network!";
stream.write(msg).unwrap();
println!("Sent Hello, awaiting reply...");
let mut data = [0 as u8; 6];
let mut data = [0 as u8; 15];
match stream.read_exact(&mut data) {
Ok(_) => {
if &data == msg {
@ -141,6 +141,6 @@ fn main() -> std::io::Result<()> {
println!("Terminated.");
}
nn.stop();
node.stop();
Ok(())
}

3136
pkg/crate/libzt/src/include/ZeroTierSockets.h

File diff suppressed because it is too large Load Diff

2
pkg/crate/libzt/wrapper.h

@ -1 +1 @@
#include "../../../include/ZeroTierSockets.h"
#include "include/ZeroTierSockets.h"

Loading…
Cancel
Save