- DEV=`grep -i -e '^[[:space:]]*TapDevice' $CFG | sed 's/[[:space:]]//g; s/^.*=//g'`
- VPN=`grep -i -e '^[[:space:]]*(MyOwnVPNIP|MyVirtualIP)' -E $CFG | head -1 | sed 's/[[:space:]]//g; s/^.*=//g'`
-
- # discourage empty and multiple entries
- [ -z "$DEV" ] && \
- { echo "tinc: TapDevice required" >&2 ; return 2; }
- echo $DEV | grep -q '^/dev/tap' ||
- { echo "tinc: TapDevice should be in form /dev/tapX" >&2 ; return 2; }
- [ `echo $DEV | wc -l` -gt 1 ] &&
- { echo "tinc: multiple TapDevice entries not allowed" >&2 ; return 3; }
- [ -z "$VPN" ] && \
- { echo "tinc: MyOwnVPNIP/MyVirtualIP required" >&2 ; return 2; }
- [ `echo $VPN | wc -l` -gt 1 ] &&
- { echo "tinc: multiple MyOwnVPNIP/MyVirtualIP entries not allowed" >&2 ; return 3; }
- echo $VPN | grep -q -x \
- '\([[:digit:]]\{1,3\}\.\)\{3\}[[:digit:]]\{1,3\}/[[:digit:]]\{1,2\}' || \
- { echo "tinc: badly formed MyOwnVPNIP/MyVirtualIP address $VPN" ; return 3; }
-
- # network device
- TAP=`echo $DEV | cut -d"/" -f3`
- NUM=`echo $TAP | sed 's/tap//'`
-
- # IP address, netmask length
- ADR=`echo $VPN | cut -d"/" -f1`
- LEN=`echo $VPN | cut -d"/" -f2`
-
- # Expand bitlength to netmask
- MSK=""; len=$LEN
- for cnt in 1 1 1 0 ; do
- if [ $len -ge 8 ]; then
- msk=8
- else
- msk=$len
- fi
-
- MSK="$MSK$((255 & (255 << (8 - msk))))"
- [ $cnt -ne 0 ] && MSK="$MSK."
- len=$((len-msk))
- done
-
- # 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 | sed 's/\./ /g')`
- echo
- echo "TAP $TAP NUM $NUM ADR $ADR LEN $LEN MSK $MSK BRD $BRD NET $NET MAC $MAC" >&2
- return 0
+ while(<$CFG>) {
+ if( /^[ ]*TapDevice[ =]+([^ \#]+)/i ) {
+ $DEV=$1;
+ chomp($DEV);
+ $DEV =~ s/^.*\/([^\/0-9]+)([0-9]+)$/$1$2/;
+ $NUM = $2;
+ } elsif ( /^[ ]*(MyOwnVPNIP|MyVirtualIP)[ =]+([^ \#]+)/i ) {
+ $VPN=$2;
+ chomp($VPN);
+ }
+ }
+ if(!defined($DEV)) {
+ die "tinc: There must be a TapDevice";
+ }
+ if($DEV eq "") {
+ die "tinc: TapDevice should be of the form /dev/tapN";
+ }
+ if(!defined($VPN)) {
+ die "tinc: MyVirtualIP required";
+ }
+ if($VPN eq "") {
+ die "tinc: No argument to MyVirtualIP/MyOwnVPNIP";
+ }
+ $ADR = $VPN;
+ $ADR =~ s/^([^\/]+)\/.*$/$1/;
+ $LEN = $VPN;
+ $LEN =~ s/^.*\/([^\/]+)$/$1/;
+ if($ADR eq "" || $LEN eq "") {
+ die "tinc: Badly formed MyVirtualIP/MyOwnVPNIP";
+ }
+ @addr = split(/\./, $ADR);
+
+ $ADR = pack('C4', @addr);
+ $MSK = pack('N4', -1 << (32 - $LEN));
+ $BRD = join(".", unpack('C4', $ADR | ~$MSK));
+# $NET = join(".", unpack('C4', $ADR & $MSK));
+ $MAC = "fe:fd:" . join(":", map { sprintf "%02x", $_ } unpack('C4', $ADR));
+ $ADR = join(".", unpack('C4', $ADR));
+ $MSK = join(".", unpack('C4', $MSK));
+
+# print "$DEV $VPN $NUM $LEN @addr $MAC $MASK $BRD $NET\n";
+
+ 1;