Skip to content

Commit

Permalink
Merge branch 'main' into fix-shutdown-node
Browse files Browse the repository at this point in the history
  • Loading branch information
dignifiedquire authored Apr 2, 2024
2 parents 2d2faca + 6417816 commit 3239034
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 29 deletions.
20 changes: 7 additions & 13 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion iroh-net/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ http = "1"
http-body-util = "0.1.0"
hyper = { version = "1", features = ["server", "client", "http1"] }
hyper-util = "0.1.1"
igd = { version = "0.12.1", features = ["aio"] }
igd-next = { version = "0.14.3", features = ["aio_tokio"] }
iroh-base = { version = "0.13.0", path = "../iroh-base", features = ["key"] }
libc = "0.2.139"
num_enum = "0.7"
Expand Down
2 changes: 1 addition & 1 deletion iroh-net/src/portmapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ impl Client {
struct Probe {
/// When was the probe last updated.
last_probe: Instant,
/// The last [`igd::aio::Gateway`] and when was it last seen.
/// The last [`upnp::Gateway`] and when was it last seen.
last_upnp_gateway_addr: Option<(upnp::Gateway, Instant)>,
/// Last time PCP was seen.
last_pcp: Option<Instant>,
Expand Down
2 changes: 1 addition & 1 deletion iroh-net/src/portmapper/mapping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl Mapping {
pub(crate) async fn new_upnp(
local_ip: Ipv4Addr,
local_port: NonZeroU16,
gateway: Option<igd::aio::Gateway>,
gateway: Option<upnp::Gateway>,
external_port: Option<NonZeroU16>,
) -> Result<Self> {
upnp::Mapping::new(local_ip, local_port, gateway, external_port)
Expand Down
28 changes: 15 additions & 13 deletions iroh-net/src/portmapper/upnp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ use std::{
time::Duration,
};

use anyhow::Result;
use igd::aio as aigd;
use anyhow::{anyhow, Result};
use igd_next::aio as aigd;

use iroh_metrics::inc;
use tracing::debug;

use super::Metrics;

pub use aigd::Gateway;
pub type Gateway = aigd::Gateway<aigd::tokio::Tokio>;

/// Seconds we ask the router to maintain the port mapping. 0 means infinite.
const PORT_MAPPING_LEASE_DURATION_SECONDS: u32 = 0;
Expand All @@ -31,7 +31,7 @@ const PORT_MAPPING_DESCRIPTION: &str = "iroh-portmap";
pub struct Mapping {
/// The internet Gateway device (router) used to create this mapping.
#[debug("{}", gateway)]
gateway: aigd::Gateway,
gateway: Gateway,
/// The external address obtained by this mapping.
external_ip: Ipv4Addr,
/// External port obtained by this mapping.
Expand All @@ -42,7 +42,7 @@ impl Mapping {
pub(crate) async fn new(
local_addr: Ipv4Addr,
port: NonZeroU16,
gateway: Option<aigd::Gateway>,
gateway: Option<Gateway>,
preferred_port: Option<NonZeroU16>,
) -> Result<Self> {
let local_addr = SocketAddrV4::new(local_addr, port.into());
Expand All @@ -51,23 +51,25 @@ impl Mapping {
let gateway = if let Some(known_gateway) = gateway {
known_gateway
} else {
aigd::search_gateway(igd::SearchOptions {
aigd::tokio::search_gateway(igd_next::SearchOptions {
timeout: Some(SEARCH_TIMEOUT),
..Default::default()
})
.await?
};

let external_ip = gateway.get_external_ip().await?;
let std::net::IpAddr::V4(external_ip) = gateway.get_external_ip().await? else {
return Err(anyhow!("igd device's external ip is ipv6"));
};

// if we are trying to get a specific external port, try this first. If this fails, default
// to try to get any port
if let Some(external_port) = preferred_port {
if gateway
.add_port(
igd::PortMappingProtocol::UDP,
igd_next::PortMappingProtocol::UDP,
external_port.into(),
local_addr,
local_addr.into(),
PORT_MAPPING_LEASE_DURATION_SECONDS,
PORT_MAPPING_DESCRIPTION,
)
Expand All @@ -84,8 +86,8 @@ impl Mapping {

let external_port = gateway
.add_any_port(
igd::PortMappingProtocol::UDP,
local_addr,
igd_next::PortMappingProtocol::UDP,
local_addr.into(),
PORT_MAPPING_LEASE_DURATION_SECONDS,
PORT_MAPPING_DESCRIPTION,
)
Expand All @@ -112,7 +114,7 @@ impl Mapping {
..
} = self;
gateway
.remove_port(igd::PortMappingProtocol::UDP, external_port.into())
.remove_port(igd_next::PortMappingProtocol::UDP, external_port.into())
.await?;
Ok(())
}
Expand All @@ -126,7 +128,7 @@ impl Mapping {
/// Searches for UPnP gateways.
pub async fn probe_available() -> Option<Gateway> {
inc!(Metrics, upnp_probes);
match aigd::search_gateway(igd::SearchOptions {
match aigd::tokio::search_gateway(igd_next::SearchOptions {
timeout: Some(SEARCH_TIMEOUT),
..Default::default()
})
Expand Down

0 comments on commit 3239034

Please sign in to comment.