Skip to content

Commit 043b9de

Browse files
devon-margopherbot
authored andcommitted
net: parse addresses without separators in ParseMac
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 (Azure/azure-container-networking#4122) among others. Fixes #66682 Change-Id: Id66c23d50ebb1fed1f3bdb5cf3822a8fd60b886d GitHub-Last-Rev: 77900cc GitHub-Pull-Request: #76387 Reviewed-on: https://go-review.googlesource.com/c/go/+/722720 Auto-Submit: Damien Neil <[email protected]> Reviewed-by: Damien Neil <[email protected]> Reviewed-by: Dmitri Shuralyov <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent e432b4f commit 043b9de

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

src/net/mac.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,9 @@ func (a HardwareAddr) String() string {
3636
// 0000.5e00.5301
3737
// 0200.5e10.0000.0001
3838
// 0000.0000.fe80.0000.0000.0000.0200.5e10.0000.0001
39+
// 00005e005301
3940
func ParseMAC(s string) (hw HardwareAddr, err error) {
40-
if len(s) < 14 {
41+
if len(s) < 12 {
4142
goto error
4243
}
4344

@@ -77,7 +78,23 @@ func ParseMAC(s string) (hw HardwareAddr, err error) {
7778
x += 5
7879
}
7980
} else {
80-
goto error
81+
if len(s)%2 != 0 {
82+
goto error
83+
}
84+
85+
n := len(s) / 2
86+
if n != 6 && n != 8 && n != 20 {
87+
goto error
88+
}
89+
90+
hw = make(HardwareAddr, len(s)/2)
91+
for x, i := 0, 0; i < n; i++ {
92+
var ok bool
93+
if hw[i], ok = xtoi2(s[x:x+2], 0); !ok {
94+
goto error
95+
}
96+
x += 2
97+
}
8198
}
8299
return hw, nil
83100

src/net/mac_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@ var parseMACTests = []struct {
1919
{"00:00:5e:00:53:01", HardwareAddr{0x00, 0x00, 0x5e, 0x00, 0x53, 0x01}, ""},
2020
{"00-00-5e-00-53-01", HardwareAddr{0x00, 0x00, 0x5e, 0x00, 0x53, 0x01}, ""},
2121
{"0000.5e00.5301", HardwareAddr{0x00, 0x00, 0x5e, 0x00, 0x53, 0x01}, ""},
22+
{"00005e005301", HardwareAddr{0x00, 0x00, 0x5e, 0x00, 0x53, 0x01}, ""},
2223

2324
// See RFC 7042, Section 2.2.2.
2425
{"02:00:5e:10:00:00:00:01", HardwareAddr{0x02, 0x00, 0x5e, 0x10, 0x00, 0x00, 0x00, 0x01}, ""},
2526
{"02-00-5e-10-00-00-00-01", HardwareAddr{0x02, 0x00, 0x5e, 0x10, 0x00, 0x00, 0x00, 0x01}, ""},
2627
{"0200.5e10.0000.0001", HardwareAddr{0x02, 0x00, 0x5e, 0x10, 0x00, 0x00, 0x00, 0x01}, ""},
28+
{"02005e1000000001", HardwareAddr{0x02, 0x00, 0x5e, 0x10, 0x00, 0x00, 0x00, 0x01}, ""},
2729

2830
// See RFC 4391, Section 9.1.1.
2931
{
@@ -53,6 +55,15 @@ var parseMACTests = []struct {
5355
},
5456
"",
5557
},
58+
{
59+
"00000000fe8000000000000002005e1000000001",
60+
HardwareAddr{
61+
0x00, 0x00, 0x00, 0x00,
62+
0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
63+
0x02, 0x00, 0x5e, 0x10, 0x00, 0x00, 0x00, 0x01,
64+
},
65+
"",
66+
},
5667

5768
{"ab:cd:ef:AB:CD:EF", HardwareAddr{0xab, 0xcd, 0xef, 0xab, 0xcd, 0xef}, ""},
5869
{"ab:cd:ef:AB:CD:EF:ab:cd", HardwareAddr{0xab, 0xcd, 0xef, 0xab, 0xcd, 0xef, 0xab, 0xcd}, ""},
@@ -78,6 +89,7 @@ var parseMACTests = []struct {
7889
{"01:02-03-04-05-06", nil, "invalid MAC address"},
7990
{"0123:4567:89AF", nil, "invalid MAC address"},
8091
{"0123-4567-89AF", nil, "invalid MAC address"},
92+
{"0123456789AF0", nil, "invalid MAC address"},
8193
}
8294

8395
func TestParseMAC(t *testing.T) {

0 commit comments

Comments
 (0)