2024-06-24 19:58:15 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-06-25 12:44:53 +00:00
|
|
|
_, err = sb.WriteString(i.pfx)
|
2024-06-24 19:58:15 +00:00
|
|
|
_, err = sb.WriteString("x")
|
|
|
|
for _, v := range addr {
|
2024-07-07 12:18:53 +00:00
|
|
|
_, err = sb.WriteString(fmt.Sprintf("%02x", v))
|
2024-06-24 19:58:15 +00:00
|
|
|
}
|
|
|
|
|
2024-06-25 12:44:53 +00:00
|
|
|
i.names[IfNameMAC] = sb.String()
|
2024-06-24 19:58:15 +00:00
|
|
|
return
|
|
|
|
}
|