Shorter implementation of random_ether_addr().

This commit is contained in:
Philipp Marek 2015-10-07 11:09:00 +02:00 committed by Dietmar Maurer
parent e88f9c631c
commit de9a267fec

View File

@ -940,21 +940,13 @@ sub random_ether_addr {
my $rand = Digest::SHA::sha1_hex($$, rand(), $seconds, $microseconds); my $rand = Digest::SHA::sha1_hex($$, rand(), $seconds, $microseconds);
my $mac = ''; # clear multicast, set local id
for (my $i = 0; $i < 6; $i++) { vec($rand, 0, 8) = (vec($rand, 0, 8) & 0xfe) | 2;
my $ss = hex(substr($rand, $i*2, 2));
if (!$i) {
$ss &= 0xfe; # clear multicast
$ss |= 2; # set local id
}
$ss = sprintf("%02X", $ss);
if (!$i) { my $mac = sprintf("%02X:" x 6, unpack("C6", $rand));
$mac .= "$ss";
} else { # remove superfluous ":" at end
$mac .= ":$ss"; chop($mac);
}
}
return $mac; return $mac;
} }