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 solana::crdt::FULLNODE_PORT_RANGE;
use solana::fullnode::Config;
use solana::logger;
use solana::netutil::{get_ip_addr, get_public_ip_addr, parse_port_or_addr};
use solana::signature::read_pkcs8;
use std::io;
use std::net::SocketAddr;
fn main() {
logger::setup();
let matches = App::new("fullnode-config")
.version(crate_version!())
.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> {
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 {
if !p.ip().is_loopback() && !p.ip().is_multicast() {
match p.ip() {
IpAddr::V4(addr) => {
if !addr.is_link_local() {
return Some(p.ip());
}
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() {
IpAddr::V4(addr) => {
if addr.is_link_local() {
trace!(" link local");
continue;
}
IpAddr::V6(_addr) => {
// Select an ipv6 address if the config is selected
#[cfg(feature = "ipv6")]
{
return Some(p.ip());
}
trace!(" using ==> {}", p.ip());
return Some(p.ip());
}
IpAddr::V6(_addr) => {
// Select an ipv6 address if the config is selected
#[cfg(feature = "ipv6")]
{
return Some(p.ip());
}
}
}