]> git.ipfire.org Git - people/ms/network.git/commitdiff
mac_generate: Refactor to make this function faster
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 13 Aug 2015 18:32:29 +0000 (20:32 +0200)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 13 Aug 2015 18:32:29 +0000 (20:32 +0200)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/functions/functions.util

index fdb2ce47b395df150d97096b1b7f091f44afe05c..de2cb1e69fa8ee7d931866f8bf151a8bba55e861 100644 (file)
@@ -157,36 +157,20 @@ enabled() {
 }
 
 mac_generate() {
-       # Get a bunch of random hex digits
-       # and remove all dashes from the input.
-       local random=$(</proc/sys/kernel/random/uuid)
-       random=${random//-/}
-       assert isset random
+       local b="$(random 12)"
 
-       local output
-
-       local i o
-       for i in $(seq 0 5); do
-               o="0x${random:0:2}"
-               random="${random:2:${#random}}"
+       # Remove multicast bit
+       # and set address is software assigned
+       local first_byte=$(( 0x${b:0:2} & 0xfe ))
+       first_byte=$(( ${first_byte} | 0x02 ))
 
-               case "${i}" in
-                       0)
-                               # Remove multicast bit
-                               # and set address is software assigned
-                               o=$(( ${o} & 0xfe ))
-                               o=$(( ${o} | 0x02 ))
+       local output
+       printf -v output "%02x" "${first_byte}"
 
-                               printf -v output "%02x" "${o}"
-                               ;;
-                       *)
-                               printf -v output "%s:%02x" "${output}" "${o}"
-                               ;;
-               esac
-       done
+       output="${output}:${b:2:2}:${b:4:2}:${b:6:2}:${b:8:2}:${b:10:2}"
 
        # Check if output is valid
-       assert mac_is_valid ${output}
+       assert mac_is_valid "${output}"
 
        echo "${output}"
 }
@@ -226,6 +210,22 @@ uuid() {
        echo $(</proc/sys/kernel/random/uuid)
 }
 
+rand() {
+       local uuid="$(uuid)"
+       echo "${uuid//-/}"
+}
+
+random() {
+       local length="${1:-8}"
+
+       local random
+       while [ ${#random} -lt ${length} ]; do
+               random="${random}$(rand)"
+       done
+
+       echo "${random:0:${length}}"
+}
+
 isset() {
        local var=${1}