#!/bin/ksh -p
#pragma ident "@(#)net_number	1.3 97/02/27 SMI"
#
# Copyright 02/27/97 Sun Microsystems, Inc. All Rights Reserved
#
# Synopsis: net_number <ip address>

net_number() {
	[[ $# != 1 || -z $1 ]] && return 1
	typeset -i16 ip=16#$(hexIP $1)
	typeset -i16 ifip mask net ifnet
	set -A n $(get_nets)
	typeset -i i=0
	while (( i < ${#n[*]} ))
	do
		ifip=16#${n[$i]}
		mask=16#${n[$i+1]}
		(( ifnet = ifip & mask ))
		(( net = ip & mask ))
		(( net == ifnet )) && {
			decIP $net
			return 0
		}
		(( i += 2 ))
	done
	return 1
}

get_nets() {
	# Cache internal netmasks until rebooted on assumption that
	# network configuration won't change without rebooting system.  
	# Also, not a big deal if accidently blown away

	jdnetmasks=/tmp/.jdnetmasks
	[[ -f $jdnetmasks ]] && {
		cat $jdnetmasks
		return
	}

	# Cache not available, create the internal netmasks file

	netmasks=$(ifconfig -a | nawk '
		/flags=/	{ skip = 0; }
				{ if (skip) next }
		/LOOPBACK/	{ skip = 1; next }
		/POINTOPOINT/	{ skip = 1; next }
		/inet/		{ n = split($2, octets, ".")
				  printf("%.2X%.2X%.2X%.2X %s\n", octets[1],
				  octets[2], octets[3], octets[4], $4)
				  skip = 1; next
				}
	') 
	print $netmasks >| $jdnetmasks

	# And create/update /etc/netmasks

	update_etc_netmasks $netmasks

	# Remember to return the netmasks array

	print $netmasks
}

update_etc_netmasks() {
	typeset netmasks=/etc/netmasks

	trap "rm -f $netmasks.$$ $netmasks.$$.$$" EXIT

	[[ -f $netmasks ]] && {
		cp $netmasks $netmasks.$$
	}

	# Loop through ip/netmask pairs in the input list
	# creating the default net number (i.e. non-subnetted)
	# and dotted decimal netmask for each pair and store
	# the result at the end of the temporary netmasks file

	typeset mask net
	typeset -i i=$# hibyte
	while (( i > 0 ))
	do
		(( hibyte = (16#$1 >> 24) & 16#FF ))
		if (( hibyte < 128 ))
		then
			# Class A
			net=$hibyte.0.0.0
		elif (( hibyte < 192 ))
		then
			# Class B
			net=$(decIP $((16#$1 & 16#FFFF0000)))
		elif (( hibyte < 224 ))
		then
			# Class C
			net=$(decIP $((16#$1 & 16#FFFFFF00)))
		elif (( hibyte < 240 ))
		then
			# Class D -- can we get these off an interface?
			net=240.0.0.0
		else
			# Class E -- reserved for future use, better not get this
			net=240.0.0.0	# just a guess!
		fi
		shift
		mask=$(decIP 16#$1)
		print "$net\t$mask" >> $netmasks.$$
		shift
		(( i -= 2 ))
	done

	[[ -f $netmasks.$$ ]] || return 0	# Nothing else to do

	# Now go through the temporary file and remove
	# duplicate records, keeping the last record
	# since we most likely added that in the above
	# loop

	nawk '
		$1 ~ "#" { print; next }
			 { ip[$1] = $0; next }
		END	 { for (i in ip)
				print ip[i]
			 }
	' $netmasks.$$ > $netmasks.$$.$$

	# Finally create the /etc/netmasks file

	cp $netmasks.$$.$$ $netmasks

	rm -f $netmasks.$$ $netmasks.$$.$$

	trap - EXIT

	# Add files to netmasks lookup in /etc/nsswitch.conf

	nsswitch=/etc/nsswitch.conf
	[[ -f $nsswitch ]] || {
		print "$nsswitch does not exist"
		exit 1
	}

	trap "rm -f $nsswitch.$$" EXIT

	nawk '
		$1 == "netmasks:" && $2 != "files" {
			sub(/netmasks:[ \t]+/, "netmasks:\tfiles ") }
			{ print }
	' $nsswitch > $nsswitch.$$

	cp $nsswitch.$$ $nsswitch

	rm -f $nsswitch.$$

	trap - EXIT

	return 0
}
