#!/bin/sh

PATH="/usr/lib/alterator-net-functions:$PATH"

etcnet_iface_dir=/etc/net/ifaces
etcnet_default_iface_dir="$etcnet_iface_dir/default"
resolvconf_rdelim='[[:space:]]\+'
resolvconf_wdelim=' '

. alterator-hw-functions
. shell-config
. shell-var
. shell-ip-address

### IPv4 helpers
valid_ipv4prefix()
{
	[ "$1" -ge 1 -a "$1" -le 32 ] 2>/dev/null
}

# Heavy based on valid_ipv4() from libshell.
# We can't use valid_ipv4() because it
# don't treat 127.* and 224.* addresses as valid addresses.
valid_ipv4addr()
{
	local ipaddr="$1"
	local i=0 byte

	byte="${ipaddr##*.}"
	ipaddr="${ipaddr%.$byte}"

	[ "$byte" -gt 0 -a "$byte" -lt 255 ] 2>/dev/null ||
		return 1

	while [ $i -lt 3 ]; do
		byte="${ipaddr##*.}"

		[ "$byte" != "$ipaddr" ] ||
			break

		ipaddr="${ipaddr%.$byte}"

		[ "$byte" -ge 0 -a "$byte" -lt 255 ] 2>/dev/null ||
			return 1

		i=$(($i+1))
	done

	[ $i -eq 2 ] 2>/dev/null ||
		return 1
}


### IPv6 helpers
is_ipv6_enabled()
{
	[ -e /proc/net/if_inet6 ]
}

valid_ipv6addr()
{
	local c=0 s=

	# Check that there is no single ':' in the beginning and the end
	# of the string
	case "$1" in
		:[0-9a-fA-F]*|*[0-9a-fA-F]:) return 1;;
		*);;
	esac

	local IFS=':'

	for a in $1; do
		c=$(($c + 1))
		[ "$c" -le 8 ] || return 1
		case "$a" in
			[0-9a-fA-F]) ;;
			[0-9a-fA-F][0-9a-fA-F]) ;;
			[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]) ;;
			[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]) ;;
			'') if [ $c -gt 1 ]; then
					[ -z "$s" ] && s=1 || return 1
				fi
				;;
			*) return 1;;
		esac
	done

	if [ "$c" -lt 8 ]; then
		if [ -n "$s" ]; then
			return 0
		else
			return 1
		fi
	else
		return 0
	fi
}

valid_ipv6prefix()
{
	[ "$1" -ge 1 -a "$1" -le 128 ] 2>/dev/null
}

# FIXME: Should replace longest zeros sequence
ipv6addr_terse()
{
	case "$1" in
		*::) echo "$1" | sed -r 's/:[0:]+::$/::/' ;;
		::*) echo "$1" | sed -r 's/^::[0:]+:/::/' ;;
		*::*) echo "$1" ;;
		*) echo "$1" | sed -r 's/(:[0:]+:|^[0:]+:|:[0:]+$)/::/' ;;
	esac | sed -r 's/(^|:)(0+)([[:xdigit:]])/\1\3/g'
}

ipv6addr_expand()
{
	local in="$1"; shift
	local c= tmp= out= z=

	valid_ipv6addr "$in" || return 1
	c=$(echo "$in" | grep -o ":" | wc -l)

	# Replace :: with zero segmets
	case "$in" in
		*::*)
			if [ $c -eq 8 ]; then
				if [ -z "${in%%::*}" ]; then
					# ::a:a:a:a:a:a:a expanded to 0:a:a:a:a:a:a:a
					tmp="0${in#:}"
				elif [ -z "${in##*::}" ]; then
					# a:a:a:a:a:a:a:: expanded to a:a:a:a:a:a:a:0
					tmp="${in%:}0"
				else
					return 1
				fi
			elif [ $c -gt 0 -a $c -le 7 ]; then
				while [ "$c" -le 7 ]; do
					c=$((c + 1))
					z="$z${z:+:}0"
				done

				if [ -z "${in%%::*}" ]; then
					z="0:$z:"
				elif [ -z "${in##*::}" ]; then
					z=":$z:0"
				else
					z=":$z:"
				fi
				tmp="$(echo "$in" | sed s/::/$z/)"
			else
				return 1
			fi
			;;
		*) [ $c -eq 7 ] && tmp="$in" || return 1
			;;
	esac

	# Expand each segment to four hex digits
	local i= s=
	local IFS=:
	for i in $tmp; do
		case $i in
			[0-9a-fA-F]) s=000$i;;
			[0-9a-fA-F][0-9a-fA-F]) s=00$i;;
			[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]) s=0$i;;
			[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]) s=$i;;
		esac
		out="$out${out:+:}$s"
	done

	echo "$out"
}

ipv6_network()
{
	local network= n= r= c= tmp=
	local ip="$(ipv6addr_expand "${1%%/*}")"
	local prefix="${1##*/}"

	if [ -z "$ip" ] || ! valid_ipv6prefix "$prefix"; then
		return 1
	fi

	local IFS=":"
	set -- $ip

	n=$(($prefix / 16))
	r=$(($prefix % 16))
	c=0

	for i; do
		if [ $c -eq $n ]; then
			tmp=$(( ~(0xffff >> $r) & 0xffff & 0x$i))
			if [ $tmp -gt 0 ]; then
				network="$network${network:+:}$(printf "%x" "$tmp")"
			fi
			[ $c -eq 7 -a $tmp -gt 0 ] || network="$network::"
			break
		fi

		network="$network${network:+:}$i"
		c=$(($c + 1))
	done

	echo "$(ipv6addr_terse "$network")/$prefix"
}

ipv6addr_is_in_subnet()
{
	local ip="${1-}"; shift
	local net="${1-}"; shift
	local prefix="${net##*/}"
	local p=$((0xFFFF))
	local pos seg ip_seg net_seg n hex_mask

	valid_ipv6prefix "$prefix" ||
		return 2

	n=$(($prefix % 16))
	hex_mask="$(($p - ($p >> $n)))"

	pos=$(($prefix / 16))
	seg="$(($pos * 4 + $pos))"

	net="$(ipv6addr_expand ${net%%/*})"
	[ -n "$net" ] || return 2
	net_head="${net:0:$seg}"
	# get IPv6 network segment
	net_seg="${net:$seg:4}"

	ip="$(ipv6addr_expand $ip)"
	[ -n "$ip" ] || return 2
	ip_head="${ip:0:$seg}"
	# get IPv6 address segment
	ip_seg="${ip:$seg:4}"

	[ "$net_head" = "$ip_head" ] &&
	[ "$((0x$net_seg & $hex_mask))" -eq "$((0x$ip_seg & $hex_mask))" ] ||
		return 1
}

### common helpers

get_ip_version()
{
	local ip="$1"
	if valid_ipv4addr "$ip"; then
		echo 4
	elif valid_ipv6addr "$ip"; then
		echo 6
	else
		return 1
	fi

	return 0
}

next_iface()
{
	local name="$1";shift
	local i="${1:-0}"
	local path="$etcnet_iface_dir/$name"
	while true; do
		[ -d "$path$i" ] || { echo "$name$i" && break; }
		i=$(($i + 1))
	done
}

### common iface options

write_iface_option()
{
    shell_config_set "$1/options" "$2" "$3"
}

# write_iface_addr <iface_path> <address> [ ip_version ]
write_iface_addr()
{
	local p="${3:-4}"
    echo "$2" >"$1/ipv${p}address"
}

# write_iface_addresses <iface_path> <address_list> [ ip_version ]
write_iface_addresses()
{
	local p="${3:-4}"
	local addresses_file="$1/ipv${p}address"
	>"$addresses_file"
	for i in $2;do
	    echo "$i" >>"$addresses_file"
	done
}

read_iface_option()
{
    # shell_config_get always returns 0,
    # so we can't merge commands with || :(

    local ret="$(shell_config_get "$1/options" "$2")"
    if [ -z "$ret" ]; then
      local type="$(shell_config_get "$1/options" "TYPE")"
      [ -z "$type" ] ||
        ret="$(shell_config_get "$etcnet_default_iface_dir/options-$type" "$2")"
    fi
    if [ -z "$ret" ]; then
      ret="$(shell_config_get "$etcnet_default_iface_dir/options" "$2")"
    fi
    echo "$ret"
}

__ipv4_addr_start_re='^[0-9]'
__ipv6_addr_start_re='^[[:xdigit:]:]'

__ipv4_network_re="[[:space:]]+inet[[:space:]]+([.0-9/]+).*"
__ipv6_network_re="[[:space:]]+inet6[[:space:]]+([[:xdigit:]:/]+).*"

# read_iface_addr <iface_path> [ ip_version ]
read_iface_addr()
{
	local p="${2:-4}" r=
	eval "r=\$__ipv${p}_addr_start_re"
    [ ! -s "$1/ipv${p}address" ] || grep -m 1 "$r" "$1/ipv${p}address"
}

# read_iface_addresses <iface_path> [ ip_version ]
read_iface_addresses()
{
    local retval
    local v=
	local p="${2:-4}" r=

	eval "r=\$__ipv${p}_addr_start_re"
    [ ! -s "$1/ipv${p}address" ] || v=$(grep "$r" "$1"/ipv${p}address| tr '\n' ' ')
    shell_var_trim retval "$v"
    echo "$retval"
}

# read_iface_current_addr <iface_path> [ ip_version ]
read_iface_current_addr()
{
	local name="${1##*/}"
	local p="${2:-4}" r=
	eval "r=\$__ipv${p}_network_re"
	/sbin/ip -$p a s "$name" 2>/dev/null|sed -r -n "s,$r,\1,p" | head -n1
}

# read_iface_current_addr <iface_path> [ ip_version ]
read_iface_current_addresses()
{
	local name="${1##*/}"
	local p="${2:-4}" r=
	local p="${2:-4}" r=
	eval "r=\$__ipv${p}_network_re"
	/sbin/ip -$p a s "$name" 2>/dev/null|sed -r -n "s,$r,\1,p"
}

__default_ipv4_gw_re='^[[:space:]]*default[[:space:]]\+via[[:space:]]\([0-9.]\+\).*'
__default_ipv6_gw_re='^[[:space:]]*default[[:space:]]\+via[[:space:]]\([[:xdigit:]:]\+\).*'

# read_iface_default_gw <iface_path> [ ip_version ]
read_iface_default_gw()
{
	local p="${2:-4}" r=

	eval "r=\$__default_ipv${p}_gw_re"
    [ ! -s "$1/ipv${p}route" ] ||
	sed -n -e "s/$r/\1/p" \
	       -e 't l1' \
	       -e 'b' \
	       -e ': l1' \
	       -e 'q' \
	       "$1/ipv${p}route"
}

# write_iface_default_gw <iface_path> <address> [ ip_version ]
write_iface_default_gw()
{
	local p="${3:-4}" r=

	eval "r=\$__default_ipv${p}_gw_re"
    [ ! -s "$1/ipv${p}route" ] ||
	sed "/$r/ d" -i "$1/ipv${p}route"
    [ -z "$2" ] ||
	printf 'default via %s\n' "$2" >>"$1/ipv${p}route"
}

read_iface_search()
{
    local retval
    local v="$(shell_config_get "$1/resolv.conf" search "$resolvconf_rdelim"| tr '\n' ' ')"
    shell_var_trim retval "$v"
    echo "$retval"
}

write_iface_search()
{
	local resolvconf_file="$1/resolv.conf"

	shell_config_del "$resolvconf_file" search "$resolvconf_rdelim"
	[ -z "$2" ] || shell_config_set "$resolvconf_file" search "$2" "$resolvconf_rdelim" "$resolvconf_wdelim"

	[ -s "$resolvconf_file" ] || rm -f -- "$resolvconf_file"
}

read_iface_dns()
{
    local retval
    local v="$(shell_config_get "$1/resolv.conf" nameserver "$resolvconf_rdelim"| tr '\n' ' ')"
    shell_var_trim retval "$v"
    echo "$retval"
}

write_iface_dns()
{
	local resolvconf_file="$1/resolv.conf"

	shell_config_del "$resolvconf_file" nameserver "$resolvconf_rdelim"

	local IFS=' '
	for i in $2;do
	    printf 'nameserver %s\n' "$i" >>"$resolvconf_file"
	done

	[ -s "$resolvconf_file" ] || rm -f -- "$resolvconf_file"
}

### string ppp options

write_ppp_option()
{
    shell_config_set "$1/pppoptions" "$2" "$3" '[[:space:]]\+' ' '
    chmod o-rw "$name/pppoptions"
}

read_ppp_option()
{
    shell_config_get "$1/pppoptions" "$2" '[[:space:]]\+'
}

### boolean ppp options

read_ppp_option1()
{
    local file="$1";shift
    local name1="$1";shift
    local name2="$1";shift
    local defvalue="${1:-no}";shift ||:

    if grep -qs "^$name1\$" "$file/pppoptions";then
	echo 'yes'
    elif grep -qs "^$name2\$" "$file/pppoptions"; then
	echo 'no'
    else
	echo "$defvalue"
    fi
}

write_ppp_option1()
{
    local file="$1";shift
    local name1="$1";shift
    local name2="$1";shift
    local value="$1";shift

    [ -f "$file/pppoptions" ] || touch "$file/pppoptions"

    sed \
	-e "/^$name1/d" \
	-e "/^$name2/d " \
	-i "$file/pppoptions"

    if [ "$value" -eq 0 ]; then
	echo "$name1" >> "$file/pppoptions"
    else
	echo "$name2" >> "$file/pppoptions"
    fi
}

next_ppp()
{
    # not to interfere with dialup ppp0
    next_iface ppp 1
}

### list various interface types

list_ppp()
{
    local t="${1:-}"
    for i in `find "$etcnet_iface_dir" -type d`; do
	[ "$(read_iface_option "$i" TYPE)" != "ppp" ] ||
	[ -n "$t" -a "$(read_iface_option "$i" PPPTYPE)" != "$t" ] ||
	    echo "${i##*/}" 2>/dev/null
    done
}

list_eth()
{
    netdev_list|
	while read iface; do
	    netdev_is_eth "$iface" || continue
	    netdev_is_real "$iface" || continue

	    local b="$(netdev_find_bridge "$iface")"
	    echo "${b:-$iface}" 2>/dev/null
	done
}

list_iface()
{
    netdev_list|
	while read iface; do
	    [ "$iface" != "lo" ] || continue
	    [ -z "$(netdev_find_bridge "$iface")" ] || continue
	    local tf="/sys/class/net/$iface/type"
	    [ ! -f "$tf" ] || [ "$(cat "$tf")" != 801 ] || continue
	    echo "$iface" 2>/dev/null
	done
}

# list_static_iface [ ip_version ]
list_static_iface()
{
	local p="${1:-4}"

    for i in `find "$etcnet_iface_dir" -type d`; do
	local name="${i##*/}"
	[ "$name" != "lo" -a "$name" != "default" -a "$name" != "unknown" ] || continue
	[ "$(read_iface_option $i BOOTPROTO)" = "static" -a -s "$i/ipv${p}address" ] || continue
	echo "${i##*/}"
    done
}

# list_network [ ip_version ]
list_network()
{
	local p="${1:-4}" r=

	eval "r=\$__ipv${p}_network_re"
    for i in `find "$etcnet_iface_dir" -type d`; do
	local name="${i##*/}"
	[ "$name" != "lo" -a "$name" != "default" -a "$name" != "unknown" ] || continue

	local proto="$(read_iface_option $i BOOTPROTO)"
	local addr=
	if [ "$proto" = "static" ];then
	    addr="$(read_iface_addresses $i "$p")"
	else
	    addr="$(read_iface_current_addresses $i "$p")"
	fi
	if [ "$p" = 4 ]; then
		[ -z "$addr" ] || netname "$addr" | cut -f1
	else
		[ -z "$addr" ] || ipv6_network "$addr"
	fi
    done
}

### start/stop interfaces

iface_up()
{
    local iface="$1";shift
    local n="${1:-0}";shift

    env -i PATH="$PATH" HOME="$HOME" TMPDIR="$TMPDIR" /sbin/ifup "$iface" >/dev/null
    for i in $(seq 0 "$n"); do
	netdev_is_up "$iface" && return 0
	[ "$i" != "$n" ] || sleep 1
    done
    return 1
}

iface_down()
{
    local iface="$1";shift
    local n="${1:-0}";shift

    env -i PATH="$PATH" HOME="$HOME" TMPDIR="$TMPDIR" /sbin/ifdown "$iface" >/dev/null
    for i in $(seq 0 "$n"); do
	netdev_is_up "$iface" || return 0
	[ "$i" != "$n" ] || sleep 1
    done
    return 1
}

### ipv4address calculations
# For backward compatibility only.
# In new code use functions from shell-ip-address directly.
ipv4addr_is_in_subnet()
{
	ipv4_ip_subnet "$@"
}

ipv4addr_mask_to_prefix()
{
	ipv4_mask2prefix "$@"
}

ipv4addr_prefix_to_mask()
{
	ipv4_prefix2mask "$@"
}
