#!/bin/sh /etc/rc.common

DHCP6C_REQUEST_OPTIONS='domain_name_servers domain_name ntp_servers sip_server_address sip_domain_name nis_server_address nis_domain_name nisp_server_address nisp_domain_name bcmcs_server_address bcmcs_domain_name'

dhcp6c_get_enabled() {
	local cfg=$1
	local en
	config_get_bool en "$cfg" enabled 0
	[ $en -eq 1 ] && dhcp6c_enable=1
}

dhcp6c_write_duid() {
	local mac="${1:-$(ifconfig "$client_device" | sed -ne 's/[[:space:]]*$//; s/.*HWaddr //p')}"
	local pat="[0-9A-F][0-9A-F]"

	case "$mac" in
		# exactly 6 octets -> assume type 3 (DUID-LL - RFC3315, 9.4)
		$pat:$pat:$pat:$pat:$pat:$pat)
			local oIFS="$IFS"; IFS=":"; set -- $mac; IFS="$oIFS"

			# low endian
			if [ "$(printf \\1 | hexdump -n1 -ve '8/2 "" "%04x" ""')" = "0001" ]; then
				printf \\x0a\\x00

			# big endian
			else
				printf \\x00\\x0a
			fi

			printf \\x00\\x03\\x00\\x06\\x$1\\x$2\\x$3\\x$4\\x$5\\x$6
			logger -t dhcp6c "Using MAC address DUID 00:03:00:06:$1:$2:$3:$4:$5:$6"
		;;
		# at least 7 octets -> could be type 1 or type 2
		$pat:$pat:$pat:$pat:$pat:$pat:*)
			local len_id=":$(echo "$mac" | sed -e 's/[^:]//g')"
			local len_hi=$(printf "%02x" $((${#len_id} / 0xFF)) )
			local len_lo=$(printf "%02x" $((${#len_id} % 0xFF)) )

			# low endian
			if [ "$(printf \\1 | hexdump -n1 -ve '8/2 "" "%04x" ""')" = "0001" ]; then
				printf \\x$len_lo\\x$len_hi

			# big endian
			else
				printf \\x$len_hi\\x$len_lo
			fi

			printf $(echo "$mac" | sed -e 's/^/\\x/; s/:/\\x/g')
			logger -t dhcp6c "Using user provided DUID $mac"
		;;
		*)
			logger -t dhcp6c "Unable to derive DUID from interface '$client_device' and no valid user DUID given"
		;;
	esac
}

dhcp6c_write_interface() {
	local cfg=$1
	local sla_id
	local sla_len
	local enabled
	local device

	config_get_bool enabled "$cfg" enabled 0

	if [ $enabled -ne 0 ]; then
		config_get sla_id "$cfg" sla_id
		#config_get sla_len "$cfg" sla_len
		config_get device "$cfg" lanif
		[ -z "$device" ] && network_get_device device "$cfg"

		[ -n "device" ] && {
			printf '\tprefix-interface %s {\n' "$device"
			printf '\t\tsla-id %s;\n' "$sla_id"
			#printf '\t\tsla-len %s;\n' "$sla_len"
			printf '\t};\n'
		}
	fi
}

dhcp6c_write_request() {
	printf '\trequest %s;\n' $1 | sed -e s/_/-/g
}

dhcp6c_write_config() {
	local cfg=$1
	local pd
	local na
	local en
	local interface
	local zonex

	[ "$cfg" != "$2" ] && return 0

	config_get_bool en "$cfg" enabled 0
	[ $en -eq 0 ] && return

	config_get_bool pd "$cfg" pd 0
	config_get_bool na "$cfg" na 0

	config_get interface "$cfg" interface
	[ -z "$interface" ] && {
		network_get_device  interface "$cfg"
	}

	zonex=`echo "${cfg%_v6}" | tr '[a-z]' '[A-Z]'`

	[ -z "$first_iface" ] && {
		first_cfg="$cfg"
		first_iface="$interface"
		interfaces="${interface}@$zonex"
	} || interfaces="${interfaces} ${interface}@$zonex"

	printf 'interface %s {\n' "$interface"

	if [ $pd -ne 0 ]; then
		printf '\tsend ia-pd %d;\n' "$iaid"
	fi

	if [ $na -ne 0 ]; then
		printf '\tsend ia-na %d;\n' "$iaid"
	fi

	if [ $pd -eq 0 -a $na -eq 0 ]; then
		printf '\tinformation-only;\n'
	fi

	local rapid_commit
	config_get_bool rapid_commit "$cfg" rapid_commit 0
	[ $rapid_commit -ne 0 ] && printf '\tsend rapid-commit;\n'

	local script
	config_get script "$cfg" script
	[ "$script" != "" ] && printf '\tscript "%s";\n' "$script"

	local request
	local value
	for request in $DHCP6C_REQUEST_OPTIONS; do
		config_get_bool value "$cfg" "$request" 0
		[ $value -ne 0 ] && dhcp6c_write_request "$request"
	done

	printf '};\n\n'

	if [ $pd -ne 0 ]; then
		printf 'id-assoc pd %d {\n' "$iaid"
		config_foreach dhcp6c_write_interface interface
		printf '};\n\n'
	fi

	if [ $na -ne 0 ]; then
		printf 'id-assoc na %d {\n' "$iaid"
		printf '};\n\n'
	fi

	iaid=`expr $iaid + 1`

	return 0
}

start() {
	local iface="$1"
	local pexec="$2"
	local config_file="/tmp/dhcp6c/dhcp6c${iface:+.${iface}}.conf"
	local pid_file="/tmp/dhcp6c/dhcp6c${iface:+.${iface}}.pid"

	#[ ! -e /etc/dhcp6cctlkey ] && `dd if=/dev/urandom count=1 2> /dev/null | md5sum | cut -d" " -f1 > /etc/dhcp6cctlkey`

	[ -e "${pid_file}" ] && return 0

	. /lib/functions/network.sh

	config_load "dhcp6c"

	local dhcp6c_enable=0
	config_foreach dhcp6c_get_enabled dhcp6c
	[ $dhcp6c_enable -eq 0 ] && return 0

	#local user_duid
	#config_get user_duid basic duid

	logger -t dhcp6c starting dhcp6c

	#local client_interface client_ifname client_device
	#config_get client_interface basic interface
	#network_get_device  client_ifname "$client_interface"
	#network_get_physdev client_device "$client_interface" || client_device="$client_ifname"

	local iaid=1
	local interfaces=""
	local first_cfg=""
	local first_iface=""
	#local duid_file="/var/dhcp6c_duid"
	mkdir -m 755 -p /tmp/dhcp6c
	config_foreach dhcp6c_write_config dhcp6c "$iface" > $config_file
	#dhcp6c_write_duid "$user_duid" > $duid_file

	local debug
	local debug_option
	config_get_bool debug basic debug 0
	[ $debug -eq 1 ] && debug_option="-D"

	# if only one interface, add -t param.
	local iface_otpion
	[ $iaid -eq 2 ] && iface_otpion="-t ${first_cfg}"

	local duid_option
	local duid_ifname
	#[ "${first_iface:0:4}" != "eth0" ] && {
		network_get_physdev duid_ifname "$first_cfg" || duid_ifname=`uci get network.${first_cfg}.ifname`
		[ -n "$duid_ifname" ] && duid_option="-u ${duid_ifname}"
	#}
	local vendor=$(cat /proc/sys/kernel/hostname)
	vendor=$(echo ${vendor// /%})
	local vendor_option="-v 11863,$vendor" # vendor: TP-Link

	if [ -z "$pexec" ]; then
		/usr/sbin/dhcp6c -c $config_file -p $pid_file $vendor_option $debug_option $iface_otpion $duid_option $interfaces
	else
		echo -n "/usr/sbin/dhcp6c -f -c $config_file -p $pid_file $vendor_option $debug_option $iface_otpion $duid_option $interfaces" > "$pexec"
	fi
	#sleep 3
	#ACTION=start /sbin/hotplug-call dhcp6c

	return 0
}

stop() {
	local iface="$1"
	local pexec="$2"
	local config_file="/tmp/dhcp6c/dhcp6c${iface:+.${iface}}.conf"
	local pid_file="/tmp/dhcp6c/dhcp6c${iface:+.${iface}}.pid"

	logger -t dhcp6c stopping dhcp6c

	[ -e "${pid_file}" -a -z "$pexec" ] && {
		# send signal 15 to dhcp6c in order to release ipv6 address.
		local pid=$(cat "${pid_file}")
		kill -s 15 $pid
		sleep 1		
	}

	# stop dhcp6c
	#service_stop /usr/sbin/dhcp6c
	rm -f ${config_file} ${pid_file}
	#ACTION=stop /sbin/hotplug-call dhcp6c

	[ -n "$pexec" ] && rm -rf ${pexec}

	return 0
}

restart() {
	stop $1
	start $1

}

reload() {
	stop $1
	start $1
}
