5bcf8bc99eb5ac1823e63600dedb466ff057ccce
[openwrt/svn-archive/archive.git] / net / miniupnpd / files / miniupnpd.init
1 #!/bin/sh /etc/rc.common
2 START=95
3
4 start() {
5 config_load "upnpd"
6 local extiface intiface upload download logging secure
7
8 config_get extiface config external_iface
9 config_get intiface config internal_iface
10 config_get upload config upload
11 config_get download config download
12 config_get_bool logging config log_output 0
13 config_get_bool secure config secure_mode 0
14
15 include /lib/network
16 scan_interfaces
17
18 local ifname
19 config_get ifname ${extiface:-wan} ifname
20
21 if [ -n "$ifname" ]; then
22 local args="-i $ifname"
23 local iface
24
25 for iface in ${intiface:-lan}; do
26 local ipaddr
27 config_get ipaddr "$iface" ipaddr
28 [ -n "$ipaddr" ] && append args "-a $ipaddr"
29 done
30
31 append args "-p 5000 -U"
32
33 [ -n "$upload" -a -n "$download" ] && \
34 append args "-B $(($download * 1024 * 8)) $(($upload * 1024 * 8))"
35
36 [ "$secure" -gt 0 ] && \
37 append args "-S"
38
39 if [ "$logging" = "1" ]; then
40 eval start-stop-daemon -S -x miniupnpd -- $args -d | logger -t miniupnpd &
41 else
42 eval start-stop-daemon -S -x miniupnpd -- $args 2>/dev/null
43 fi
44
45 # start firewall
46 local zone
47 config_load firewall
48 config_get zone core "${extiface:-wan}_zone"
49 [ -n "$zone" ] && \
50 ACTION="add" ZONE="$zone" INTERFACE="${extiface:-wan}" DEVICE="$ifname" \
51 . /etc/hotplug.d/firewall/50-miniupnpd
52 else
53 logger -t "upnp daemon" "external interface not found, not starting"
54 fi
55 }
56
57 clear_rule() {
58 local state="$1"
59 local ifname ipaddr
60
61 config_get ifname "$state" ifname
62 config_get ipaddr "$state" ipaddr
63
64 [ -n "$ifname" ] && [ -n "$ipaddr" ] && {
65 iptables -t nat -D prerouting_rule -i $ifname -d $ipaddr -j MINIUPNPD
66 iptables -t filter -D forwarding_rule -i $ifname ! -o $ifname -j MINIUPNPD
67 uci_revert_state upnpd "$state"
68 unset "CONFIG_${state}_ifname"
69 unset "CONFIG_${state}_ipaddr"
70 }
71 }
72
73 stop() {
74 start-stop-daemon -K -q -x miniupnpd -p /var/run/miniupnpd.pid
75 rm -f /var/run/miniupnpd.pid
76
77 logger -t "upnp" "removing firewall rules"
78
79 config_load upnpd
80 config_foreach clear_rule firewall
81
82 iptables -t nat -F MINIUPNPD 2>/dev/null
83 iptables -t nat -X MINIUPNPD 2>/dev/null
84 iptables -t filter -F MINIUPNPD 2>/dev/null
85 iptables -t filter -X MINIUPNPD 2>/dev/null
86 }
87