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

START=80

EXTRA_COMMANDS='update_auto_limit'
EXTRA_HELP='	update_auto_limit setup/shutdown '
update_auto_limit()
{

	tx=`uci get bandwidth_limit.smartbandwidthlimit.tx`
	rx=`uci get bandwidth_limit.smartbandwidthlimit.rx`
	if [ "$3" = "setup" ]; then
		range=$4
		num_tx=`expr $tx / 12`
		#iptables -A BW_LIMIT -m iprange --src-range $range -j CONNMARK --set-mark 0x10000000/0x90008000
		iptables -A BW_LIMIT -m iprange --src-range $range -m limit --limit $num_tx/s --limit-burst $num_tx -j RETURN
		iptables -A BW_LIMIT -m iprange --src-range $range -j DROP		

		num_rx=`expr $rx / 12`
		#iptables -A BW_LIMIT -m iprange --dst-range $range -j CONNMARK --set-mark 0x10000000/0x90008000
		iptables -A BW_LIMIT -m iprange --dst-range $range -m limit --limit $num_rx/s --limit-burst $num_rx -j RETURN
		iptables -A BW_LIMIT -m iprange --dst-range $range -j DROP		

	else
		range=$4
		num_tx=`expr $tx / 12`
		#iptables -D BW_LIMIT -m iprange --src-range $range -j CONNMARK --set-mark 0x10000000/0x90008000
		iptables -D BW_LIMIT -m iprange --src-range $range -m limit --limit $num_tx/s --limit-burst $num_tx -j RETURN
		iptables -D BW_LIMIT -m iprange --src-range $range -j DROP			
		

		num_rx=`expr $rx / 12`
		#iptables -D BW_LIMIT -m iprange --dst-range $range -j CONNMARK --set-mark 0x10000000/0x90008000	
		iptables -D BW_LIMIT -m iprange --dst-range $range -m limit --limit $num_rx/s --limit-burst $num_rx -j RETURN
		iptables -D BW_LIMIT -m iprange --dst-range $range -j DROP
				

	fi
}

bl_handle()
{
	local status
	local start_ip
    local end_ip
    local tx
    local rx
    local num_tx
    local num_rx
    local range
	local time_obj
    
	config_get status $1 status
	
	if [ "$status" = "disable" ];then
		return;
	elif [ "$status" = "" ];then
		uci set bandwidth_limit.$1.status=enable
	fi
    config_get start_ip $1 start_ip
    config_get end_ip $1 end_ip
    config_get rx $1 rx
    config_get tx $1 tx
	config_get time_obj $1 time_obj
    
    if [ "$start_ip" == "0.0.0.0" ] && [ "$end_ip" == "0.0.0.0" ] ;then
        return;
    elif [ "$start_ip" != "0.0.0.0" ] && [ "$end_ip" == "0.0.0.0" ];then
        range=$start_ip
    elif [ "$start_ip" == "0.0.0.0" ] && [ "$end_ip" != "0.0.0.0" ];then
        range=$end_ip    
    elif [ "$start_ip" != "0.0.0.0" ] && [ "$end_ip" != "0.0.0.0" ];then
        range=${start_ip}-${end_ip}
    fi
	
	tmo_src=""
	tmo_dst=""
    if [ ! "$time_obj" = "None" ] && [ ! "$time_obj" = "" ]; then
        tmo_src="-m set --set tmo_$time_obj src"
		tmo_dst="-m set --set tmo_$time_obj dst"
	fi
    
    if [ "$tx" != "0" ];then
        num_tx=`expr $tx / 12`
	#	iptables -I BW_LIMIT -m iprange --src-range $range $tmo_src -j DROP
        #iptables -I BW_LIMIT -m iprange --src-range $range -m limit --limit $num_tx/s --limit-burst $num_tx $tmo_src -j RETURN
        #iptables -I BW_LIMIT -m iprange --src-range $range $tmo_src -j CONNMARK --set-mark 0x10000000/0x90008000
    fi

    if [ "$rx" != "0" ];then
        num_rx=`expr $rx / 12`
        #iptables -I BW_LIMIT -m iprange --dst-range $range $tmo_dst -j DROP
        #iptables -I BW_LIMIT -m iprange --dst-range $range -m limit --limit $num_rx/s --limit-burst $num_rx $tmo_dst -j RETURN
        #iptables -I BW_LIMIT -m iprange --dst-range $range $tmo_dst -j CONNMARK --set-mark 0x10000000/0x90008000
    fi
}

handle_old_srcip()
{
	config_get start_ip $1 start_ip
	if [ -n "$start_ip" ] ;then
		uci set bandwidth_limit.$1.status=disable
		uci delete bandwidth_limit.$1.start_ip
		uci delete bandwidth_limit.$1.end_ip
		uci delete bandwidth_limit.$1.time_obj
	fi
}

boot()
{
    #iptables -N BW_LIMIT
    #iptables -I FORWARD 1 -j BW_LIMIT
    #iptables -N BW_LIMIT -t mangle
    #iptables -A POSTROUTING -j BW_LIMIT -t mangle
	
	#Handle old version config
	config_load bandwidth_limit
	config_foreach handle_old_srcip profile
	
    start
}

apply()
{	
    stop
    start
    /etc/init.d/fpp_policy apply
	pid=$(pidof dataflow)
	kill -48 $pid 2>/dev/null >/dev/null
}

start()
{    
	local status=`uci get bandwidth_limit.smartbandwidthlimit.status`
	local default_limit=`uci get bandwidth_limit.smartbandwidthlimit.default_limit`
	if [ -z "$status" ] || [ -z "$default_limit" ]; then
		uci set bandwidth_limit.smartbandwidthlimit=smart_bandwidth_limit
		uci set bandwidth_limit.smartbandwidthlimit.status=disable
		uci set bandwidth_limit.smartbandwidthlimit.sessions=1000
		uci set bandwidth_limit.smartbandwidthlimit.tx=5000
		uci set bandwidth_limit.smartbandwidthlimit.rx=5000
		uci set bandwidth_limit.smartbandwidthlimit.unit=kbps
		uci set bandwidth_limit.smartbandwidthlimit.default_limit="Default limit applys to the LAN IP not in Limitation List, and also not bounded by smart bandwidth limit."
		uci set bandwidth_limit.smartbandwidthlimit.start_ip="0.0.0.0"
		uci set bandwidth_limit.smartbandwidthlimit.end_ip="0.0.0.0"
		uci set bandwidth_limit.smartbandwidthlimit.default_tx=0
		uci set bandwidth_limit.smartbandwidthlimit.default_rx=0
	fi
#	sbl_sessions=`uci get bandwidth_limit.smartbandwidthlimit.sessions`
#	[ "$status" = "enable" ] && {
#		/sbin/smart_bandwidth_limit $sbl_sessions >/dev/null 2>/dev/null &
#		/sbin/dataflow >/dev/null 2>/dev/null &
#	}
    #config_load bandwidth_limit
    #config_foreach bl_handle profile
    uci commit bandwidth_limit
    
}

stop()
{
#	pid=$(ps | awk '/\/sbin\/smart_bandwidth_limit/ {print $1}')
#	kill -9 $pid 2>/dev/null >/dev/null
#    iptables -F BW_LIMIT
    #iptables -F BW_LIMIT -t mangle
    return 0
}

