include fixes from #2429 Thanks Lubos Stanek
[oweals/openwrt.git] / package / dnsmasq / files / dnsmasq.init
1 #!/bin/sh /etc/rc.common
2 # Copyright (C) 2007 OpenWrt.org
3
4 START=60
5 DNS_SERVERS=""
6
7 dhcp_calc() {
8         local ip="$1"
9         local res=0
10
11         while [ -n "$ip" ]; do
12                 part="${ip%%.*}"
13                 res="$(($res * 256))"
14                 res="$(($res + $part))"
15                 [ "${ip%.*}" != "$ip" ] && ip="${ip#*.}" || ip=
16         done
17         echo "$res"
18 }
19
20 append_bool() {
21         local section="$1"
22         local option="$2"
23         local value="$3"
24         local _loctmp
25         config_get_bool _loctmp "$section" "$option"
26         [ "$_loctmp" -gt 0 ] && append args "$3"
27 }
28
29 append_parm() {
30         local section="$1"
31         local option="$2"
32         local switch="$3"
33         local _loctmp
34         config_get _loctmp "$section" "$option"
35         [ -z "$_loctmp" ] && return 0
36         append args "$switch $_loctmp"
37 }
38
39 dnsmasq() {
40         local cfg="$1"
41         append_bool "$cfg" authoritative "-K"
42         append_bool "$cfg" nodaemon "-d"
43         append_bool "$cfg" domainneeded "-D"
44         append_bool "$cfg" filterwin2k "-f"
45         append_bool "$cfg" nohosts "-h"
46         append_bool "$cfg" nonegcache "-N"
47         append_bool "$cfg" strictorder "-o"
48         append_bool "$cfg" logqueries "-q"
49         append_bool "$cfg" noresolv "-R"
50         append_bool "$cfg" localise_queries "-y"
51         append_bool "$cfg" readethers "-Z"
52         append_bool "$cfg" dbus "-l"
53
54         append_parm "$cfg" dnsforwardmax "-0"
55         append_parm "$cfg" port "-p"
56         append_parm "$cfg" ednspacket_max "-P"
57         append_parm "$cfg" dhcpleasemax "-X"
58
59         config_get addnhosts "$cfg" addnhosts
60         config_get interface "$cfg" interface
61         config_get exceptinterface "$cfg" exceptinterface
62         config_get queryport "$cfg" queryport
63         config_get domain "$cfg" domain
64 }
65
66 dhcp_subscrid_add() {
67         local cfg="$1"
68
69         config_get name "$cfg" name
70         [ -n "$name" ] || return 0
71
72         config_get subscriberid "$cfg" subscriberid
73         [ -n "$subscriberid" ] || return 0
74
75         append args "--dhcp-subscrid=$name,$subscriberid"
76
77         dhcp_option_add "$cfg" "$name"
78 }
79
80 dhcp_remoteid_add() {
81         local cfg="$1"
82
83         config_get name "$cfg" name
84         [ -n "$name" ] || return 0
85
86         config_get remoteid "$cfg" remoteid
87         [ -n "$remoteid" ] || return 0
88
89         append args "--dhcp-remoteid=$name,$remoteid"
90
91         dhcp_option_add "$cfg" "$name"
92 }
93
94 dhcp_circuitid_add() {
95         local cfg="$1"
96
97         config_get name "$cfg" name
98         [ -n "$name" ] || return 0
99
100         config_get circuitid "$cfg" circuitid
101         [ -n "$circuitid" ] || return 0
102
103         append args "--dhcp-circuitid=$name,$circuitid"
104
105         dhcp_option_add "$cfg" "$name"
106 }
107
108 dhcp_userclass_add() {
109         local cfg="$1"
110
111         config_get name "$cfg" name
112         [ -n "$name" ] || return 0
113
114         config_get userclass "$cfg" userclass
115         [ -n "$userclass" ] || return 0
116
117         append args "--dhcp-userclass=$name,$userclass"
118
119         dhcp_option_add "$cfg" "$name"
120 }
121
122 dhcp_vendorclass_add() {
123         local cfg="$1"
124
125         config_get name "$cfg" name
126         [ -n "$name" ] || return 0
127
128         config_get vendorclass "$cfg" vendorclass
129         [ -n "$vendorclass" ] || return 0
130
131         append args "--dhcp-vendorclass=$name,$vendorclass"
132
133         dhcp_option_add "$cfg" "$name"
134 }
135
136 dhcp_host_add() {
137         local cfg="$1"
138
139         config_get name "$cfg" name
140         [ -n "$name" ] || return 0
141
142         config_get mac "$cfg" mac
143         [ -n "$mac" ] || return 0
144
145
146         append args "--dhcp-host=$mac,$ip"
147
148         dhcp_option_add "$cfg" "$name"
149 }
150
151 dhcp_mac_add() {
152         local cfg="$1"
153
154         config_get name "$cfg" name
155         [ -n "$name" ] || return 0
156
157         config_get mac "$cfg" mac
158         [ -n "$mac" ] || return 0
159
160         append args "--dhcp-mac=$name,$mac"
161
162         dhcp_option_add "$cfg" "$name"
163 }
164
165 dhcp_add() {
166         local cfg="$1"
167         config_get net "$cfg" interface
168         [ -n "$net" ] || return 0
169
170         config_get name "$cfg" name
171         [ -n "$name" ] || name="$net"
172
173         config_get ifname "$net" ifname
174         [ -n "$ifname" ] || return 0
175
176         config_get dnsserver "$net" dns
177         [ -n "$dnsserver" ] && {
178                 DNS_SERVERS="$DNS_SERVERS $dnsserver"
179         }
180
181         append_bool "$cfg" ignore "-I $ifname"
182
183         config_get proto "$net" proto
184         [ static = "$proto" ] || return 0
185
186         config_get ipaddr "$net" ipaddr
187         config_get netmask "$net" netmask
188
189         #check for an already active dhcp server on the interface, unless 'force' is set
190         config_get_bool force "$cfg" force 0
191         [ "$force" -gt 0 ] || {
192                 udhcpc -n -q -R -s /bin/true -t 1 -i $ifname >&- && return 0
193         }
194
195         config_get start "$cfg" start
196         config_get limit "$cfg" limit
197         config_get leasetime "$cfg" leasetime
198         config_get options "$cfg" options
199
200         leasetime="${leasetime:-12h}"
201         start="$(dhcp_calc "${start:-100}")"
202         limit="$((${limit:-150} + 1))"
203         eval "$(ipcalc.sh $ipaddr $netmask $start $limit)"
204         append args "--dhcp-range=$name,$START,$END,$NETMASK,$leasetime${options:+ $options}"
205
206         dhcp_option_add "$cfg" "$name"
207 }
208
209 dhcp_option_add() {
210         local cfg="$1"
211         local name="$2"
212
213         for count in $(seq 0 100); do
214                 eval current_value=\$CONFIG_"$cfg"_dhcp"$count"
215                 if [ -z "$current_value" ]; then
216                         let "count-=1"
217                         break
218                 fi
219                 append args "-O $name","$current_value"
220         done
221
222 }
223
224 start() {
225         include /lib/network
226         scan_interfaces
227         config_load /var/state/network
228         config_load dhcp
229
230         args=""
231         config_foreach dnsmasq dnsmasq
232         config_foreach dhcp_host_add host
233         config_foreach dhcp_mac_add mac
234         config_foreach dhcp_vendorclass_add vendorclass
235         config_foreach dhcp_userclass_add userclass
236         config_foreach dhcp_circuitid_add circuitid
237         config_foreach dhcp_remoteid_add remoteid
238         config_foreach dhcp_subscrid_add subscrid
239         config_foreach dhcp_add dhcp
240
241         /usr/sbin/dnsmasq $args && {
242                 rm -f /tmp/resolv.conf
243                 DNS_SERVERS="$DNS_SERVERS 127.0.0.1"
244                 for DNS_SERVER in $DNS_SERVERS ; do
245                         echo "nameserver $DNS_SERVER" >> /tmp/resolv.conf
246                 done
247         }
248 }
249
250 stop() {
251         killall dnsmasq
252 }