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