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,14 +49,36 @@ 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);
if p.ip().is_loopback() {
trace!(" loopback");
continue;
}
if p.ip().is_multicast() {
trace!(" multicast");
continue;
}
match p.ip() { match p.ip() {
IpAddr::V4(addr) => { IpAddr::V4(addr) => {
if !addr.is_link_local() { if addr.is_link_local() {
return Some(p.ip()); trace!(" link local");
continue;
} }
trace!(" using ==> {}", p.ip());
return Some(p.ip());
} }
IpAddr::V6(_addr) => { IpAddr::V6(_addr) => {
// Select an ipv6 address if the config is selected // Select an ipv6 address if the config is selected
@ -68,7 +90,6 @@ pub fn get_ip_addr() -> Option<IpAddr> {
} }
} }
} }
}
None None
} }