
#-----------------------------------------------------------------------
# Copyright (C) 2000-2001, Jean-Sebastien Morisset <jsmoriss@mvlan.net>
#-----------------------------------------------------------------------
# $Id: interface_functions.sh,v 1.4 2001/08/11 17:01:20 jsmoriss Exp $
#-----------------------------------------------------------------------
#  $RCSfile: interface_functions.sh,v $
#   $Author: jsmoriss $
# $Revision: 1.4 $
#     $Date: 2001/08/11 17:01:20 $
#   $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>.
#
# Using ifconfig or ip, return the ip address, netmask, etc. for an 
# interface.
#
#-----------------------------------------------------------------------
# FUNCTION
#-----------------------------------------------------------------------

All_Interfaces () {
	if [ "`which ifconfig 2>/dev/null`" ]
	then
		ifconfig -a|sed -ne 's/^\([^ ][^ ]*\).*$/\1/p'|sort
	elif [ "`which ip 2>/dev/null`" ]
	then
		ip addr show|sed -ne 's/^  *inet .* scope .* \([^ ]*\)$/\1/p'|sort
	fi
}

Interface_Addr () {
	if [ "$1" ]
	then
		if [ "`which ifconfig 2>/dev/null`" ]
		then
			ifconfig $1|sed -ne 's/.* addr:\([0-9\.]*\) .*/\1/p'
		elif [ "`which ip 2>/dev/null`" ]
		then
			ip addr show|sed -ne "s/  *inet \([0-9][0-9\.]*\).*\/\([0-9][0-9]*\) .* ${1}$/\1/p"
		fi
	fi
}

Interface_Bitlen () {
	if [ "$1" -a "`which ip 2>/dev/null`" ]
	then
		ip addr show|sed -ne "s/  *inet \([0-9][0-9\.]*\).*\/\([0-9][0-9]*\) .* ${1}$/\2/p"
	fi
}

Interface_Netmask () {
	if [ "$1" -a "`which ifconfig 2>/dev/null`" ]
	then
		ifconfig $1|sed -ne 's/.* Mask:\([0-9\.]*\)$/\1/p'
	fi
}

Interface_Broadcast () {
	if [ "$1" -a "`which ifconfig 2>/dev/null`" ]
	then
		ifconfig $1|sed -ne 's/.* Bcast:\([0-9\.]*\) .*/\1/p'
	fi
}

Interface_Up () {
	if [ "$1" ]
	then
		if [ "`Interface_Addr $1`" ]
		then
			device="`echo $1|sed -n -e 's/^\([^\:]*\).*\$/\1/p'`"
			if [ "`which ifconfig 2>/dev/null`" ]
			then
				ifconfig $1|sed -ne 's/^.* \(UP\) .*$/\1/p'
			elif [ "`which ip 2>/dev/null`" ]
			then
				ip addr show $device|sed -ne "s/^.*UP.*$/\1/p"
			fi
		fi
	fi
}

