siproxd: use UCI callback processing, reduce code size
[feed/telephony.git] / net / siproxd / files / siproxd.init
1 #!/bin/sh /etc/rc.common
2 # Copyright (C) 2008 Alina Friedrichsen
3 # Copyright (C) 2011 OpenWrt.org
4
5 START=50
6
7 SERVICE_USE_PID=1
8
9 siproxd_bin="/usr/sbin/siproxd"
10 siproxd_conf_dir="/var/etc/siproxd"
11 siproxd_conf_prefix="$siproxd_conf_dir/siproxd-"
12 siproxd_registration_dir="/var/lib/siproxd"
13 siproxd_registration_prefix="$siproxd_registration_dir/siproxd-"
14 siproxd_pid_dir="/var/run/siproxd"
15
16
17 # Check if a UCI option is set, or else apply a provided default.
18
19 default_conf() {
20 local opt="$1"
21 local default="$2"
22 local val
23
24 config_get "$opt" "$sec" "$opt"
25 eval "val=\"\${$opt}\""
26
27 [ -z "$val" ] || return 0
28 [ -n "$default" ] || return 0
29 echo "$opt" = "$default" >> "$siproxd_conf_prefix$sec.conf"
30 }
31
32 # Use user-friendly network names (e.g. "wan", "lan") from options
33 # 'interface_inbound' and 'interface_outbound', but use standard siproxd
34 # parameters 'if_inbound' and 'if_outbound' if explicitly set.
35
36 setup_networks() {
37 local sec="$1"
38 local _int_inbound
39 local _int_outbound
40 local _dev_inbound
41 local _dev_outbound
42
43 config_get _int_inbound "$sec" interface_inbound
44 config_get _int_outbound "$sec" interface_outbound
45
46 network_get_physdev _dev_inbound $_int_inbound
47 network_get_physdev _dev_outbound $_int_outbound
48
49 default_conf if_inbound $_dev_inbound
50 default_conf if_outbound $_dev_outbound
51 }
52
53 # Apply default values to key options if unset in user's UCI config.
54
55 apply_defaults() {
56 local sec="$1"
57
58 default_conf sip_listen_port 5060
59 default_conf daemonize 1
60 default_conf silence_log 1
61 default_conf user nobody
62 default_conf registration_file "$siproxd_registration_prefix$sec.reg"
63 default_conf autosave_registrations 300
64 default_conf pid_file "$siproxd_pid_dir/siproxd-$sec.pid"
65 default_conf rtp_proxy_enable 1
66 default_conf rtp_port_low 7070
67 default_conf rtp_port_high 7089
68 default_conf rtp_timeout 300
69 default_conf rtp_dscp 46
70 default_conf sip_dscp 0
71 default_conf rtp_input_dejitter 0
72 default_conf rtp_output_dejitter 0
73 default_conf tcp_timeout 600
74 default_conf tcp_connect_timeout 500
75 default_conf tcp_keepalive 20
76 default_conf default_expires 600
77 default_conf debug_level 0x00000000
78 default_conf debug_port 0
79 default_conf ua_string Siproxd-UA
80 default_conf use_rport 0
81 default_conf plugindir "/usr/lib/siproxd/"
82 }
83
84 # Setup callbacks for parsing siproxd sections, options, and lists.
85 # This avoids hardcoding all supported siproxd configuration parameters.
86
87 siproxd_cb() {
88 config_cb() {
89 local _int_inbound
90 local _int_outbound
91 local _dev_inbound
92 local _dev_outbound
93
94 case "$1" in
95 # Initialize section processing and save section name.
96 "siproxd")
97 sec="$2"
98 if [ -f "$siproxd_conf_prefix$sec.conf" ]; then
99 rm "$siproxd_conf_prefix$sec.conf"
100 fi
101 echo "# auto-generated config file from /etc/config/siproxd" > \
102 "$siproxd_conf_prefix$sec.conf"
103 ;;
104 # Parse OpenWRT interface names (e.g. "wan") and apply defaults,
105 # using saved section name.
106 "")
107 local chrootjail
108 local pid_file
109
110 setup_networks "$sec"
111 apply_defaults "$sec"
112
113 config_get chrootjail "$sec" chrootjail
114 if [ -n "$chrootjail" ]; then
115 if [ ! -d "$chrootjail" ]; then
116 mkdir -p "$chrootjail"
117 chmod 0755 "$chrootjail"
118 fi
119 fi
120
121 config_get pid_file "$sec" pid_file
122 SERVICE_PID_FILE="$pid_file" service_start \
123 $siproxd_bin --config "$siproxd_conf_prefix$sec.conf"
124 ;;
125 esac
126 return 0
127 }
128
129 option_cb() {
130 # These 2 OpenWRT-specific options are handled in post-processing.
131 case "$1" in
132 "interface_inbound"|"interface_outbound") return 0 ;;
133 esac
134 # Other options match siproxd docs, so write directly to config.
135 [ -n "$2" ] && echo "$1" = "$2" >> "$siproxd_conf_prefix$sec.conf"
136 return 0
137 }
138
139 list_cb() {
140 # All list items match siproxd docs, so write directly to config.
141 [ -n "$2" ] && echo "$1" = "$2" >> "$siproxd_conf_prefix$sec.conf"
142 return 0
143 }
144 }
145
146 stop_instance() {
147 local sec="$1"
148
149 config_get pid_file "$sec" pid_file "$siproxd_pid_dir/siproxd-$sec.pid"
150
151 SERVICE_PID_FILE="$pid_file" \
152 service_stop $siproxd_bin
153 }
154
155 start() {
156 mkdir -p "$siproxd_conf_dir"
157 chmod 755 "$siproxd_conf_dir"
158
159 mkdir -p "$siproxd_registration_dir"
160 chmod 700 "$siproxd_registration_dir"
161 chown nobody:nogroup "$siproxd_registration_dir"
162
163 mkdir -p "$siproxd_pid_dir"
164 chmod 700 "$siproxd_pid_dir"
165 chown nobody:nogroup "$siproxd_pid_dir"
166
167 . /lib/functions/network.sh
168 siproxd_cb
169 config_load 'siproxd'
170 }
171
172 stop() {
173 config_load 'siproxd'
174 config_foreach stop_instance 'siproxd'
175 }