kdhcp/leases/hardware_address.go

42 lines
775 B
Go
Raw Permalink Normal View History

2023-05-15 13:09:12 +00:00
package leases
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)
}