Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/net/mac.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -76,6 +77,14 @@ func ParseMAC(s string) (hw HardwareAddr, err error) {
}
x += 5
}
} else if len(s)%2 == 0 {
hw = make(HardwareAddr, len(s)/2)
for i := range hw {
var ok bool
if hw[i], ok = xtoi2(s[i*2:i*2+2], 0); !ok {
goto error
}
}
} else {
goto error
}
Expand Down
12 changes: 12 additions & 0 deletions src/net/mac_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
{
Expand Down Expand Up @@ -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}, ""},
Expand All @@ -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) {
Expand Down