Compare commits
2 commits
08ff7799ab
...
ca1f02ecce
Author | SHA1 | Date | |
---|---|---|---|
ca1f02ecce | |||
dc1ac494ae |
9 changed files with 222 additions and 3 deletions
2
base.go
2
base.go
|
@ -24,6 +24,8 @@ const (
|
|||
|
||||
const (
|
||||
SATAMessaging DeviceNodeSubType = 18
|
||||
MacMessagingSubType DeviceNodeSubType = 11
|
||||
IPv4MessagingSubType DeviceNodeSubType = 12
|
||||
)
|
||||
|
||||
const EndOfEntireDevicePath DeviceNodeSubType = 0xFF
|
||||
|
|
55
ipv4.go
Normal file
55
ipv4.go
Normal file
|
@ -0,0 +1,55 @@
|
|||
package devicepath
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const ipv4Prefix string = "IPv4"
|
||||
|
||||
type Ipv4DevicePath struct {
|
||||
remoteIP string
|
||||
protocol string
|
||||
static bool
|
||||
}
|
||||
|
||||
func (ip *Ipv4DevicePath) Type() DeviceNodeType {
|
||||
return MessagingDevicePath
|
||||
}
|
||||
|
||||
func (ip *Ipv4DevicePath) SubType() DeviceNodeSubType {
|
||||
return IPv4MessagingSubType
|
||||
}
|
||||
|
||||
func (ip *Ipv4DevicePath) Bytes() []byte {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ip *Ipv4DevicePath) String() string {
|
||||
ipType := 0
|
||||
if ip.static {
|
||||
ipType = 1
|
||||
}
|
||||
return fmt.Sprintf("%s(%s,%s,%d)", ipv4Prefix, ip.remoteIP, ip.protocol, ipType)
|
||||
}
|
||||
|
||||
func (ip *Ipv4DevicePath) ParseString(raw string) error {
|
||||
if !checkStringFormat(raw, ipv4Prefix, -1) {
|
||||
return ErrMalformedString
|
||||
}
|
||||
args := strings.Split(raw[len(ipv4Prefix)+1:len(raw)-1], ",")
|
||||
if len(args) != 3 {
|
||||
return errors.New("unexpected number of arguments")
|
||||
}
|
||||
|
||||
ip.remoteIP = args[0]
|
||||
ip.protocol = args[1]
|
||||
if ipType, err := strconv.Atoi(args[2]); err != nil {
|
||||
return err
|
||||
} else {
|
||||
ip.static = ipType == 1
|
||||
}
|
||||
return nil
|
||||
}
|
18
ipv4_test.go
Normal file
18
ipv4_test.go
Normal file
|
@ -0,0 +1,18 @@
|
|||
package devicepath_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"git.faercol.me/faercol/devicepath"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestParseIpv4String(t *testing.T) {
|
||||
t.Run("OK", func(t *testing.T) {
|
||||
expected := "IPv4(0.0.0.00.0.0.0,0,0)"
|
||||
var dev devicepath.Ipv4DevicePath
|
||||
require.NoError(t, dev.ParseString(expected))
|
||||
assert.Equal(t, expected, dev.String())
|
||||
})
|
||||
}
|
55
ipv6.go
Normal file
55
ipv6.go
Normal file
|
@ -0,0 +1,55 @@
|
|||
package devicepath
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const ipv6Prefix string = "IPv6"
|
||||
|
||||
type Ipv6DevicePath struct {
|
||||
remoteIP string
|
||||
protocol string
|
||||
static bool
|
||||
}
|
||||
|
||||
func (ip *Ipv6DevicePath) Type() DeviceNodeType {
|
||||
return MessagingDevicePath
|
||||
}
|
||||
|
||||
func (ip *Ipv6DevicePath) SubType() DeviceNodeSubType {
|
||||
return IPv4MessagingSubType
|
||||
}
|
||||
|
||||
func (ip *Ipv6DevicePath) Bytes() []byte {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ip *Ipv6DevicePath) String() string {
|
||||
ipType := 0
|
||||
if ip.static {
|
||||
ipType = 1
|
||||
}
|
||||
return fmt.Sprintf("%s(%s,%s,%d)", ipv6Prefix, ip.remoteIP, ip.protocol, ipType)
|
||||
}
|
||||
|
||||
func (ip *Ipv6DevicePath) ParseString(raw string) error {
|
||||
if !checkStringFormat(raw, ipv6Prefix, -1) {
|
||||
return ErrMalformedString
|
||||
}
|
||||
args := strings.Split(raw[len(ipv6Prefix)+1:len(raw)-1], ",")
|
||||
if len(args) != 3 {
|
||||
return errors.New("unexpected number of arguments")
|
||||
}
|
||||
|
||||
ip.remoteIP = args[0]
|
||||
ip.protocol = args[1]
|
||||
if ipType, err := strconv.Atoi(args[2]); err != nil {
|
||||
return err
|
||||
} else {
|
||||
ip.static = ipType == 1
|
||||
}
|
||||
return nil
|
||||
}
|
18
ipv6_test.go
Normal file
18
ipv6_test.go
Normal file
|
@ -0,0 +1,18 @@
|
|||
package devicepath_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"git.faercol.me/faercol/devicepath"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestParseIpv6String(t *testing.T) {
|
||||
t.Run("OK", func(t *testing.T) {
|
||||
expected := "IPv6([::]:\u003c-\u003e[::]:,0,0)"
|
||||
var dev devicepath.Ipv6DevicePath
|
||||
require.NoError(t, dev.ParseString(expected))
|
||||
assert.Equal(t, expected, dev.String())
|
||||
})
|
||||
}
|
53
mac.go
Normal file
53
mac.go
Normal file
|
@ -0,0 +1,53 @@
|
|||
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
|
||||
}
|
18
mac_test.go
Normal file
18
mac_test.go
Normal file
|
@ -0,0 +1,18 @@
|
|||
package devicepath_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"git.faercol.me/faercol/devicepath"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestParseStringMac(t *testing.T) {
|
||||
t.Run("OK", func(t *testing.T) {
|
||||
var macDev devicepath.MacDevicePath
|
||||
expected := "MAC(3c7c3fc1f935,0)"
|
||||
require.NoError(t, macDev.ParseString(expected))
|
||||
assert.Equal(t, expected, macDev.String())
|
||||
})
|
||||
}
|
|
@ -36,6 +36,8 @@ var prefixTypeAssociation = map[string]nodeGenerator{
|
|||
pciRootPrefix: func() DevicePathNode { return &PCIRootDevicePath{} },
|
||||
hdPrefix: func() DevicePathNode { return &HardDriveDevicePath{} },
|
||||
filePrefix: func() DevicePathNode { return &FileDevicePath{} },
|
||||
macPrefix: func() DevicePathNode { return &MacDevicePath{} },
|
||||
ipv4Prefix: func() DevicePathNode { return &Ipv4DevicePath{} },
|
||||
}
|
||||
|
||||
func Parsenode(raw string) (DevicePathNode, error) {
|
||||
|
|
2
pci.go
2
pci.go
|
@ -52,13 +52,11 @@ func (pcp *PCIDevicePath) ParseString(raw string) error {
|
|||
if function, err := strconv.ParseUint(args[0], 0, 8); err != nil {
|
||||
return err
|
||||
} else {
|
||||
fmt.Println("function", function)
|
||||
pcp.function = uint8(function)
|
||||
}
|
||||
if device, err := strconv.ParseUint(args[1], 0, 8); err != nil {
|
||||
return err
|
||||
} else {
|
||||
fmt.Println("device", device)
|
||||
pcp.device = uint8(device)
|
||||
}
|
||||
return nil
|
||||
|
|
Loading…
Reference in a new issue