Skip to content

Commit

Permalink
fix(config): do not use the IGD gateway to realize local IP address
Browse files Browse the repository at this point in the history
  • Loading branch information
lionel-faber committed Feb 19, 2021
1 parent 2e782a2 commit 62dbcc4
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 39 deletions.
37 changes: 9 additions & 28 deletions src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use super::{
peer_config::{self, DEFAULT_IDLE_TIMEOUT_MSEC, DEFAULT_KEEP_ALIVE_INTERVAL_MSEC},
};
use futures::{future, TryFutureExt};
use log::{debug, error, info, trace, warn};
use log::{debug, error, info, trace};
use std::net::{IpAddr, Ipv4Addr, SocketAddr, UdpSocket};
use std::path::PathBuf;

Expand Down Expand Up @@ -77,20 +77,7 @@ impl QuicP2p {
.map(|p| (p, false))
.unwrap_or((DEFAULT_PORT_TO_TRY, true));

let mut ip = cfg.local_ip.unwrap_or(IpAddr::V4(Ipv4Addr::UNSPECIFIED));

// check hard coded contacts for being local (aka loopback)
if let Some(contact) = cfg.hard_coded_contacts.iter().next() {
let potential_ip = contact.ip();

if potential_ip.is_loopback() {
trace!(
"IP from hardcoded contact is loopback, setting our IP to: {:?}",
potential_ip
);
ip = potential_ip;
}
}
let ip = cfg.local_ip.unwrap_or(IpAddr::V4(Ipv4Addr::UNSPECIFIED));

let idle_timeout_msec = cfg.idle_timeout_msec.unwrap_or(DEFAULT_IDLE_TIMEOUT_MSEC);

Expand Down Expand Up @@ -316,21 +303,15 @@ fn bind(
fn unwrap_config_or_default(cfg: Option<Config>) -> Result<Config> {
let mut cfg = cfg.map_or(Config::read_or_construct_default(None)?, |cfg| cfg);
if cfg.local_ip.is_none() {
cfg.local_ip = match crate::igd::get_local_ip() {
Ok(addr) => Some(addr),
Err(err) => {
warn!("Error realizing local IP using IGD gateway: {}", err);
let socket = UdpSocket::bind("0.0.0.0:0")?;
let mut local_ip = None;
for addr in cfg.hard_coded_contacts.iter() {
if let Ok(Ok(local_addr)) = socket.connect(addr).map(|()| socket.local_addr()) {
local_ip = Some(local_addr.ip());
break;
}
}
local_ip
let socket = UdpSocket::bind("0.0.0.0:0")?;
let mut local_ip = None;
for addr in cfg.hard_coded_contacts.iter() {
if let Ok(Ok(local_addr)) = socket.connect(addr).map(|()| socket.local_addr()) {
local_ip = Some(local_addr.ip());
break;
}
}
cfg.local_ip = local_ip;
};
if cfg.clean {
Config::clear_config_from_disk(None)?;
Expand Down
12 changes: 1 addition & 11 deletions src/igd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use crate::error::{Error, Result};
use igd::SearchOptions;
use log::{debug, info, warn};
use std::net::{IpAddr, SocketAddr, SocketAddrV4};
use std::net::{SocketAddr, SocketAddrV4};
use std::time::Duration;
use tokio::time::{self, Instant};

Expand Down Expand Up @@ -104,13 +104,3 @@ pub(crate) async fn renew_port(
Err(Error::IgdNotSupported)
}
}

// 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(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());
Ok(local_sa.ip())
}

0 comments on commit 62dbcc4

Please sign in to comment.