local-ip-address icon indicating copy to clipboard operation
local-ip-address copied to clipboard

Determine the primary outbound local IP if multiple present in interfaces

Open jakewilliami opened this issue 2 years ago • 1 comments

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.

jakewilliami avatar Nov 28 '22 06:11 jakewilliami

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),
    }
}

jakewilliami avatar Nov 28 '22 06:11 jakewilliami