Skip to content

Commit

Permalink
feat(cli): amtinfo command add amt ipaddr and osipaddr (#560)
Browse files Browse the repository at this point in the history
Add os ipaddress to amtinfo command.
amtinfo command now show amt ipaddress and os ipaddress.
os ipaddress select by mapping to mac address from amt.

Resolves: #467

Co-authored-by: Mike <[email protected]>
  • Loading branch information
tongsean9807 and rsdmike authored Jun 27, 2024
1 parent c89646a commit 77b42aa
Show file tree
Hide file tree
Showing 3 changed files with 173 additions and 2 deletions.
2 changes: 2 additions & 0 deletions internal/amt/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type InterfaceSettings struct {
DHCPEnabled bool `json:"dhcpEnabled"`
DHCPMode string `json:"dhcpMode"`
IPAddress string `json:"ipAddress"` //net.IP
OsIPAddress string `json:"osIpAddress"`
MACAddress string `json:"macAddress"`
}

Expand Down Expand Up @@ -351,6 +352,7 @@ func (amt AMTCommand) GetLANInterfaceSettings(useWireless bool) (InterfaceSettin

settings := InterfaceSettings{
IPAddress: "0.0.0.0",
OsIPAddress: "0.0.0.0",
IsEnabled: result.Enabled == 1,
DHCPEnabled: result.DhcpEnabled == 1,
LinkStatus: "down",
Expand Down
90 changes: 88 additions & 2 deletions internal/local/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@
package local

import (
"bytes"
"encoding/json"
"errors"
"fmt"
"net"
"os"
"rpc/internal/amt"
"rpc/internal/flags"
"rpc/pkg/utils"
"strconv"
"strings"
Expand All @@ -25,6 +29,63 @@ type PrivateKeyPairReference struct {
AssociatedCerts []string
}

func GetOSIPAddress(mac_addr string, netEnumerator flags.NetEnumerator) (string, error) {
mac_in_byte := make([]uint8, 6)

mac_str := strings.Split(mac_addr, ":")

for i, v := range mac_str {
value, _ := strconv.ParseUint(v, 16, 8)
mac_in_byte[i] = uint8(value)
}
interfaces, err := netEnumerator.Interfaces()
if err != nil {
return "0.0.0.0", errors.New("Failed to get net interfaces")
}

if bytes.Equal(mac_in_byte, make([]byte, 6)) {
return "0.0.0.0", nil
}

for _, iface := range interfaces {
if iface.Flags&net.FlagUp == 0 || iface.Flags&net.FlagLoopback != 0 {
continue // interface down || loopback interface
}

hwaddr := iface.HardwareAddr

if bytes.Equal(hwaddr, mac_in_byte) {
addrs, err := netEnumerator.InterfaceAddrs(&iface)
if err != nil {
return "0.0.0.0", errors.New("Failed to get interface addresses")
}

for _, addr := range addrs {
var ip net.IP

switch v := addr.(type) {
case *net.IPNet:
ip = v.IP
case *net.IPAddr:
ip = v.IP
}

// Check if the IP address is not nil and is an IPv4 address
if ip == nil || ip.IsLoopback() {
continue
}
ip = ip.To4()
if ip == nil {
continue // not an ipv4 address
}

return ip.String(), nil
}
}
}
return "Not Found", nil
}

func (service *ProvisioningService) DisplayAMTInfo() (err error) {
dataStruct := make(map[string]interface{})
cmd := service.amtCommand
Expand Down Expand Up @@ -158,28 +219,53 @@ func (service *ProvisioningService) DisplayAMTInfo() (err error) {
if err != nil {
log.Error(err)
}

netEnumerator := flags.NetEnumerator{
Interfaces: func() ([]net.Interface, error) {
return net.Interfaces()
},
InterfaceAddrs: func(iface *net.Interface) ([]net.Addr, error) {
return iface.Addrs()
},
}

wired_osIpAddress, err := GetOSIPAddress(wired.MACAddress, netEnumerator)
if err != nil {
log.Error(err)
}
wired.OsIPAddress = wired_osIpAddress

dataStruct["wiredAdapter"] = wired

if wired.MACAddress != "00:00:00:00:00:00" {
service.PrintOutput("---Wired Adapter---")
service.PrintOutput("DHCP Enabled : " + strconv.FormatBool(wired.DHCPEnabled))
service.PrintOutput("DHCP Mode : " + wired.DHCPMode)
service.PrintOutput("Link Status : " + wired.LinkStatus)
service.PrintOutput("IP Address : " + wired.IPAddress)
service.PrintOutput("AMT IP Address : " + wired.IPAddress)
service.PrintOutput("OS IP Address : " + wired.OsIPAddress)
service.PrintOutput("MAC Address : " + wired.MACAddress)
}

wireless, err := cmd.GetLANInterfaceSettings(true)
if err != nil {
log.Error(err)
}

wireless_osIpAddress, err := GetOSIPAddress(wireless.MACAddress, netEnumerator)
if err != nil {
log.Error(err)
}
wireless.OsIPAddress = wireless_osIpAddress

dataStruct["wirelessAdapter"] = wireless

service.PrintOutput("---Wireless Adapter---")
service.PrintOutput("DHCP Enabled : " + strconv.FormatBool(wireless.DHCPEnabled))
service.PrintOutput("DHCP Mode : " + wireless.DHCPMode)
service.PrintOutput("Link Status : " + wireless.LinkStatus)
service.PrintOutput("IP Address : " + wireless.IPAddress)
service.PrintOutput("AMT IP Address : " + wireless.IPAddress)
service.PrintOutput("OS IP Address : " + wireless.OsIPAddress)
service.PrintOutput("MAC Address : " + wireless.MACAddress)

}
Expand Down
83 changes: 83 additions & 0 deletions internal/local/info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package local

import (
"errors"
"net"
"rpc/internal/flags"
"rpc/pkg/utils"
"testing"
Expand Down Expand Up @@ -181,3 +182,85 @@ func TestDecodeAMT(t *testing.T) {
}
}
}

var testNetEnumerator1 = flags.NetEnumerator{
Interfaces: func() ([]net.Interface, error) {
return []net.Interface{
{
Index: 0, MTU: 1200, Name: "ethTest01",
HardwareAddr: []byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05},
Flags: 1,
},
}, nil
},
InterfaceAddrs: func(i *net.Interface) ([]net.Addr, error) {
if i.Name == "errTest01" {
return nil, errors.New("test message")
} else {
return []net.Addr{
&net.IPNet{
IP: net.ParseIP("127.0.0.1"),
Mask: net.CIDRMask(8, 32),
},
&net.IPNet{
IP: net.ParseIP("192.168.1.1"),
Mask: net.CIDRMask(24, 32),
},
}, nil
}
},
}

var testNetEnumerator2 = flags.NetEnumerator{
Interfaces: func() ([]net.Interface, error) {
return []net.Interface{
{
Index: 0, MTU: 1200, Name: "errTest01",
HardwareAddr: []byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05},
Flags: 1,
},
}, nil
},
InterfaceAddrs: func(i *net.Interface) ([]net.Addr, error) {
if i.Name == "errTest01" {
return nil, errors.New("test message")
} else {
return []net.Addr{
&net.IPNet{
IP: net.ParseIP("127.0.0.1"),
Mask: net.CIDRMask(8, 32),
},
&net.IPNet{
IP: net.ParseIP("192.168.1.1"),
Mask: net.CIDRMask(24, 32),
},
}, nil
}
},
}

func TestGetOSIPAddress(t *testing.T) {
t.Run("Valid MAC address", func(t *testing.T) {
osIpAddress, err := GetOSIPAddress("00:01:02:03:04:05", testNetEnumerator1)
assert.NoError(t, err)
assert.Equal(t, "192.168.1.1", osIpAddress)
})

t.Run("Zero MAC address", func(t *testing.T) {
osIpAddress, err := GetOSIPAddress("00:00:00:00:00:00", testNetEnumerator1)
assert.NoError(t, err)
assert.Equal(t, "0.0.0.0", osIpAddress)
})

t.Run("net interface fail", func(t *testing.T) {
osIpAddress, err := GetOSIPAddress("00:01:02:03:04:05", testNetEnumerator2)
assert.Equal(t, "0.0.0.0", osIpAddress)
assert.Equal(t, errors.New("Failed to get interface addresses"), err)
})

t.Run("no matching mac address to map into os ipaddress", func(t *testing.T) {
osIpAddress, err := GetOSIPAddress("00:11:22:33:44:55", testNetEnumerator1)
assert.Equal(t, "Not Found", osIpAddress)
assert.NoError(t, err)
})
}

0 comments on commit 77b42aa

Please sign in to comment.