3 # System startup script for tinc
4 # $Id: init.d,v 1.5 2000/05/15 17:15:52 zarq Exp $
7 my $DAEMON="/usr/sbin/tincd";
9 my $DESC="tinc daemons";
10 my $TCONF="/etc/tinc";
13 my $NETS=""; # This is a space-separated list of networks to be started.
16 if (! -f $DAEMON) { exit 0; }
19 warn "Please edit /etc/init.d/tinc before attempting to start tinc.\n";
23 ##############################################################################
24 # vpn_load () Loads VPN configuration
26 # $_[0] ... VPN to load
31 $CFG="$TCONF/$_[0]/tinc.conf";
32 open($CFG, "< $CFG") || die "tinc: $CFG does not exist";
36 if( /^[ ]*TapDevice[ =]+([^ \#]+)/i ) {
39 $DEV =~ s/^.*\/([^\/0-9]+)([0-9]+)$/$1$2/;
41 } elsif ( /^[ ]*(MyOwnVPNIP|MyVirtualIP)[ =]+([^ \#]+)/i ) {
47 die "tinc: There must be a TapDevice";
50 die "tinc: TapDevice should be of the form /dev/tapN";
53 die "tinc: MyVirtualIP required";
56 die "tinc: No argument to MyVirtualIP/MyOwnVPNIP";
59 $ADR =~ s/^([^\/]+)\/.*$/$1/;
61 $LEN =~ s/^.*\/([^\/]+)$/$1/;
62 if($ADR eq "" || $LEN eq "") {
63 die "tinc: Badly formed MyVirtualIP/MyOwnVPNIP";
65 @addr = split(/\./, $ADR);
67 $ADR = pack('C4', @addr);
68 $MSK = pack('N4', -1 << (32 - $LEN));
69 $BRD = join(".", unpack('C4', $ADR | ~$MSK));
70 # $NET = join(".", unpack('C4', $ADR & $MSK));
71 $MAC = "fe:fd:" . join(":", map { sprintf "%02x", $_ } unpack('C4', $ADR));
72 $ADR = join(".", unpack('C4', $ADR));
73 $MSK = join(".", unpack('C4', $MSK));
75 # print "$DEV $VPN $NUM $LEN @addr $MAC $MASK $BRD $NET\n";
81 ##############################################################################
82 # vpn_start () starts specified VPN
84 # $_[0] ... VPN to start
87 vpn_load($_[0]) || die "tinc: could not vpn_load $_[0]";
89 system("insmod ethertap -s --name=\"ethertap$NUM\" unit=\"$NUM\" >/dev/null");
90 system("ifconfig $DEV hw ether $MAC");
91 system("ifconfig $DEV $ADR netmask $MSK broadcast $BRD -arp");
92 system("start-stop-daemon --start --quiet --pidfile /var/run/$NAME.$_[0].pid --exec $DAEMON -- -n $_[0] $EXTRA");
98 ##############################################################################
99 # vpn_stop () Stops specified VPN
101 # $_[0] ... VPN to stop
104 vpn_load($_[0]) || return 1;
106 system("start-stop-daemon --stop --quiet --pidfile /var/run/$NAME.$_[0].pid --exec $DAEMON -- -n $_[0] $EXTRA -k");
108 system("ifconfig $DEV down");
109 system("rmmod ethertap$NUM -s");
113 if(!defined($ARGV[0])) {
114 die "Usage: /etc/init.d/$NAME {start|stop|restart|force-reload}\n";
117 if($ARGV[0] eq "start") {
118 print "Starting $DESC:";
119 foreach $n (split(" ", $NETS)) {
124 } elsif ($ARGV[0] eq "stop") {
125 print "Stopping $DESC:";
126 foreach $n (split(" ", $NETS)) {
131 } elsif ($ARGV[0] eq "restart" || $ARGV[0] eq "force-reload") {
132 print "Stopping $DESC:";
133 foreach $n (split(" ", $NETS)) {
138 print "Starting $DESC:";
139 foreach $n (split(" ", $NETS)) {
145 die "Usage: /etc/init.d/$NAME {start|stop|restart|force-reload}\n";