[packages_10.03.2] quicktun: merge r29168
[10.03/packages.git] / net / quicktun / files / quicktun.init
1 #!/bin/sh /etc/rc.common
2 # Copyright (C) 2010-2011 OpenWrt.org
3
4 # Partly taken the OpenVPN init script (Copyright (C) 2008 Jo-Philipp Wich)
5 # This is free software, licensed under the GNU General Public License v2.
6 # See /LICENSE for more information.
7
8 START=95
9
10 SERVICE_DAEMONIZE=1
11 SERVICE_WRITE_PID=1
12
13 EXTRA_COMMANDS="up down"
14
15 LIST_SEP="
16 "
17
18 append_opt() {
19         local p="$1"; local v="$2"; local p_uc
20         
21         p_uc=$(echo "$p" | tr '[a-z]' '[A-Z]')
22         OPTS="$OPTS \"$p_uc=$v\""
23 }
24
25 append_opts() {
26         local p; local v; local s="$1"; shift
27         for p in $*; do
28                 config_get v "$s" "$p"
29                 [ -n "$v" ] && append_opt "$p" "$v"
30         done
31 }
32
33 section_enabled() {
34         config_get_bool enabled "$1" 'enabled' 0
35         [ $enabled -gt 0 ]
36 }
37
38 error() {
39         echo "${initscript}:" "$@" 1>&2
40 }
41
42 start_instance() {
43         local s="$1"
44
45         section_enabled "$s" || return 1
46
47         SERVICE_PID_FILE="/var/run/quicktun-$s.pid"
48         OPTS=""
49
50         config_get interface "$s" interface
51         if [ -z "$interface" ]; then
52                 error "$s: interface '$interface' is not set"
53                 return 1
54         fi
55
56         if ifconfig "$interface" &>/dev/null; then
57                 error "$s: interface '$interface' is already in use"
58                 return 1
59         fi
60
61         append_opts "$s" interface local_address local_port remote_address remote_port \
62                 protocol private_key public_key time_window
63
64         config_get_bool tun_mode "$s" tun_mode 0
65         [ "$tun_mode" == 1 ] && append_opt tun_mode 1
66
67         config_get_bool remote_float "$s" remote_float 0
68         [ "$remote_float" == 1 ] && append_opt remote_float 1
69
70         eval env $OPTS service_start /usr/sbin/quicktun
71
72         while ! ifconfig "$interface" >/dev/null 2>&1; do
73                 if ! service_check /usr/sbin/quicktun; then
74                         error "$s: startup failed"
75                         return 1
76                 fi
77
78                 sleep 1
79         done
80
81         config_get up "$s" up
82         [ -n "$up" ] && sh -c "$up" - "$interface"
83 }
84
85 stop_instance() {
86         local s="$1"
87
88         section_enabled "$s" || return 1
89
90         SERVICE_PID_FILE="/var/run/quicktun-$s.pid"
91
92         config_get interface "$s" interface
93         if [ -z "$interface" ]; then
94                 error "$s: interface '$interface' is not set"
95                 return 1
96         fi
97
98         if ! ifconfig "$interface" &>/dev/null; then
99                 error "$s: interface '$interface' does not exist"
100                 return 1
101         fi
102
103         config_get down "$s" down
104         [ -n "$down" ] && sh -c "$down" - "$interface"
105
106         service_stop /usr/sbin/quicktun
107 }
108
109 start() {
110         config_load 'quicktun'
111         config_foreach start_instance 'quicktun'
112 }
113
114 stop() {
115         config_load 'quicktun'
116         config_foreach stop_instance 'quicktun'
117 }
118
119 up() {
120         local exists
121         local instance
122         config_load 'quicktun'
123         for instance in "$@"; do
124                 config_get exists "$instance" 'TYPE'
125                 if [ "$exists" == "quicktun" ]; then
126                         start_instance "$instance"
127                 fi
128         done
129 }
130
131 down() {
132         local exists
133         local instance
134         config_load 'quicktun'
135         for instance in "$@"; do
136                 config_get exists "$instance" 'TYPE'
137                 if [ "$exists" == "quicktun" ]; then
138                         stop_instance "$instance"
139                 fi
140         done
141 }