## # \file # To each interface a set of caller functions exist, that take an instance # of an object and then in turn call the implementation for the class of # this object. If there is none within the class it looks into its # parent class and so forth. # # This is somewhat similar to late binding in real OOP languages, but # by far not so elaborated. This is not a real object oriented language # and will surely never ever provide all features these have. # # That said it has proven very usefull for me to orgnize code and prevent # code duplication. # # \author Georg Hopp # # \copyright # Copyright © 2014 Georg Hopp # # 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 3 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, see . # . utils/file.sh ## # Function calculates number of bit in a netmask # mask2cidr() { local NBITS=0 local OLDIFS="${IFS}" IFS=. for DEC in $1 do case ${DEC} in 255) NBITS=$((NBITS+8));; 254) NBITS=$((NBITS+7));; 252) NBITS=$((NBITS+6));; 248) NBITS=$((NBITS+5));; 240) NBITS=$((NBITS+4));; 224) NBITS=$((NBITS+3));; 192) NBITS=$((NBITS+2));; 128) NBITS=$((NBITS+1));; 0);; *) NBITS=0; IFS="${OLDIFS}"; return 1 esac done IFS="${OLDIFS}" ${ECHO} "${NBITS}" } ## # Function calculates a netmask from number of bits # cidr2mask() { local I MASK="" local FULL_OCTETS=$((${1}/8)) local PARTIAL_OCTET=$((${1}%8)) for I in 0 1 2 3 do if [ ${I} -lt ${FULL_OCTETS} ] then MASK="${MASK}255" elif [ ${I} -eq ${FULL_OCTETS} ] then MASK="${MASK}$((256-(1<<(8-${PARTIAL_OCTET}))))" else MASK="${MASK}0" fi [ ${I} -lt 3 ] && MASK="${MASK}." done ${ECHO} ${MASK} } # vim: set ts=4 sw=4: