X-Git-Url: http://git.datanom.net/pve-dhcp-server.git/blobdiff_plain/21630ca358e995111f707f4d29094aeffa27ce48..HEAD:/DHCPServer.pm?ds=sidebyside diff --git a/DHCPServer.pm b/DHCPServer.pm index 10d72cb..8084c3f 100644 --- a/DHCPServer.pm +++ b/DHCPServer.pm @@ -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