| 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
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 32 | 89 | my $handle = $self->get_inherited( 'cache_handle' ); | |||||||||||||||||||||||||||||||||
| 33 | 89 | ▶ | if ( $handle ) { return $handle } | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 34 | 17 | $handle = {}; | |||||||||||||||||||||||||||||||||
| 35 | 17 | ▶ | if ( my $db = $self->cache_db ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
@_ and not ref $_[0]
MC/DC100%2/2
@_ and not ref $_[0]
@_
not ref $_[0]
Truth table100%3/3
A and not(B)
A= @_B= not ref $_[0]
| |||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||
| 50 | 77 | my $handle = $self->cache_handle; | |||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||
| 52 | 77 | ▶ | if ( @_ ) { | ||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||
| 53 | 53 | my %hash = %{ shift() }; | |||||||||||||||||||||||||||||||||
| 54 | 53 | for my $x ( keys %hash ) { | |||||||||||||||||||||||||||||||||
| 55 | 186 | ▶ | if ( not defined $hash{ $x } ) { $hash{ $x } = '' } | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 74 | 96 | my $oui = shift; | |||||||||||||||||||||||||||||||||
| 75 | 96 | ▶ | if ( my $norm = normalize_oui( $oui ) ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 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)
MC/DC100%1/1
shift() || (return)
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||
| 92 | 5153 | ▶ | if ( ref $oui ) { return map { hex } split( '-', $oui->norm ) } | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||
| 139 | 13 | ▶ | if ( $x = $self->cache ) { return $self->{ 'lookup' } = $x } | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 140 | 11 | ▶ | if ( $x = $self->update_from_file ) { return $self->{ 'lookup' } = $x } | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 141 | 0 | ▶ | if ( $x = $self->update_from_web ) { return $self->{ 'lookup' } = $x } | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 142 | 0 | ▶ | if ( $self->mirror_file ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 143 | 0 | ▶ | if ( $x = $self->update_from_file ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 157 | 12 | my $fh = IO::File->new( $cf, 'r' ); | |||||||||||||||||||||||||||||||||
| 158 | 12 | ▶ | if ( ! $fh ) { return } | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||
| 160 | 11 | local $/ = ""; | |||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||
| 162 | 11 | ▶ | while ( my $entry = $fh->getline ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 163 | 46 | ▶ | if ( substr( $entry, 0, 8 ) eq $oui ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 177 | 2 | eval "require LWP::Simple"; ## no critic | |||||||||||||||||||||||||||||||||
| 178 | 2 | ▶ | if ( $@ ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 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
MC/DC100%2/2
shift() || $self->file_url
shift()
$self->file_url
Truth table100%3/3
A || B
A= shift()B= $self->file_url
| |||||||||||||||||||||||||||||||||||
| 190 | 14 | ▶ | if ( ! $url ) { return } | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 191 | 12 | ▶ | my $file = shift || $self->cache_file; | ||||||||||||||||||||||||||||||||
|
Condition100%3/3
shift() || $self->cache_file
MC/DC100%2/2
shift() || $self->cache_file
shift()
$self->cache_file
Truth table100%3/3
A || B
A= shift()B= $self->cache_file
| |||||||||||||||||||||||||||||||||||
| 192 | 12 | ▶ | if ( ! $file ) { return } | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 193 | 0 | ▶ | if ( ! $self->have_lwp_simple ) { return } | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||||
| 195 | 11 | my $res = LWP::Simple::mirror( $url, $file ); | |||||||||||||||||||||||||||||||||
| 196 | 11 | ▶ | if ( $res == LWP::Simple::RC_NOT_MODIFIED() ) { return 0 } | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 197 | 7 | ▶ | if ( ! LWP::Simple::is_success( $res ) ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 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
MC/DC0%0/2
shift() || $self->file_url
shift()
$self->file_url
Truth table33%1/3
A || B
A= shift()B= $self->file_url
| |||||||||||||||||||||||||||||||||||
| 215 | 0 | ▶ | if ( ! $url ) { return } | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 216 | 3 | ▶ | my $file = shift || $self->cache_file; | ||||||||||||||||||||||||||||||||
|
Condition66%2/3
shift() || $self->cache_file
MC/DC0%0/2
shift() || $self->cache_file
shift()
$self->cache_file
Truth table66%2/3
A || B
A= shift()B= $self->cache_file
| |||||||||||||||||||||||||||||||||||
| 217 | 0 | ▶ | if ( ! $file ) { return } | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||||||||
| 219 | 3 | ▶ | if ( $self->mirror_file( $url, $file ) ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 229 | 0 | ▶ | if ( ! $file ) { return } | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||||||||
| 248 | 8 | my $url_format = $self->search_url; | |||||||||||||||||||||||||||||||||
| 249 | 0 | ▶ | if ( ! $url_format ) { return } | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||||||||
| 251 | 8 | ▶ | if ( $url_format =~ /%s/ ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 262 | 0 | ▶ | if ( not $self->have_lwp_simple ) { return } | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||||||||
| 264 | 6 | my $url = $self->search_url_for; | |||||||||||||||||||||||||||||||||
| 265 | 0 | ▶ | if ( ! $url ) { return } | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||||||||
| 267 | 6 | ▶ | if ( my $page = $self->get_url( $url ) ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 268 | 0 | ▶ | if ( $page =~ /listing contains no match/ ) { return } | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 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[0] <=> $r[0] or $l[1] <=> $r[1]
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]
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 325 | $org eq 'PRIVATE' ? '' : $org, | ||||||||||||||||||||||||||||||||||
| 326 | ), | ||||||||||||||||||||||||||||||||||
| 327 | ); | ||||||||||||||||||||||||||||||||||
| 328 | 25 | ▶ | if ( my $a = $d->{ 'address' } ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||||