Skip to content

Commit

Permalink
Merge pull request #1 from djzort/master
Browse files Browse the repository at this point in the history
updates
  • Loading branch information
irgeek committed Sep 6, 2011
2 parents d0b80a9 + 8cdbea1 commit 7ee62cc
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 38 deletions.
125 changes: 88 additions & 37 deletions lib/NetAddr/MAC.pm
Expand Up @@ -25,6 +25,7 @@ $VERSION = (qw$Revision: 0.71 $)[1];
mac_as_basic mac_as_sun
mac_as_microsoft mac_as_cisco
mac_as_bpr mac_as_ieee
mac_as_ipv6_suffix
)
],
properties => [
Expand All @@ -39,6 +40,7 @@ $VERSION = (qw$Revision: 0.71 $)[1];
mac_as_basic mac_as_sun
mac_as_microsoft mac_as_cisco
mac_as_bpr mac_as_ieee
mac_as_ipv6_suffix
)
],
);
Expand All @@ -52,7 +54,7 @@ OIE::Utils::MAC - Handles hardware MAC Addresses (EUI-48 and EUI-64)
=head1 SYNOPSIS
use OIE::Utils::MAC;
my $mac = OIE::Utils::MAC->new( '00:11:22:aa:bb:cc' );
my $mac = OIE::Utils::MAC->new( mac => '0011.22AA.BBCC' );
Expand Down Expand Up @@ -181,10 +183,10 @@ sub _mac_to_integers {

# 00:19:e3:01:0e:72
if ( @parts == EUI48LENGTHDEC || @parts == EUI64LENGTHDEC )
{
{
return [ map { hex } @parts ];
}

# 0019:e301:0e72
if ( @parts == EUI48LENGTHDEC / 2 || @parts == EUI64LENGTHDEC / 2 )
{
Expand Down Expand Up @@ -343,6 +345,37 @@ sub as_ieee {
return join( q{-}, map { sprintf( '%02x', $_ ) } @{ $self->{mac} } );
}

=head2 as_ipv6_suffix
returns the EUI-64 address in the format used for an IPv6 autoconf address suffix
=cut

sub as_ipv6_suffix {

my $self = shift;
my @tmpmac;

# be slightly evil here, so that hashrefs and objects work
if (is_eui48($self)) {
# save this for later
@tmpmac = @{ $self->{mac} };

to_eui64($self);

}

my @suffix = (
@{ $self->{mac} }[0] ^ 0x02,
@{ $self->{mac} }[1..7]
);

# restore the eui48 if needed
$self->{mac} = \@tmpmac if @tmpmac;

return join( q{:}, map { my $i = $_; $i *= 2; sprintf( '%02x%02x', $suffix[$i], $suffix[$i + 1]) } 0 .. 3 );
}

=head2 as_microsoft
returns the mac address normalized as a hexidecimal string that is 0 padded and with B<:> delimiting every octet
Expand Down Expand Up @@ -371,25 +404,6 @@ sub as_sun {
return join( q{-}, map { sprintf( '%01x', $_ ) } @{ $self->{mac} } );
}

=head2 as_ipv6_suffix
returns the EUI-64 address in the format used for an IPv6 autoconf address suffix
=cut

sub as_ipv6_suffix {
my $self = shift;
if ($self->is_eui64) {
my @suffix = (
@{ $self->{mac} }[0] ^ 0x02,
@{ $self->{mac} }[1..7]
);
return join( q{:}, map { $_ *= 2; sprintf( '%02x%02x', $suffix[$_], $suffix[$_ + 1]) } 0 .. 3 );
} else {
croak 'please convert to eui-64 first';
}
}

=head2 as_tokenring
returns the mac address normalized as a hexidecimal string that is 0 padded and with B<-> delimiting every octet
Expand All @@ -406,32 +420,50 @@ sub as_tokenring {
# return join( q{-}, map { sprintf( '%01x', $_ ) } @{ $self->{mac} } );
}

=head2 convert
=head2 to_eui48
converts between EUI-48 and EUI-64 addresses
converts to EUI-48 (if the eui-64 was derived from eui-48)
=cut

sub convert {
sub to_eui48 {

my $self = shift;
if ($self->is_eui48) {

# be slightly evil here, so that hashrefs and objects work
if (is_eui64($self)) {
if (@{ $self->{mac} }[3] == 0xff and (@{ $self->{mac} }[4] == 0xff or @{ $self->{mac} }[4] == 0xfe)) {
# convert to eui-48
$self->{mac} = [ @{ $self->{mac} }[0..2,5..7] ];
} else {
croak 'eui-64 address is not derived from an eui-48 address';
}
}

return 1
}

=head2 to_eui64
converts to EUI-64
=cut

sub to_eui64 {

my $self = shift;

# be slightly evil here so that hashrefs and objects work
if (is_eui48($self)) {
# convert to eui-64
$self->{mac} = [
@{ $self->{mac} }[0..2],
0xff, 0xfe,
@{ $self->{mac} }[3..5]
];
} else {
if (@{ $self->{mac} }[3] == 0xff and (@{ $self->{mac} }[4] == 0xff or @{ $self->{mac} }[4] == 0xfe)) {
# convert to eui-48
$self->{mac} = [
@{ $self->{mac} }[0..2,5..7]
];
} else {
croak 'eui-64 address is not derived from an eui-48 address';
}
}
return 1;

return 1
}

=head1 STAND ALONE PROPERTY FUNCTIONS
Expand Down Expand Up @@ -562,7 +594,7 @@ sub mac_as_basic {
=head2 mac_as_bpr($mac)
returns the mac address in $mac normalized as a hexidecimal string that is 0 padded, with B<:> delimiting and
B<1,length> leading. I<length> is the number of hex pairs (6 for EUI48)
B<1,length> leading. I<length> is the number of hex pairs (6 for EUI48)
1,6,00:11:22:aa:bb:cc
Expand Down Expand Up @@ -620,6 +652,25 @@ sub mac_as_ieee {

}

=head2 mac_as_ipv6_suffix($mac)
returns the mac address in $mac in the format used for an IPv6 autoconf address suffix
will convert from eui48 or eui64 if needed
=cut

sub mac_as_ipv6_suffix {

my $mac = shift;
croak 'please use as_ipv6_suffix'
if ref $mac eq __PACKAGE__;
croak 'argument must be a string' if ref $mac;

return as_ipv6_suffix( { mac => _mac_to_integers($mac) } )

}

=head2 mac_as_microsoft($mac)
returns the mac address in $mac normalized as a hexidecimal string that is 0 padded and with B<:> delimiting every octet
Expand Down
2 changes: 1 addition & 1 deletion t/03.manifest.t
Expand Up @@ -9,4 +9,4 @@ if ( not $ENV{TEST_AUTHOR} ) {

eval "use Test::CheckManifest 0.9";
plan skip_all => "Test::CheckManifest 0.9 required" if $@;
ok_manifest({filter => [qr/\.svn|\.old$/]});
ok_manifest({filter => [qr/\.svn|\.old$|\.git/]});
2 changes: 2 additions & 0 deletions t/130-utils-mac.t
Expand Up @@ -15,6 +15,7 @@ my @all = qw(
mac_as_basic mac_as_sun
mac_as_microsoft mac_as_cisco
mac_as_bpr mac_as_ieee
mac_as_ipv6_suffix
);

Expand All @@ -28,6 +29,7 @@ my @normals = qw(
mac_as_basic mac_as_sun
mac_as_microsoft mac_as_cisco
mac_as_bpr mac_as_ieee
mac_as_ipv6_suffix
);

can_ok('NetAddr::MAC', @all, @properties, @normals);
Expand Down

0 comments on commit 7ee62cc

Please sign in to comment.