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