https://github.com/fengyc/natpmp
NAT-PMP library in rust
https://github.com/fengyc/natpmp
nat rust rust-library
Last synced: 12 months ago
JSON representation
NAT-PMP library in rust
- Host: GitHub
- URL: https://github.com/fengyc/natpmp
- Owner: fengyc
- License: mit
- Created: 2018-11-25T11:57:58.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2024-11-06T14:44:14.000Z (over 1 year ago)
- Last Synced: 2025-06-21T07:40:42.621Z (about 1 year ago)
- Topics: nat, rust, rust-library
- Language: Rust
- Size: 85 KB
- Stars: 29
- Watchers: 2
- Forks: 7
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
natpmp
======
[](https://github.com/fengyc/natpmp/actions/workflows/main.yml)
[](https://github.com/fengyc/natpmp/actions/workflows/nightly.yml)
NAT-PMP client library in rust, a rust implementation of the c library libnatpmp([https://github.com/miniupnp/libnatpmp](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::*
let n = Natpmp::new()?
Or a specified gataway:
use std::str::FromStr;
use natpmp::*;
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`).
cargo add natpmp --features tokio
Or
cargo add natpmp --features async-std
License
-------
MIT