ppforge/devices/devices.go

59 lines
1.3 KiB
Go

package devices
import (
"net"
)
// [impl->dsn~low-level-abstraction~0>>utest]
// [impl->dsn~list-devices~0>>utest]
// Transmitter interface for sending packets
type Transmitter interface {
Send(data []byte) error
Close() error
}
// Receiver interface for receiving packets
type Receiver interface {
Receive(f func(data []byte))
Close() error
}
// List the existing network interfaces by name
func List() ([]string, error) {
// Network interfaces
// [impllnx->dsn~list-devices~0]
// [implwin->dsn~list-devices~0]
ifaces, err := net.Interfaces()
if err != nil {
return nil, err
}
names := make([]string, 0, 10)
if ifaces != nil {
for _, e := range ifaces {
names = append(names, e.Name)
}
}
//TODO: other interface types which may accept binary packets through a socket like interface?
return names, nil
}
// GetHardwareAddress returns the hardware address as string for a given network interface
func GetHardwareAddress(iface string) (string, error) {
i, err := net.InterfaceByName(iface)
if err != nil {
return "", err
}
return i.HardwareAddr.String(), nil
}
// GetAddresses returns the address list from the interface
func GetAddresses(iface string) ([]net.Addr, error) {
i, err := net.InterfaceByName(iface)
if err != nil {
return nil, err
}
// TODO: Just return a string slice?
return i.Addrs()
}