net-lib: add configured_ifaces()

configured_ifaces is a function that returns the names of each interface
that the user wanted configured.

Currently, this is accomplished by reading the list from
/tmp/net.ifaces. But if we want to allow the user to specify an
interface by its MAC address or IP or something, we need a function that
will read the cache and convert the MACs etc. to names.

(Obviously this conversion only works once udev starts, so it will warn
you if you try it too early.)
master
Will Woods 2012-09-13 10:52:33 -04:00 committed by Harald Hoyer
parent bd0f2c034a
commit 4ef0e2d92a
1 changed files with 31 additions and 0 deletions

View File

@ -14,6 +14,11 @@ iface_for_remote_addr() {
echo $5
}

iface_for_ip() {
set -- $(ip -o addr show to $1)
echo $2
}

iface_for_mac() {
local interface="" mac="$(echo $1 | sed 'y/ABCDEF/abcdef/')"
for interface in /sys/class/net/*; do
@ -47,6 +52,32 @@ find_iface_with_link() {
return 1
}

# get the iface name for the given identifier - either a MAC, IP, or iface name
iface_name() {
case $1 in
??:??:??:??:??:??|??-??-??-??-??-??) iface_for_mac $1 ;;
*:*:*|*.*.*.*) iface_for_ip $1 ;;
*) echo $1 ;;
esac
}

# list the configured interfaces
configured_ifaces() {
local IFACES="" iface_id="" rv=1
[ -e "/tmp/net.ifaces" ] && read IFACES < /tmp/net.ifaces
if { pidof udevd || pidof systemd-udevd; } > /dev/null; then
for iface_id in $IFACES; do
echo $(iface_name $iface_id)
rv=0
done
else
warn "configured_ifaces called before udev is running"
echo $IFACES
[ -n "$IFACES" ] && rv=0
fi
return $rv
}

all_ifaces_up() {
local iface="" IFACES=""
[ -e "/tmp/net.ifaces" ] && read IFACES < /tmp/net.ifaces