Skip to content
This repository has been archived by the owner on Dec 8, 2023. It is now read-only.

Commit

Permalink
Add an example that instantly removes the port it added.
Browse files Browse the repository at this point in the history
  • Loading branch information
sbstp committed Jan 7, 2016
1 parent 5e43ed2 commit 9e7e54f
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions examples/add_remove.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use std::net::SocketAddrV4;
use std::env;

extern crate igd;

fn main() {
match igd::search_gateway() {
Err(ref err) => match *err {
igd::SearchError::IoError(ref ioe) => println!("IoError: {}", ioe),
_ => println!("{:?}", err),
},
Ok(gateway) => {
let args: Vec<_> = env::args().collect();
if args.len() != 4 {
println!("Usage: add_remove <local_ip> <local_port> <remote_port>");
return;
}
let local_ip = args[1].parse().expect("Invalid IP address");
let local_port = args[2].parse().expect("Invalid local port");
let remote_port = args[3].parse().expect("Invalid remote port");

let local_addr = SocketAddrV4::new(local_ip, local_port);

match gateway.add_port(igd::PortMappingProtocol::TCP, remote_port,
local_addr, 60, "crust") {
Err(ref err) => println!("{:?}", err),
Ok(()) => {
println!("AddPortMapping successful.");
match gateway.remove_port(igd::PortMappingProtocol::TCP, remote_port) {
Err(ref err) => println!("Error removing: {:?}", err),
Ok(_) => println!("DeletePortMapping successful."),
}
},
}
},
}
}

0 comments on commit 9e7e54f

Please sign in to comment.