1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
use std::net::{IpAddr, UdpSocket};

/// Get IP address of the default Network Interface
pub fn get_local_ipaddr() -> Option<IpAddr> {
    let socket = match UdpSocket::bind("0.0.0.0:0") {
        Ok(s) => s,
        Err(_) => return None,
    };

    match socket.connect("1.1.1.1:80") {
        Ok(()) => (),
        Err(_) => return None,
    };

    match socket.local_addr() {
        Ok(addr) => return Some(addr.ip()),
        Err(_) => return None,
    };
}