github.com/jaypipes/ghw@v0.21.1/pkg/net/net_windows.go (about)

     1  // Use and distribution licensed under the Apache license version 2.
     2  //
     3  // See the COPYING file in the root project directory for full text.
     4  //
     5  
     6  package net
     7  
     8  import (
     9  	"strings"
    10  
    11  	"github.com/yusufpapurcu/wmi"
    12  )
    13  
    14  const wqlNetworkAdapter = "SELECT Description, DeviceID, Index, InterfaceIndex, MACAddress, Manufacturer, Name, NetConnectionID, ProductName, ServiceName, PhysicalAdapter FROM Win32_NetworkAdapter"
    15  
    16  type win32NetworkAdapter struct {
    17  	Description     *string
    18  	DeviceID        *string
    19  	Index           *uint32
    20  	InterfaceIndex  *uint32
    21  	MACAddress      *string
    22  	Manufacturer    *string
    23  	Name            *string
    24  	NetConnectionID *string
    25  	ProductName     *string
    26  	ServiceName     *string
    27  	PhysicalAdapter *bool
    28  }
    29  
    30  func (i *Info) load() error {
    31  	// Getting info from WMI
    32  	var win32NetDescriptions []win32NetworkAdapter
    33  	if err := wmi.Query(wqlNetworkAdapter, &win32NetDescriptions); err != nil {
    34  		return err
    35  	}
    36  
    37  	i.NICs = nics(win32NetDescriptions)
    38  	return nil
    39  }
    40  
    41  func nics(win32NetDescriptions []win32NetworkAdapter) []*NIC {
    42  	// Converting into standard structures
    43  	nics := make([]*NIC, 0)
    44  	for _, nicDescription := range win32NetDescriptions {
    45  		nic := &NIC{
    46  			Name:         netDeviceName(nicDescription),
    47  			MacAddress:   *nicDescription.MACAddress,
    48  			MACAddress:   *nicDescription.MACAddress,
    49  			IsVirtual:    netIsVirtual(nicDescription),
    50  			Capabilities: []*NICCapability{},
    51  		}
    52  		nics = append(nics, nic)
    53  	}
    54  
    55  	return nics
    56  }
    57  
    58  func netDeviceName(description win32NetworkAdapter) string {
    59  	var name string
    60  	if strings.TrimSpace(*description.NetConnectionID) != "" {
    61  		name = *description.NetConnectionID + " - " + *description.Description
    62  	} else {
    63  		name = *description.Description
    64  	}
    65  	return name
    66  }
    67  
    68  func netIsVirtual(description win32NetworkAdapter) bool {
    69  	if description.PhysicalAdapter == nil {
    70  		return false
    71  	}
    72  
    73  	return !(*description.PhysicalAdapter)
    74  }