natpmp-ng

Crates.ionatpmp-ng
lib.rsnatpmp-ng
version0.5.0
sourcesrc
created_at2024-08-03 07:32:25.891064
updated_at2024-08-03 07:32:25.891064
descriptionNAT-PMP client library
homepagehttps://gitlab.com/sgued/natpmp-ng
repositoryhttps://gitlab.com/sgued/natpmp-ng
max_upload_size
id1324163
size99,174
Soso (sgued)

documentation

https://docs.rs/natpmp-ng/

README

natpmp-ng

This is a maintained for of natpmp, used in Ten Forward.

NAT-PMP client library in rust, a rust implementation of the c library libnatpmp(https://github.com/miniupnp/libnatpmp).

Note: src/getgateway.h and src/getgateway.c are from https://github.com/miniupnp/libnatpmp .

Versions

Version 0.2.x supports rust 2018 edition.

Version 0.3.x+ supports tokio and async-std.

Example

Create a natpmp object with system default gateway:

use natpmp_ng::*

let n = Natpmp::new()?

Or a specified gataway:

use std::str::FromStr;
use natpmp_ng::*;

let n = Natpmp::new("192.168.0.1").parse.unwrap())?

To determine the external address, send a public address request:

n.send_public_address_request()?;

To add a port mapping, send a port mapping request:

n.send_port_mapping_request(Protocol::UDP, 4020, 4020, 30)?;

And then read response after a few milliseconds:

use std::thread;
use std::time::Duration;

thread::sleep(Duration::from_millis(250));
let response = n.read_response_or_retry()?;

Check response type and and result:

match response {
    Response::Gateway(gr) => {}
    Response::UDP(ur) => {}
    Response::TCP(tr) => {}
}

Async

Enable feature tokio or async-std in Cargo.toml (default feature tokio).

[dependencies]
natpmp_ng = { version = "0.5", features = ["tokio"] }

Or

[dependencies]
natpmp_ng = { version = "0.5", features = ["async-std"] }

License

MIT

Commit count: 0

cargo fmt