14fc698cff7e5fe3294b770c00cd22329916a4c4
[openwrt-10.03/.git] / package / dnsmasq / files / dnsmasq.init
1 #!/bin/sh
2 . /etc/functions.sh
3 include network
4 scan_interfaces
5
6 # The following is to automatically configure the DHCP settings
7 # based on config settings. Feel free to replace all this crap
8 # with a simple "dnsmasq" and manage everything via the
9 # /etc/dnsmasq.conf config file
10
11 [ -f /etc/dnsmasq.conf ] || exit
12
13 args=""
14 iface=lan
15 config_get ifname "$iface" ifname
16 config_get proto "$iface" proto
17
18 [ "$proto" = static ] && dhcp_enable="${dhcp_enable:-1}"
19 dhcp_start="${dhcp_start:-100}"
20 dhcp_num="${dhcp_num:-50}"
21 dhcp_lease="${dhcp_lease:-12h}"
22
23 # if dhcp_enable is unset and there is a dhcp server on the network already, default to dhcp_enable=0
24 [ -z "$dhcp_enable" ] && udhcpc -n -q -R -s /bin/true -i $ifname >&- && dhcp_enable="${dhcp_enable:-0}"
25
26 # dhcp_enable=0 disables the dhcp server
27 (
28         [ -z "$dhcp_enable" -o "$dhcp_enable" -eq 1 ] && {
29                 # no existing DHCP server?
30
31                 # calculate settings
32                 config_get ipaddr "$iface" ipaddr
33                 config_get netmask "$iface" netmask
34                 eval $(ipcalc $ipaddr $netmask ${dhcp_start:-100} ${dhcp_num:-150})
35                 
36                 # and pass the args via config parser defines
37                 echo "${dhcp_enable:+@define dhcp_enable 1}"
38                 echo "@define netmask $NETMASK"
39                 echo "@define start $START"
40                 echo "@define end $END"
41                 echo "@define lease ${dhcp_lease:-12h}"
42         }
43
44         # ignore requests from wan interface
45         config_get wan_proto wan proto
46         config_get wan_ifname wan ifname
47         [ -z "$wan_proto" -o "$wan_proto" = "none" ] || echo "@define wan_ifname $wan_ifname"
48
49         cat /etc/dnsmasq.conf
50 ) | awk -f /usr/lib/parse-config.awk | dnsmasq -C /proc/self/fd/0