#!/bin/sh /etc/rc.common
# Copyright (C) 2007 OpenWrt.org

# Preferably start dnscache after udhcpd/dnsmasq, to be sure /etc/resolv.conf is set correctly. dnsmasq is at S60, so dnscache is put at S65 as seen below. If /etc/resolv.conf is not used, then dnscache may be started at S47, just after the firewall has been set up.
START=65
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/bin/dnscache
NAME=dnscache
DESC="Caching name server"

ROOT="/tmp/dnscache"
resolvfile="/etc/resolv.conf"

start() {
    echo "Starting $DESC: $NAME"
    config_load djbdns
    config_foreach get_userids global
    mkdir -p $ROOT
    cp -fa /etc/dnscache/ip $ROOT
    cp -fa /etc/dnscache/servers $ROOT
    rm -f $ROOT/resolvers
    chown -R $UID:$GID $ROOT
    config_foreach start_dnscache dnscache
}

get_userids() {
    local cfg="$1"
    config_get UID "$cfg" runasuser
    config_get GID "$cfg" runasgroup
    UID=`id -u $UID`
    GID=`id -g $GID`
}

append_server() {
    echo $1 >> $ROOT/resolvers
}

start_dnscache() {
    local cfg="$1"
    config_get logging        "$cfg" logging
    config_get useresolvconf  "$cfg" useresolvconf
    config_get ifaces         "$cfg" interface
    config_get ifacesend      "$cfg" interfacesend
    config_get defaultallowif "$cfg" defaultallowif
    config_get CACHESIZE      "$cfg" cachesize
    config_get FORWARDONLY    "$cfg" forwardonly
    config_list_foreach       "$cfg" "resolver" append_server
# Add some interface network addresses to list of allowed IP addresses (assumes /24 networks)
    include /lib/network
    scan_interfaces
    if [ -n "$defaultallowif" ] ; then
        for myinterface in `echo $defaultallowif`; do
            config_get addr $myinterface ipaddr
            # the next line strips the last octet from the ip address
            addr=`echo $addr|sed 's/.[0-9]\+$//'`
            touch $ROOT/ip/$addr
            echo Adding $ROOT/ip/$addr
        done
    fi
# Translate listen interface names to IP addresses
    IP=""
    for iface in $ifaces; do
       config_get ipaddr "$iface" ipaddr
       IP="$ipaddr/$IP"
    done
# Strip trailing slashes
    IP=`echo $IP|sed 's/\/\+$//g'`
# Translante send interface name to an IP address
    config_get IPSEND "$ifacesend" ipaddr
# If forwardingonly set then activate list of resolvers
    if [ "$FORWARDONLY" == "0" ] ; then
        # Unset FORWARDONLY if equal to '0'
        export -n FORWARDONLY
    else
# Use either fixed list of dnsservers or resolv.conf data
        if [ "$useresolvconf" == "1" ] ; then
            if [ -s "$resolvfile" ] ; then
               cat $resolvfile|grep nameserver|sed 's/nameserver //g' > $ROOT/resolvers
            else
               echo Error: $resolvfile does not exist.
               exit 1
            fi
        fi
        if [ ! "`cat $ROOT/resolvers|wc -L`" == "0" ] ; then
            cp -f $ROOT/resolvers $ROOT/servers/@ > /dev/null 2>&1
        else
            echo Error: No resolvers are defined.
            exit 1
        fi
     fi
    export IP 
    export ROOT
# dnscache likes to receive a 128bit random seed
    dd if=/dev/urandom of=$ROOT/randomseed bs=128 count=1  > /dev/null 2>&1
    if [ "$DEBUG" == 1 ] ; then
        $DAEMON < /$ROOT/randomseed
    elif [ "$logging" == '1' ] ; then
        $DAEMON 2>&1 < /$ROOT/randomseed | logger -p local1.info -t $NAME &
    else
        $DAEMON > /dev/null 2>&1 < /$ROOT/randomseed &
    fi
}

stop() {
    echo -n "Stopping $DESC: $NAME"
    kill `pidof $NAME|sed "s/$$//g"` > /dev/null 2>&1
    echo " ."
}

restart() {
    echo "Restarting $DESC: $NAME... "
    stop
    sleep 2
    start
}

