dnsmasq: support hostid ipv6 address suffix option
[openwrt/staging/yousong.git] / package / network / services / dnsmasq / files / dnsmasq.init
index 61ded6a53a8dd9e6d6b2608dba62245474c3e63a..1a9903e50dc8edb2eb11f6d8c23ba12ba81f846b 100644 (file)
@@ -23,6 +23,25 @@ xappend() {
        echo "${value#--}" >> $CONFIGFILE
 }
 
+hex_to_hostid() {
+        local var="$1"
+        local hex="${2#0x}"     # strip optional "0x" prefix
+
+        if [ -n "${hex//[0-9a-fA-F]/}" ]; then
+                # is invalid hex literal
+                return 1
+        fi
+
+        # convert into host id
+        export "$var=$(
+                printf "%0x:%0x"  \
+                        $(((0x$hex >> 16) % 65536)) \
+                        $(( 0x$hex        % 256))
+        )"
+
+        return 0
+}
+
 dhcp_calc() {
        local ip="$1"
        local res=0
@@ -72,8 +91,9 @@ append_parm() {
        local section="$1"
        local option="$2"
        local switch="$3"
+       local default="$4"
        local _loctmp
-       config_get _loctmp "$section" "$option"
+       config_get _loctmp "$section" "$option" "$default"
        [ -z "$_loctmp" ] && return 0
        xappend "$switch=$_loctmp"
 }
@@ -148,6 +168,7 @@ dnsmasq() {
        append_parm "$cfg" dhcpleasemax "--dhcp-lease-max"
        append_parm "$cfg" "queryport" "--query-port"
        append_parm "$cfg" "minport" "--min-port"
+       append_parm "$cfg" "maxport" "--max-port"
        append_parm "$cfg" "domain" "--domain"
        append_parm "$cfg" "local" "--server"
        config_list_foreach "$cfg" "server" append_server
@@ -157,8 +178,8 @@ dnsmasq() {
        config_list_foreach "$cfg" "notinterface" append_notinterface
        config_list_foreach "$cfg" "addnhosts" append_addnhosts
        config_list_foreach "$cfg" "bogusnxdomain" append_bogusnxdomain
-       append_parm "$cfg" "leasefile" "--dhcp-leasefile"
-       append_parm "$cfg" "resolvfile" "--resolv-file"
+       append_parm "$cfg" "leasefile" "--dhcp-leasefile" "/tmp/dhcp.leases"
+       append_parm "$cfg" "resolvfile" "--resolv-file" "/tmp/resolv.conf.auto"
        append_parm "$cfg" "serversfile" "--servers-file"
        append_parm "$cfg" "tftp_root" "--tftp-root"
        append_parm "$cfg" "dhcp_boot" "--dhcp-boot"
@@ -173,10 +194,17 @@ dnsmasq() {
        config_get_bool readethers "$cfg" readethers
        [ "$readethers" = "1" -a \! -e "/etc/ethers" ] && touch /etc/ethers
 
-       config_get leasefile $cfg leasefile
+       config_get leasefile $cfg leasefile "/tmp/dhcp.leases"
        [ -n "$leasefile" -a \! -e "$leasefile" ] && touch "$leasefile"
        config_get_bool cachelocal "$cfg" cachelocal 1
 
+       config_get_bool noresolv "$cfg" noresolv 0
+       if [ "$noresolv" != "1" ]; then
+               config_get resolvfile "$cfg" resolvfile "/tmp/resolv.conf.auto"
+               # So jail doesn't complain if file missing
+               [ -n "$resolvfile" -a \! -e "$resolvfile" ] && touch "$resolvfile"
+       fi
+
        config_get hostsfile "$cfg" dhcphostsfile
        [ -e "$hostsfile" ] && xappend "--dhcp-hostsfile=$hostsfile"
 
@@ -218,6 +246,8 @@ dnsmasq() {
        mkdir -p /tmp/hosts /tmp/dnsmasq.d
        xappend "--addn-hosts=/tmp/hosts"
        xappend "--conf-dir=/tmp/dnsmasq.d"
+       xappend "--user=dnsmasq"
+       xappend "--group=dnsmasq"
 
        echo >> $CONFIGFILE
 
@@ -316,6 +346,9 @@ dhcp_host_add() {
        config_get networkid "$cfg" networkid
        [ -n "$networkid" ] && dhcp_option_add "$cfg" "$networkid" "$force"
 
+       config_get_bool enable "$cfg" enable 1
+       [ "$enable" = "0" ] && return 0
+
        config_get name "$cfg" name
        config_get ip "$cfg" ip
        [ -n "$ip" -o -n "$name" ] || return 0
@@ -339,12 +372,19 @@ dhcp_host_add() {
 
        config_get tag "$cfg" tag
 
+       if [ "$DHCPv6CAPABLE" -eq 1 ]; then
+               config_get hostid "$cfg" hostid
+               if [ -n "$hostid" ]; then
+                       hex_to_hostid hostid "$hostid"
+               fi
+       fi
+
        config_get_bool broadcast "$cfg" broadcast 0
        [ "$broadcast" = "0" ] && broadcast=
 
        config_get leasetime "$cfg" leasetime
 
-       xappend "--dhcp-host=$macs${networkid:+,net:$networkid}${broadcast:+,set:needs-broadcast}${tag:+,set:$tag}${ip:+,$ip}${name:+,$name}${leasetime:+,$leasetime}"
+       xappend "--dhcp-host=$macs${networkid:+,net:$networkid}${broadcast:+,set:needs-broadcast}${tag:+,set:$tag}${ip:+,$ip${hostid:+,[::$hostid]}}${name:+,$name}${leasetime:+,$leasetime}"
 }
 
 dhcp_tag_add() {
@@ -589,7 +629,7 @@ start_service() {
 
        if [ ! -f "$TIMESTAMPFILE" ]; then
                touch "$TIMESTAMPFILE"
-               chown nobody.nogroup "$TIMESTAMPFILE"
+               chown dnsmasq.dnsmasq "$TIMESTAMPFILE"
        fi
 
        echo "# auto-generated config file from /etc/config/dhcp" > $CONFIGFILE
@@ -600,6 +640,8 @@ start_service() {
                xappend "--conf-file=/etc/dnsmasq.conf"
        }
 
+       $PROG --version | grep -osqE "^Compile time options:.* DHCPv6( |$)" && DHCPv6CAPABLE=1 || DHCPv6CAPABLE=0
+
        args=""
        config_foreach dnsmasq dnsmasq
        config_foreach dhcp_host_add host
@@ -620,7 +662,7 @@ start_service() {
        [ $ADD_LOCAL_HOSTNAME -eq 1 ] && {
                local lanaddr lanaddr6
                local ulaprefix="$(uci_get network @globals[0] ula_prefix)"
-               local hostname="$(uci_get system @system[0] hostname OpenWrt)"
+               local hostname="$(uci_get system @system[0] hostname Lede)"
 
                network_get_ipaddr lanaddr "lan" && {
                        dhcp_domain_add "" "$hostname" "$lanaddr"