pub struct Natpmp { /* private fields */ }
Expand description
NAT-PMP main struct.
Examples
use std::thread;
use std::time::Duration;
use natpmp::*;
let mut n = Natpmp::new()?;
n.send_port_mapping_request(Protocol::UDP, 4020, 4020, 30)?;
thread::sleep(Duration::from_millis(100));
let response = n.read_response_or_retry()?;
match response {
Response::UDP(ur) => {
assert_eq!(ur.private_port(), 4020);
assert_eq!(ur.public_port(), 4020);
}
_ => panic!("Not a udp mapping response"),
}
Implementations
sourceimpl Natpmp
impl Natpmp
sourcepub fn new() -> Result<Natpmp>
pub fn new() -> Result<Natpmp>
Create a NAT-PMP object with default gateway.
Errors
See get_default_gateway
and Natpmp::new_with
.
Examples
use natpmp::*;
let n = Natpmp::new();
assert_eq!(n.is_ok(), true);
sourcepub fn new_with(gateway: Ipv4Addr) -> Result<Natpmp>
pub fn new_with(gateway: Ipv4Addr) -> Result<Natpmp>
sourcepub fn gateway(&self) -> &Ipv4Addr
pub fn gateway(&self) -> &Ipv4Addr
NAT-PMP gateway address.
Examples
use std::net::Ipv4Addr;
use natpmp::*;
let gateway = Ipv4Addr::from([192, 168, 0, 1]);
let n = Natpmp::new_with(gateway)?;
assert_eq!(n.gateway(), &gateway);
sourcepub fn get_natpmp_request_timeout(&self) -> Result<Duration>
pub fn get_natpmp_request_timeout(&self) -> Result<Duration>
Get timeout duration of the currently pending NAT-PMP request.
Errors:
Examples
use std::time::Duration;
use natpmp::*;
let mut n = Natpmp::new()?;
n.send_public_address_request()?;
// do something
let duration = n.get_natpmp_request_timeout()?;
if duration <= Duration::from_millis(10) {
// read response ...
}
sourcepub fn send_public_address_request(&mut self) -> Result<()>
pub fn send_public_address_request(&mut self) -> Result<()>
sourcepub fn send_port_mapping_request(
&mut self,
protocol: Protocol,
private_port: u16,
public_port: u16,
lifetime: u32
) -> Result<()>
pub fn send_port_mapping_request(
&mut self,
protocol: Protocol,
private_port: u16,
public_port: u16,
lifetime: u32
) -> Result<()>
sourcepub fn read_response_or_retry(&mut self) -> Result<Response>
pub fn read_response_or_retry(&mut self) -> Result<Response>
Read NAT-PMP response if possible
Errors
Error::NATPMP_TRYAGAIN
Error::NATPMP_ERR_NOPENDINGREQ
Error::NATPMP_ERR_NOGATEWAYSUPPORT
Error::NATPMP_ERR_RECVFROM
Error::NATPMP_ERR_WRONGPACKETSOURCE
Error::NATPMP_ERR_UNSUPPORTEDVERSION
Error::NATPMP_ERR_UNSUPPORTEDOPCODE
Error::NATPMP_ERR_UNSUPPORTEDVERSION
Error::NATPMP_ERR_NOTAUTHORIZED
Error::NATPMP_ERR_NETWORKFAILURE
Error::NATPMP_ERR_OUTOFRESOURCES
Error::NATPMP_ERR_UNSUPPORTEDOPCODE
Error::NATPMP_ERR_UNDEFINEDERROR
Examples
use std::thread;
use std::time::Duration;
use natpmp::*;
let mut n = Natpmp::new()?;
n.send_public_address_request()?;
thread::sleep(Duration::from_millis(250));
let response = n.read_response_or_retry()?;
Trait Implementations
Auto Trait Implementations
impl RefUnwindSafe for Natpmp
impl Send for Natpmp
impl Sync for Natpmp
impl Unpin for Natpmp
impl UnwindSafe for Natpmp
Blanket Implementations
sourceimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more