sort local interfaces before selecting one

This commit is contained in:
Rob Walker 2018-09-19 14:34:21 -07:00
parent 270fd6d61c
commit 12a474b6ee
2 changed files with 36 additions and 13 deletions

View File

@ -7,12 +7,14 @@ extern crate solana;
use clap::{App, Arg}; use clap::{App, Arg};
use solana::crdt::FULLNODE_PORT_RANGE; use solana::crdt::FULLNODE_PORT_RANGE;
use solana::fullnode::Config; use solana::fullnode::Config;
use solana::logger;
use solana::netutil::{get_ip_addr, get_public_ip_addr, parse_port_or_addr}; use solana::netutil::{get_ip_addr, get_public_ip_addr, parse_port_or_addr};
use solana::signature::read_pkcs8; use solana::signature::read_pkcs8;
use std::io; use std::io;
use std::net::SocketAddr; use std::net::SocketAddr;
fn main() { fn main() {
logger::setup();
let matches = App::new("fullnode-config") let matches = App::new("fullnode-config")
.version(crate_version!()) .version(crate_version!())
.arg( .arg(

View File

@ -49,21 +49,42 @@ pub fn parse_port_or_addr(optstr: Option<&str>, default_port: u16) -> SocketAddr
} }
pub fn get_ip_addr() -> Option<IpAddr> { pub fn get_ip_addr() -> Option<IpAddr> {
for iface in datalink::interfaces() { let mut ifaces = datalink::interfaces();
// put eth0 and wifi0, etc. up front of our list of candidates
ifaces.sort_by(|a, b| {
a.name
.chars()
.last()
.unwrap()
.cmp(&b.name.chars().last().unwrap())
});
for iface in ifaces {
for p in iface.ips { for p in iface.ips {
if !p.ip().is_loopback() && !p.ip().is_multicast() { trace!("get_ip_addr considering iface {} {}", iface.name, p);
match p.ip() { if p.ip().is_loopback() {
IpAddr::V4(addr) => { trace!(" loopback");
if !addr.is_link_local() { continue;
return Some(p.ip()); }
} if p.ip().is_multicast() {
trace!(" multicast");
continue;
}
match p.ip() {
IpAddr::V4(addr) => {
if addr.is_link_local() {
trace!(" link local");
continue;
} }
IpAddr::V6(_addr) => { trace!(" using ==> {}", p.ip());
// Select an ipv6 address if the config is selected return Some(p.ip());
#[cfg(feature = "ipv6")] }
{ IpAddr::V6(_addr) => {
return Some(p.ip()); // Select an ipv6 address if the config is selected
} #[cfg(feature = "ipv6")]
{
return Some(p.ip());
} }
} }
} }