devicepath/mac.go

54 lines
1,006 B
Go
Raw Normal View History

2023-09-18 11:47:44 +00:00
package devicepath
import (
"encoding/hex"
"errors"
"fmt"
"strconv"
"strings"
)
const macPrefix string = "MAC"
type MacDevicePath struct {
addr []byte
ifType uint8
}
func (mp *MacDevicePath) Type() DeviceNodeType {
return MessagingDevicePath
}
func (mp *MacDevicePath) SubType() DeviceNodeSubType {
return MacMessagingSubType
}
func (mp *MacDevicePath) Bytes() []byte {
return nil
}
func (mp *MacDevicePath) String() string {
return fmt.Sprintf("%s(%x,%d)", macPrefix, mp.addr, mp.ifType)
}
func (mp *MacDevicePath) ParseString(raw string) error {
if !checkStringFormat(raw, macPrefix, -1) {
return ErrMalformedString
}
args := strings.Split(raw[len(macPrefix)+1:len(raw)-1], ",")
if len(args) != 2 {
return errors.New("unexpected number of arguments")
}
if mac, err := hex.DecodeString(args[0]); err != nil {
return err
} else {
mp.addr = mac
}
if ifType, err := strconv.Atoi(args[1]); err != nil {
return err
} else {
mp.ifType = uint8(ifType)
}
return nil
}