Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "netbird-embed"
version = "0.1.1"
version = "0.1.2"
edition = "2021"
description = "Rust bindings for NetBird's client/embed via Go C-shared FFI"
license = "BSD-3-Clause"
Expand Down
4 changes: 3 additions & 1 deletion build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ fn main() {

// Rerun if Go sources change
println!("cargo:rerun-if-changed=go-lib/netbird_embed.go");
println!("cargo:rerun-if-changed=go-lib/socketpair.go");
println!("cargo:rerun-if-changed=go-lib/socketpair_unix.go");
println!("cargo:rerun-if-changed=go-lib/socketpair_windows.go");
println!("cargo:rerun-if-changed=go-lib/go.mod");
println!("cargo:rerun-if-changed=go-lib/go.sum");
}
Expand Down Expand Up @@ -115,6 +116,7 @@ unsafe extern "C" {
management_url: *const c_char,
device_name: *const c_char,
token: *const c_char,
wireguard_port: c_int,
) -> c_int;

pub fn nb_create_errmsg(buf: *mut c_char, buf_len: c_int);
Expand Down
7 changes: 6 additions & 1 deletion go-lib/netbird_embed.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func writeJSON(data []byte, buf *C.char, bufLen C.int) C.int {
// On failure, use nb_create_errmsg to retrieve the error.
//
//export nb_new
func nb_new(setup_key *C.char, management_url *C.char, device_name *C.char, token *C.char) C.int {
func nb_new(setup_key *C.char, management_url *C.char, device_name *C.char, token *C.char, wireguard_port C.int) C.int {
opts := embed.Options{}

if setup_key != nil {
Expand All @@ -86,6 +86,11 @@ func nb_new(setup_key *C.char, management_url *C.char, device_name *C.char, toke
}
}

if wireguard_port >= 0 {
port := int(wireguard_port)
opts.WireguardPort = &port
}

client, err := embed.New(opts)
if err != nil {
handleMu.Lock()
Expand Down
6 changes: 6 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ pub struct ClientOptions {
pub device_name: Option<String>,
/// JWT token for OIDC-based registration.
pub token: Option<String>,
/// WireGuard listen port. Use `Some(0)` for a random port (avoids conflicts
/// with other WireGuard instances). `None` uses the NetBird default (51820).
pub wireguard_port: Option<i32>,
}

/// A NetBird embedded client.
Expand All @@ -48,12 +51,15 @@ impl Client {
let device_name = make_cstring(opts.device_name.as_deref())?;
let token = make_cstring(opts.token.as_deref())?;

let wg_port = opts.wireguard_port.unwrap_or(-1);

let handle = unsafe {
ffi::nb_new(
cstr_ptr(&setup_key),
cstr_ptr(&management_url),
cstr_ptr(&device_name),
cstr_ptr(&token),
wg_port as c_int,
)
};

Expand Down
Loading