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.
7 const hexDigit
= "0123456789abcdef"
9 // A HardwareAddr represents a physical hardware address.
10 type HardwareAddr
[]byte
12 func (a HardwareAddr
) String() string {
16 buf
:= make([]byte, 0, len(a
)*3-1)
19 buf
= append(buf
, ':')
21 buf
= append(buf
, hexDigit
[b
>>4])
22 buf
= append(buf
, hexDigit
[b
&0xF])
27 // ParseMAC parses s as an IEEE 802 MAC-48, EUI-48, EUI-64, or a 20-octet
28 // IP over InfiniBand link-layer address using one of the following formats:
30 // 01:23:45:67:89:ab:cd:ef
31 // 01:23:45:67:89:ab:cd:ef:00:00:01:23:45:67:89:ab:cd:ef:00:00
33 // 01-23-45-67-89-ab-cd-ef
34 // 01-23-45-67-89-ab-cd-ef-00-00-01-23-45-67-89-ab-cd-ef-00-00
36 // 0123.4567.89ab.cdef
37 // 0123.4567.89ab.cdef.0000.0123.4567.89ab.cdef.0000
38 func ParseMAC(s
string) (hw HardwareAddr
, err error
) {
43 if s
[2] == ':' || s
[2] == '-' {
44 if (len(s
)+1)%3
!= 0 {
48 if n
!= 6 && n
!= 8 && n
!= 20 {
51 hw
= make(HardwareAddr
, n
)
52 for x
, i
:= 0, 0; i
< n
; i
++ {
54 if hw
[i
], ok
= xtoi2(s
[x
:], s
[2]); !ok
{
59 } else if s
[4] == '.' {
60 if (len(s
)+1)%5
!= 0 {
63 n
:= 2 * (len(s
) + 1) / 5
64 if n
!= 6 && n
!= 8 && n
!= 20 {
67 hw
= make(HardwareAddr
, n
)
68 for x
, i
:= 0, 0; i
< n
; i
+= 2 {
70 if hw
[i
], ok
= xtoi2(s
[x
:x
+2], 0); !ok
{
73 if hw
[i
+1], ok
= xtoi2(s
[x
+2:], s
[4]); !ok
{
84 return nil, &AddrError
{Err
: "invalid MAC address", Addr
: s
}