Added the new version for dummynet.
[ipfw.git] / planetlab / ipfw-cleanup
1 #!/bin/sh
2 #
3 # Marta Carbone
4 # Copyright (C) 2009 Universita` di Pisa
5 #
6 # This script parse the ipfw rules
7 # and remove the old ones.
8 #
9 # The ipfw output is parsed and each time
10 # value stored as comment is compared against
11 # the current time.
12 # If the time value is older than current,
13 # the rules and related pipes will be deleted.
14 #
15 # $Id:$
16
17 DEBUG=0         # 1 to enable debug messages
18 LOG_FILE=/tmp/ipfw.log
19 # variable shared with the vsys ipfw-be backend
20 DBFILE=/tmp/ff
21
22 debug() # $1 debug message
23 {
24         if [ ! $DEBUG ]; then
25                 echo "$1" >> $LOG_FILE
26         fi
27 }
28
29 # Get $NOW referred to UTC
30 NOW=`date -u +%s`
31
32 # check for module existence
33 /sbin/lsmod | grep ipfw >> /dev/null
34 if [ x"$?" == x"1" ]; then
35         debug "ipfw module does not exist"
36         exit 0;
37 fi
38
39 cat ${DBFILE} | 
40 awk '
41         BEGIN {
42                 system("echo Start to clean rules >> /tmp/ipfw.log");
43                 cleaned=0;
44         }
45
46         # awk main body
47         {
48                 slice_id=$1;
49                 type=$2;
50                 port=$3;
51                 timeout=$6;
52                 if (now > timeout) {
53                         # call the backend script to cleanup expired rules
54                         command="echo delete " type " " port " | /vsys/ipfw-be ";
55                         # grep for the username
56                         command=command "`grep :" slice_id ": /etc/passwd | cut -d ':' -f 1`";
57                         system(command);
58                         cleaned++;
59                 }
60         }
61
62         END {   system("echo " cleaned " rules cleaned >> /tmp/ipfw.log");
63         }
64 ' now=${NOW}