]> git.datanom.net - pve-dhcp-server.git/blobdiff - DHCPServer.pm
Added lease maintainance monitor
[pve-dhcp-server.git] / DHCPServer.pm
index 10d72cb1181eeb9a8309b79e763788a7418a0219..8084c3f98670de6b0cb555c20f9d5e176ed2cc93 100644 (file)
@@ -432,6 +432,42 @@ my $read_config = sub {
        }
 };
 
+my $cleanup_leases = sub {
+       my ($self, $last_run) = @_;
+       my ($current, $last, $lease, $dirty);
+
+    $self->{INTERVAL} = 5 if $self->{INTERVAL} <= 0;
+    $current = time;
+    $last = $last_run + ($self->{INTERVAL} * 60);
+
+       $self->$logger("Run 'cleanup_leases' $last < $current", INFO);
+
+       if ($last < $current) {
+               $last_run = $current;
+               my $leases = $self->{_leases};
+               $dirty = 0;
+               while ((my $lease, my $elems) = each (%$leases)) {
+                       $self->$logger("Clean up lease: $lease\n". Dumper($elems));
+                       if ($elems->{ends} < $last_run) {
+                               $self->$logger("Considering $lease for clean up: $elems->{ends} < $last_run\n". Dumper($elems));
+                               if ($elems->{'binding state'} eq 'active') {
+                                       $self->$logger("Setting $lease 'binding state' to free", INFO);
+                                       $elems->{'binding state'} = 'free';
+                                       $dirty = 1;
+                               }
+                       }
+               }
+               if ($dirty) {
+                       my $res = $self->$write_lease_file();
+                       if ($res) {
+                               $self->$logger("Updated lease file", INFO);
+                       }
+               }
+       }
+
+       return $last_run;
+};
+
 #########################################################################
 #  Private methods which handle DHCP4 requests
 #########################################################################
@@ -1004,6 +1040,7 @@ sub new {
        $self->{timeout}                        ||= 10;
        $self->{lease_file}                     ||= '/tmp/dhcpd.leases';
        $self->{conf_file}                      ||= '/tmp/dhcpd.cfg';
+       $self->{INTERVAL}                       ||= 5;
 
     return $self;
 }
@@ -1078,6 +1115,8 @@ sub run {
        $sel = IO::Select->new($self->{_sock_in_ip4});
        $sel->add($self->{_sock_in_ip6});
 
+       my $last_run = time;
+
        until ($time_to_die) {
                my $buf = undef;
                my $fromaddr;
@@ -1086,6 +1125,7 @@ sub run {
                eval {  # catch fatal errors
                        while (@ready = $sel->can_read) {
                                $self->$logger("Waiting for incoming packet", INFO);
+                               $last_run = $self->$cleanup_leases($last_run);
                                foreach $socket (@ready) {
                                        if ($socket == $self->{_sock_in_ip4}) {
                                                # receive ipv4 packet
This page took 0.046728 seconds and 5 git commands to generate.