[pve-devel] [PATCH] Shorter implementation of random_ether_addr().
Philipp Marek
philipp.marek at linbit.com
Wed Oct 7 11:09:00 CEST 2015
---
src/PVE/Tools.pm | 20 ++++++--------------
1 file changed, 6 insertions(+), 14 deletions(-)
diff --git a/src/PVE/Tools.pm b/src/PVE/Tools.pm
index 3f3958e..cda0797 100644
--- a/src/PVE/Tools.pm
+++ b/src/PVE/Tools.pm
@@ -940,21 +940,13 @@ sub random_ether_addr {
my $rand = Digest::SHA::sha1_hex($$, rand(), $seconds, $microseconds);
- my $mac = '';
- for (my $i = 0; $i < 6; $i++) {
- my $ss = hex(substr($rand, $i*2, 2));
- if (!$i) {
- $ss &= 0xfe; # clear multicast
- $ss |= 2; # set local id
- }
- $ss = sprintf("%02X", $ss);
+# clear multicast, set local id
+ vec($rand, 0, 8) = (vec($rand, 0, 8) & 0xfe) | 2;
- if (!$i) {
- $mac .= "$ss";
- } else {
- $mac .= ":$ss";
- }
- }
+ my $mac = sprintf("%02X:" x 6, unpack("C6", $rand));
+
+# remove superfluous ":" at end
+ chop($mac);
return $mac;
}
--
2.5.3
More information about the pve-devel
mailing list