50fe7819c11a18819064e2b044b34f66f885ae00
[ipfw.git] / slice / netconfig
1 #!/bin/sh
2 #
3 # Marta Carbone
4 # Copyright (C) 2009 Universita` di Pisa
5 # $Id$
6 #
7 # This script is the frontend to be used with
8 # the vsys system.
9 # It allows to configure dummynet pipes and queues.
10 # In detail it:
11 # - get user input
12 # - send command to the vsys input pipe
13 # - wait a bit
14 # - get the reply from the output vsys pipe
15 # - show the output to the user
16
17 # A simple usage is:
18 # ./netconfig -p 9898 <dummynet configuration parameters>
19 # mandatory fields are the port to be configured
20 # and dummynet configuration parameters
21
22 PIPE_IN=/vsys/ipfw-be.in
23 PIPE_OUT=/vsys/ipfw-be.out
24 LOGFILE=/tmp/netconfig.log
25
26 SED=/bin/sed            # used to extract the profile name
27 DEBUG=0                 # 0 disable debug messages
28 prog=$0                 # this program
29
30 # Print the first argument and exit
31 abort()
32 {
33         echo "$1"; exit 1
34 }
35
36 # if $DEBUG is enabled, print a debug message
37 # $1 the message to print
38 debug()
39 {
40         [ "${DEBUG}" != "0" ] && echo "$1" >> ${LOGFILE}
41 }
42
43 # print the usage and exit
44 help()
45 {
46         cat << EOF
47 Usage: $prog [-n node_ip][-s slice] [-i key] -d dummynetbox_ip -p port [-U] [-t timeout[STRING]] ipfw_configuration_parameters \n
48   -h show help \n
49   -p specify the port to configure \n
50   -t specify the timeout. STRING can be "1minute" or "4hours" (default 1 hour) \n
51
52    ipfw configuration parameters (mandatory) \n
53            extra-delay <filename> bw <value> delay <value> proto... plr... \n
54 EOF
55         exit 0
56 }
57
58 # parse input line and fill missing fields
59 parse_input()
60 {
61         # counters used to check if at least
62         # two ipfw configuration parameters are present
63         local OPT_ARGS=2        # optional (timeout)
64         local MAND_ARGS=2       # mandatory (port)
65
66         [ -z "$1" ] && help;
67
68         while getopts ":hp:t:" opt; do
69                 case $opt in
70                 h | \?) help;;
71                 p) PORT=$OPTARG;;
72                 t) TIMEOUT=$OPTARG;;
73                 esac
74         done
75
76         # check for mandatory arguments
77         [ -z ${PORT} ] && abort "a port value is mandatory";
78
79         # the default value for the timeout is 1H
80         if [ -z ${TIMEOUT} ]; then
81                 TIMEOUT="1hour"
82                 OPT_ARGS=$((${OPT_ARGS}-2))
83         fi
84
85         # compute residue argument, we need at least 2 
86         # mandatory arguments (for ipfw), exit on error
87         #debug "Passed args $# Mandatory ${MAND_ARGS} Optional ${OPT_ARGS} Extra $(($#-${MAND_ARGS}-${OPT_ARGS}))"
88         if [ $(($#-${MAND_ARGS}-${OPT_ARGS})) -lt 2 ]; then
89                 help
90         fi
91 }
92
93 # main starts here
94
95         # allow ipfw show and ipfw pipe show
96         if [ x$1 = x"ipfw" ]; then
97                 echo "received" $1
98                 echo "ipfw" >> ${PIPE_IN}
99                 cat ${PIPE_OUT}
100                 exit 0;
101         else if [ x$1 = x"pipe" ]; then
102                 echo "pipe" >> ${PIPE_IN}
103                 cat ${PIPE_OUT}
104                 exit 0;
105         fi
106         fi
107
108         # parse the input
109         parse_input $*
110         shift $((${OPTIND}-1));
111
112         # print debug
113         debug "PORT ${PORT}"
114         debug "TIMEOUT ${TIMEOUT}"
115         debug "PIPE_CONFIGURATION $*"
116
117         # format CMD as expected by the backend script
118         CMD="${PORT} ${TIMEOUT} $*";
119
120         # send the command
121         debug "Sending: ${CMD}"
122         echo ${CMD} >> ${PIPE_IN}
123
124         cat ${PIPE_OUT}