Home | History | Annotate | Download | only in examples
      1 #!/bin/sh
      2 
      3 #
      4 # Setup address label from /etc/gai.conf
      5 #
      6 # Written by YOSHIFUJI Hideaki <yoshfuji (at] linux-ipv6.org>, 2010.
      7 #
      8 
      9 IP=ip
     10 DEFAULT_GAICONF=/etc/gai.conf
     11 verbose=
     12 debug=
     13 
     14 function run ()
     15 {
     16 	if [ x"$verbose" != x"" ]; then
     17 		echo "$@"
     18 	fi
     19 	if [ x"$debug" = x"" ]; then
     20 		"$@"
     21 	fi
     22 }
     23 
     24 function do_load_config ()
     25 {
     26 	file=$1; shift
     27 	flush=1
     28 	cat $file | while read command prefix label; do
     29 		if [ x"$command" = x"#label" ]; then
     30 			if [ ${flush} = 1 ]; then
     31 				run ${IP} -6 addrlabel flush
     32 				flush=0
     33 			fi
     34 			run ${IP} -6 addrlabel add prefix $prefix label $label
     35 		fi
     36 	done
     37 }
     38 
     39 function do_list_config ()
     40 {
     41 	${IP} -6 addrlabel list | while read p pfx l lbl; do
     42 		echo label ${pfx} ${lbl}
     43 	done
     44 }
     45 
     46 function help ()
     47 {
     48 	echo "Usage: $0 [-v] {--list | --config [ ${DEFAULT_GAICONF} ] | --default}"
     49 	exit 1
     50 }
     51 
     52 TEMP=`getopt -o c::dlv -l config::,default,list,verbose -n gaiconf -- "$@"`
     53 
     54 if [ $? != 0 ]; then
     55 	echo "Terminating..." >&2
     56 	exit 1
     57 fi
     58 
     59 TEMPFILE=`mktemp`
     60 
     61 eval set -- "$TEMP"
     62 
     63 while true ; do
     64 	case "$1" in
     65 		-c|--config)
     66 			if [ x"$cmd" != x"" ]; then
     67 				help
     68 			fi
     69 			case "$2" in
     70 			"")	gai_conf="${DEFAULT_GAICONF}"
     71 				shift 2
     72 				;;
     73 			*)	gai_conf="$2"
     74 				shift 2
     75 			esac
     76 			cmd=config
     77 			;;
     78 		-d|--default)
     79 			if [ x"$cmd" != x"" ]; then
     80 				help
     81 			fi
     82 			gai_conf=${TEMPFILE}
     83 			cmd=config
     84 			;;
     85 		-l|--list)
     86 			if [ x"$cmd" != x"" ]; then
     87 				help
     88 			fi
     89 			cmd=list
     90 			shift
     91 			;;
     92 		-v)
     93 			verbose=1
     94 			shift
     95 			;;
     96 		--)
     97 			shift;
     98 			break
     99 			;;
    100 		*)
    101 			echo "Internal error!" >&2
    102 			exit 1
    103 			;;
    104 	esac
    105 done
    106 
    107 case "$cmd" in
    108 	config)
    109 		if [ x"$gai_conf" = x"${TEMPFILE}" ]; then
    110 			sed -e 's/^[[:space:]]*//' <<END_OF_DEFAULT >${TEMPFILE}
    111 				label ::1/128       0
    112 				label ::/0          1
    113 				label 2002::/16     2
    114 				label ::/96         3
    115 				label ::ffff:0:0/96 4
    116 				label fec0::/10     5
    117 				label fc00::/7      6
    118 				label 2001:0::/32   7
    119 END_OF_DEFAULT
    120 		fi
    121 		do_load_config "$gai_conf"
    122 		;;
    123 	list)
    124 		do_list_config
    125 		;;
    126 	*)
    127 		help
    128 		;;
    129 esac
    130 
    131 rm -f "${TEMPFILE}"
    132 
    133 exit 0
    134 
    135