base-files: ipcalc.sh: Rewrite in pure shell
Also add better error checking on input. Signed-off-by: Philip Prindeville <philipp@redfish-solutions.com>
This commit is contained in:
parent
1438bc583c
commit
854739b32c
2 changed files with 232 additions and 83 deletions
|
@ -1,100 +1,109 @@
|
||||||
#!/usr/bin/awk -f
|
#!/bin/sh
|
||||||
|
|
||||||
function bitcount(c) {
|
. /lib/functions/ipv4.sh
|
||||||
c=and(rshift(c, 1),0x55555555)+and(c,0x55555555)
|
|
||||||
c=and(rshift(c, 2),0x33333333)+and(c,0x33333333)
|
PROG="$(basename "$0")"
|
||||||
c=and(rshift(c, 4),0x0f0f0f0f)+and(c,0x0f0f0f0f)
|
|
||||||
c=and(rshift(c, 8),0x00ff00ff)+and(c,0x00ff00ff)
|
usage() {
|
||||||
c=and(rshift(c,16),0x0000ffff)+and(c,0x0000ffff)
|
echo "Usage: $PROG address/prefix [ start limit ]" >&2
|
||||||
return c
|
exit 1
|
||||||
}
|
}
|
||||||
|
|
||||||
function ip2int(ip) {
|
if [ $# -eq 0 ]; then
|
||||||
ret=0
|
usage
|
||||||
n=split(ip,a,"\\.")
|
fi
|
||||||
for (x=1;x<=n;x++)
|
|
||||||
ret=or(lshift(ret,8),a[x])
|
|
||||||
return ret
|
|
||||||
}
|
|
||||||
|
|
||||||
function int2ip(ip,ret,x) {
|
case "$1" in
|
||||||
ret=and(ip,255)
|
*/*.*)
|
||||||
ip=rshift(ip,8)
|
str2ip ipaddr "${1%/*}" || exit 1
|
||||||
for(;x<3;x++) {
|
str2ip netmask "${1#*/}" || exit 1
|
||||||
ret=and(ip,255)"."ret
|
shift
|
||||||
ip=rshift(ip,8)
|
;;
|
||||||
}
|
*/*)
|
||||||
return ret
|
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
|
||||||
|
netmask=$(((0xffffffff << (32 - prefix)) & 0xffffffff))
|
||||||
|
shift
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
str2ip ipaddr "$1" || exit 1
|
||||||
|
str2ip netmask "$2" || exit 1
|
||||||
|
shift 2
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
function compl32(v) {
|
if [ $# -ne 0 ] && [ $# -ne 2 ]; then
|
||||||
ret=xor(v, 0xffffffff)
|
usage
|
||||||
return ret
|
fi
|
||||||
}
|
|
||||||
|
|
||||||
BEGIN {
|
if ! bitcount prefix "$netmask"; then
|
||||||
slpos=index(ARGV[1],"/")
|
printf "Invalid netmask (%s)\n" "$netmask" >&2
|
||||||
if (slpos != 0) {
|
exit 1
|
||||||
# rearrange arguments to not use compound notation
|
fi
|
||||||
ARGV[4]=ARGV[3]
|
|
||||||
ARGV[3]=ARGV[2]
|
# complement of the netmask, i.e. the hostmask
|
||||||
ARGV[2]=substr(ARGV[1],slpos+1)
|
hostmask=$((netmask ^ 0xffffffff))
|
||||||
ARGV[1]=substr(ARGV[1],0,slpos-1)
|
network=$((ipaddr & netmask))
|
||||||
}
|
broadcast=$((network | hostmask))
|
||||||
ipaddr=ip2int(ARGV[1])
|
|
||||||
dotpos=index(ARGV[2],".")
|
ip2str IP "$ipaddr"
|
||||||
if (dotpos == 0)
|
ip2str NETMASK "$netmask"
|
||||||
netmask=compl32(2**(32-int(ARGV[2]))-1)
|
ip2str NETWORK "$network"
|
||||||
|
|
||||||
|
echo "IP=$IP"
|
||||||
|
echo "NETMASK=$NETMASK"
|
||||||
|
if [ "$prefix" -le 30 ]; then
|
||||||
|
ip2str BROADCAST "$broadcast"
|
||||||
|
echo "BROADCAST=$BROADCAST"
|
||||||
|
fi
|
||||||
|
echo "NETWORK=$NETWORK"
|
||||||
|
echo "PREFIX=$prefix"
|
||||||
|
|
||||||
|
[ $# -eq 0 ] && exit 0
|
||||||
|
|
||||||
|
if [ "$prefix" -le 30 ]; then
|
||||||
|
lower=$((network + 1))
|
||||||
else
|
else
|
||||||
netmask=ip2int(ARGV[2])
|
lower="$network"
|
||||||
|
fi
|
||||||
|
|
||||||
network=and(ipaddr,netmask)
|
start="$1"
|
||||||
prefix=32-bitcount(compl32(netmask))
|
assert_uint32 "$start" || exit 1
|
||||||
|
start=$((network | (start & hostmask)))
|
||||||
|
[ "$start" -lt "$lower" ] && start="$lower"
|
||||||
|
[ "$start" -eq "$ipaddr" ] && start=$((start + 1))
|
||||||
|
|
||||||
print "IP="int2ip(ipaddr)
|
if [ "$prefix" -le 30 ]; then
|
||||||
print "NETMASK="int2ip(netmask)
|
upper=$(((network | hostmask) - 1))
|
||||||
print "NETWORK="int2ip(network)
|
|
||||||
if (prefix<=30) {
|
|
||||||
broadcast=or(network,compl32(netmask))
|
|
||||||
print "BROADCAST="int2ip(broadcast)
|
|
||||||
}
|
|
||||||
print "PREFIX="prefix
|
|
||||||
|
|
||||||
# range calculations:
|
|
||||||
# ipcalc <ip> <netmask> <range_start> <range_size>
|
|
||||||
|
|
||||||
if (ARGC <= 3)
|
|
||||||
exit(0)
|
|
||||||
|
|
||||||
if (prefix<=30)
|
|
||||||
limit=network+1
|
|
||||||
else
|
else
|
||||||
limit=network
|
upper="$network"
|
||||||
|
fi
|
||||||
|
|
||||||
start=or(network,and(ip2int(ARGV[3]),compl32(netmask)))
|
range="$2"
|
||||||
if (start<limit) start=limit
|
assert_uint32 "$range" || exit 1
|
||||||
if (start==ipaddr) start=ipaddr+1
|
end=$((start + range - 1))
|
||||||
|
[ "$end" -gt "$upper" ] && end="$upper"
|
||||||
|
[ "$end" -eq "$ipaddr" ] && end=$((end - 1))
|
||||||
|
|
||||||
if (prefix<=30)
|
if [ "$start" -gt "$end" ]; then
|
||||||
limit=or(network,compl32(netmask))-1
|
echo "network ($NETWORK/$prefix) too small" >&2
|
||||||
else
|
exit 1
|
||||||
limit=or(network,compl32(netmask))
|
fi
|
||||||
|
|
||||||
end=start+ARGV[4]-1
|
ip2str START "$start"
|
||||||
if (end>limit) end=limit
|
ip2str END "$end"
|
||||||
if (end==ipaddr) end=ipaddr-1
|
|
||||||
|
|
||||||
if (start>end) {
|
if [ "$start" -le "$ipaddr" ] && [ "$ipaddr" -le "$end" ]; then
|
||||||
print "network ("int2ip(network)"/"prefix") too small" > "/dev/stderr"
|
echo "error: address $IP inside range $START..$END" >&2
|
||||||
exit(1)
|
exit 1
|
||||||
}
|
fi
|
||||||
|
|
||||||
if (ipaddr >= start && ipaddr <= end) {
|
echo "START=$START"
|
||||||
print "warning: ipaddr inside range - this might not be supported in future releases of Openwrt" > "/dev/stderr"
|
echo "END=$END"
|
||||||
# turn this into an error after Openwrt 24 has been released
|
|
||||||
# exit(1)
|
|
||||||
}
|
|
||||||
|
|
||||||
print "START="int2ip(start)
|
exit 0
|
||||||
print "END="int2ip(end)
|
|
||||||
}
|
|
||||||
|
|
140
package/base-files/files/lib/functions/ipv4.sh
Normal file
140
package/base-files/files/lib/functions/ipv4.sh
Normal file
|
@ -0,0 +1,140 @@
|
||||||
|
uint_max=4294967295
|
||||||
|
|
||||||
|
assert_uint32() {
|
||||||
|
local __n="$1"
|
||||||
|
|
||||||
|
if [ -z "$__n" -o -n "${__n//[0-9]/}" ]; then
|
||||||
|
printf "Not a decimal integer (%s)\n" "$__n ">&2
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ "$__n" -gt $uint_max ]; then
|
||||||
|
printf "Out of range (%s)\n" "$__n" >&2
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ "$((__n + 0))" != "$__n" ]; then
|
||||||
|
printf "Not normalized notation (%s)\n" "$__n" >&2
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
bitcount() {
|
||||||
|
local __var="$1" __c="$2"
|
||||||
|
assert_uint32 "$__c" || return 1
|
||||||
|
|
||||||
|
__c=$((((__c >> 1) & 0x55555555) + (__c & 0x55555555)))
|
||||||
|
__c=$((((__c >> 2) & 0x33333333) + (__c & 0x33333333)))
|
||||||
|
__c=$((((__c >> 4) & 0x0f0f0f0f) + (__c & 0x0f0f0f0f)))
|
||||||
|
__c=$((((__c >> 8) & 0x00ff00ff) + (__c & 0x00ff00ff)))
|
||||||
|
__c=$((((__c >> 16) & 0x0000ffff) + (__c & 0x0000ffff)))
|
||||||
|
|
||||||
|
export -- "$__var=$__c"
|
||||||
|
}
|
||||||
|
|
||||||
|
# tedious but portable with busybox's limited shell
|
||||||
|
str2ip() {
|
||||||
|
local __var="$1" __ip="$2" __n __val=0
|
||||||
|
|
||||||
|
case "$__ip" in
|
||||||
|
[0-9].*)
|
||||||
|
__n="${__ip:0:1}"
|
||||||
|
__ip="${__ip:2}"
|
||||||
|
;;
|
||||||
|
[1-9][0-9].*)
|
||||||
|
__n="${__ip:0:2}"
|
||||||
|
__ip="${__ip:3}"
|
||||||
|
;;
|
||||||
|
1[0-9][0-9].*|2[0-4][0-9].*|25[0-5].*)
|
||||||
|
__n="${__ip:0:3}"
|
||||||
|
__ip="${__ip:4}"
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
printf "Not a dotted quad (%s)\n" "$2" >&2
|
||||||
|
return 1
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
__val=$((__n << 24))
|
||||||
|
|
||||||
|
case "$__ip" in
|
||||||
|
[0-9].*)
|
||||||
|
__n="${__ip:0:1}"
|
||||||
|
__ip="${__ip:2}"
|
||||||
|
;;
|
||||||
|
[1-9][0-9].*)
|
||||||
|
__n="${__ip:0:2}"
|
||||||
|
__ip="${__ip:3}"
|
||||||
|
;;
|
||||||
|
1[0-9][0-9].*|2[0-4][0-9].*|25[0-5].*)
|
||||||
|
__n="${__ip:0:3}"
|
||||||
|
__ip="${__ip:4}"
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
printf "Not a dotted quad (%s)\n" "$2" >&2
|
||||||
|
return 1
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
__val=$((__val + (__n << 16)))
|
||||||
|
|
||||||
|
case "$__ip" in
|
||||||
|
[0-9].*)
|
||||||
|
__n="${__ip:0:1}"
|
||||||
|
__ip="${__ip:2}"
|
||||||
|
;;
|
||||||
|
[1-9][0-9].*)
|
||||||
|
__n="${__ip:0:2}"
|
||||||
|
__ip="${__ip:3}"
|
||||||
|
;;
|
||||||
|
1[0-9][0-9].*|2[0-4][0-9].*|25[0-5].*)
|
||||||
|
__n="${__ip:0:3}"
|
||||||
|
__ip="${__ip:4}"
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
printf "Not a dotted quad (%s)\n" "$2" >&2
|
||||||
|
return 1
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
__val=$((__val + (__n << 8)))
|
||||||
|
|
||||||
|
case "$__ip" in
|
||||||
|
[0-9])
|
||||||
|
__n="${__ip:0:1}"
|
||||||
|
__ip="${__ip:1}"
|
||||||
|
;;
|
||||||
|
[1-9][0-9])
|
||||||
|
__n="${__ip:0:2}"
|
||||||
|
__ip="${__ip:2}"
|
||||||
|
;;
|
||||||
|
1[0-9][0-9]|2[0-4][0-9]|25[0-5])
|
||||||
|
__n="${__ip:0:3}"
|
||||||
|
__ip="${__ip:3}"
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
printf "Not a dotted quad (%s)\n" "$2" >&2
|
||||||
|
return 1
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
__val=$((__val + __n))
|
||||||
|
|
||||||
|
if [ -n "$__ip" ]; then
|
||||||
|
printf "Not a dotted quad (%s)\n" "$2" >&2
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
export -- "$__var=$__val"
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
ip2str() {
|
||||||
|
local __var="$1" __n="$2"
|
||||||
|
assert_uint32 "$__n" || return 1
|
||||||
|
|
||||||
|
export -- "$__var=$((__n >> 24)).$(((__n >> 16) & 255)).$(((__n >> 8) & 255)).$((__n & 255))"
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue