3 # System startup script for tinc
4 # $Id: init.d,v 1.4 2000/05/15 09:41:34 guus Exp $
7 my $DAEMON="/usr/sbin/tincd";
9 my $DESC="tinc daemons";
10 my $TCONF="/etc/tinc";
13 # $NETS is a space seperated list of all tinc networks.
16 if ("$NETS" eq "") { print "No tinc networks configured."; exit 0; }
18 if (! -f $DAEMON) { exit 0; }
20 ##############################################################################
21 # vpn_load () Loads VPN configuration
23 # $_[0] ... VPN to load
28 $CFG="$TCONF/$_[0]/tinc.conf";
29 open($CFG, "< $CFG") || die "tinc: $CFG does not exist";
33 if( /^[ ]*TapDevice[ =]+([^ \#]+)/i ) {
36 $DEV =~ s/^.*\/([^\/0-9]+)([0-9]+)$/$1$2/;
38 } elsif ( /^[ ]*(MyOwnVPNIP|MyVirtualIP)[ =]+([^ \#]+)/i ) {
44 die "tinc: There must be a TapDevice";
47 die "tinc: TapDevice should be of the form /dev/tapN";
50 die "tinc: MyVirtualIP required";
53 die "tinc: No argument to MyVirtualIP/MyOwnVPNIP";
56 $ADR =~ s/^([^\/]+)\/.*$/$1/;
58 $LEN =~ s/^.*\/([^\/]+)$/$1/;
59 if($ADR eq "" || $LEN eq "") {
60 die "tinc: Badly formed MyVirtualIP/MyOwnVPNIP";
62 @addr = split(/\./, $ADR);
64 $ADR = pack('C4', @addr);
65 $MSK = pack('N4', -1 << (32 - $LEN));
66 $BRD = join(".", unpack('C4', $ADR | ~$MSK));
67 # $NET = join(".", unpack('C4', $ADR & $MSK));
68 $MAC = "fe:fd:" . join(":", map { sprintf "%02x", $_ } unpack('C4', $ADR));
69 $ADR = join(".", unpack('C4', $ADR));
70 $MSK = join(".", unpack('C4', $MSK));
72 # print "$DEV $VPN $NUM $LEN @addr $MAC $MASK $BRD $NET\n";
78 ##############################################################################
79 # vpn_start () starts specified VPN
81 # $_[0] ... VPN to start
84 vpn_load($_[0]) || die "tinc: could not vpn_load $_[0]";
86 if (! -c "/dev/$DEV") {
91 system("echo mknod --mode=0600 /dev/$DEV c 36 $num");
93 system("insmod ethertap -s --name=\"ethertap$NUM\" unit=\"$NUM\" >/dev/null");
94 system("ifconfig $DEV hw ether $MAC");
95 system("ifconfig $DEV $ADR netmask $MSK broadcast $BRD");
96 system("start-stop-daemon --start --quiet --pidfile /var/run/$NAME.$_[0].pid --exec $DAEMON -- -n $_[0] $EXTRA");
102 ##############################################################################
103 # vpn_stop () Stops specified VPN
108 vpn_load($_[0]) || return 1;
110 system("start-stop-daemon --stop --quiet --pidfile /var/run/$NAME.$_[0].pid --exec $DAEMON -- -n $_[0] $EXTRA -k");
112 system("ifconfig $DEV down");
113 system("rmmod ethertap$NUM -s");
117 if(!defined($ARGV[0])) {
118 die "Usage: /etc/init.d/$NAME {start|stop|restart|force-reload}\n";
121 if($ARGV[0] eq "start") {
122 print "Starting $DESC:";
123 foreach $n (split(" ", $NETS)) {
128 } elsif ($ARGV[0] eq "stop") {
129 print "Stopping $DESC:";
130 foreach $n (split(" ", $NETS)) {
135 } elsif ($ARGV[0] eq "restart" || $ARGV[0] eq "force-reload") {
136 print "Stopping $DESC:";
137 foreach $n (split(" ", $NETS)) {
142 print "Starting $DESC:";
143 foreach $n (split(" ", $NETS)) {
149 die "Usage: /etc/init.d/$NAME {start|stop|restart|force-reload}\n";