net-predictable/mac.go

52 lines
938 B
Go
Raw Normal View History

package main
import (
"fmt"
"io"
"net"
"os"
"strings"
)
func (i *iface) ProcMAC() (err error) {
var (
sb strings.Builder
file *os.File
cbytes []byte
)
macpath := strings.Join([]string{i.netpath, "address"}, "/")
if file, err = os.Open(macpath); err != nil {
err = fmt.Errorf("Failed to open file: %w", err)
return
}
defer file.Close()
if cbytes, err = io.ReadAll(file); err != nil {
err = fmt.Errorf("Failed to read file: %w", err)
return
}
var (
strmac string
ok bool
)
if strmac, ok = strings.CutSuffix(string(cbytes), "\n"); !ok {
err = ErrParseMac
return
}
var addr net.HardwareAddr
if addr, err = net.ParseMAC(strmac); err != nil {
err = fmt.Errorf("%w: %w", ErrParseMac, err)
return
}
_, err = sb.WriteString(i.pfx)
_, err = sb.WriteString("x")
for _, v := range addr {
2024-07-07 12:18:53 +00:00
_, err = sb.WriteString(fmt.Sprintf("%02x", v))
}
i.names[IfNameMAC] = sb.String()
return
}