]> git.feebdaed.xyz Git - 0xmirror/go.git/commitdiff
net: parse addresses without separators in ParseMac
authorDevon Mar <devonm@mdmm.ca>
Tue, 25 Nov 2025 07:38:09 +0000 (07:38 +0000)
committerGopher Robot <gobot@golang.org>
Tue, 2 Dec 2025 20:42:13 +0000 (12:42 -0800)
IEEE EUI guidelines states that "an EUI-48 can be represented in the IEEE RA
hexadecimal (hex) form with the octets separated by hyphens, or as a pure
base-16 numerical representation without hyphens"
(https://standards.ieee.org/wp-content/uploads/import/documents/tutorials/eui.pdf p.9).
The latter form is used in Azure Instance Metadata Service
(https://github.com/Azure/azure-container-networking/pull/4122) among others.

Fixes #66682

Change-Id: Id66c23d50ebb1fed1f3bdb5cf3822a8fd60b886d
GitHub-Last-Rev: 77900cc1a68cb535b685a63cf84b5413b480df2c
GitHub-Pull-Request: golang/go#76387
Reviewed-on: https://go-review.googlesource.com/c/go/+/722720
Auto-Submit: Damien Neil <dneil@google.com>
Reviewed-by: Damien Neil <dneil@google.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>

src/net/mac.go
src/net/mac_test.go

index 53d5b2dbf596b43b8b05b25169bfce8b6705cb39..b69a42d40b949d450bf1bc2c3b68c38f5ff00be7 100644 (file)
@@ -36,8 +36,9 @@ func (a HardwareAddr) String() string {
 //     0000.5e00.5301
 //     0200.5e10.0000.0001
 //     0000.0000.fe80.0000.0000.0000.0200.5e10.0000.0001
+//     00005e005301
 func ParseMAC(s string) (hw HardwareAddr, err error) {
-       if len(s) < 14 {
+       if len(s) < 12 {
                goto error
        }
 
@@ -77,7 +78,23 @@ func ParseMAC(s string) (hw HardwareAddr, err error) {
                        x += 5
                }
        } else {
-               goto error
+               if len(s)%2 != 0 {
+                       goto error
+               }
+
+               n := len(s) / 2
+               if n != 6 && n != 8 && n != 20 {
+                       goto error
+               }
+
+               hw = make(HardwareAddr, len(s)/2)
+               for x, i := 0, 0; i < n; i++ {
+                       var ok bool
+                       if hw[i], ok = xtoi2(s[x:x+2], 0); !ok {
+                               goto error
+                       }
+                       x += 2
+               }
        }
        return hw, nil
 
index cad884fcf5d42df96fefd720e63f4823ae86c388..9bfad4f12e806f0c003b6192a67859c09d930a72 100644 (file)
@@ -19,11 +19,13 @@ var parseMACTests = []struct {
        {"00:00:5e:00:53:01", HardwareAddr{0x00, 0x00, 0x5e, 0x00, 0x53, 0x01}, ""},
        {"00-00-5e-00-53-01", HardwareAddr{0x00, 0x00, 0x5e, 0x00, 0x53, 0x01}, ""},
        {"0000.5e00.5301", HardwareAddr{0x00, 0x00, 0x5e, 0x00, 0x53, 0x01}, ""},
+       {"00005e005301", HardwareAddr{0x00, 0x00, 0x5e, 0x00, 0x53, 0x01}, ""},
 
        // See RFC 7042, Section 2.2.2.
        {"02:00:5e:10:00:00:00:01", HardwareAddr{0x02, 0x00, 0x5e, 0x10, 0x00, 0x00, 0x00, 0x01}, ""},
        {"02-00-5e-10-00-00-00-01", HardwareAddr{0x02, 0x00, 0x5e, 0x10, 0x00, 0x00, 0x00, 0x01}, ""},
        {"0200.5e10.0000.0001", HardwareAddr{0x02, 0x00, 0x5e, 0x10, 0x00, 0x00, 0x00, 0x01}, ""},
+       {"02005e1000000001", HardwareAddr{0x02, 0x00, 0x5e, 0x10, 0x00, 0x00, 0x00, 0x01}, ""},
 
        // See RFC 4391, Section 9.1.1.
        {
@@ -53,6 +55,15 @@ var parseMACTests = []struct {
                },
                "",
        },
+       {
+               "00000000fe8000000000000002005e1000000001",
+               HardwareAddr{
+                       0x00, 0x00, 0x00, 0x00,
+                       0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+                       0x02, 0x00, 0x5e, 0x10, 0x00, 0x00, 0x00, 0x01,
+               },
+               "",
+       },
 
        {"ab:cd:ef:AB:CD:EF", HardwareAddr{0xab, 0xcd, 0xef, 0xab, 0xcd, 0xef}, ""},
        {"ab:cd:ef:AB:CD:EF:ab:cd", HardwareAddr{0xab, 0xcd, 0xef, 0xab, 0xcd, 0xef, 0xab, 0xcd}, ""},
@@ -78,6 +89,7 @@ var parseMACTests = []struct {
        {"01:02-03-04-05-06", nil, "invalid MAC address"},
        {"0123:4567:89AF", nil, "invalid MAC address"},
        {"0123-4567-89AF", nil, "invalid MAC address"},
+       {"0123456789AF0", nil, "invalid MAC address"},
 }
 
 func TestParseMAC(t *testing.T) {