diff options
Diffstat (limited to 'libgo/go/net/interface.go')
-rw-r--r-- | libgo/go/net/interface.go | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/libgo/go/net/interface.go b/libgo/go/net/interface.go index 8a14cb23202..2696b7f4c58 100644 --- a/libgo/go/net/interface.go +++ b/libgo/go/net/interface.go @@ -26,6 +26,63 @@ func (a HardwareAddr) String() string { return buf.String() } +// ParseMAC parses s as an IEEE 802 MAC-48, EUI-48, or EUI-64 using one of the +// following formats: +// 01:23:45:67:89:ab +// 01:23:45:67:89:ab:cd:ef +// 01-23-45-67-89-ab +// 01-23-45-67-89-ab-cd-ef +// 0123.4567.89ab +// 0123.4567.89ab.cdef +func ParseMAC(s string) (hw HardwareAddr, err os.Error) { + if len(s) < 14 { + goto error + } + + if s[2] == ':' || s[2] == '-' { + if (len(s)+1)%3 != 0 { + goto error + } + n := (len(s) + 1) / 3 + if n != 6 && n != 8 { + goto error + } + hw = make(HardwareAddr, n) + for x, i := 0, 0; i < n; i++ { + var ok bool + if hw[i], ok = xtoi2(s[x:], s[2]); !ok { + goto error + } + x += 3 + } + } else if s[4] == '.' { + if (len(s)+1)%5 != 0 { + goto error + } + n := 2 * (len(s) + 1) / 5 + if n != 6 && n != 8 { + goto error + } + hw = make(HardwareAddr, n) + for x, i := 0, 0; i < n; i += 2 { + var ok bool + if hw[i], ok = xtoi2(s[x:x+2], 0); !ok { + goto error + } + if hw[i+1], ok = xtoi2(s[x+2:], s[4]); !ok { + goto error + } + x += 5 + } + } else { + goto error + } + return hw, nil + +error: + return nil, os.NewError("invalid MAC address: " + s) +} + // Interface represents a mapping between network interface name // and index. It also represents network interface facility // information. |