cd5697c7729b9fd93d9963f1071a0b73ef89f9a0
[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 _tmp
25         config_get_bool _tmp "$section" "$option"
26         [ "$_tmp" -gt 0 ] && append args "$3"
27 }
28
29 dnsmasq() {
30         local cfg="$1"
31         append_bool "$cfg" authoritative "-K"
32         append_bool "$cfg" nodaemon "-d"
33         append_bool "$cfg" domainneeded "-D"
34         append_bool "$cfg" filterwin2k "-f"
35         append_bool "$cfg" nohosts "-h"
36         append_bool "$cfg" nonegcache "-N"
37         append_bool "$cfg" strictorder "-o"
38         append_bool "$cfg" logqueries "-q"
39         append_bool "$cfg" noresolv "-R"
40         append_bool "$cfg" localise_queries "-y"
41         append_bool "$cfg" readethers "-Z"
42         append_bool "$cfg" dbus "-l"
43         
44         config_get dnsforwardmax "$cfg" dnsforwardmax
45         dnsforwardmax="${dnsforwardmax:-150}"
46         append args "-0 $dnsforwardmax"
47         
48         config_get port "$cfg" port
49         port="${port:-53}"
50         append args "-p $port"
51         
52         config_get ednspacket_max "$cfg" ednspacket_max
53         ednspacket_max="${ednspacket_max:-1280}"
54         append args "-P $ednspacket_max"
55         
56         config_get dhcpleasemax "$cfg" dhcpleasemax
57         dhcpleasemax="${dhcpleasemax:-150}"
58         append args "-X $dhcpleasemax"
59         
60         config_get addnhosts "$cfg" addnhosts
61         config_get interface "$cfg" interface
62         config_get exceptinterface "$cfg" exceptinterface
63         config_get queryport "$cfg" queryport
64         config_get domain "$cfg" domain
65 }
66
67 dhcp_subscrid_add() {
68         local cfg="$1"
69         
70         config_get name "$cfg" name
71         [ -n "$name" ] || return 0
72         
73         config_get subscriberid "$cfg" subscriberid
74         [ -n "$subscriberid" ] || return 0
75         
76         append args "--dhcp-subscrid=$name,$subscriberid"
77         
78         dhcp_option_add "$cfg" "$name"
79 }
80
81 dhcp_remoteid_add() {
82         local cfg="$1"
83
84         config_get name "$cfg" name
85         [ -n "$name" ] || return 0
86         
87         config_get remoteid "$cfg" remoteid
88         [ -n "$remoteid" ] || return 0
89         
90         append args "--dhcp-remoteid=$name,$remoteid"
91         
92         dhcp_option_add "$cfg" "$name"
93 }
94
95 dhcp_circuitid_add() {
96         local cfg="$1"
97         
98         config_get name "$cfg" name
99         [ -n "$name" ] || return 0
100         
101         config_get circuitid "$cfg" circuitid
102         [ -n "$circuitid" ] || return 0
103         
104         append args "--dhcp-circuitid=$name,$circuitid"
105         
106         dhcp_option_add "$cfg" "$name"
107 }
108
109 dhcp_userclass_add() {
110         local cfg="$1"
111         
112         config_get name "$cfg" name
113         [ -n "$name" ] || return 0
114         
115         config_get userclass "$cfg" userclass
116         [ -n "$userclass" ] || return 0
117         
118         append args "--dhcp-userclass=$name,$userclass"
119         
120         dhcp_option_add "$cfg" "$name"
121 }
122
123 dhcp_vendorclass_add() {
124         local cfg="$1"
125         
126         config_get name "$cfg" name
127         [ -n "$name" ] || return 0
128         
129         config_get vendorclass "$cfg" vendorclass
130         [ -n "$vendorclass" ] || return 0
131         
132         append args "--dhcp-vendorclass=$name,$vendorclass"
133         
134         dhcp_option_add "$cfg" "$name"
135 }
136
137 dhcp_host_add() {
138         local cfg="$1"
139         
140         config_get name "$cfg" name
141         [ -n "$name" ] || return 0
142         
143         config_get mac "$cfg" mac
144         [ -n "$mac" ] || return 0
145
146         
147         append args "--dhcp-host=$mac,$ip"
148         
149         dhcp_option_add "$cfg" "$name"
150 }
151
152 dhcp_mac_add() {
153         local cfg="$1"
154         
155         config_get name "$cfg" name
156         [ -n "$name" ] || return 0
157         
158         config_get mac "$cfg" mac
159         [ -n "$mac" ] || return 0
160         
161         append args "--dhcp-mac=$name,$mac"
162         
163         dhcp_option_add "$cfg" "$name"
164 }
165
166 dhcp_add() {
167         local cfg="$1"
168         config_get net "$cfg" interface
169         [ -n "$net" ] || return 0
170
171         config_get name "$cfg" name
172         [ -n "$name" ] || name="$net"
173
174         config_get ifname "$net" ifname
175         [ -n "$ifname" ] || return 0
176
177         config_get dnsserver "$net" dns
178         [ -n "$dnsserver" ] && {
179                 DNS_SERVERS="$DNS_SERVERS $dnsserver"
180         }
181
182         append_bool "$cfg" ignore "-I $ifname"
183
184         config_get proto "$net" proto
185         [ static = "$proto" ] || return 0
186
187         config_get ipaddr "$net" ipaddr
188         config_get netmask "$net" netmask
189
190         #check for an already active dhcp server on the interface, unless 'force' is set
191         config_get_bool force "$cfg" force 0
192         [ "$force" -gt 0 ] || {
193                 udhcpc -n -q -R -s /bin/true -t 1 -i $ifname >&- && return 0
194         }
195         
196         config_get start "$cfg" start
197         config_get limit "$cfg" limit
198         config_get leasetime "$cfg" leasetime
199         config_get options "$cfg" options
200
201         leasetime="${leasetime:-12h}"
202         start="$(dhcp_calc "${start:-100}")"
203         limit="$((${limit:-150} + 1))"
204         eval "$(ipcalc.sh $ipaddr $netmask $start $limit)"
205         append args "--dhcp-range=$name,$START,$END,$NETMASK,$leasetime${options:+ $options}"
206         
207         dhcp_option_add "$cfg" "$name"
208 }
209
210 dhcp_option_add () {
211         local cfg="$1"
212         local name="$2"
213         
214         for count in $(seq 0 100); do
215                 eval current_value=\$CONFIG_"$cfg"_dhcp"$count"
216                 if [ -z "$current_value" ]; then
217                         let "count-=1"
218                         break
219                 fi
220                 append args "-O $name","$current_value"
221         done
222
223 }
224
225 start() {
226         include /lib/network
227         scan_interfaces
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 }