| 1 | package POE::Filter::DHCPd::Lease; | ||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||
| 3 | =head1 NAME | ||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||
| 5 | POE::Filter::DHCPd::Lease - parses leases from isc dhcpd leases file | ||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||
| 7 | =head1 VERSION | ||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||
| 9 | 0.0703 | ||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||
| 11 | =cut | ||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||
| 13 | 3 | our $VERSION = '0.0703'; | |||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||
| 15 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 16 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 17 | 3 | ▶ | use base qw/POE::Filter/; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 18 | 3 | ▶ | use Time::Local; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 19 | 3 | ▶ | use constant BUFFER => 0; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 20 | 3 | ▶ | use constant LEASE => 1; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 21 | 3 | ▶ | use v5.10; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||
| 23 | 3 | our $DATE = qr# (\d{4})/(\d\d)/(\d\d) \s (\d\d):(\d\d):(\d\d) #mx; | |||||||||||||||||||||||||||||||
| 24 | 3 | our $START = qr#^ lease \s ([\d\.]+) \s \{ #mx; | |||||||||||||||||||||||||||||||
| 25 | 3 | our $END = qr# } [\n\r]+ #mx; | |||||||||||||||||||||||||||||||
| 26 | 3 | our $PARSER = qr / (?: (?<name>starts) \s\d+\s (?<value>.+?) | |||||||||||||||||||||||||||||||
| 27 | | (?<name>ends) \s\d+\s (?<value>.+?) | ||||||||||||||||||||||||||||||||
| 28 | | ^\s*(?<name>binding) \s state \s (?<value>\S+) | ||||||||||||||||||||||||||||||||
| 29 | | ^\s*(?<name>next) \s binding \s state \s (?<value>\S+) | ||||||||||||||||||||||||||||||||
| 30 | | hardware \s (?<name>ethernet) \s (?<value>\S+) | ||||||||||||||||||||||||||||||||
| 31 | | option \s agent.(?<name>remote-id) \s (?<value>.+?) | ||||||||||||||||||||||||||||||||
| 32 | | option \s agent.(?<name>circuit-id) \s (?<value>.+?) | ||||||||||||||||||||||||||||||||
| 33 | | client-(?<name>hostname) \s "(?<value>[^"]+)" | ||||||||||||||||||||||||||||||||
| 34 | ) /mx; | ||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||
| 36 | =head1 METHODS | ||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||
| 38 | =head2 new | ||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||
| 40 | my $filter = POE::Filter::DHCPd::Lease->new; | ||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||
| 42 | =cut | ||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||
| 44 | 1 | ▶ | sub new { | ||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 45 | 1 | my $class = shift; | |||||||||||||||||||||||||||||||
| 46 | 1 | return bless [ q(), undef ], $class; | |||||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||
| 49 | =head2 get_one_start | ||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||
| 51 | $self->get_one_start($stream); | ||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||
| 53 | C<$stream> is an array-ref of data, that will eventually be parsed into a | ||||||||||||||||||||||||||||||||
| 54 | qualified lease, returned by L<get()> or L<get_one>. | ||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||
| 56 | =cut | ||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||
| 58 | 24 | ▶ | sub get_one_start { | ||||||||||||||||||||||||||||||
|
Subroutine
get_one_start: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 59 | 24 | my $self = shift; | |||||||||||||||||||||||||||||||
| 60 | 24 | my $data = shift; # array-ref of data | |||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||
| 62 | 24 | $self->[BUFFER] .= join '', @$data; | |||||||||||||||||||||||||||||||
| 63 | 24 | return; | |||||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||
| 66 | =head2 get_one | ||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||
| 68 | $leases = $self->get_one; | ||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||
| 70 | C<$leases> is an array-ref, containing zero or one leases. | ||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||
| 72 | starts => epoch value | ||||||||||||||||||||||||||||||||
| 73 | ends => epoch value | ||||||||||||||||||||||||||||||||
| 74 | binding => "active" or "free" | ||||||||||||||||||||||||||||||||
| 75 | hw_ethernet => 12 chars, without ":" | ||||||||||||||||||||||||||||||||
| 76 | hostname => the client hostname | ||||||||||||||||||||||||||||||||
| 77 | circuit_id => circuit id from relay agent (option 82) | ||||||||||||||||||||||||||||||||
| 78 | remote_id => remote id from relay agent (option 82) | ||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||
| 80 | =cut | ||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||
| 82 | 28 | ▶ | sub get_one { | ||||||||||||||||||||||||||||||
|
Subroutine
get_one: called CC=8 SCAR=20.8
Pod
documented
| |||||||||||||||||||||||||||||||||
| 83 | 28 | my $self = shift; | |||||||||||||||||||||||||||||||
| 84 | # look for as many lines as we can find in the current buffer | ||||||||||||||||||||||||||||||||
| 85 | 28 | while(1) { | |||||||||||||||||||||||||||||||
| 86 | 56 | my $string; | |||||||||||||||||||||||||||||||
| 87 | # look for lines with \r\n endings | ||||||||||||||||||||||||||||||||
| 88 | 56 | ▶ | if($self->[BUFFER] =~ /^(.*?\x0d?\x0a)/s) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 89 | 32 | my $length = length $1; | |||||||||||||||||||||||||||||||
| 90 | 32 | $string = substr($self->[BUFFER],0,$length,''); | |||||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||
| 93 | 56 | ▶ | return [] unless $string; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||
| 95 | 32 | ▶ | if(!$self->[LEASE] and $string =~ /$START/) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
not $self->[1] and $string =~ /$POE::Filter::DHCPd::Lease::START/
MC/DC100%2/2
not $self->[1] and $string =~ /$POE::Filter::DHCPd::Lease::START/
not $self->[1]
$string =~ /$POE::Filter::DHCPd::Lease::START/
Truth table100%3/3
not(A) and B
A= not $self->[1]B= $string =~ /$POE::Filter::DHCPd::Lease::START/
| |||||||||||||||||||||||||||||||||
| 96 | 4 | $self->[LEASE] = { ip => $1 }; | |||||||||||||||||||||||||||||||
| 97 | } elsif ($self->[LEASE]) { | ||||||||||||||||||||||||||||||||
| 98 | 24 | ▶ | if ($string =~ /$PARSER;/) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 99 | 16 | $self->[LEASE]{$+{name}} = $+{value}; | |||||||||||||||||||||||||||||||
| 100 | } elsif($string =~ /.*?$END/) { | ||||||||||||||||||||||||||||||||
| 101 | 4 | return $self->_done(); | |||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||
| 107 | 0 | return []; | |||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||
| 110 | 4 | ▶ | sub _done { | ||||||||||||||||||||||||||||||
|
Subroutine
_done: called CC=7 SCAR=20.2
| |||||||||||||||||||||||||||||||||
| 111 | 4 | my $self = shift; | |||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||
| 113 | 4 | my $lease = delete $self->[LEASE]; | |||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||
| 115 | 4 | for my $k (qw/starts ends/) { | |||||||||||||||||||||||||||||||
| 116 | 8 | ▶ | next unless($lease->{$k}); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 117 | 8 | ▶ | if(my @values = $lease->{$k} =~ $DATE) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 118 | 8 | $values[1]--; # decrease month | |||||||||||||||||||||||||||||||
| 119 | 8 | $lease->{$k} = timelocal(reverse @values); | |||||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||
| 123 | 4 | ▶ | if(my $mac = _mac(delete $lease->{'ethernet'})) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 124 | 4 | $lease->{'hw_ethernet'} = $mac; | |||||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||||||
| 126 | # compatibility with old parser output | ||||||||||||||||||||||||||||||||
| 127 | 4 | ▶ | $lease->{'circuit_id'} = delete $lease->{'circuit-id'} if ($lease->{'circuit-id'}); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 128 | 4 | ▶ | $lease->{'remote_id'} = delete $lease->{'remote-id'} if ($lease->{'remote-id'}); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||
| 130 | 4 | return [ $lease ]; | |||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||
| 134 | 4 | ▶ | sub _mac { | ||||||||||||||||||||||||||||||
|
Subroutine
_mac: called CC=3 SCAR=11.3
| |||||||||||||||||||||||||||||||||
| 135 | 4 | ▶ | my $str = shift or return; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||
| 137 | 4 | $str = join "", map { sprintf "%02s", $_ } split /:/, $str; | |||||||||||||||||||||||||||||||
| 138 | 4 | $str =~ tr/[0-9a-fA-F]//cd; | |||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||
| 140 | 4 | ▶ | return length $str == 12 ? lc($str) : undef; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||
| 143 | =head2 get | ||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||
| 145 | See L<POE::Filter>. | ||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||
| 147 | =head2 put | ||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||
| 149 | Returns an empty string. Should not be used. | ||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||
| 151 | =cut | ||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||
| 153 | 0 | ▶ | sub put { | ||||||||||||||||||||||||||||||
|
Subroutine
put: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 154 | 0 | return q(); | |||||||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||
| 157 | =head2 get_pending | ||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||
| 159 | my $buffer = $self->get_pending; | ||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||
| 161 | Returns any data left in the buffer. | ||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||
| 163 | =cut | ||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||
| 165 | 2 | ▶ | sub get_pending { | ||||||||||||||||||||||||||||||
|
Subroutine
get_pending: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 166 | 2 | return shift->[BUFFER]; | |||||||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||
| 169 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||
| 171 | Jan Henning Thorsen, C<< <jhthorsen-at-cpan-org> >> | ||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||
| 173 | =head1 COPYRIGHT & LICENSE | ||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||
| 175 | Copyright 2007 Jan Henning Thorsen, all rights reserved. | ||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||
| 177 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||||||
| 178 | under the same terms as Perl itself. | ||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||
| 180 | =cut | ||||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||
| 182 | 3 | 1; | |||||||||||||||||||||||||||||||