-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
65 lines (56 loc) · 1.39 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package main
import (
"fmt"
"net"
"os"
"strconv"
"strings"
)
func parse_args() net.HardwareAddr {
if len(os.Args[1:]) < 1 {
fmt.Printf("Usage: %s MAC_ADDRESS\n", os.Args[0])
os.Exit(1)
}
mac_address, err := net.ParseMAC(os.Args[1])
if err != nil {
fmt.Printf("%s\n", err)
os.Exit(1)
}
return mac_address
}
func flip_seventh_bit(mac_segment string) string {
mac_segment_as_int, _ := strconv.Atoi(mac_segment)
shifted := mac_segment_as_int ^ (1 << 1)
return strconv.Itoa(shifted)
}
func trim_leading_zeros(mac_segment string) string {
return strings.TrimLeft(mac_segment, "0")
}
func trim_address(mac_segments []string) []string {
// Start by trimming the 1st, 3rd, and 5th mac_segments,
// which are always safe to trim
for i := 0; i < 5; i = i + 2 {
mac_segments[i] = trim_leading_zeros(mac_segments[i])
// The 2nd and 4th mac_segments can be trimmed
// only if their preceding mac_segment is empty
if i != 2 && mac_segments[i] == "" {
mac_segments[i+1] = trim_leading_zeros(mac_segments[i+1])
}
}
return mac_segments
}
func main() {
mac_address := parse_args()
mac_segments := strings.Split(mac_address.String(), ":")
mac_segments[0] = flip_seventh_bit(mac_segments[0])
mac_segments = trim_address(mac_segments)
fmt.Printf(
"%s%s:%sff:fe%s:%s%s\n",
mac_segments[0],
mac_segments[1],
mac_segments[2],
mac_segments[3],
mac_segments[4],
mac_segments[5],
)
}