Skip to content

Commit

Permalink
fix: unmarshal HardwareAddr without stdlib help
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
smira committed May 26, 2022
1 parent f2e94d6 commit f9c46fb
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions pkg/machinery/nethelpers/hwaddr.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@

package nethelpers

import "net"
import (
"bytes"
"encoding/hex"
"net"
)

// HardwareAddr wraps net.HardwareAddr for YAML marshaling.
type HardwareAddr net.HardwareAddr
Expand All @@ -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
}
Expand Down

0 comments on commit f9c46fb

Please sign in to comment.