2023-10-22 19:27:50 +00:00
|
|
|
#!/bin/sh
|
2011-02-13 01:44:57 +00:00
|
|
|
|
2023-10-22 19:27:50 +00:00
|
|
|
. /lib/functions/ipv4.sh
|
2011-02-13 01:44:57 +00:00
|
|
|
|
2023-10-22 19:27:50 +00:00
|
|
|
PROG="$(basename "$0")"
|
2011-02-13 01:44:57 +00:00
|
|
|
|
2023-10-24 06:16:25 +00:00
|
|
|
# wrapper to convert an integer to an address, unless we're using
|
|
|
|
# decimal output format.
|
2023-10-22 19:32:06 +00:00
|
|
|
# hook for library function
|
|
|
|
_ip2str() {
|
|
|
|
local var="$1" n="$2"
|
|
|
|
assert_uint32 "$n" || exit 1
|
|
|
|
|
|
|
|
if [ "$decimal" -ne 0 ]; then
|
|
|
|
export -- "$var=$n"
|
|
|
|
elif [ "$hexadecimal" -ne 0 ]; then
|
|
|
|
export -- "$var=$(printf "%x" "$n")"
|
|
|
|
else
|
|
|
|
ip2str "$@"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2023-10-22 19:27:50 +00:00
|
|
|
usage() {
|
2023-10-22 19:32:06 +00:00
|
|
|
echo "Usage: $PROG [ -d | -x ] address/prefix [ start limit ]" >&2
|
2023-10-22 19:27:50 +00:00
|
|
|
exit 1
|
2012-12-23 22:18:43 +00:00
|
|
|
}
|
|
|
|
|
2023-10-22 19:32:06 +00:00
|
|
|
decimal=0
|
|
|
|
hexadecimal=0
|
|
|
|
if [ "$1" = "-d" ]; then
|
|
|
|
decimal=1
|
|
|
|
shift
|
|
|
|
elif [ "$1" = "-x" ]; then
|
|
|
|
hexadecimal=1
|
|
|
|
shift
|
|
|
|
fi
|
|
|
|
|
2023-10-22 19:27:50 +00:00
|
|
|
if [ $# -eq 0 ]; then
|
|
|
|
usage
|
|
|
|
fi
|
|
|
|
|
|
|
|
case "$1" in
|
|
|
|
*/*.*)
|
2023-10-24 06:16:25 +00:00
|
|
|
# data is n.n.n.n/m.m.m.m format, like on a Cisco router
|
2023-10-22 19:27:50 +00:00
|
|
|
str2ip ipaddr "${1%/*}" || exit 1
|
|
|
|
str2ip netmask "${1#*/}" || exit 1
|
|
|
|
shift
|
|
|
|
;;
|
|
|
|
*/*)
|
2023-10-24 06:16:25 +00:00
|
|
|
# more modern prefix notation of n.n.n.n/p
|
2023-10-22 19:27:50 +00:00
|
|
|
str2ip ipaddr "${1%/*}" || exit 1
|
|
|
|
prefix="${1#*/}"
|
|
|
|
assert_uint32 "$prefix" || exit 1
|
|
|
|
if [ "$prefix" -gt 32 ]; then
|
|
|
|
printf "Prefix out of range (%s)\n" "$prefix" >&2
|
|
|
|
exit 1
|
|
|
|
fi
|
2023-11-04 03:11:49 +00:00
|
|
|
prefix2netmask netmask "$prefix" || exit 1
|
2023-10-22 19:27:50 +00:00
|
|
|
shift
|
|
|
|
;;
|
|
|
|
*)
|
2023-10-24 06:16:25 +00:00
|
|
|
# address and netmask as two separate arguments
|
2023-10-22 19:27:50 +00:00
|
|
|
str2ip ipaddr "$1" || exit 1
|
|
|
|
str2ip netmask "$2" || exit 1
|
|
|
|
shift 2
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
2023-10-24 06:16:25 +00:00
|
|
|
# we either have no arguments left, or we have a range start and length
|
2023-10-22 19:27:50 +00:00
|
|
|
if [ $# -ne 0 ] && [ $# -ne 2 ]; then
|
|
|
|
usage
|
|
|
|
fi
|
|
|
|
|
|
|
|
if ! bitcount prefix "$netmask"; then
|
|
|
|
printf "Invalid netmask (%s)\n" "$netmask" >&2
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
# complement of the netmask, i.e. the hostmask
|
|
|
|
hostmask=$((netmask ^ 0xffffffff))
|
|
|
|
network=$((ipaddr & netmask))
|
|
|
|
broadcast=$((network | hostmask))
|
2023-10-24 06:16:25 +00:00
|
|
|
count=$((hostmask + 1))
|
2023-10-22 19:27:50 +00:00
|
|
|
|
2023-10-22 19:32:06 +00:00
|
|
|
_ip2str IP "$ipaddr"
|
|
|
|
_ip2str NETMASK "$netmask"
|
|
|
|
_ip2str NETWORK "$network"
|
2023-10-22 19:27:50 +00:00
|
|
|
|
|
|
|
echo "IP=$IP"
|
|
|
|
echo "NETMASK=$NETMASK"
|
2023-10-24 06:16:25 +00:00
|
|
|
# don't include this-network or broadcast addresses
|
2023-10-22 19:27:50 +00:00
|
|
|
if [ "$prefix" -le 30 ]; then
|
2023-10-22 19:32:06 +00:00
|
|
|
_ip2str BROADCAST "$broadcast"
|
2023-10-22 19:27:50 +00:00
|
|
|
echo "BROADCAST=$BROADCAST"
|
|
|
|
fi
|
|
|
|
echo "NETWORK=$NETWORK"
|
|
|
|
echo "PREFIX=$prefix"
|
2023-10-24 06:16:25 +00:00
|
|
|
echo "COUNT=$count"
|
2023-10-22 19:27:50 +00:00
|
|
|
|
2023-10-24 06:16:25 +00:00
|
|
|
# if there's no range, we're done
|
2023-10-22 19:27:50 +00:00
|
|
|
[ $# -eq 0 ] && exit 0
|
|
|
|
|
|
|
|
if [ "$prefix" -le 30 ]; then
|
|
|
|
lower=$((network + 1))
|
|
|
|
else
|
|
|
|
lower="$network"
|
|
|
|
fi
|
|
|
|
|
|
|
|
start="$1"
|
|
|
|
assert_uint32 "$start" || exit 1
|
|
|
|
start=$((network | (start & hostmask)))
|
|
|
|
[ "$start" -lt "$lower" ] && start="$lower"
|
|
|
|
[ "$start" -eq "$ipaddr" ] && start=$((start + 1))
|
|
|
|
|
|
|
|
if [ "$prefix" -le 30 ]; then
|
|
|
|
upper=$(((network | hostmask) - 1))
|
|
|
|
else
|
|
|
|
upper="$network"
|
|
|
|
fi
|
|
|
|
|
|
|
|
range="$2"
|
|
|
|
assert_uint32 "$range" || exit 1
|
|
|
|
end=$((start + range - 1))
|
|
|
|
[ "$end" -gt "$upper" ] && end="$upper"
|
|
|
|
[ "$end" -eq "$ipaddr" ] && end=$((end - 1))
|
|
|
|
|
|
|
|
if [ "$start" -gt "$end" ]; then
|
|
|
|
echo "network ($NETWORK/$prefix) too small" >&2
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2023-10-22 19:32:06 +00:00
|
|
|
_ip2str START "$start"
|
|
|
|
_ip2str END "$end"
|
2023-10-22 19:27:50 +00:00
|
|
|
|
|
|
|
if [ "$start" -le "$ipaddr" ] && [ "$ipaddr" -le "$end" ]; then
|
|
|
|
echo "error: address $IP inside range $START..$END" >&2
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo "START=$START"
|
|
|
|
echo "END=$END"
|
|
|
|
|
|
|
|
exit 0
|