mirror of
https://github.com/siderolabs/talos.git
synced 2025-10-25 14:31:11 +02:00
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:
parent
f2e94d6020
commit
f9c46fb18d
@ -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
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user