github.com/haraldrudell/parl@v0.4.176/pnet/hardware-addr.go (about)

     1  /*
     2  © 2020–present Harald Rudell <harald.rudell@gmail.com> (https://haraldrudell.github.io/haraldrudell/)
     3  ISC License
     4  */
     5  
     6  package pnet
     7  
     8  import (
     9  	"net"
    10  
    11  	"github.com/haraldrudell/parl/perrors"
    12  	"golang.org/x/exp/slices"
    13  )
    14  
    15  func DumpHardwareAddr(a net.HardwareAddr) (s string) {
    16  	if len(a) == 0 {
    17  		return ":"
    18  	}
    19  	return a.String()
    20  }
    21  
    22  func HardwareAddrInterface(a net.HardwareAddr) (netInterface *net.Interface, isErrNoSuchInterface bool, err error) {
    23  	if len(a) == 0 {
    24  		err = perrors.NewPF("HardwareAddr cannot be empty")
    25  		return
    26  	}
    27  	var interfaces []net.Interface
    28  	if interfaces, err = net.Interfaces(); err != nil {
    29  		return
    30  	}
    31  	for i := 0; i < len(interfaces); i++ {
    32  		var iface = &interfaces[i]
    33  		if slices.Equal(a, iface.HardwareAddr) {
    34  			netInterface = iface
    35  			return
    36  		}
    37  	}
    38  	isErrNoSuchInterface = true
    39  	err = perrors.ErrorfPF("No interface has mac: %s %w", a, ErrNoSuchInterface)
    40  	return
    41  }