#! /bin/sh # # network-functions: Network functions script for openmamba # # Copyright (c) 2003-2005 by Silvan Calarco # Copyright (c) 2003-2005,2011 by Davide Madrisan . /etc/sysconfig/rc # ip_check_args() # # parameters: # $1 : ip/bitmask (like 192.168.0.1/24) -or- # $1 $2 : ip netmask (like 192.168.0.1 255.255.255.0) # description: # This function checks if its first / first two arguments are # a valid ipv4 address. # If it is a valid one, returns '1' and set the variables # $ip (ip address) # $mask_bit (number of active bits in the maks) # $mask (mask) # otherwise returns '0' ip_check_args () { local i # checks if the first argument is a valid one, that is something # like "192.168.0.1" or "192.168.0.1/24" if ! echo $1 | grep -Eq "^([0-9]+\.){3}[0-9]+(/[0-9]+){0,1}$"; then return 0 fi ip=`echo $1 | cut -f1 -d/` for i in `seq 1 4`; do # check for range in [0..255] local byte=`echo $ip | cut -f$i -d.` [ $byte -le 255 ] || return 0 done # gets the mask from $1, if $1 is like '192.168.0.1/24', # otherwise 'mask_bit' is set to "" mask_bits=`echo $1 | cut -f2 -s -d/` if [ $mask_bits ]; then # 'mask_bit' is not "" [ $mask_bits -le 32 ] || return 0 # illegal 'mask_bit' # set 'mask'. i.e. mask_bits = 24 --> mask = 255.255.255.0 local mb=$mask_bits for i in `seq 1 4`; do if [ $mb -ge 8 ]; then mask=$mask"255" mb=$(($mb - 8)) else mask=$mask$(( 256 - (2 << (7-$mb)) )) [ $mb -gt 0 ] && mb=0 fi [ $i -lt 4 ] && mask=$mask"." done else # $2 should be a network mask (like 255.255.255.0) if ! echo $2 | grep -Eq "^([0-9]+\.){3}[0-9]+$"; then return 0 fi mask=$2 local prev_byte=255 local prev_bit=1 mask_bits=0 for i in `seq 1 4`; do local byte=`echo $mask | cut -f$i -d.` [ $byte -gt 255 ] && return 0 # if the previous was < 255, the current one must be 0 [ $prev_byte -lt 255 -a $byte -ne 0 ] && return 0 # internal loop to set 'mask_bits' for j in `seq 7 0`; do local bit_value=$(( ($byte >> j) & 1 )) # if the previous bit was '0', the current can't be '1' [ $prev_bit -eq 0 -a $bit_value -eq 1 ] && return 0 [ $bit_value -eq 1 ] && mask_bits=$(( $mask_bits + 1 )) prev_bit=$bit_value done prev_byte=$byte done fi return 1 } # calculate_ip_parameters() # # parameters: # $1 : ip # $2 : netmask # description: # This function set the variables # $network (network address) # $broadcast (broadcast address) # $wildcard (wildcard mask) # calculated using as input the ip/network address $1/$2. # If $1/$2 is a valid ipv4 address, returns '0', otherwise # returns '1' calculate_ip_parameters () { local i unset network broadcast wildcard ip_check_args $1 $2 [ $? -eq 0 ] && return 1 for i in 1 2 3 4; do ip_byte=`echo $ip | cut -f$i -d.` mask_byte=`echo $mask | cut -f$i -d.` ip_and_mask=$(( $ip_byte & $mask_byte )) mask_cplm=$(( 255 - $mask_byte )) wildcard=$wildcard$mask_cplm network=$network$ip_and_mask broadcast=$broadcast$((ip_and_mask + mask_cplm)) if [ $i -lt 4 ]; then network=$network"." broadcast=$broadcast"." wildcard=$wildcard"." fi done return 0 } # get_interface_parameters() # # parameters: # $1 : interface name (i.e. eth0) # description: # If no error occurs, this function return '0' and set the variables # $int_ip # $int_netmask # $int_network # $int_broadcast # $int_static_ip (0: static IP; 1: dynamic IP (DHCP)) # to the system values found for interface $1, otherwise returns '1' get_interface_parameters () { local int_name="$1" [ x"$int_name" != x ] || return 1 int_ip=`/sbin/ifconfig $int_name 2>/dev/null | \ sed -n '/inet addr/{s,.*inet addr:\([^ \t]*\).*,\1,;p}'` int_broadcast=`/sbin/ifconfig $int_name 2>/dev/null | \ sed -n '/inet addr:.*Bcast:/{s,.*Bcast:\([^ \t]*\).*,\1,;p}'` int_netmask=`/sbin/ifconfig $int_name 2>/dev/null | \ sed -n '/inet addr:.*Mask:/{s,.*Mask:\([^ \t]*\).*,\1,;p}'` # note: loopback interface has null 'int_broadcast' value [ -z "$int_ip" -o -z "$int_netmask" ] && return 1 int_static_ip=1 # this check was written to avoid backward compatibility issues if [ -r $network_devices/ifconfig.$int_name ]; then if grep -Eq "^BOOTPROTO=dhcp" $network_devices/ifconfig.$int_name; then int_static_ip=0 fi fi calculate_ip_parameters "$int_ip" "$int_netmask" int_network=$network return 0 }