1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3
4NR_FILES=32768
5SAVED_NR_FILES=$(ulimit -n)
6
7# default values
8port=443
9addr_v6="2001:0db8:0:f101::1"
10addr_v4="10.8.8.8"
11use_v6=true
12addr=""
13
14usage() {
15    echo "Usage: $0 [-6 | -4] [-p port] [-a address]"
16    echo -e "\t6: use ipv6"
17    echo -e "\t4: use ipv4"
18    echo -e "\tport: Port number"
19    echo -e "\taddress: ip address"
20}
21
22while getopts "ha:p:64" opt; do
23    case ${opt} in
24	h)
25	    usage $0
26	    exit 0
27	    ;;
28	a)  addr=$OPTARG;;
29	p)
30	    port=$OPTARG;;
31	6)
32	    use_v6=true;;
33	4)
34	    use_v6=false;;
35    esac
36done
37
38setup() {
39    if [[ "$use_v6" == true ]]; then
40	ip addr add $addr_v6 nodad dev eth0
41    else
42	ip addr add $addr_v4 dev lo
43    fi
44	ulimit -n $NR_FILES
45}
46
47cleanup() {
48    if [[ "$use_v6" == true ]]; then
49	ip addr del $addr_v6 dev eth0
50    else
51	ip addr del $addr_v4/32 dev lo
52    fi
53    ulimit -n $SAVED_NR_FILES
54}
55
56if [[ "$addr" != "" ]]; then
57    addr_v4=$addr;
58    addr_v6=$addr;
59fi
60setup
61if [[ "$use_v6" == true ]] ; then
62    ./bind_bhash $port "ipv6" $addr_v6
63else
64    ./bind_bhash $port "ipv4" $addr_v4
65fi
66cleanup
67