Always release the reference to the sock structure.
[ipfw.git] / ipfw-cleanup
1 #!/bin/sh
2 #
3 # Marta Carbone
4 # Copyright (C) 2009 Universita` di Pisa
5 # $Id$
6 #
7 # This script parse the ipfw rules
8 # and remove the old ones.
9 #
10 # The ipfw output is parsed and each time
11 # value stored as comment is compared against
12 # the current time.
13 # If the time value is older than current,
14 # the rules and related pipes will be deleted.
15 #
16 # $Id$
17
18 RULE_LIST="ipfw show"
19 # Get $NOW referred to UTC
20 NOW=`date -u +%s`
21
22 # check for module existence
23 /sbin/lsmod | grep ipfw
24 if [ x"$?" == x"1" ]; then
25         echo "ipfw module does not exist";
26         exit 0;
27 fi
28
29 ${RULE_LIST} | 
30 awk '
31         BEGIN {
32                 print now a "Start to clean rules ";
33                 cleaned=0;
34         }
35
36         # delete rules and pipes
37         function delete_rule(rule_id) {
38                 command="/sbin/ipfw delete " rule_id "; ipfw pipe delete " rule_id;
39                 system(command);
40         }
41
42         # awk main body
43         /\/\/\ [0-9]*/ { # select timeout string
44
45                         timeout=$13;
46
47                         if (now > timeout) {
48                                 delete_rule($1);
49                                 cleaned++;
50                         }
51         }
52
53         END {   print " " cleaned " rules cleaned";
54         }
55 ' now=${NOW}