Skip to main content

Crate oxy_upnp_igd

Crate oxy_upnp_igd 

Source
Expand description

§oxy-upnp-igd

Minimal UPnP Internet Gateway Device (IGD) port mapping library.

§Features

  • UPnP gateway discovery via SSDP (discovers ALL available gateways)
  • Port mapping (Add/Delete) for TCP and UDP
  • Automatic port mapping renewal
  • External IP address retrieval
  • Smol runtime compatible
  • Minimal dependencies

§Supported Protocols

  • TCP
  • UDP

§Quick Start

§Discover Gateways and Get External IP

use oxy_upnp_igd::discover_gateways;
use std::time::Duration;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    smol::block_on(async {
        // Discover all UPnP gateways on the network
        let gateways = discover_gateways(Duration::from_secs(3)).await?;

        // Use the first available gateway
        if let Some(gateway) = gateways.first() {
            let external_ip = gateway.get_external_ip().await?;
            println!("External IP: {}", external_ip);
        }

        Ok::<(), Box<dyn std::error::Error>>(())
    })
}

§Add Port Mapping with Automatic Renewal

use oxy_upnp_igd::{discover_gateways, Protocol};
use smol::Executor;
use std::sync::Arc;
use std::time::Duration;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    smol::block_on(async {
        let ex = Arc::new(Executor::new());
        let gateways = discover_gateways(Duration::from_secs(3)).await?;
        let gateway = &gateways[0];

        // Add port mapping with auto-renewal
        let handle = gateway.add_port_mapping_with_renewal(
            ex.clone(),
            8080,  // internal port
            8080,  // external port
            "192.168.1.100".parse()?,
            Protocol::TCP,
            "My App",
            3600,  // lease duration (seconds)
            Duration::from_secs(3),
        ).await?;

        println!("Port mapping active and will auto-renew");
        // Mapping stays active until `handle` is dropped

        Ok::<(), Box<dyn std::error::Error>>(())
    })
}

Re-exports§

pub use client::add_port_mapping_lazy;
pub use client::discover_gateways;
pub use client::get_local_ipv4_for_gateway;
pub use client::is_port_conflict_error;
pub use client::Protocol;
pub use client::DEFAULT_DISCOVERY_TIMEOUT;
pub use client::DEFAULT_LEASE_DURATION;
pub use error::Error;
pub use error::Result;
pub use gateway::Gateway;
pub use gateway::PortMappingEntry;
pub use renewal::RenewalConfig;
pub use renewal::RenewalHandle;

Modules§

client
High-level UPnP client API
error
Error types for UPnP IGD operations
gateway
UPnP Gateway implementation
http
Minimal HTTP client for UPnP SOAP requests Uses async-net from smol ecosystem to avoid dependency conflicts
renewal
Port mapping renewal handle
ssdp
SSDP (Simple Service Discovery Protocol) implementation Finds UPnP gateways on the local network