local-ip-address
local-ip-address copied to clipboard
Determine the primary outbound local IP if multiple present in interfaces
It may occur that multiple local IP addresses are present, however as of #95 (at least for the BSD implementation), this library will find the first non-loopback local IP. I think we should modify this to determine the primary outbound local IP.
POC:
pub fn local_ip() -> Result<IpAddr, std::io::Error> {
let sock_addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(8, 8, 8, 8)), 80);
let socket = UdpSocket::bind("0.0.0.0:0").expect("Could not bind to address");
match socket.connect(sock_addr) {
Ok(_) => Ok(socket.local_addr().unwrap().ip()),
Err(e) => Err(e),
}
}