This repository has been archived by the owner on Dec 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an example that instantly removes the port it added.
- Loading branch information
Showing
1 changed file
with
37 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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."), | ||
} | ||
}, | ||
} | ||
}, | ||
} | ||
} |