140f727f4683ff2789a23bac71ed595885e7785a
[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
17 dhcp_enable="${dhcp_enable:-1}"
18 dhcp_start="${dhcp_start:-100}"
19 dhcp_num="${dhcp_num:-50}"
20 dhcp_lease="${dhcp_lease:-12h}"
21
22 # if dhcp_enable is unset and there is a dhcp server on the network already, default to dhcp_enable=0
23 [ -z "$dhcp_enable" ] && udhcpc -n -q -R -s /bin/true -i $ifname >&- && dhcp_enable="${dhcp_enable:-0}"
24
25 # dhcp_enable=0 disables the dhcp server
26 (
27         [ -z "$dhcp_enable" -o "$dhcp_enable" -eq 1 ] && {
28                 # no existing DHCP server?
29
30                 # calculate settings
31                 config_get ipaddr "$iface" ipaddr
32                 config_get netmask "$iface" netmask
33                 eval $(ipcalc $ipaddr $netmask ${dhcp_start:-100} ${dhcp_num:-150})
34                 
35                 # and pass the args via config parser defines
36                 echo "@define dhcp_enable 1"
37                 echo "@define netmask $NETMASK"
38                 echo "@define start $START"
39                 echo "@define end $END"
40                 echo "@define lease ${dhcp_lease:-12h}"
41         }
42
43         # ignore requests from wan interface
44         config_get wan_proto wan proto
45         config_get wan_ifname wan ifname
46         [ -z "$wan_proto" -o "$wan_proto" = "none" ] || echo "@define wan_ifname $wan_ifname"
47
48         cat /etc/dnsmasq.conf
49 ) | awk -f /usr/lib/parse-config.awk | dnsmasq -C /proc/self/fd/0