1 // Copyright 2011 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // MAC address manipulations
11 const hexDigit
= "0123456789abcdef"
13 // A HardwareAddr represents a physical hardware address.
14 type HardwareAddr
[]byte
16 func (a HardwareAddr
) String() string {
20 buf
:= make([]byte, 0, len(a
)*3-1)
23 buf
= append(buf
, ':')
25 buf
= append(buf
, hexDigit
[b
>>4])
26 buf
= append(buf
, hexDigit
[b
&0xF])
31 // ParseMAC parses s as an IEEE 802 MAC-48, EUI-48, or EUI-64 using one of the
34 // 01:23:45:67:89:ab:cd:ef
36 // 01-23-45-67-89-ab-cd-ef
38 // 0123.4567.89ab.cdef
39 func ParseMAC(s
string) (hw HardwareAddr
, err error
) {
44 if s
[2] == ':' || s
[2] == '-' {
45 if (len(s
)+1)%3
!= 0 {
52 hw
= make(HardwareAddr
, n
)
53 for x
, i
:= 0, 0; i
< n
; i
++ {
55 if hw
[i
], ok
= xtoi2(s
[x
:], s
[2]); !ok
{
60 } else if s
[4] == '.' {
61 if (len(s
)+1)%5
!= 0 {
64 n
:= 2 * (len(s
) + 1) / 5
68 hw
= make(HardwareAddr
, n
)
69 for x
, i
:= 0, 0; i
< n
; i
+= 2 {
71 if hw
[i
], ok
= xtoi2(s
[x
:x
+2], 0); !ok
{
74 if hw
[i
+1], ok
= xtoi2(s
[x
+2:], s
[4]); !ok
{
85 return nil, errors
.New("invalid MAC address: " + s
)