1#!/bin/sh
2
3set -eu
4
5ping_once()
6{
7	type ping${1} >/dev/null 2>&1 && PING="ping${1}" || PING="ping -${1}"
8	$PING -q -c 1 -W 1 ${2%%/*} >/dev/null 2>&1
9}
10
11wait_for_ip()
12{
13	local _i
14	echo -n "Wait for testing IPv4/IPv6 to become available "
15	for _i in $(seq ${MAX_PING_TRIES}); do
16		echo -n "."
17		if ping_once 4 ${TEST_IPv4} && ping_once 6 ${TEST_IPv6}; then
18			echo " OK"
19			return
20		fi
21	done
22	echo 1>&2 "ERROR: Timeout waiting for test IP to become available."
23	exit 1
24}
25
26setup()
27{
28	# Create testing interfaces not to interfere with current environment.
29	ip link add dev ${TEST_IF} type veth peer name ${TEST_IF_PEER}
30	ip link set ${TEST_IF} up
31	ip link set ${TEST_IF_PEER} up
32
33	ip -4 addr add ${TEST_IPv4} dev ${TEST_IF}
34	ip -6 addr add ${TEST_IPv6} dev ${TEST_IF}
35	wait_for_ip
36}
37
38cleanup()
39{
40	ip link del ${TEST_IF} 2>/dev/null || :
41	ip link del ${TEST_IF_PEER} 2>/dev/null || :
42}
43
44main()
45{
46	trap cleanup EXIT 2 3 6 15
47	setup
48	./test_sock_addr setup_done
49}
50
51BASENAME=$(basename $0 .sh)
52TEST_IF="${BASENAME}1"
53TEST_IF_PEER="${BASENAME}2"
54TEST_IPv4="127.0.0.4/8"
55TEST_IPv6="::6/128"
56MAX_PING_TRIES=5
57
58main
59