3532dbd652c56c83cc3c94eb18f5c3a279158bca
[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 DOMAIN=""
7
8 dhcp_calc() {
9 local ip="$1"
10 local res=0
11
12 while [ -n "$ip" ]; do
13 part="${ip%%.*}"
14 res="$(($res * 256))"
15 res="$(($res + $part))"
16 [ "${ip%.*}" != "$ip" ] && ip="${ip#*.}" || ip=
17 done
18 echo "$res"
19 }
20
21 append_bool() {
22 local section="$1"
23 local option="$2"
24 local value="$3"
25 local _loctmp
26 config_get_bool _loctmp "$section" "$option"
27 [ "$_loctmp" -gt 0 ] && append args "$value"
28 }
29
30 append_parm() {
31 local section="$1"
32 local option="$2"
33 local switch="$3"
34 local _loctmp
35 config_get _loctmp "$section" "$option"
36 [ -z "$_loctmp" ] && return 0
37 append args "$switch $_loctmp"
38 }
39
40 append_server() {
41 append args "-S $1"
42 }
43
44 append_interface() {
45 append args "-i $1"
46 }
47
48 append_notinterface() {
49 append args "-I $1"
50 }
51
52 append_addnhosts() {
53 append args "-H $1"
54 }
55
56 dnsmasq() {
57 local cfg="$1"
58 append_bool "$cfg" authoritative "-K"
59 append_bool "$cfg" nodaemon "-d"
60 append_bool "$cfg" domainneeded "-D"
61 append_bool "$cfg" filterwin2k "-f"
62 append_bool "$cfg" nohosts "-h"
63 append_bool "$cfg" nonegcache "-N"
64 append_bool "$cfg" strictorder "-o"
65 append_bool "$cfg" logqueries "-q"
66 append_bool "$cfg" noresolv "-R"
67 append_bool "$cfg" localise_queries "-y"
68 append_bool "$cfg" readethers "-Z"
69 append_bool "$cfg" dbus "-1"
70 append_bool "$cfg" boguspriv "-b"
71 append_bool "$cfg" expandhosts "-E"
72 append_bool "$cfg" enable_tftp "--enable-tftp"
73 append_bool "$cfg" nonwildcard "-z"
74
75 append_parm "$cfg" dnsforwardmax "-0"
76 append_parm "$cfg" port "-p"
77 append_parm "$cfg" ednspacket_max "-P"
78 append_parm "$cfg" dhcpleasemax "-X"
79 append_parm "$cfg" "queryport" "-Q"
80 append_parm "$cfg" "domain" "-s"
81 append_parm "$cfg" "local" "-S"
82 config_list_foreach "$cfg" "server" append_server
83 config_list_foreach "$cfg" "interface" append_interface
84 config_list_foreach "$cfg" "notinterface" append_notinterface
85 config_list_foreach "$cfg" "addnhosts" append_addnhosts
86 append_parm "$cfg" "leasefile" "-l"
87 append_parm "$cfg" "resolvfile" "-r"
88 append_parm "$cfg" "tftp_root" "--tftp-root"
89 append_parm "$cfg" "dhcp_boot" "--dhcp-boot"
90
91 config_get DOMAIN "$cfg" domain
92
93 config_get leasefile $cfg leasefile
94 [ -e "$leasefile" ] || touch "$leasefile"
95 config_get_bool cachelocal "$cfg" cachelocal 1
96
97 config_get hostsfile "$cfg" dhcphostsfile
98 [ -e "$hostsfile" ] && append args "--dhcp-hostsfile=$hostsfile"
99 }
100
101 dhcp_subscrid_add() {
102 local cfg="$1"
103
104 config_get name "$cfg" name
105 [ -n "$name" ] || return 0
106
107 config_get subscriberid "$cfg" subscriberid
108 [ -n "$subscriberid" ] || return 0
109
110 append args "--dhcp-subscrid=$name,$subscriberid"
111
112 dhcp_option_add "$cfg" "$name"
113 }
114
115 dhcp_remoteid_add() {
116 local cfg="$1"
117
118 config_get name "$cfg" name
119 [ -n "$name" ] || return 0
120
121 config_get remoteid "$cfg" remoteid
122 [ -n "$remoteid" ] || return 0
123
124 append args "--dhcp-remoteid=$name,$remoteid"
125
126 dhcp_option_add "$cfg" "$name"
127 }
128
129 dhcp_circuitid_add() {
130 local cfg="$1"
131
132 config_get name "$cfg" name
133 [ -n "$name" ] || return 0
134
135 config_get circuitid "$cfg" circuitid
136 [ -n "$circuitid" ] || return 0
137
138 append args "--dhcp-circuitid=$name,$circuitid"
139
140 dhcp_option_add "$cfg" "$name"
141 }
142
143 dhcp_userclass_add() {
144 local cfg="$1"
145
146 config_get name "$cfg" name
147 [ -n "$name" ] || return 0
148
149 config_get userclass "$cfg" userclass
150 [ -n "$userclass" ] || return 0
151
152 append args "--dhcp-userclass=$name,$userclass"
153
154 dhcp_option_add "$cfg" "$name"
155 }
156
157 dhcp_vendorclass_add() {
158 local cfg="$1"
159
160 config_get name "$cfg" name
161 [ -n "$name" ] || return 0
162
163 config_get vendorclass "$cfg" vendorclass
164 [ -n "$vendorclass" ] || return 0
165
166 append args "--dhcp-vendorclass=$name,$vendorclass"
167
168 dhcp_option_add "$cfg" "$name"
169 }
170
171 dhcp_host_add() {
172 local cfg="$1"
173
174 config_get name "$cfg" name
175 [ -n "$name" ] && dhcp_option_add "$cfg" "$name"
176
177 config_get mac "$cfg" mac
178 [ -n "$mac" ] || return 0
179
180 config_get ip "$cfg" ip
181 [ -n "$ip" ] || return 0
182
183 append args "--dhcp-host=$mac,${name:+net:$name,}$ip"
184 }
185
186 dhcp_mac_add() {
187 local cfg="$1"
188
189 config_get name "$cfg" name
190 [ -n "$name" ] || return 0
191
192 config_get mac "$cfg" mac
193 [ -n "$mac" ] || return 0
194
195 append args "--dhcp-mac=$name,$mac"
196
197 dhcp_option_add "$cfg" "$name"
198 }
199
200 dhcp_boot_add() {
201 local cfg="$1"
202
203 config_get name "$cfg" name
204
205 config_get filename "$cfg" filename
206 [ -n "$filename" ] || return 0
207
208 config_get servername "$cfg" servername
209 [ -n "$servername" ] || return 0
210
211 config_get serveraddress "$cfg" serveraddress
212 [ -n "$serveraddress" ] || return 0
213
214 append args "--dhcp-boot=${name:+net:$name,}$filename,$servername,$serveraddress"
215
216 dhcp_option_add "$cfg" "$name"
217 }
218
219
220 dhcp_add() {
221 local cfg="$1"
222 config_get net "$cfg" interface
223 [ -n "$net" ] || return 0
224
225 config_get name "$cfg" name
226 [ -n "$name" ] || name="$net"
227
228 config_get ifname "$net" ifname
229 [ -n "$ifname" ] || return 0
230
231 config_get dnsserver "$net" dns
232 [ "$cachelocal" = "0" -a -n "$dnsserver" ] && {
233 DNS_SERVERS="$DNS_SERVERS $dnsserver"
234 }
235
236 append_bool "$cfg" ignore "-2 $ifname" && return 0
237
238 config_get proto "$net" proto
239 [ static = "$proto" ] || return 0
240
241 config_get ipaddr "$net" ipaddr
242 config_get netmask "$cfg" netmask
243 [ -n "$netmask" ] || config_get netmask "$net" netmask
244
245 #check for an already active dhcp server on the interface, unless 'force' is set
246 config_get_bool force "$cfg" force 0
247 [ "$force" -gt 0 ] || {
248 udhcpc -n -q -s /bin/true -t 1 -i $ifname >&- && return 0
249 }
250
251 config_get start "$cfg" start
252 config_get limit "$cfg" limit
253 config_get leasetime "$cfg" leasetime
254 config_get options "$cfg" options
255 config_get_bool dynamicdhcp "$cfg" dynamicdhcp 1
256
257 leasetime="${leasetime:-12h}"
258 start="$(dhcp_calc "${start:-100}")"
259 limit="$((${limit:-150} + 1))"
260 eval "$(ipcalc.sh $ipaddr $netmask $start $limit)"
261 if [ "$dynamicdhcp" = "0" ]; then END="static"; fi
262 append args "--dhcp-range=$name,$START,$END,$NETMASK,$leasetime${options:+ $options}"
263
264 dhcp_option_add "$cfg" "$name"
265 }
266
267 dhcp_option_add() {
268 local cfg="$1"
269 local name="$2"
270
271 config_get dhcp_option "$cfg" dhcp_option
272 for o in $dhcp_option; do
273 append args "-O $name","$o"
274 done
275
276 }
277
278 dhcp_domain_add() {
279 local cfg="$1"
280
281 config_get name "$cfg" name
282 [ -n "$name" ] || return 0
283
284 config_get ip "$cfg" ip
285 [ -n "$ip" ] || return 0
286
287 local oIFS="$IFS"; IFS="."; set -- $ip; IFS="$oIFS"
288 append args "-A /$name${DOMAIN:+.$DOMAIN}/$ip"
289 append args "--ptr-record=$4.$3.$2.$1.in-addr.arpa,$name${DOMAIN:+.$DOMAIN}"
290 }
291
292 start() {
293 include /lib/network
294 scan_interfaces
295 config_load dhcp
296
297 args=""
298 config_foreach dnsmasq dnsmasq
299 config_foreach dhcp_host_add host
300 config_foreach dhcp_boot_add boot
301 config_foreach dhcp_mac_add mac
302 config_foreach dhcp_vendorclass_add vendorclass
303 config_foreach dhcp_userclass_add userclass
304 config_foreach dhcp_circuitid_add circuitid
305 config_foreach dhcp_remoteid_add remoteid
306 config_foreach dhcp_subscrid_add subscrid
307 config_foreach dhcp_domain_add domain
308 config_foreach dhcp_add dhcp
309
310 /usr/sbin/dnsmasq $args && {
311 rm -f /tmp/resolv.conf
312 DNS_SERVERS="$DNS_SERVERS 127.0.0.1"
313 for DNS_SERVER in $DNS_SERVERS ; do
314 echo "nameserver $DNS_SERVER" >> /tmp/resolv.conf
315 done
316 }
317 }
318
319 stop() {
320 [ -f /tmp/resolv.conf ] && {
321 rm -f /tmp/resolv.conf
322 ln -s /tmp/resolv.conf.auto /tmp/resolv.conf
323 }
324 killall dnsmasq
325 return 0
326 }
This page took 0.064958 seconds and 3 git commands to generate.