prereq check for find -exec +
[openwrt.git] / package / dnsmasq / files / dnsmasq.init
1 #!/bin/sh /etc/rc.common
2 # Copyright (C) 2006 OpenWrt.org
3
4 START=60
5
6 dhcp_calc() {
7         local ip="$1"
8         local res=0
9         
10         while [ -n "$ip" ]; do
11                 part="${ip%%.*}"
12                 res="$(($res * 256))"
13                 res="$(($res + $part))"
14                 [ "${ip%.*}" != "$ip" ] && ip="${ip#*.}" || ip=
15         done
16         echo "$res"
17 }
18
19 dhcp_add() {
20         local cfg="$1"
21
22         config_get net "$cfg" interface
23         [ -n "$net" ] || return 0
24         
25         config_get ifname "$net" ifname
26         [ -n "$ifname" ] || return 0
27         
28         config_get_bool ignore "$cfg" ignore
29         [ "$ignore" -gt 0 ] && {
30                 append args "-I $ifname"
31                 return 0
32         }
33         
34         config_get proto "$net" proto
35         [ static = "$proto" ] || return 0
36         
37         config_get ipaddr "$net" ipaddr
38         config_get netmask "$net" netmask
39
40         # check for an already active dhcp server on the interface, unless 'force' is set
41         config_get_bool force "$cfg" force 0
42         [ "$force" -gt 0 ] || {
43                 udhcpc -n -q -R -s /bin/true -i $ifname >&- && return 0
44         }
45         
46         config_get start "$cfg" start
47         config_get end "$cfg" end
48         config_get leasetime "$cfg" leasetime
49         config_get options "$cfg" options
50
51         leasetime="${leasetime:-12h}"
52         start="$(dhcp_calc "${start:-100}")"
53         end="$((${end:-150} + 1))"
54         eval "$(ipcalc.sh $ipaddr $netmask $start $end)"
55         append args "-F $START,$END,$NETMASK,$leasetime${options:+ $options}"
56 }
57
58 start() {
59         include /lib/network
60         scan_interfaces
61         config_load dhcp
62
63         args=""
64         config_foreach dhcp_add dhcp
65         
66         dnsmasq $args && {
67                 rm -f /tmp/resolv.conf
68                 cat > /tmp/resolv.conf <<EOF
69 nameserver 127.0.0.1
70 search lan
71 EOF
72         }
73 }
74
75 stop() {
76         killall dnsmasq
77 }