summaryrefslogtreecommitdiff
path: root/utils
diff options
context:
space:
mode:
Diffstat (limited to 'utils')
-rw-r--r--utils/net.go26
1 files changed, 26 insertions, 0 deletions
diff --git a/utils/net.go b/utils/net.go
index 76454bd..c69d90f 100644
--- a/utils/net.go
+++ b/utils/net.go
@@ -16,6 +16,13 @@ func (m *Mac) Format() string {
m.Addr[0], m.Addr[1], m.Addr[2], m.Addr[3], m.Addr[4], m.Addr[5])
}
+// Compact renders the MAC as 12 lowercase hex digits with no separators, the
+// form used as a DNS label (e.g. aabbccddeeff).
+func (m *Mac) Compact() string {
+ return fmt.Sprintf("%02x%02x%02x%02x%02x%02x",
+ m.Addr[0], m.Addr[1], m.Addr[2], m.Addr[3], m.Addr[4], m.Addr[5])
+}
+
// ParseMac reads a colon-separated hex MAC. It tolerates uppercase and the
// zero-stripped octets macOS prints (e.g. "8:0:27:ab:cd:ef").
func ParseMac(s string) (*Mac, error) {
@@ -32,6 +39,25 @@ func ParseMac(s string) (*Mac, error) {
return mac, nil
}
+// ParseMacCompact reads the 12-hex-digit, separator-less form used as a DNS
+// label (e.g. aabbccddeeff) back into a Mac.
+func ParseMacCompact(s string) (*Mac, error) {
+ mac := &Mac{}
+ t := strings.TrimSpace(s)
+ if len(t) != 12 {
+ return nil, fmt.Errorf("invalid compact mac %q: want 12 hex digits, got %d", s, len(t))
+ }
+ n, err := fmt.Sscanf(t, "%2x%2x%2x%2x%2x%2x",
+ &mac.Addr[0], &mac.Addr[1], &mac.Addr[2], &mac.Addr[3], &mac.Addr[4], &mac.Addr[5])
+ if err != nil {
+ return nil, err
+ }
+ if n != 6 {
+ return nil, fmt.Errorf("invalid compact mac %q: parsed %d of 6 octets", s, n)
+ }
+ return mac, nil
+}
+
// INet4 is an IPv4 address.
type INet4 struct {
Addr [4]byte