#!BOURNESHELL
#	program:	subnetmask
#	purpose:	Fetch the netmask RR from the name server.  This is
#			an RFC1101 idea, which is officially optional but
#			is the ONLY way to determine the true subnet mask
#			of a network!
#	input:		network address in normal format ("134.114.64").
#	output:		network mask address record if any found,
#			or the word "ERROR" (distinguishable from a real
#			record because it has no period on the end).
#	exit value:	0 if succeeds, 1 if fails (and "ERROR" output).

if test $# -ne 1; then
	echo "usage: $0 net-IP-addr" >&2
	echo 'ERROR'
	exit 1
fi

# Force network number into host-zero, in-addr format.
addr="`DOMBIN/netwithzeros $1`"
if test x"$addr" = x"ERROR"; then
	echo "$0: netwithzeros failed to work on $1 network address." >&2
	echo 'ERROR'
	exit 1
fi
net="`DOMBIN/f2iaddr $addr`"

# Do an "A" RR query to get netmask record, ala RFC 1101.
subnetmask="`DOMBIN/address $net 2> /dev/null`"
if test $? -ne 0; then
	echo 'ERROR'
	exit 1
fi

echo "$subnetmask"
exit 0
