efad8e7f984e628770f1b26b7a80b3a29ec2f23d
[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 -p port [-U] [-t timeout[STRING]] ipfw_configuration_parameters \n
48        $prog ipfw show  \n
49        $prog pipe show  \n
50
51   -h show help \n
52   -p specify the port to configure \n
53   -d delete specified rules and pipes \n
54   -t specify the timeout. STRING can be "1minute" or "4hours" (default 1 hour) \n
55
56    ipfw configuration parameters (mandatory) \n
57            extra-delay <filename> bw <value> delay <value> proto... plr... \n
58
59    ipfw show show the ipfw ruleset
60    ipfw pipe show the configured pipes
61 EOF
62         exit 0
63 }
64
65 # parse input line and fill missing fields
66 parse_input()
67 {
68         # counters used to check if at least
69         # two ipfw configuration parameters are present
70         local OPT_ARGS=2        # optional (timeout)
71         local MAND_ARGS=2       # mandatory (port)
72
73         [ -z "$1" ] && help;
74
75         while getopts ":hdp:t:" opt; do
76                 case $opt in
77                 h | \?) help;;
78                 p) PORT=$OPTARG;;
79                 d) DELETE=1;;
80                 t) TIMEOUT=$OPTARG;;
81                 esac
82         done
83
84         # check for mandatory arguments
85         [ -z ${PORT} ] && abort "a port value is mandatory";
86
87         # the default value for the timeout is 1H
88         if [ -z ${TIMEOUT} ]; then
89                 TIMEOUT="1hour"
90                 OPT_ARGS=$((${OPT_ARGS}-2))
91         fi
92
93         # check for deletion flag
94         if [ -z ${DELETE} ]; then
95                 DELETE=0
96         else
97                 # if d is present configuration is not required
98                 MAND_ARGS=$((${MAND_ARGS}-2))
99         fi
100
101         # compute residue argument, we need at least 2 
102         # mandatory arguments (for ipfw), exit on error
103         # debug "Passed args $# Mandatory ${MAND_ARGS} Optional ${OPT_ARGS} Extra $(($#-${MAND_ARGS}-${OPT_ARGS}))"
104         if [ $(($#-${MAND_ARGS}-${OPT_ARGS})) -lt 2 ]; then
105                 help
106         fi
107 }
108
109 # the backend expects lines in the format:
110 # ipfw
111 # pipe
112 # port timeout configuration_string
113 # main starts here
114
115         # allow ipfw show and ipfw pipe show commands
116         if [ x$1 = x"ipfw" ]; then
117                 echo "received" $1
118                 echo "ipfw" >> ${PIPE_IN}
119                 cat ${PIPE_OUT}
120                 exit 0;
121         else if [ x$1 = x"pipe" ]; then
122                 echo "pipe" >> ${PIPE_IN}
123                 cat ${PIPE_OUT}
124                 exit 0;
125         fi
126         fi
127
128         # parse the input
129         parse_input $*
130         shift $((${OPTIND}-1));
131
132         # print debug
133         debug "PORT ${PORT}"
134         debug "DELETE ${DELETE}"
135         debug "TIMEOUT ${TIMEOUT}"
136         debug "PIPE_CONFIGURATION $*"
137
138         # format CMD as expected by the backend script
139         CMD="${PORT} ${TIMEOUT} ${DELETE} $*";
140
141         # send the command
142         debug "Sending: ${CMD}"
143         echo ${CMD} >> ${PIPE_IN}
144
145         cat ${PIPE_OUT}