Prefer routable IPs when an interface has multiple addresses
Author: junekiwiCreated Aug 9, 2023Updated Jan 19, 2025
We have servers where the main interface has multiple IP addresses. For example:
# ip addr show venet0
2: venet0: <BROADCAST,POINTOPOINT,NOARP,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN group default
link/void
inet 127.0.0.1/32 scope host venet0
valid_lft forever preferred_lft forever
inet 69.48.142.148/24 brd 69.48.142.255 scope global venet0:0
valid_lft forever preferred_lft foreverZmap uses SIOCGIFADDR to get the IP address of an interface. However this returns only a single IP address. In this case 127.0.0.1.
This is an edge case, and this can be fixed manually with -S, but it might be nice for Zmap to enumerate all the IP addresses of the interface, and choose a routable one by default.
I have implemented this logic in a project of mine:
/// Return the preferred IPv4 address for the device.
pub fn get_ipv4_address(interface: &str) -> Option<Ipv4Addr> {
// ...
// Prefer Internet-routable addresses over loopback over private unicast.
addresses.sort_by_key(|addr| (addr.is_global(), addr.is_loopback(), addr.is_private()));
// ...
}However I know that this can be quite complicated to do in C.
Source: zmap/zmap