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:
31 // 02:00:5e:10:00:00:00:01
32 // 00:00:00:00:fe:80:00:00:00:00:00:00:02:00:5e:10:00:00:00:01
34 // 02-00-5e-10-00-00-00-01
35 // 00-00-00-00-fe-80-00-00-00-00-00-00-02-00-5e-10-00-00-00-01
37 // 0200.5e10.0000.0001
38 // 0000.0000.fe80.0000.0000.0000.0200.5e10.0000.0001
39 func ParseMAC(s string) (hw HardwareAddr, err error) {
44 if s[2] == ':' || s[2] == '-' {
45 if (len(s)+1)%3 != 0 {
49 if n != 6 && n != 8 && n != 20 {
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
65 if n != 6 && n != 8 && n != 20 {
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, &AddrError{Err: "invalid MAC address", Addr: s}