
#-----------------------------------------------------------------------
# Copyright (C) 2000-2001, Jean-Sebastien Morisset <jsmoriss@mvlan.net>
#-----------------------------------------------------------------------
# $Id: device_subnets.sh,v 1.6 2001/09/02 22:09:26 jsmoriss Exp $
#-----------------------------------------------------------------------
#  $RCSfile: device_subnets.sh,v $
#   $Author: jsmoriss $
# $Revision: 1.6 $
#     $Date: 2001/09/02 22:09:26 $
#   $Locker:  $ 
#     $Name:  $
#-----------------------------------------------------------------------
#
# 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 <http://www.gnu.org/copyleft/gpl.txt>.
# 
# 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 (LICENSE file in archive) for more details.
#
#-----------------------------------------------------------------------
# README
#-----------------------------------------------------------------------
#
# A bourne shell function used by rcf; a very secure, fast, and complete
# firewall for linux <http://jsmoriss.mvlan.net/linux/rcf.html>.
#
# Examine routing table and determine which networks are being routed 
# through this interface. Also add our own network/netmask and remove
# duplicates.
#
#-----------------------------------------------------------------------
# FUNCTION
#-----------------------------------------------------------------------

Device_Subnets () {

	[ "$1" ] && prefix="${1}_" || prefix=""

	eval device=\"\$${prefix}DEVICE\"
	eval network=\"\$${prefix}NETWORK\"
	eval netmask=\"\$${prefix}NETMASK\"
	eval ipaddr=\"\$${prefix}IPADDR\"

	[ "$network" = "$ipaddr" ] && netmask=""

	[ "$device" ] && {
		[ "$network" -a "$netmask" ] && echo "$network/$netmask"

		# Extract the network, gateway, and netmask for this interface.
		# Remove the default route and broadcast address (255.255.255.255).
		# Lastly, if the gateway is 0.0.0.0 and the netmask is 255.255.255.255,
		# remove this entry since it's probably a virtual interface IP.
		#
		netstat -nre 2>/dev/null | \
			sed -n -e "s/^\([0-9\.]*\)  *\([0-9\.]*\)  *\([0-9\.]*\)  *U[HG]*  *[0-9]*  *[0-9]*  *[0-9]*  *${device}$/\1 \2 \3/p" | \
			grep -v '^\(0\.0\.0\.0\|255\.255\.255\.255\) ' | \
			sed -n -e 's/^\([0-9\.]*\) [0-9\.]* \([0-9\.]*\)$/\1\/\2/p'
	} | sort | Sed_Uniq

	unset prefix device network netmask ipaddr
}

