#!/bin/sh
#	program:	addr2mask
#	release:	1.5.0
#	input:		only one network address on the command line,
#			consisting of dot-separated octets or words.
#	output:		the default netmask for the network, according to
#			it's classification (class A, B, C).  No "D" or beyond.
#			No RFC1101 resource-records taken into consideration.
#			Usage printed only if called with wrong number of args.
#	example:	"134.114.64.24" -> "255.255.0.0"
#	exit value:	0 if nothing goes wrong, 1 if something did.
#
# Copyright (C) 1993-2000 Paul A. Balyoz <pab@domtools.com>
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#

if test $# -ne 1; then
	echo "usage: $0 ipaddress" >&2
	echo '       where address must be in class A, B, or C.' >&2
	echo 'ERROR'
	exit 1
fi

if test "`/usr/local/domtools/bin/type $1`" != "forward"; then
        echo "$0: argument $1 is not an IP address." >&2
        echo 'ERROR'
        exit 1
fi

firstoct=`echo $1 | awk -F. '{print $1}'`
status=$?

if test $status -ne 0; then
	status=1
else
	if test $firstoct -eq 0; then
		status=1
	else

		if test $firstoct -lt 128; then
			echo "255.0.0.0"
		else
			if test $firstoct -lt 192; then
				echo "255.255.0.0"
			else
				if test $firstoct -lt 224; then
					echo "255.255.255.0"
				else
					status=1
				fi
			fi
		fi
	fi
fi

if test $status -ne 0; then
	echo 'ERROR'
fi

exit $status
