#!/bin/sh

. /etc/net6conf/6data.conf

#Fix Bug 23366[IPv6-Auto Config] DUT should disable prefix option in Router Advertisement 
#message when the prefix valid lifetime is expired.
prefix_timeout() {
	# Remove the LAN side IPv6 address, which has been expired
	# timeout_prefix would be like: 5600 2800 2000:458:ff01:3800:: 56
	prefix_addr=`$ECHO $timeout_prefix |cut -f3 -d' ' |sed s/:://`
	lan6_ip=`ifconfig $bridge |grep "inet6 addr: $prefix" |grep -v "Link" |awk '{print $3}'`
	$ECHO "Try to delete $lan6_ip from $bridge" > /dev/console
	[ "x$lan6_ip" != "x" ] && $IP -6 addr del $lan6_ip dev $bridge

	#when prefix is timeout, remove old prefix info files
	rm -f /tmp/dhcp6c_script_envs
	rm -f $DHCP6C_PD
	rm -f $DHCP6S_PD

	#reload the LAN side IPv6 related services:
	#rewrite config file: radvd_write_config, dhcp6s_write_config.
	$ECHO "reload 6service" > /dev/console
	/etc/net6conf/6service reload
}

#To do prefix timeout envents.
if [ "x$REASON" = "xprefix_timeout" ] ;then
	lan_prefix_info=`cat $DHCP6S_PD`
	#value $timeout_prefix is got from wide-dhcpv6
	[ "$timeout_prefix" != "$lan_prefix_info" ] && exit
	prefix_timeout
	exit
fi

#fix the bug 27061[ST/Netgear-67-71--IPv6 DHCP] There is IPv6 address On LAN port in GUI When DUT send a Decline message to the server.
if [ "x$REASON" = "xdecline" ] ;then
	lan6_ip=`ifconfig $bridge |grep "inet6 addr" |grep -v "Link" |awk '{print $3}'`
	$ECHO "Try to delete $lan6_ip from $bridge" > /dev/console
	[ "x$lan6_ip" != "x" ] && $IP -6 addr del $lan6_ip dev $bridge

	#when duplicate address detected, remove old prefix info files
	rm -f /tmp/dhcp6c_script_envs
	rm -f $DHCP6S_PD

	#After DUT sending decline packet, stop the DHCPv6 server and autoconfigure address service(radvd)
	killall dhcp6s
	killall radvd
	exit
fi

del_dns6() {
        sed '/.:./d' /tmp/resolv.conf > /tmp/resolv.conf$$
	#avoid that the permission of /tmp/resolv.conf$$ have be changed to -rw------ under pppoev6
	chmod 644 /tmp/resolv.conf$$
        mv /tmp/resolv.conf$$ /tmp/resolv.conf
        $ECHO -n "" > $IPV6_DNS
}


#write ra_dns to resolv.conf
write_ra_tmp_dns(){
        sed '/.:./d' /tmp/resolv.conf > /tmp/resolv.conf$$
        mv /tmp/resolv.conf$$ /tmp/resolv.conf
        $ECHO -n "" >  $RA_DNS_FILE
        for loop in $new_ra_dns_servers
                do
                        $ECHO "nameserver $loop" >> $RA_DNS_FILE
                done
        write_ra_dns $IPV6_DNS  /tmp/resolv.conf
        write_ra_dns $RA_DNS_FILE  /tmp/resolv.conf
}
#when received ra packet with RDNSS option
conn_type=`$CONFIG get ipv6_type`
PPPOEv6_WAY=`$CONFIG get ipv6_pppoe_way`
if [ "x$REASON" = "xra_dns" ]; then
	if [ "x$conn_type" = "xpppoe" -a "$PPPOEv6_WAY" = "1" ] || [ "x$conn_type" = "xautoConfig" -o "x$conn_type" = "xdslite" -o "x$conn_type" = "xv6plus" -o "x`$CONFIG get enable_ds`" = "x1" ] ; then
		$ECHO "write ra_dns..."  > /dev/console
		write_ra_tmp_dns
	fi
fi

lease_changed()
{
	local envs_p_file="/tmp/dhcp6c_script_envs"

	[ -f "$envs_p_file" ] && . $envs_p_file
	[ -z "$new_domain_name" ] && new_domain_name="$new_domain_name_p"
	[ -z "$new_sip_name" ] && new_sip_name="$new_sip_name_p"
	[ -z "$new_domain_name_servers" ] && new_domain_name_servers="$new_domain_name_servers_p"
	[ -z "$new_ntp_servers" ] && new_ntp_servers="$new_ntp_servers_p"
	[ -z "$new_sip_servers" ] && new_sip_servers="$new_sip_servers_p"
	cat > $envs_p_file << EOF
new_domain_name_p="$new_domain_name"
new_sip_name_p="$new_sip_name"
new_domain_name_servers_p="$new_domain_name_servers"
new_ntp_servers_p="$new_ntp_servers"
new_sip_servers_p="$new_sip_servers"
EOF

	if [ "x$REASON" = "xNBI" ] ;then
		[ -f "$DHCP6C_PD" ] && . $DHCP6C_PD
		[ -z "$new_prefix" ] && new_prefix="$prefix_p"
		[ -z "$new_address" ] && new_address="$address_p"
		cat > $DHCP6C_PD << EOF
address_p="$new_address"
prefix_p="$new_prefix"
EOF
		# Determine the Prefix change no matter the prefer and valid lifetime
		OLD_PREFIX_AND_LEN=`$ECHO "$prefix_p" | awk '{print $3$4}'`
		PREFIX_AND_LEN=`$ECHO "$new_prefix" | awk '{print $3$4}'`

		[ "$PREFIX_AND_LEN" != "$OLD_PREFIX_AND_LEN" ] && return 0
	fi

	[ "$new_domain_name" != "$new_domain_name_p" ] && return 0
	[ "$new_sip_name" != "$new_sip_name_p" ] && return 0
	[ "$new_domain_name_servers" != "$new_domain_name_servers_p" ] && return 0
	[ "$new_ntp_servers" != "$new_ntp_servers_p" ] && return 0
	[ "$new_sip_servers" != "$new_sip_servers_p" ] && return 0

	return 1
}

if [ "x$conn_type" = "xdslite" -o "x`$CONFIG get enable_ds`" = "x1" ]; then
	if [ -n "$dslite_servers" ]; then
		aftr_type=`$CONFIG get aftr_ipv6`
		if [ "x$aftr_type" = "x0" ] || [ -z "$aftr_type" ]; then
			if [ "x`$CONFIG get manual_aftr_ipv6`" != "x$dslite_servers" ]; then
				$CONFIG set manual_aftr_ipv6=$dslite_servers
				/etc/net6conf/dslite reload
			fi
		fi
	fi
fi

# if dhcp6c lease is not changed, don't need to do anything.
# let radvd know prefix lifetime is available
if ! lease_changed ; then
	killall -SIGUSR2 radvd #SIGUSR1 will kill radvd
	exit 0
fi

cat /proc/uptime | awk -F . '{print $1}' > /tmp/dhcp6c_obtain_lease
$ECHO "dhcp6c lease changed !" > /dev/console


# The prefix changed, we delete all the ip of the bridge
pre_ip=`ifconfig $bridge |grep "inet6 addr" |grep -v "Link" |awk '{print $3}'`
for loop in $pre_ip
do
	$IP -6 addr del $loop dev $bridge
done

dns_type=`$CONFIG get ipv6_${conn_type}_dns_assign`

# wirte the dhcp6s_pd and dhcp6s_dsn file

	if [ "x$new_prefix" != "x" ]; then
		$ECHO $new_prefix > $DHCP6S_PD
	fi

	# for avoid adding more line
	$ECHO " " > $DHCP6S_DSN

	for loop in $new_domain_name
	do
		$ECHO "option domain-name \"$loop\";" >> $DHCP6S_DSN
	done
	
	for loop in $new_sip_name
	do
		$ECHO "option sip-server-domain-name \"$loop\";" >> $DHCP6S_DSN
	done

# According to NTGR Router Spec:
# The domain-name-servers should be DUT's IPv6 LAN interface's link local address.
# The link local address could be added in net6conf/6service script, so comment below code.
#	if [ "x$new_domain_name_servers" != "x" ]; then
#		$ECHO "option domain-name-servers $new_domain_name_servers;" >> $DHCP6S_DSN
#	fi

	# only add dns to resolv.conf when dns is not assigned(static)
	if [ "x$dns_type" != "x1" -a "x$conn_type" != "xfixed" ] ;then
		del_dns6
		for loop in $new_domain_name_servers
		do 
			$ECHO "nameserver $loop" >> $IPV6_DNS
		done
		if [ "x$conn_type" = "xpppoe" -a "$PPPOEv6_WAY" = "1" ] || [ "x$conn_type" = "xautoConfig" -o "x$conn_type" = "xdslite" -o "x$conn_type" = "xv6plus" -o "x`$CONFIG get enable_ds`" = "x1" ] ; then
			write_ra_dns $RA_DNS_FILE  /tmp/resolv.conf
		fi
		write_ra_dns $IPV6_DNS  /tmp/resolv.conf
	fi

	if [ "x$new_ntp_servers" != "x" ]; then
		$ECHO "option ntp-servers $new_ntp_servers;" >> $DHCP6S_DSN
	fi

	if [ "x$new_sip_servers" != "x" ]; then
		$ECHO "option sip-server-address $new_sip_servers;" >> $DHCP6S_DSN
	fi

if [ -s $DHCP6S_PD ] && [ -s $DHCP6S_CONF ]; then
		$ECHO "Got the prefix, reload the config file" > /dev/console
		$ECHO "The prefix for LAN is $new_prefix" > /dev/console
		srv_reload="1"
fi

if [ "$conn_type" = "dslite" -o "x`$CONFIG get enable_ds`" = "x1" ] && [ "`$CONFIG get enable_nd`" -eq "1" ]; then
	srv_reload="1"
fi

	#For NO-HIKARI-PHONE topology, the DHCP6S_PD is empty, and br0 ip is
	#deleted on line 149, need to call 6service to update or add br0 ip
	if [ "x$conn_type" = "xv6plus" ]; then
		srv_reload=1
	fi

	if [ $srv_reload -eq 1 ]; then
		$ECHO "set lan ipv6 address and reload dhcp6s config file !"
		/etc/net6conf/6service reload &
	fi

