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