Metabrik-Repository

 view release on metacpan or  search on metacpan

lib/Metabrik/Lookup/Oui.pm  view on Meta::CPAN

#
# $Id$
#
# lookup::oui Brik
#
package Metabrik::Lookup::Oui;
use strict;
use warnings;

use base qw(Metabrik::File::Text);

sub brik_properties {
   return {
      revision => '$Revision$',
      tags => [ qw(unstable ieee) ],
      author => 'GomoR <GomoR[at]metabrik.org>',
      license => 'http://opensource.org/licenses/BSD-3-Clause',
      attributes => {
         datadir => [ qw(datadir) ],
         input => [ qw(input) ],
         _load => [ qw(INTERNAL) ],
      },
      attributes_default => {
         input => 'oui.txt',
      },
      commands => {
         update => [ qw(output|OPTIONAL) ],
         load => [ qw(input|OPTIONAL) ],
         from_hex => [ qw(mac_address) ],
         from_string => [ qw(company_string) ],
         all => [ ],
      },
      require_modules => {
         'Metabrik::Client::Www' => [ ],
      },
   };
}

sub update {
   my $self = shift;
   my ($output) = @_;

   my $input = $self->input;
   my $datadir = $self->datadir;
   $output ||= $input;

   # XXX: should also check for generic attribution:
   # http://www.iana.org/assignments/ethernet-numbers/ethernet-numbers-2.csv

   my $url = 'http://standards-oui.ieee.org/oui.txt';

   my $cw = Metabrik::Client::Www->new_from_brik_init($self) or return;
   $cw->mirror($url, $output, $datadir) or return;

   return $output;
}

sub load {
   my $self = shift;
   my ($input) = @_;

   $input ||= $self->datadir.'/'.$self->input;
   $self->brik_help_run_file_not_found('load', $input) or return;

   $self->as_array(1);

   my $data = $self->read($input) or return;

   return $self->_load($data);
}

sub from_hex {
   my $self = shift;
   my ($hex) = @_;

   $self->brik_help_run_undef_arg('from_hex', $hex) or return;

   my $data = $self->_load || $self->load;
   if (! defined($data)) {
      return $self->log->error("from_hex: load failed");
   }

   my $db = $self->all;

   my @lookup = ();
   if (ref($hex) eq 'ARRAY') {
      for my $h (@$hex) {
         push @lookup, $h;
      }
   }
   elsif (! ref($hex)) {
      push @lookup, $hex;
   }
   else {
      return $self->log->error("from_hex: MAC address format not recognized [$hex]");
   }

   my %result = ();
   for my $hex (@lookup) {



( run in 2.307 seconds using v1.01-cache-2.11-cpan-5837b0d9d2c )