42 lines
776 B
Go
42 lines
776 B
Go
|
package iptools
|
||
|
|
||
|
import (
|
||
|
"bytes"
|
||
|
"fmt"
|
||
|
"strconv"
|
||
|
"strings"
|
||
|
)
|
||
|
|
||
|
type HardwareAddress []byte
|
||
|
|
||
|
func (mac HardwareAddress) String() string {
|
||
|
marshalled := []string{}
|
||
|
for i := 0; i < len(mac); i++ {
|
||
|
marshalled = append(marshalled, fmt.Sprintf("%02x", []byte(mac[i:i+1])))
|
||
|
}
|
||
|
|
||
|
return strings.Join(marshalled, ":")
|
||
|
}
|
||
|
|
||
|
func (mac HardwareAddress) MarshalText() ([]byte, error) {
|
||
|
return []byte(mac.String()), nil
|
||
|
}
|
||
|
|
||
|
func (mac *HardwareAddress) UnmarshalText(b []byte) error {
|
||
|
rb := bytes.Split(b, []byte(":"))
|
||
|
for _, octet := range rb {
|
||
|
n, err := strconv.ParseUint(string(octet), 16, 8)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
*mac = append(*mac, uint8(n))
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func (mac HardwareAddress) Match(other HardwareAddress) bool {
|
||
|
return bytes.Equal(mac, other)
|
||
|
}
|