fix: unmarshal HardwareAddr without stdlib help

Stdlib `net.ParseMAC` does lots of validations, but some hardware addrs
we can see (on logical interfaces) are not valid, so parse MACs in a
simple way.

Signed-off-by: Andrey Smirnov <andrey.smirnov@talos-systems.com>
This commit is contained in:
Andrey Smirnov 2022-05-26 23:30:31 +04:00
parent f2e94d6020
commit f9c46fb18d
No known key found for this signature in database
GPG Key ID: 7B26396447AB6DFD

View File

@ -4,7 +4,11 @@
package nethelpers
import "net"
import (
"bytes"
"encoding/hex"
"net"
)
// HardwareAddr wraps net.HardwareAddr for YAML marshaling.
type HardwareAddr net.HardwareAddr
@ -16,12 +20,17 @@ func (addr HardwareAddr) MarshalText() ([]byte, error) {
// UnmarshalText implements text.Unmarshaler interface.
func (addr *HardwareAddr) UnmarshalText(b []byte) error {
mac, err := net.ParseMAC(string(b))
rawHex := bytes.ReplaceAll(b, []byte(":"), []byte(""))
dstLen := hex.DecodedLen(len(rawHex))
dst := make([]byte, dstLen)
n, err := hex.Decode(dst, rawHex)
if err != nil {
return err
}
*addr = HardwareAddr(mac)
*addr = HardwareAddr(dst[:n])
return nil
}