-SED=/bin/sed # used to extract the profile name
-DEBUG=0 # 0 disable debug messages
-prog=$0 # this program
-
-# Print the first argument and exit
-abort()
-{
- echo "$1"; exit 1
-}
-
-# if $DEBUG is enabled, print a debug message
-# $1 the message to print
-debug()
-{
- [ "${DEBUG}" != "0" ] && echo "$1" >> ${LOGFILE}
-}
-
-# print the usage and exit
-help()
-{
- cat << EOF
-Usage: $prog -p port [-U] [-t timeout[STRING]] ipfw_configuration_parameters \n
- $prog ipfw show \n
- $prog pipe show \n
-
- -h show help \n
- -p specify the port to configure \n
- -d delete specified rules and pipes \n
- -t specify the timeout. STRING can be "1minute" or "4hours" (default 1 hour) \n
-
- ipfw configuration parameters (mandatory) \n
- extra-delay <filename> bw <value> delay <value> proto... plr... \n
-
- ipfw show show the ipfw ruleset
- ipfw pipe show the configured pipes
-EOF
- exit 0
-}
-
-# parse input line and fill missing fields
-parse_input()
-{
- # counters used to check if at least
- # two ipfw configuration parameters are present
- local OPT_ARGS=2 # optional (timeout)
- local MAND_ARGS=2 # mandatory (port)
-
- [ -z "$1" ] && help;
-
- while getopts ":hdp:t:" opt; do
- case $opt in
- h | \?) help;;
- p) PORT=$OPTARG;;
- d) DELETE=1;;
- t) TIMEOUT=$OPTARG;;
- esac
- done
-
- # check for mandatory arguments
- [ -z ${PORT} ] && abort "a port value is mandatory";
-
- # the default value for the timeout is 1H
- if [ -z ${TIMEOUT} ]; then
- TIMEOUT="1hour"
- OPT_ARGS=$((${OPT_ARGS}-2))
- fi
-
- # check for deletion flag
- if [ -z ${DELETE} ]; then
- DELETE=0
- else
- # if d is present configuration is not required
- MAND_ARGS=$((${MAND_ARGS}-2))
- fi
-
- # compute residue argument, we need at least 2
- # mandatory arguments (for ipfw), exit on error
- # debug "Passed args $# Mandatory ${MAND_ARGS} Optional ${OPT_ARGS} Extra $(($#-${MAND_ARGS}-${OPT_ARGS}))"
- if [ $(($#-${MAND_ARGS}-${OPT_ARGS})) -lt 2 ]; then
- help
- fi
-}
-
-# the backend expects lines in the format:
-# ipfw
-# pipe
-# port timeout configuration_string
-# main starts here
-
- # allow ipfw show and ipfw pipe show commands
- if [ x$1 = x"ipfw" ]; then
- echo "received" $1
- echo "ipfw" >> ${PIPE_IN}
- cat ${PIPE_OUT}
- exit 0;
- else if [ x$1 = x"pipe" ]; then
- echo "pipe" >> ${PIPE_IN}
- cat ${PIPE_OUT}
- exit 0;
- fi
- fi
-
- # parse the input
- parse_input $*
- shift $((${OPTIND}-1));
-
- # print debug
- debug "PORT ${PORT}"
- debug "DELETE ${DELETE}"
- debug "TIMEOUT ${TIMEOUT}"
- debug "PIPE_CONFIGURATION $*"
-
- # format CMD as expected by the backend script
- CMD="${PORT} ${TIMEOUT} ${DELETE} $*";
-
- # send the command
- debug "Sending: ${CMD}"
- echo ${CMD} >> ${PIPE_IN}
-
- cat ${PIPE_OUT}