From 996ceafbf7337d8ea96c4c070ff04e8752729c82 Mon Sep 17 00:00:00 2001 From: Jason Khanlar Date: Wed, 15 Jun 2016 16:41:14 -0400 Subject: [PATCH 1/9] sync/merge with upstream --- mm-mac2ipv4.bash | 5 +++-- mm-mac2ipv4.pl | 24 +++++++++++++++--------- mm-mac2ipv4.sh | 5 +++-- 3 files changed, 21 insertions(+), 13 deletions(-) diff --git a/mm-mac2ipv4.bash b/mm-mac2ipv4.bash index e24dc39..9065379 100755 --- a/mm-mac2ipv4.bash +++ b/mm-mac2ipv4.bash @@ -23,7 +23,7 @@ function list-all { for ip2 in `seq 0 255`;do mac4=$(printf "%02X\n" $ip2) - ((ip2=ip2%64 + 64)) + ((ip2=ip2 % 32 + 96)) # Format IP address ip="$ip1.$ip2.$ip3.$ip4" @@ -99,6 +99,7 @@ ip2=$(printf "%d" "0x$mac4") ip3=$(printf "%d" "0x$mac5") ip4=$(printf "%d" "0x$mac6") -((ip2=ip2%64 + 64)) +((ip2=ip2 % 32 + 96)) +((ip4=ip4 - (ip4 % 64 - ip4 % 32))) echo "$ip1.$ip2.$ip3.$ip4" diff --git a/mm-mac2ipv4.pl b/mm-mac2ipv4.pl index 7417701..cd7faa5 100755 --- a/mm-mac2ipv4.pl +++ b/mm-mac2ipv4.pl @@ -12,14 +12,14 @@ sub usage { print < -Usage: ./mm-mac2ipv4.pl --list-all +Usage: ./mm-commotion-mac2ip.pl +Usage: ./mm-commotion-mac2ip.pl --list-all Examples: - ./mm-mac2ipv4.pl DC:9F:DB:CE:13:57 - ./mm-mac2ipv4.pl DC-9F-DB-CE-13-57 - ./mm-mac2ipv4.pl DC 9F DB CE 13 57 - ./mm-mac2ipv4.pl dc 9f db ce 13 57 + ./mm-commotion-mac2ip.pl DC:9F:DB:CE:13:57 + ./mm-commotion-mac2ip.pl DC-9F-DB-CE-13-57 + ./mm-commotion-mac2ip.pl DC 9F DB CE 13 57 + ./mm-commotion-mac2ip.pl dc 9f db ce 13 57 USAGE exit 1; } @@ -32,7 +32,7 @@ sub list_all { $mac[3] = $i; # Format IP address - my $ip = sprintf('%d.%d.%d.%d', $ip[0], $i % 64 + 64, $ip[2], $ip[3]); + my $ip = sprintf('%d.%d.%d.%d', $ip[0], $i % 32 + 96, $ip[2], $ip[3]); # Format MAC address my $mac = sprintf('%02X:%02X:%02X:%02X:%02X:%02X', @mac); @@ -66,9 +66,15 @@ sub list_all { my @mac = @ARGV; $_ = uc for @mac; -# Ensure nothing +# Ensure that we are working with the correct large MAC address block +# DC-9F-DB + +if ($mac[0] ne "DC" || $mac[1] ne "9F" || $mac[2] ne "DB") { + print "Unsupported MAC address. Only Ubiquiti-assigned MAC addresses beginning with DC:9F:DB are supported.\n"; + exit 1; +} # Convert last three hexadecimal octets to decimal values -my @ip = (100, hex($mac[3]) % 64 + 64, hex($mac[4]), hex($mac[5])); +my @ip = (100, hex($mac[3]) % 32 + 96, hex($mac[4]), hex($mac[5]) - (hex($mac[5]) % 64 - hex($mac[5]) % 32)); print "$ip[0].$ip[1].$ip[2].$ip[3]\n"; diff --git a/mm-mac2ipv4.sh b/mm-mac2ipv4.sh index d73554f..023d697 100755 --- a/mm-mac2ipv4.sh +++ b/mm-mac2ipv4.sh @@ -26,7 +26,7 @@ while getopts ":-:" opt; do for ip2 in `seq 0 255`;do mac4=$(printf "%02X\n" $ip2) - ip2=$(expr $ip2 % 64 + 64) + ip2=$(expr $ip2 % 32 + 96) # Format IP address ip="$ip1.$ip2.$ip3.$ip4" @@ -87,6 +87,7 @@ ip2=$(printf "%d" "0x$mac4") ip3=$(printf "%d" "0x$mac5") ip4=$(printf "%d" "0x$mac6") -ip2=$(expr $ip2 % 64 + 64) +ip2=$(expr $ip2 % 32 + 96) +ip4=$(expr $ip4 - $(expr $ip4 % 64 - $ip4 % 32)) echo "$ip1.$ip2.$ip3.$ip4" From d05c76670d2a50fb42a85a7493c71d775483b275 Mon Sep 17 00:00:00 2001 From: Jason Khanlar Date: Wed, 15 Jun 2016 16:45:45 -0400 Subject: [PATCH 2/9] oops? revert commotion --- mm-mac2ipv4.pl | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/mm-mac2ipv4.pl b/mm-mac2ipv4.pl index cd7faa5..bd9ed3b 100755 --- a/mm-mac2ipv4.pl +++ b/mm-mac2ipv4.pl @@ -12,14 +12,14 @@ sub usage { print < -Usage: ./mm-commotion-mac2ip.pl --list-all +Usage: ./mm-mac2ipv4.pl +Usage: ./mm-mac2ipv4.pl --list-all Examples: - ./mm-commotion-mac2ip.pl DC:9F:DB:CE:13:57 - ./mm-commotion-mac2ip.pl DC-9F-DB-CE-13-57 - ./mm-commotion-mac2ip.pl DC 9F DB CE 13 57 - ./mm-commotion-mac2ip.pl dc 9f db ce 13 57 + ./mm-mac2ipv4.pl DC:9F:DB:CE:13:57 + ./mm-mac2ipv4.pl DC-9F-DB-CE-13-57 + ./mm-mac2ipv4.pl DC 9F DB CE 13 57 + ./mm-mac2ipv4.pl dc 9f db ce 13 57 USAGE exit 1; } From 4e65742091ea0f48985d32e5ddaa877e58adb3f7 Mon Sep 17 00:00:00 2001 From: Jason Khanlar Date: Wed, 15 Jun 2016 17:36:15 -0400 Subject: [PATCH 3/9] fix tests to be compatible with collision fix --- mm-mac2ipv4.tests | 38 ++++++++++++++++++++++++++------------ 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/mm-mac2ipv4.tests b/mm-mac2ipv4.tests index 6497903..70e3c9a 100755 --- a/mm-mac2ipv4.tests +++ b/mm-mac2ipv4.tests @@ -30,8 +30,8 @@ test() { fi } -rand64to127() { - ((randdec=$RANDOM / 512 + 64)) +rand96to127() { + ((randdec=$RANDOM / 1024 + 96)) return $randdec } @@ -40,24 +40,38 @@ rand0to255() { return $randdec } +rand0to255eo32() { + ((randdec=$RANDOM / 128)) + ((randdec=randdec - (randdec % 64 - randdec % 32))) + return $randdec +} + randIPMA() { local a randMA=("DC:9F:DB" "DC:9F:DB" "DC:9F:DB" "DC:9F:DB") + # 1st IPv4 octet randIP="100" - rand64to127 + # 2nd IPv4 octet + rand96to127 randMA[0]="${randMA[0]}:$(printf "%02X\n" $((randdec - 64)))" randMA[1]="${randMA[1]}:$(printf "%02X\n" $randdec)" randMA[2]="${randMA[2]}:$(printf "%02X\n" $((randdec + 64)))" randMA[3]="${randMA[3]}:$(printf "%02X\n" $((randdec + 128)))" randIP="$randIP.$randdec" - for face in ^ ^; do - rand0to255 - randMA[0]="${randMA[0]}:$(printf "%02X\n" $randdec)" - randMA[1]="${randMA[1]}:$(printf "%02X\n" $randdec)" - randMA[2]="${randMA[2]}:$(printf "%02X\n" $randdec)" - randMA[3]="${randMA[3]}:$(printf "%02X\n" $randdec)" - randIP="$randIP.$randdec" - done + # 3rd IPv4 octet + rand0to255 + randMA[0]="${randMA[0]}:$(printf "%02X\n" $randdec)" + randMA[1]="${randMA[1]}:$(printf "%02X\n" $randdec)" + randMA[2]="${randMA[2]}:$(printf "%02X\n" $randdec)" + randMA[3]="${randMA[3]}:$(printf "%02X\n" $randdec)" + randIP="$randIP.$randdec" + # 4th IPv4 octet + rand0to255eo32 # every other 32 + randMA[0]="${randMA[0]}:$(printf "%02X\n" $randdec)" + randMA[1]="${randMA[1]}:$(printf "%02X\n" $randdec)" + randMA[2]="${randMA[2]}:$(printf "%02X\n" $randdec)" + randMA[3]="${randMA[3]}:$(printf "%02X\n" $randdec)" + randIP="$randIP.$randdec" return } @@ -86,7 +100,7 @@ list-all-tests() { } for script_to_test in "${scripts_to_test[@]}"; do - list-all-tests + #list-all-tests random-tests done From f79022b887035750e3e66886d538d834514f3241 Mon Sep 17 00:00:00 2001 From: Jason Khanlar Date: Tue, 21 Jun 2016 17:16:42 -0400 Subject: [PATCH 4/9] It's almost never a good idea to modify a variable which is the element of a collection under iteration - https://github.com/pittmesh/ip-calculator/pull/26#discussion_r67288529 --- mm-mac2ipv4.bash | 4 ++-- mm-mac2ipv4.sh | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/mm-mac2ipv4.bash b/mm-mac2ipv4.bash index 9065379..8e6a4db 100755 --- a/mm-mac2ipv4.bash +++ b/mm-mac2ipv4.bash @@ -20,10 +20,10 @@ function list-all { ip3=0 ip4=0 - for ip2 in `seq 0 255`;do + for octet in `seq 0 255`;do mac4=$(printf "%02X\n" $ip2) - ((ip2=ip2 % 32 + 96)) + ((ip2=octet % 32 + 96)) # Format IP address ip="$ip1.$ip2.$ip3.$ip4" diff --git a/mm-mac2ipv4.sh b/mm-mac2ipv4.sh index 023d697..fc0b85d 100755 --- a/mm-mac2ipv4.sh +++ b/mm-mac2ipv4.sh @@ -23,10 +23,10 @@ while getopts ":-:" opt; do ip3=0 ip4=0 - for ip2 in `seq 0 255`;do + for octet in `seq 0 255`;do mac4=$(printf "%02X\n" $ip2) - ip2=$(expr $ip2 % 32 + 96) + ip2=$(expr $octet % 32 + 96) # Format IP address ip="$ip1.$ip2.$ip3.$ip4" From af0698c81468c66438805f9428c6fba254cd384b Mon Sep 17 00:00:00 2001 From: Jason Khanlar Date: Tue, 21 Jun 2016 18:09:26 -0400 Subject: [PATCH 5/9] Avoid redundancy and offer brief explanation (per function name) of calculations - https://github.com/pittmesh/ip-calculator/pull/26#discussion_r67288591 --- mm-mac2ipv4.bash | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/mm-mac2ipv4.bash b/mm-mac2ipv4.bash index 8e6a4db..dec3ae6 100755 --- a/mm-mac2ipv4.bash +++ b/mm-mac2ipv4.bash @@ -7,6 +7,14 @@ # * Jason Khanlar # +function normalize-ipv4-octet-2 { + ((ip2=$1 % 32 + 96)) +} + +function normalize-ipv4-octet-4 { + ((ip4=$1 - ($1 % 64 - $1 % 32))) +} + function list-all { mac1=DC mac2=9F @@ -23,7 +31,7 @@ function list-all { for octet in `seq 0 255`;do mac4=$(printf "%02X\n" $ip2) - ((ip2=octet % 32 + 96)) + normalize-ipv4-octet-2 $octet # Format IP address ip="$ip1.$ip2.$ip3.$ip4" @@ -99,7 +107,7 @@ ip2=$(printf "%d" "0x$mac4") ip3=$(printf "%d" "0x$mac5") ip4=$(printf "%d" "0x$mac6") -((ip2=ip2 % 32 + 96)) -((ip4=ip4 - (ip4 % 64 - ip4 % 32))) +normalize-ipv4-octet-2 $ip2 +normalize-ipv4-octet-4 $ip4 echo "$ip1.$ip2.$ip3.$ip4" From b172c683ea3eb100182e73786279c3d7fe0aab9d Mon Sep 17 00:00:00 2001 From: Jason Khanlar Date: Tue, 21 Jun 2016 18:11:13 -0400 Subject: [PATCH 6/9] Adjust 4th octet of mac address after adjusting 2nd octet of ipv4 address since it depends on correct value --- mm-mac2ipv4.bash | 4 ++-- mm-mac2ipv4.sh | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/mm-mac2ipv4.bash b/mm-mac2ipv4.bash index dec3ae6..b39cf36 100755 --- a/mm-mac2ipv4.bash +++ b/mm-mac2ipv4.bash @@ -29,10 +29,10 @@ function list-all { ip4=0 for octet in `seq 0 255`;do - mac4=$(printf "%02X\n" $ip2) - normalize-ipv4-octet-2 $octet + mac4=$(printf "%02X\n" $ip2) + # Format IP address ip="$ip1.$ip2.$ip3.$ip4" diff --git a/mm-mac2ipv4.sh b/mm-mac2ipv4.sh index fc0b85d..a52d1b9 100755 --- a/mm-mac2ipv4.sh +++ b/mm-mac2ipv4.sh @@ -24,10 +24,10 @@ while getopts ":-:" opt; do ip4=0 for octet in `seq 0 255`;do - mac4=$(printf "%02X\n" $ip2) - ip2=$(expr $octet % 32 + 96) + mac4=$(printf "%02X\n" $ip2) + # Format IP address ip="$ip1.$ip2.$ip3.$ip4" From 67c1a96305095c1f9bd710b22e784bdba5c00c4d Mon Sep 17 00:00:00 2001 From: Jason Khanlar Date: Tue, 21 Jun 2016 18:28:53 -0400 Subject: [PATCH 7/9] Adjusted function names and relocated comments - https://github.com/pittmesh/ip-calculator/pull/26#discussion_r67288726 --- mm-mac2ipv4.tests | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/mm-mac2ipv4.tests b/mm-mac2ipv4.tests index 70e3c9a..5fa946e 100755 --- a/mm-mac2ipv4.tests +++ b/mm-mac2ipv4.tests @@ -30,17 +30,24 @@ test() { fi } -rand96to127() { +randIPv4octet1() { + randIP="100" +} + +randIPv4octet2() { + # Random 96-127 ((randdec=$RANDOM / 1024 + 96)) return $randdec } -rand0to255() { +randIPv4octet3() { + # Random 0-255 ((randdec=$RANDOM / 128)) return $randdec } -rand0to255eo32() { +randIPv4octet4() { + # Random 0-255 every other 32 ((randdec=$RANDOM / 128)) ((randdec=randdec - (randdec % 64 - randdec % 32))) return $randdec @@ -49,24 +56,20 @@ rand0to255eo32() { randIPMA() { local a randMA=("DC:9F:DB" "DC:9F:DB" "DC:9F:DB" "DC:9F:DB") - # 1st IPv4 octet - randIP="100" - # 2nd IPv4 octet - rand96to127 + randIPv4octet1 + randIPv4octet2 randMA[0]="${randMA[0]}:$(printf "%02X\n" $((randdec - 64)))" randMA[1]="${randMA[1]}:$(printf "%02X\n" $randdec)" randMA[2]="${randMA[2]}:$(printf "%02X\n" $((randdec + 64)))" randMA[3]="${randMA[3]}:$(printf "%02X\n" $((randdec + 128)))" randIP="$randIP.$randdec" - # 3rd IPv4 octet - rand0to255 + randIPv4octet3 randMA[0]="${randMA[0]}:$(printf "%02X\n" $randdec)" randMA[1]="${randMA[1]}:$(printf "%02X\n" $randdec)" randMA[2]="${randMA[2]}:$(printf "%02X\n" $randdec)" randMA[3]="${randMA[3]}:$(printf "%02X\n" $randdec)" randIP="$randIP.$randdec" - # 4th IPv4 octet - rand0to255eo32 # every other 32 + randIPv4octet4 randMA[0]="${randMA[0]}:$(printf "%02X\n" $randdec)" randMA[1]="${randMA[1]}:$(printf "%02X\n" $randdec)" randMA[2]="${randMA[2]}:$(printf "%02X\n" $randdec)" From f32d19bd40b5d3c1fc13cfc60fa63e0d1cb54c11 Mon Sep 17 00:00:00 2001 From: Jason Khanlar Date: Tue, 21 Jun 2016 19:48:56 -0400 Subject: [PATCH 8/9] remove unnecessary return statement in randIPMA function --- mm-mac2ipv4.tests | 1 - 1 file changed, 1 deletion(-) diff --git a/mm-mac2ipv4.tests b/mm-mac2ipv4.tests index 5fa946e..7b9d4e4 100755 --- a/mm-mac2ipv4.tests +++ b/mm-mac2ipv4.tests @@ -75,7 +75,6 @@ randIPMA() { randMA[2]="${randMA[2]}:$(printf "%02X\n" $randdec)" randMA[3]="${randMA[3]}:$(printf "%02X\n" $randdec)" randIP="$randIP.$randdec" - return } random-tests() { From 976f226402bf79b0d16799a653b62a11265bf771 Mon Sep 17 00:00:00 2001 From: Jason Khanlar Date: Tue, 21 Jun 2016 19:57:57 -0400 Subject: [PATCH 9/9] remove other unnecessary incorrectly used return statements --- mm-mac2ipv4.tests | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/mm-mac2ipv4.tests b/mm-mac2ipv4.tests index 7b9d4e4..1dfd6ec 100755 --- a/mm-mac2ipv4.tests +++ b/mm-mac2ipv4.tests @@ -37,27 +37,26 @@ randIPv4octet1() { randIPv4octet2() { # Random 96-127 ((randdec=$RANDOM / 1024 + 96)) - return $randdec } randIPv4octet3() { # Random 0-255 ((randdec=$RANDOM / 128)) - return $randdec } randIPv4octet4() { # Random 0-255 every other 32 ((randdec=$RANDOM / 128)) ((randdec=randdec - (randdec % 64 - randdec % 32))) - return $randdec } randIPMA() { local a randMA=("DC:9F:DB" "DC:9F:DB" "DC:9F:DB" "DC:9F:DB") randIPv4octet1 + echo $randdec randIPv4octet2 + echo $randdec randMA[0]="${randMA[0]}:$(printf "%02X\n" $((randdec - 64)))" randMA[1]="${randMA[1]}:$(printf "%02X\n" $randdec)" randMA[2]="${randMA[2]}:$(printf "%02X\n" $((randdec + 64)))"