Better x86 generic config, the lzma decompressor is now reliable (thanks to Daniel...
[openwrt.git] / package / dnsmasq / files / dnsmasq.init
index 17ef43c..14c77fb 100644 (file)
@@ -1,45 +1,78 @@
-#!/bin/sh
-. /etc/config/network
-
-# The following is to automatically configure the DHCP settings
-# based on config settings. Feel free to replace all this crap
-# with a simple "dnsmasq" and manage everything via the
-# /etc/dnsmasq.conf config file
-
-[ -f /etc/dnsmasq.conf ] || exit
-
-args=""
-iface=lan
-eval "ifname=\${${iface}_ifname}"
-
-dhcp_enable="${dhcp_enable:-1}"
-dhcp_start="${dhcp_start:-100}"
-dhcp_num="${dhcp_num:-50}"
-dhcp_lease="${dhcp_lease:-12h}"
-
-# if dhcp_enable is unset and there is a dhcp server on the network already, default to dhcp_enable=0
-[ -z "$dhcp_enable" ] && udhcpc -n -q -R -s /bin/true -i $ifname >&- && dhcp_enable="${dhcp_enable:-0}"
-
-# dhcp_enable=0 disables the dhcp server
-(
-       [ -z "$dhcp_enable" -o "$dhcp_enable" -eq 1 ] && {
-               # no existing DHCP server?
-
-               # calculate settings
-               eval "ipaddr=\${${iface}_ipaddr}"
-               eval "netmask=\${${iface}_netmask}"
-               eval $(ipcalc $ipaddr $netmask ${dhcp_start:-100} ${dhcp_num:-150})
-               
-               # and pass the args via config parser defines
-               echo "@define dhcp_enable 1"
-               echo "@define netmask $NETMASK"
-               echo "@define start $START"
-               echo "@define end $END"
-               echo "@define lease ${dhcp_lease:-12h}"
+#!/bin/sh /etc/rc.common
+# Copyright (C) 2006 OpenWrt.org
+
+START=60
+
+dhcp_calc() {
+       local ip="$1"
+       local res=0
+       
+       while [ -n "$ip" ]; do
+               part="${ip%%.*}"
+               res="$(($res * 256))"
+               res="$(($res + $part))"
+               [ "${ip%.*}" != "$ip" ] && ip="${ip#*.}" || ip=
+       done
+       echo "$res"
+}
+
+dhcp_add() {
+       local cfg="$1"
+
+       config_get net "$cfg" interface
+       [ -n "$net" ] || return 0
+       
+       config_get ifname "$net" ifname
+       [ -n "$ifname" ] || return 0
+       
+       config_get_bool ignore "$cfg" ignore
+       [ "$ignore" -gt 0 ] && {
+               append args "-I $ifname"
+               return 0
        }
+       
+       config_get proto "$net" proto
+       [ static = "$proto" ] || return 0
+       
+       config_get ipaddr "$net" ipaddr
+       config_get netmask "$net" netmask
 
-       # ignore requests from wan interface
-       [ -z "$wan_proto" -o "$wan_proto" = "none" ] || echo "@define wan_ifname $wan_ifname"
+       # check for an already active dhcp server on the interface, unless 'force' is set
+       config_get_bool force "$cfg" force 0
+       [ "$force" -gt 0 ] || {
+               udhcpc -n -q -R -s /bin/true -t 1 -i $ifname >&- && return 0
+       }
+       
+       config_get start "$cfg" start
+       config_get end "$cfg" end
+       config_get leasetime "$cfg" leasetime
+       config_get options "$cfg" options
+
+       leasetime="${leasetime:-12h}"
+       start="$(dhcp_calc "${start:-100}")"
+       end="$((${end:-150} + 1))"
+       eval "$(ipcalc.sh $ipaddr $netmask $start $end)"
+       append args "-F $START,$END,$NETMASK,$leasetime${options:+ $options}"
+}
+
+start() {
+       include /lib/network
+       scan_interfaces
+       config_load /var/state/network
+       config_load dhcp
+
+       args=""
+       config_foreach dhcp_add dhcp
+       
+       dnsmasq $args && {
+               rm -f /tmp/resolv.conf
+               cat > /tmp/resolv.conf <<EOF
+nameserver 127.0.0.1
+search lan
+EOF
+       }
+}
 
-       cat /etc/dnsmasq.conf
-) | awk -f /usr/lib/parse-config.awk | dnsmasq -C /proc/self/fd/0
+stop() {
+       killall dnsmasq
+}
This page took 0.023884 seconds and 4 git commands to generate.