13 releases

0.4.0 Nov 26, 2022
0.3.3 Nov 26, 2022
0.3.2 Sep 3, 2021
0.3.1 May 21, 2021
0.1.15 Nov 29, 2018

#799 in Network programming

Download history 648/week @ 2024-02-26 279/week @ 2024-03-04 160/week @ 2024-03-11 83/week @ 2024-03-18 92/week @ 2024-03-25 133/week @ 2024-04-01 89/week @ 2024-04-08 100/week @ 2024-04-15 111/week @ 2024-04-22 69/week @ 2024-04-29 62/week @ 2024-05-06 74/week @ 2024-05-13 70/week @ 2024-05-20 72/week @ 2024-05-27 58/week @ 2024-06-03 84/week @ 2024-06-10

290 downloads per month
Used in 9 crates (4 directly)

MIT license

57KB
1K SLoC

Rust 640 SLoC // 0.0% comments C 421 SLoC // 0.2% comments

natpmp

Main Nightly

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::*

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).

[dependencies]
natpmp = { version = "0.3", features = ["tokio"] }

Or

[dependencies]
natpmp = { version = "0.3", features = ["async-std"] }

License

MIT

Dependencies

~3–17MB
~180K SLoC