Skip to content

Commit

Permalink
chore(igd): upgrade igd to v0.12.0
Browse files Browse the repository at this point in the history
  • Loading branch information
bochaco committed Feb 16, 2021
1 parent 2fd0bd1 commit d3b5a33
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ webpki = "~0.21.3"
features = [ "serde" ]

[dependencies.igd]
version = "~0.11.1"
version = "~0.12.0"
features = [ "aio" ]

[dependencies.quinn]
Expand Down
9 changes: 7 additions & 2 deletions src/endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ use tokio::time::timeout;
// FIXME: make it configurable
const CERT_SERVER_NAME: &str = "MaidSAFE.net";

// Number of seconds before timing out the IGD request to forward a port.
const PORT_FORWARD_TIMEOUT: u64 = 30;

/// Channel on which incoming messages can be listened to
pub struct IncomingMessages(pub(crate) UnboundedReceiver<(SocketAddr, Bytes)>);

Expand Down Expand Up @@ -124,6 +127,7 @@ impl Endpoint {
connection_pool: connection_pool.clone(),
connection_deduplicator: ConnectionDeduplicator::new(),
};

if let Some(addr) = endpoint.public_addr {
// External IP and port number is provided
// This means that the user has performed manual port-forwarding
Expand All @@ -141,7 +145,6 @@ impl Endpoint {
WireMsg::EndpointVerficationResp(valid) => {
if valid {
info!("Endpoint verification successful! {} is reachable.", addr);
// Ok(endpoint)
} else {
error!("Endpoint verification failed! {} is not reachable.", addr);
return Err(Error::IncorrectPublicAddress);
Expand All @@ -161,13 +164,15 @@ impl Endpoint {
} else {
endpoint.public_addr = Some(endpoint.fetch_public_address().await?);
}

listen_for_incoming_connections(
quic_incoming,
connection_pool,
message_tx,
connection_tx,
disconnection_tx,
);

Ok((
endpoint,
IncomingConnections(connection_rx),
Expand Down Expand Up @@ -208,7 +213,7 @@ impl Endpoint {
if self.qp2p_config.forward_port {
// Attempt to use IGD for port forwarding
match timeout(
Duration::from_secs(30),
Duration::from_secs(PORT_FORWARD_TIMEOUT),
forward_port(
self.local_addr,
self.qp2p_config
Expand Down
7 changes: 4 additions & 3 deletions src/igd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
// Software.

use crate::error::{Error, Result};
use igd::SearchOptions;
use log::{debug, info, warn};
use std::net::{IpAddr, SocketAddr, SocketAddrV4};
use std::time::Duration;
Expand Down Expand Up @@ -51,7 +52,7 @@ pub async fn forward_port(local_addr: SocketAddr, lease_duration: u32) -> Result
/// `lease_duration` is the life time of a port mapping (in seconds). If it is 0, the
/// mapping will continue to exist as long as possible.
pub(crate) async fn add_port(local_addr: SocketAddr, lease_duration: u32) -> Result<SocketAddrV4> {
let gateway = igd::aio::search_gateway(igd::SearchOptions::default()).await?;
let gateway = igd::aio::search_gateway(SearchOptions::default()).await?;

debug!("IGD gateway found: {:?}", gateway);

Expand Down Expand Up @@ -82,7 +83,7 @@ pub(crate) async fn renew_port(
ext_port: u16,
lease_duration: u32,
) -> Result<()> {
let gateway = igd::aio::search_gateway(Default::default()).await?;
let gateway = igd::aio::search_gateway(SearchOptions::default()).await?;

if let SocketAddr::V4(socket_addr) = local_addr {
gateway
Expand All @@ -107,7 +108,7 @@ pub(crate) async fn renew_port(
// Find our local IP address by connecting to the gateway and querying local socket address.
pub(crate) fn get_local_ip() -> Result<IpAddr> {
debug!("Attempting to realise local IP address with IGD...");
let gateway = igd::search_gateway(igd::SearchOptions::default())?;
let gateway = igd::search_gateway(SearchOptions::default())?;
let gateway_conn = std::net::TcpStream::connect(gateway.addr)?;
let local_sa = gateway_conn.local_addr()?;
info!("Fetched IP address from IGD gateway: {:?}", &local_sa.ip());
Expand Down

0 comments on commit d3b5a33

Please sign in to comment.