Gitea
2 years ago
7 changed files with 271 additions and 107 deletions
@ -0,0 +1,53 @@ |
|||||||
|
/* |
||||||
|
ddtu - digital data transferring utility |
||||||
|
Copyright (C) 2022 Kasyanov Nikolay Alexeyevich (Unbewohnte) |
||||||
|
|
||||||
|
This program is free software: you can redistribute it and/or modify |
||||||
|
it under the terms of the GNU Affero General Public License as published by |
||||||
|
the Free Software Foundation, either version 3 of the License, or |
||||||
|
(at your option) any later version. |
||||||
|
|
||||||
|
This program is distributed in the hope that it will be useful, |
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||||
|
GNU Affero General Public License for more details. |
||||||
|
*/ |
||||||
|
|
||||||
|
use std::net; |
||||||
|
use crate::util::error::Error; |
||||||
|
|
||||||
|
pub fn get_addr() -> Result<net::IpAddr, Error> { |
||||||
|
let socket: net::UdpSocket; |
||||||
|
match net::UdpSocket::bind("0.0.0.0:0") { |
||||||
|
Ok(s) => { |
||||||
|
socket = s; |
||||||
|
} |
||||||
|
|
||||||
|
Err(error) => { |
||||||
|
return Err( |
||||||
|
Error::new(format!("error binding UDP socket: {}", error).as_str()) |
||||||
|
); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
match socket.connect("8.8.8.8:80") { |
||||||
|
Ok(()) => {} |
||||||
|
Err(error) => { |
||||||
|
return Err( |
||||||
|
Error::new(format!("could not connect to 8.8.8.8: {}", error).as_str()) |
||||||
|
); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
match socket.local_addr() { |
||||||
|
Ok(addr) => { |
||||||
|
return Ok(addr.ip()); |
||||||
|
} |
||||||
|
|
||||||
|
Err(error) => { |
||||||
|
return Err( |
||||||
|
Error::new(format!("could not get local IP address from socket: {}", error).as_str())
|
||||||
|
); |
||||||
|
} |
||||||
|
}
|
||||||
|
} |
Loading…
Reference in new issue