Lookup/Oui.pm

filter: Statementstmt 100% 9 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 100% 12 / 12 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Lookup/Ipoption.pm ↑ Summary Lookup/Protocol.pm »
1 #
2 # $Id$
3 #
4 # lookup::oui Brik
5 #
6 package Metabrik::Lookup::Oui;
7 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use base qw(Metabrik::File::Text);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 sub brik_properties {
13 return {
14 revision => '$Revision$',
15 tags => [ qw(unstable ieee) ],
16 author => 'GomoR <GomoR[at]metabrik.org>',
17 license => 'http://opensource.org/licenses/BSD-3-Clause',
18 attributes => {
19 datadir => [ qw(datadir) ],
20 input => [ qw(input) ],
21 _load => [ qw(INTERNAL) ],
22 },
23 attributes_default => {
24 input => 'oui.txt',
25 },
26 commands => {
27 update => [ qw(output|OPTIONAL) ],
28 load => [ qw(input|OPTIONAL) ],
29 from_hex => [ qw(mac_address) ],
30 from_string => [ qw(company_string) ],
31 all => [ ],
32 },
33 require_modules => {
34 'Metabrik::Client::Www' => [ ],
35 },
36 };
37 }
38  
39 sub update {
40 my $self = shift;
41 my ($output) = @_;
42  
43 my $input = $self->input;
44 my $datadir = $self->datadir;
45 $output ||= $input;
46  
47 # XXX: should also check for generic attribution:
48 # http://www.iana.org/assignments/ethernet-numbers/ethernet-numbers-2.csv
49  
50 my $url = 'http://standards-oui.ieee.org/oui.txt';
51  
52 my $cw = Metabrik::Client::Www->new_from_brik_init($self) or return;
53 $cw->mirror($url, $output, $datadir) or return;
54  
55 return $output;
56 }
57  
58 sub load {
59 my $self = shift;
60 my ($input) = @_;
61  
62 $input ||= $self->datadir.'/'.$self->input;
63 $self->brik_help_run_file_not_found('load', $input) or return;
64  
65 $self->as_array(1);
66  
67 my $data = $self->read($input) or return;
68  
69 return $self->_load($data);
70 }
71  
72 sub from_hex {
73 my $self = shift;
74 my ($hex) = @_;
75  
76 $self->brik_help_run_undef_arg('from_hex', $hex) or return;
77  
78 my $data = $self->_load || $self->load;
79 if (! defined($data)) {
80 return $self->log->error("from_hex: load failed");
81 }
82  
83 my $db = $self->all;
84  
85 my @lookup = ();
86 if (ref($hex) eq 'ARRAY') {
87 for my $h (@$hex) {
88 push @lookup, $h;
89 }
90 }
91 elsif (! ref($hex)) {
92 push @lookup, $hex;
93 }
94 else {
95 return $self->log->error("from_hex: MAC address format not recognized [$hex]");
96 }
97  
98 my %result = ();
99 for my $hex (@lookup) {
100 my $this = $hex;
101 $this =~ s/://g;
102 $this =~ /^([0-9a-f]{6})/i;
103 if (exists($db->{$1})) {
104 $result{$hex} = $db->{$1};
105 }
106 }
107  
108 return \%result;
109 }
110  
111 sub from_string {
112 my $self = shift;
113 my ($string) = @_;
114  
115 $self->brik_help_run_undef_arg('from_string', $string) or return;
116  
117 my $data = $self->_load || $self->load;
118 if (! defined($data)) {
119 return $self->log->error("from_string: load failed");
120 }
121  
122 my @match = ();
123 for my $this (@$data) {
124 $this =~ s/\r*$//;
125 if ($this =~ /^\s*([0-9A-F]{2}\-[0-9A-F]{2}\-[0-9A-F]{2})\s+\(hex\)\s+(.*)$/i) {
126 $self->log->debug("from_string: this[$this]");
127 my $oui = $1;
128 my $company = $2;
129 if ($company =~ /$string/i) {
130 $self->log->verbose("from_string: match [$company]");
131 $oui =~ s/\-/:/g;
132 push @match, lc($oui);
133 }
134 }
135 }
136  
137 return \@match;
138 }
139  
140 sub all {
141 my $self = shift;
142  
143 my $data = $self->_load || $self->load;
144 if (! defined($data)) {
145 return $self->log->error("all: load failed");
146 }
147  
148 my %result = ();
149 for my $this (@$data) {
150 $this =~ s/\r*$//;
151 if ($this =~ /^\s*([0-9A-F]{6})\s+\(base 16\)\s+(.*)$/i) {
152 $self->log->debug("from_hex: this[$this]");
153 my $oui = lc($1);
154 my $company = $2;
155 $result{$oui} = $company;
156 }
157 }
158  
159 return \%result;
160 }
161  
162 1;
163  
164 __END__
« Lookup/Ipoption.pm ↑ Summary Lookup/Protocol.pm »