-##############################################################################
-# prefix_to_mask Converts prefix length to netmask
-# eg. 17 -> 255.255.128.0
-# $1 ... prefix
-
-prefix_to_mask () {
- _MSK=""; _len="$1"
- for _dot in "." "." "." " "; do
- if [ ${_len} -ge 8 ]; then
- _fld=8
- else
- _fld="${_len}"
- fi
-
- _MSK="${_MSK}$((255 & (255 << (8 - _fld))))${_dot}"
- _len=$((_len - _fld))
- done
-
- echo ${_MSK}
-}
-
-
-##############################################################################
-# mask_to_prefix Converts netmask to prefix length
-# eg. 255.255.192.0 -> 18
-# $1 ... netmask
-
-mask_to_prefix () {
- _LEN=0; _msk="$1"
- for _tmp in 1 2 3 4; do
- _fld=${_msk%%.*}
- _msk=${_msk#*.}
-
- while [ ${_fld} -ne 0 ]; do
- _fld=$(((_fld << 1) & 255))
- _LEN=$((_LEN + 1))
- done
- done
-
- echo ${_LEN}
-}
-
-
-##############################################################################
-# vpn_load () Loads VPN configuration
-#
-# $1 ... VPN to load
-
-vpn_load () {
- CFG="$TCONF/$1/tinc.conf"
- [ -f $CFG ] || { MSG="$CFG does not exist!"; return 1; }
-
- # load TINCD config
- DEV="$(grep -i -e '^[[:space:]]*TapDevice' $CFG | sed 's/[[:space:]]//g; s/^.*=//g')"
- VPN="$(grep -i -e '^[[:space:]]*(MyOwnVPNIP|MyVirtualIP)' -E $CFG | sed 's/[[:space:]]//g; s/^.*=//g')"
- IFM="$(grep -i -e '^[[:space:]]*VPNMask' $CFG | sed 's/[[:space:]]//g; s/^.*=//g')"
-
- # TapDevice syntax validation
- [ -z "$DEV" ] && \
- { MSG="TapDevice required!"; return 1; }
- [ $(echo $DEV | wc -l) -gt 1 ] && \
- { MSG="multiple TapDevice entries not allowed!"; return 1; }
- echo $DEV | grep -q -x -E '/dev/tap[[:digit:]]+' ||
- { MSG="TapDevice should be in form /dev/tapX!"; return 1; }
-
- # MyOwnVPNIP/MyVirtualIP syntax validation
- [ -z "$VPN" ] && \
- { MSG="MyOwnVPNIP/MyVirtualIP required!"; return 1; }
- [ $(echo $VPN | wc -l) -gt 1 ] && \
- { MSG="multiple MyOwnVPNIP/MyVirtualIP entries not allowed!"; return 1; }
- echo $VPN | grep -q -x -E \
- '([[:digit:]]{1,3}\.){3}[[:digit:]]{1,3}/[[:digit:]]{1,2}' || \
- { MSG="badly formed MyOwnVPNIP/MyVirtualIP address $VPN!"; return 1; }
-
- # VPNMask syntax validation
- [ $(echo $IFM | wc -l) -gt 1 ] && \
- { MSG="multiple VPNMask entries not allowed!"; return 1; }
-
-
- # device & IP address extraction
- TAP=${DEV##*/}
- NUM=${TAP#tap}
- ADR=${VPN%%/*}
-
- # netmask is calculated from MyVirtualIP netmask prefix length, except when
- # VPNMask is specified, in which case it is used instead of default prefix
-
- # VPNMask not specified
- if [ -z "$IFM" ]; then
- LEN=${VPN##*/}
- MSK=$(prefix_to_mask $LEN)
-
- # VPNMask is prefix length, convert it to netmask for MSK
- elif echo $IFM | grep -q -x -E '[[:digit:]]{1,2}'; then
- VPN="$ADR/$IFM"
- MSK=$(prefix_to_mask $IFM)
-
- # VPNMask is netmask, convert it to prefix length for VPN
- elif echo $IFM | grep -q -x -E '([[:digit:]]{1,3}\.){3}[[:digit:]]{1,3}'; then
- VPN="$ADR/$(mask_to_prefix $IFM)"
- MSK="$IFM"
-
- else
- MSG="badly formed interface netmask (VPNMask=$IFM)!"
- return 1
- fi
-
-
- # Network & broadcast addresses
- BRD=$(ipcalc --broadcast $ADR $MSK | cut -d"=" -f2)
- NET=$(ipcalc --network $ADR $MSK | cut -d"=" -f2)
-
- # MAC address
- MAC=$(printf "fe:fd:%0.2x:%0.2x:%0.2x:%0.2x" $(echo $ADR | { IFS=. ; read a b c d; echo $a $b $c $d; }))
-
- # debugging
- # echo >&2
- # echo "VPN $VPN TAP $TAP NUM $NUM MAC $MAC IFM $IFM" >&2
- # echo "ADR $ADR MSK $MSK NET $NET BRD $BRD" >&2
-
- return 0
-}
-
-