blib/lib/Device/OUI.pm

filter: Statementstmt 89.7% 219 / 244 Branchbran 76.7% 86 / 112 Conditioncond 78.2% 18 / 23 MC/DCmcdc 57.1% 8 / 14 Subroutinesub 100% 33 / 33 Podpod 100% 24 / 24 totaltotal 86.2% 388 / 450 CC 67 SCAR 16.9
CC
67
Cov
85%
lookup
13.1
update_from_web
10.1
load_cache_from_web
8.5
CRAP 5.4
↑ Summary
1 package Device::OUI;
2 9 ▶use strict; use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
3 9 our $VERSION = '1.04';
4 9 ▶use IO::File ();
Subroutine BEGIN: called CC=1 SCAR=0.0
5 9 ▶use Carp qw( croak carp );
Subroutine BEGIN: called CC=1 SCAR=0.0
6 9 ▶use AnyDBM_File;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 9 ▶use overload (
Subroutine BEGIN: called CC=1 SCAR=0.0
8 9 '<=>' => 'overload_cmp',
9 'cmp' => 'overload_cmp',
10 '""' => 'normalized',
11 fallback => 1,
12 9 );
13 9 ▶use base qw( Class::Accessor::Grouped );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 9 ▶use Sub::Exporter -setup => {
Subroutine BEGIN: called CC=1 SCAR=0.0
15 exports => [qw( normalize_oui oui_cmp parse_oui_entry oui_to_integers )],
16 9 };
17  
18 9 __PACKAGE__->mk_group_accessors( inherited => qw(
19 cache_db cache_file
20 file_url search_url
21 ) );
22 9 ▶my $cache = $^O eq 'MSWin32' ? 'C:\device_oui' : '/var/cache/device_oui';
Branch: 1 branch
BranchTrueFalseTotal
$^O eq 'MSWin32' ? : 0 9 9
23 9 __PACKAGE__->cache_db( $cache );
24 9 __PACKAGE__->cache_file( $cache . '.txt' );
25 9 __PACKAGE__->search_url( 'http://standards.ieee.org/cgi-bin/ouisearch?%s' );
26 9 __PACKAGE__->file_url( 'http://standards.ieee.org/regauth/oui/oui.txt' );
27  
28 89 ▶sub cache_handle {
Subroutine cache_handle: called CC=5 SCAR=16.1
Pod documented
29 89 my $self = shift;
30  
31 89 ▶ if ( @_ ) { $self->set_inherited( 'cache_handle', @_ ) }
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 2 87 89
32 89 my $handle = $self->get_inherited( 'cache_handle' );
33 89 ▶ if ( $handle ) { return $handle }
Branch: 1 branch
BranchTrueFalseTotal
if ($handle) 72 17 89
34 17 $handle = {};
35 17 ▶ if ( my $db = $self->cache_db ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $db = $self->cache_db) 8 9 17
36 8 my $opts = Fcntl::O_RDWR | Fcntl::O_CREAT;
37 8 my %cache;
38 8 ▶ if ( tie( %cache, 'AnyDBM_File', $db, $opts, 0666 ) ) { ## no critic
Branch: 1 branch
BranchTrueFalseTotal
if (tie %cache, "AnyDBM_File", $db, $opts, 438) 7 1 8
39 7 $handle = \%cache;
40 }
41 }
42 17 $self->set_inherited( 'cache_handle', $handle );
43 17 return $handle;
44 }
45  
46 77 ▶sub cache {
Subroutine cache: called CC=7 SCAR=19.5
Pod documented
47 77 my $self = shift;
48 77 ▶ my $oui = ( @_ && not ref $_[0] ) ? shift : $self->norm;
Branch: 1 branch
BranchTrueFalseTotal
@_ && !ref($_[0]) ? : 47 30 77
Condition100%3/3
@_ and not ref $_[0]
!ll&&!rl&&r
131747
MC/DC100%2/2
@_ and not ref $_[0]
@_ not ref $_[0]
Truth table100%3/3
A and not(B)
A= @_B= not ref $_[0]
ABresult
0X0
100
111
49
50 77 my $handle = $self->cache_handle;
51  
52 77 ▶ if ( @_ ) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_) { } 53 24 77
elsif (my $x = $handle->{$oui}) { } 13 11 24
53 53 my %hash = %{ shift() };
54 53 for my $x ( keys %hash ) {
55 186 ▶ if ( not defined $hash{ $x } ) { $hash{ $x } = '' }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $hash{$x}) 1 185 186
56 }
57 53 return $handle->{ $oui } = join( "\0", %hash );
58 } elsif ( my $x = $handle->{ $oui } ) {
59 13 return { split( "\0", $x ) };
60 }
61 11 return;
62 }
63  
64 98 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
65 98 my $self = bless( {}, shift );
66 98 ▶ $self->oui( shift ) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 96 2 98
67 97 return $self;
68 }
69  
70 102 ▶sub oui {
Subroutine oui: called CC=4 SCAR=13.9
Pod documented
71 102 my $self = shift;
72  
73 102 ▶ if ( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 96 6 102
74 96 my $oui = shift;
75 96 ▶ if ( my $norm = normalize_oui( $oui ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $norm = normalize_oui($oui)) { } 95 1 96
76 95 $self->{ 'oui' } = $oui;
77 95 $self->{ 'oui_norm' } = $norm;
78 95 delete $self->{ 'lookup' };
79 } else {
80 1 croak "Invalid OUI format: $oui";
81 }
82 }
83  
84 101 ▶ if ( not $self->{ 'oui' } ) { croak "Object does not have an OUI" }
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"oui"}) 1 100 101
85  
86 100 return $self->{ 'oui' };
87 }
88  
89 5159 ▶sub oui_to_integers {
Subroutine oui_to_integers: called CC=5 SCAR=16.1
Pod documented
90 5159 ▶ my $oui = shift || return;
Condition100%2/2
shift() || (return)
l!l
51536
MC/DC100%1/1
shift() || (return)
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
91  
92 5153 ▶ if ( ref $oui ) { return map { hex } split( '-', $oui->norm ) }
Branch: 1 branch
BranchTrueFalseTotal
if (ref $oui) 292 4861 5153
93  
94 # 00-06-2A or 0:6:2a, etc. any non-hex delimiter will do
95 {
96 4861 my @parts = grep { length } split( /[^a-f0-9]+/i, $oui );
97 4861 ▶ if ( @parts == 3 ) { return map { hex } @parts }
Branch: 1 branch
BranchTrueFalseTotal
if (@parts == 3) 4140 721 4861
98 }
99  
100 # 00062a, requires exactly 6 hex characters
101 {
102 4861 my @parts = ( $oui =~ /([a-f0-9])/ig );
103 721 ▶ if ( @parts == 6 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@parts == 6) 719 2 721
104 return(
105 719 hex( $parts[0].$parts[1] ),
106 hex( $parts[2].$parts[3] ),
107 hex( $parts[4].$parts[5] ),
108 );
109 }
110 }
111 2 return ();
112 }
113  
114 3847 ▶sub normalize_oui {
Subroutine normalize_oui: called CC=2 SCAR=6.9
Pod documented
115 3847 my $oui = shift;
116 3847 ▶ my @int = oui_to_integers( $oui ) or return;
Branch: 1 branch
BranchTrueFalseTotal
my(@int) = oui_to_integers($oui) or return 3839 8 3847
117 3839 return sprintf( '%02X-%02X-%02X', @int );
118 }
119  
120 931 ▶sub normalized { return shift->{ 'oui_norm' } }
Subroutine normalized: called CC=1 SCAR=0.0
Pod documented
121 9 *norm = \&normalized;
122  
123 18 ▶sub organization { return shift->lookup->{ 'organization' } }
Subroutine organization: called CC=1 SCAR=0.0
Pod documented
124 5 ▶sub company_id { return shift->lookup->{ 'company_id' } }
Subroutine company_id: called CC=1 SCAR=0.0
Pod documented
125 5 ▶sub address { return shift->lookup->{ 'address' } }
Subroutine address: called CC=1 SCAR=0.0
Pod documented
126  
127 5 ▶sub is_private {
Subroutine is_private: called CC=2 SCAR=6.9
Pod documented
128 5 my $self = shift;
129  
130 5 ▶ return $self->organization eq 'PRIVATE' ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->organization eq 'PRIVATE' ? : 1 4 5
131 }
132  
133 28 ▶sub lookup {
Subroutine lookup: called CC=7 SCAR=25.7
Pod documented
134 28 my $self = shift;
135  
136 28 my $x;
137 28 ▶ if ( $x = $self->{ 'lookup' } ) { return $x }
Branch: 1 branch
BranchTrueFalseTotal
if ($x = $self->{"lookup"}) 15 13 28
138  
139 13 ▶ if ( $x = $self->cache ) { return $self->{ 'lookup' } = $x }
Branch: 1 branch
BranchTrueFalseTotal
if ($x = $self->cache) 2 11 13
140 11 ▶ if ( $x = $self->update_from_file ) { return $self->{ 'lookup' } = $x }
Branch: 1 branch
BranchTrueFalseTotal
if ($x = $self->update_from_file) 11 0 11
141 0 ▶ if ( $x = $self->update_from_web ) { return $self->{ 'lookup' } = $x }
Branch: 1 branch
BranchTrueFalseTotal
if ($x = $self->update_from_web) 0 0 0
142 0 ▶ if ( $self->mirror_file ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->mirror_file) 0 0 0
143 0 ▶ if ( $x = $self->update_from_file ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x = $self->update_from_file) 0 0 0
144 0 return $self->{ 'lookup' } = $x;
145 }
146 }
147  
148 0 return $self->{ 'lookup' } = {};
149 }
150  
151 13 ▶sub update_from_file {
Subroutine update_from_file: called CC=5 SCAR=16.1
Pod documented
152 13 my $self = shift;
153 13 my $oui = $self->norm;
154  
155 13 my $cf = $self->cache_file;
156 13 ▶ if ( ! $cf ) { return }
Branch: 1 branch
BranchTrueFalseTotal
unless ($cf) 1 12 13
157 12 my $fh = IO::File->new( $cf, 'r' );
158 12 ▶ if ( ! $fh ) { return }
Branch: 1 branch
BranchTrueFalseTotal
unless ($fh) 1 11 12
159  
160 11 local $/ = "";
161
162 11 ▶ while ( my $entry = $fh->getline ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $entry = $fh->getline) 46 0 46
163 46 ▶ if ( substr( $entry, 0, 8 ) eq $oui ) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($entry, 0, 8) eq $oui) 11 35 46
164 11 my $data = $self->parse_oui_entry( $entry );
165 11 $self->cache( $data );
166 11 return $data;
167 }
168 }
169 0 return;
170 }
171  
172 {
173 9 my $HAVE_LWP_SIMPLE;
174 17 ▶ sub have_lwp_simple {
Subroutine have_lwp_simple: called CC=3 SCAR=11.4
Pod documented
175 17 my $self = shift;
176 17 ▶ if ( defined $HAVE_LWP_SIMPLE ) { return $HAVE_LWP_SIMPLE }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $HAVE_LWP_SIMPLE) 15 2 17
177 2 eval "require LWP::Simple"; ## no critic
178 2 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 2 2
179 0 carp "Unable to load LWP::Simple, network access not available\n";
180 0 $HAVE_LWP_SIMPLE = 0;
181 } else {
182 2 $HAVE_LWP_SIMPLE = 1;
183 }
184 }
185 }
186  
187 9 ▶sub mirror_file {
Subroutine mirror_file: called CC=8 SCAR=21.1
Pod documented
188 14 my $self = shift;
189 14 ▶ my $url = shift || $self->file_url;
Condition100%3/3
shift() || $self->file_url
l!l&&r!l&&!r
1112
MC/DC100%2/2
shift() || $self->file_url
shift() $self->file_url
Truth table100%3/3
A || B
A= shift()B= $self->file_url
ABresult
1X1
011
000
190 14 ▶ if ( ! $url ) { return }
Branch: 1 branch
BranchTrueFalseTotal
unless ($url) 2 12 14
191 12 ▶ my $file = shift || $self->cache_file;
Condition100%3/3
shift() || $self->cache_file
l!l&&r!l&&!r
1011
MC/DC100%2/2
shift() || $self->cache_file
shift() $self->cache_file
Truth table100%3/3
A || B
A= shift()B= $self->cache_file
ABresult
1X1
011
000
192 12 ▶ if ( ! $file ) { return }
Branch: 1 branch
BranchTrueFalseTotal
unless ($file) 1 11 12
193 0 ▶ if ( ! $self->have_lwp_simple ) { return }
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->have_lwp_simple) 0 11 11
194  
195 11 my $res = LWP::Simple::mirror( $url, $file );
196 11 ▶ if ( $res == LWP::Simple::RC_NOT_MODIFIED() ) { return 0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($res == LWP::Simple::RC_NOT_MODIFIED()) 4 7 11
197 7 ▶ if ( ! LWP::Simple::is_success( $res ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (LWP::Simple::is_success($res)) 0 7 7
198 0 carp "Failed to mirror $url to $file ($res)";
199 0 return;
200 }
201 7 return 1;
202 }
203  
204 8 ▶sub get_url {
Subroutine get_url: called CC=2 SCAR=6.9
Pod documented
205 8 my $self = shift;
206 8 my $url = shift;
207 8 ▶ if ( ! $url ) { return }
Branch: 1 branch
BranchTrueFalseTotal
unless ($url) 1 7 8
208  
209 7 return LWP::Simple::get( $url );
210 }
211  
212 3 ▶sub load_cache_from_web {
Subroutine load_cache_from_web: called CC=6 SCAR=21.4
Pod documented
213 3 my $self = shift;
214 3 ▶ my $url = shift || $self->file_url;
Condition33%1/3
shift() || $self->file_url
l!l&&r!l&&!r
300
MC/DC0%0/2
shift() || $self->file_url
shift() $self->file_url
Truth table33%1/3
A || B
A= shift()B= $self->file_url
ABresult
1X1
011
000
215 0 ▶ if ( ! $url ) { return }
Branch: 1 branch
BranchTrueFalseTotal
unless ($url) 0 3 3
216 3 ▶ my $file = shift || $self->cache_file;
Condition66%2/3
shift() || $self->cache_file
l!l&&r!l&&!r
120
MC/DC0%0/2
shift() || $self->cache_file
shift() $self->cache_file
Truth table66%2/3
A || B
A= shift()B= $self->cache_file
ABresult
1X1
011
000
217 0 ▶ if ( ! $file ) { return }
Branch: 1 branch
BranchTrueFalseTotal
unless ($file) 0 3 3
218  
219 3 ▶ if ( $self->mirror_file( $url, $file ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->mirror_file($url, $file)) 3 0 3
220 3 return $self->load_cache_from_file( $file );
221 }
222 0 return;
223 }
224  
225 5 ▶sub load_cache_from_file {
Subroutine load_cache_from_file: called CC=4 SCAR=13.9
Pod documented
226 5 my $self = shift;
227 5 my $file = shift;
228 5 ▶ if ( ! $file ) { $file = $self->cache_file }
Branch: 1 branch
BranchTrueFalseTotal
unless ($file) 1 4 5
229 0 ▶ if ( ! $file ) { return }
Branch: 1 branch
BranchTrueFalseTotal
unless ($file) 0 5 5
230  
231 5 my $fh = IO::File->new( $file );
232 5 local $/ = "";
233 5 $fh->getline; # dump the header
234 5 my $counter = 0;
235 5 ▶ while ( my $entry = $fh->getline ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $entry = $fh->getline) 25 5 30
236 25 my $data = $self->parse_oui_entry( $entry );
237 25 $self->cache( $data->{ 'oui' } => $data );
238 25 $counter++;
239 }
240 5 return $counter;
241 }
242  
243 8 ▶sub search_url_for {
Subroutine search_url_for: called CC=4 SCAR=14.5
Pod documented
244 8 my $self = shift;
245 8 my $oui = normalize_oui( shift );
246 8 ▶ if ( ! $oui ) { $oui = $self->norm }
Branch: 1 branch
BranchTrueFalseTotal
unless ($oui) 6 2 8
247  
248 8 my $url_format = $self->search_url;
249 0 ▶ if ( ! $url_format ) { return }
Branch: 1 branch
BranchTrueFalseTotal
unless ($url_format) 0 8 8
250  
251 8 ▶ if ( $url_format =~ /%s/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($url_format =~ /%s/) { } 8 0 8
252 8 return sprintf( $url_format, $oui );
253 } else {
254 0 return $url_format.$oui;
255 }
256 }
257  
258 6 ▶sub update_from_web {
Subroutine update_from_web: called CC=7 SCAR=23.2
Pod documented
259 6 my $self = shift;
260  
261 0 ▶ if ( not ref $self ) { return }
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $self) 0 6 6
262 0 ▶ if ( not $self->have_lwp_simple ) { return }
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->have_lwp_simple) 0 6 6
263  
264 6 my $url = $self->search_url_for;
265 0 ▶ if ( ! $url ) { return }
Branch: 1 branch
BranchTrueFalseTotal
unless ($url) 0 6 6
266  
267 6 ▶ if ( my $page = $self->get_url( $url ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $page = $self->get_url($url)) 6 0 6
268 0 ▶ if ( $page =~ /listing contains no match/ ) { return }
Branch: 1 branch
BranchTrueFalseTotal
if ($page =~ /listing contains no match/) 0 6 6
269 6 my @entries = ( $page =~ m{<pre>(.*?)</pre>}gs );
270 0 ▶ if ( @entries > 1 ) { croak "Too many entries returned from $url\n" }
Branch: 1 branch
BranchTrueFalseTotal
if (@entries > 1) 0 6 6
271 6 my $data = $self->parse_oui_entry( shift( @entries ) );
272 6 $self->cache( $data );
273 6 return $data;
274 }
275 0 return;
276 }
277  
278 52 ▶sub parse_oui_entry {
Subroutine parse_oui_entry: called CC=3 SCAR=11.0
Pod documented
279 52 local $_ = pop( @_ ); # pop in case we get called as a class method
280 9 ▶use Carp qw( confess );
Subroutine BEGIN: called CC=1 SCAR=0.0
281 0 ▶ if ( ! $_ ) { confess "eh?" }
Branch: 1 branch
BranchTrueFalseTotal
unless ($_) 0 52 52
282 52 s{</?b>}{}g;
283 52 s/\r//g;
284  
285 52 s/\s*\(hex\)\s*/\n/gm;
286 52 s/\s*\(base 16\).*$//gm;
287 52 s/^\s*|\s*$//gsm;
288  
289 52 my %data = ();
290 52 @data{ qw( oui organization company_id address ) } = split( "\n", $_, 4 );
291 52 ▶ delete $data{ 'address' } unless $data{ 'address' };
Branch: 1 branch
BranchTrueFalseTotal
unless $data{"address"} 10 42 52
292 52 return \%data;
293 }
294  
295 146 ▶sub overload_cmp { return oui_cmp( pop( @_ ) ? reverse @_ : @_ ) }
Subroutine overload_cmp: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
pop @_ ? : 0 146 146
296 656 ▶sub oui_cmp {
Subroutine oui_cmp: called CC=3 SCAR=11.3
Pod documented
297 656 my @l = oui_to_integers( shift );
298 656 my @r = oui_to_integers( shift );
299  
300 656 ▶ return ( $l[0] <=> $r[0] || $l[1] <=> $r[1] || $l[2] <=> $r[2] );
Condition66%4/6
$l[0] <=> $r[0] || $l[1] <=> $r[1] || $l[2] <=> $r[2]
l!l&&r!l&&!r
5100146
$l[0] <=> $r[0] or $l[1] <=> $r[1]
l!l&&r!l&&!r
5100146
MC/DC33%1/3
$l[0] <=> $r[0] || $l[1] <=> $r[1] || $l[2] <=> $r[2]
$l[0] <=> $r[0] $l[1] <=> $r[1] $l[2] <=> $r[2]
Truth table50%2/4
A or B || C
A= $l[0] <=> $r[0]B= $l[1] <=> $r[1]C= $l[2] <=> $r[2]
ABCresult
1XX1
01X1
0011
0000
301 }
302  
303 5 ▶sub dump_cache {
Subroutine dump_cache: called CC=5 SCAR=16.1
Pod documented
304 5 my $self = shift;
305  
306 5 my @lines = (
307 "\n",
308 "OUI\t\t\t\tOrganization\n",
309 "company_id\t\t\tOrganization\n",
310 "\t\t\t\tAddress\n",
311 "\n", "\n",
312 );
313  
314 5 my $db = $self->cache_handle;
315  
316 5 foreach my $oui ( sort { $a cmp $b } keys %{ $db } ) {
317 25 my $d = { split( "\0", $db->{ $oui } ) };
318 25 my $org = $d->{ 'organization' };
319  
320 push( @lines,
321 sprintf( "%-10s (hex)\t\t%s\n", $d->{ 'oui' }, $org ),
322 sprintf(
323 "%-10s (base 16)\t\t%s\n",
324 25 ▶ $d->{ 'company_id' },
Branch: 1 branch
BranchTrueFalseTotal
$org eq 'PRIVATE' ? : 5 20 25
325 $org eq 'PRIVATE' ? '' : $org,
326 ),
327 );
328 25 ▶ if ( my $a = $d->{ 'address' } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $a = $d->{'address'}) { } 20 5 25
329 20 for my $x ( split( "\n", $d->{ 'address' } ) ) {
330 65 push( @lines, "\t\t\t\t$x\n" );
331 }
332 20 push( @lines, "\n" );
333 } else {
334 5 push( @lines, "\t\t\t\t\n" );
335 }
336 }
337  
338 5 return join( "", @lines );
339 }
340  
341 9 1;
342  
343 __END__
↑ Summary