6 releases

new 0.3.1 Feb 8, 2026
0.3.0 Aug 23, 2025
0.2.1 Jan 11, 2025
0.2.0 Apr 7, 2024
0.1.0 Aug 5, 2023

#744 in Network programming

Download history 41/week @ 2025-10-20 5/week @ 2025-10-27 7/week @ 2025-11-10 2/week @ 2025-11-17 2/week @ 2025-12-01 10/week @ 2025-12-22 1/week @ 2025-12-29 31/week @ 2026-01-12 245/week @ 2026-01-19 418/week @ 2026-01-26 238/week @ 2026-02-02

932 downloads per month
Used in 2 crates

WTFPL license

18KB
152 lines

easy-upnp

badge github badge crates.io badge docs.rs badge license

Easily open and close UPnP ports.

A minimalistic wrapper around IGD to open and close network ports via UPnP. Mainly this library is used in the CLI application upnp-daemon, but it can also be used as a library in other crates that just want to open and close ports with minimal possible configuration.

Example

Here is a hands-on example to demonstrate the usage. It will add some ports and immediately remove them again.

use std::error::Error;
use std::str::FromStr;
use log::error;
use easy_upnp::{add_ports, delete_ports, Ipv4Cidr, PortMappingProtocol, UpnpConfig};

fn get_configs() -> Result<[UpnpConfig; 3], Box<dyn Error>> {
    let config_no_address = UpnpConfig {
        address: None,
        port: 80,
        protocol: PortMappingProtocol::TCP,
        duration: 3600,
        comment: "Webserver".to_string(),
    };

    let config_specific_address = UpnpConfig {
        address: Some(Ipv4Cidr::from_str("192.168.0.10")?),
        port: 8080,
        protocol: PortMappingProtocol::TCP,
        duration: 3600,
        comment: "Webserver alternative".to_string(),
    };

    let config_address_range = UpnpConfig {
        address: Some(Ipv4Cidr::from_str("192.168.0.0/24")?),
        port: 8081,
        protocol: PortMappingProtocol::TCP,
        duration: 3600,
        comment: "Webserver second alternative".to_string(),
    };

    Ok([
        config_no_address,
        config_specific_address,
        config_address_range,
    ])
}

fn main() -> Result<(), Box<dyn Error>> {
    for result in add_ports(get_configs()?) {
        if let Err(err) = result {
            error!("{}", err);
        }
    }

    for result in delete_ports(get_configs()?) {
        if let Err(err) = result {
            error!("{}", err);
        }
    }

    Ok(())
}

Dependencies

~4–7.5MB
~131K SLoC