-
Notifications
You must be signed in to change notification settings - Fork 358
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
356 additions
and
385 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
#![allow(dead_code)] // some code here is not used on some targets. | ||
|
||
use std::net::{IpAddr, SocketAddr}; | ||
|
||
use anyhow::Context; | ||
use socket2::Socket; | ||
|
||
use crate::{traceroute::Ip, Interface}; | ||
|
||
pub(crate) fn get_interface_ip(interface: &Interface, ip_version: Ip) -> anyhow::Result<IpAddr> { | ||
let Interface::Name(interface) = interface; | ||
|
||
for interface_address in nix::ifaddrs::getifaddrs()? { | ||
if &interface_address.interface_name != interface { | ||
continue; | ||
}; | ||
let Some(address) = interface_address.address else { | ||
continue; | ||
}; | ||
|
||
match ip_version { | ||
Ip::V4(()) => { | ||
if let Some(address) = address.as_sockaddr_in() { | ||
return Ok(IpAddr::V4(address.ip())); | ||
}; | ||
} | ||
Ip::V6(()) => { | ||
if let Some(address) = address.as_sockaddr_in6() { | ||
return Ok(IpAddr::V6(address.ip())); | ||
}; | ||
} | ||
} | ||
} | ||
|
||
anyhow::bail!("Interface {interface:?} has no valid IP to bind to"); | ||
} | ||
|
||
pub(crate) fn bind_socket_to_interface( | ||
socket: &Socket, | ||
interface: &Interface, | ||
ip_version: Ip, | ||
) -> anyhow::Result<()> { | ||
let interface_ip = get_interface_ip(interface, ip_version)?; | ||
|
||
log::info!("Binding socket to {interface_ip} ({interface:?})"); | ||
|
||
socket | ||
.bind(&SocketAddr::new(interface_ip, 0).into()) | ||
.context("Failed to bind socket to interface address")?; | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.