/
net.go
70 lines (65 loc) · 1.9 KB
/
net.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package utils
import (
"fmt"
"net"
)
// GetConfiguredOrSuitableInterfaceName returns the configured interface name, if not empty, or tries retrieving first suitable interface name.
func GetConfiguredOrSuitableInterfaceName(configured string) (string, error) {
if configured != "" {
return configured, nil
}
iface, err := GetFirstUpBroadcastInterface()
if err != nil {
return "", err
}
return iface.Name, nil
}
// GetFirstUpBroadcastInterface retrieves the first suitable up broadact capable interface.
func GetFirstUpBroadcastInterface() (net.Interface, error) {
interfaces, err := GetUpBroadcastInterfaces()
if err != nil {
return net.Interface{}, err
}
if len(interfaces) == 0 {
return net.Interface{}, fmt.Errorf("no suitable interfaces")
}
return interfaces[0], nil
}
// GetInterfaceV4Addr fetches an IPv4 address of an interface.
func GetInterfaceV4Addr(interfaceName string) (addr string, err error) {
var (
iface *net.Interface
addrs []net.Addr
ipv4Addr net.IP
)
if iface, err = net.InterfaceByName(interfaceName); err != nil { // get interface
return
}
if addrs, err = iface.Addrs(); err != nil { // get addresses
return
}
for _, addr := range addrs { // get ipv4 address
if ipv4Addr = addr.(*net.IPNet).IP.To4(); ipv4Addr != nil {
break
}
}
if ipv4Addr == nil {
return "", fmt.Errorf("interface %s don't have an ipv4 address", interfaceName)
}
return ipv4Addr.String(), nil
}
// GetUpBroadcastInterfaces retrieves the list of up broadcast interfaces.
// These are internally sorted by an index so the output is always deterministic.
func GetUpBroadcastInterfaces() ([]net.Interface, error) {
interfaces, err := net.Interfaces()
if err != nil {
return interfaces, err
}
result := []net.Interface{}
for _, iface := range interfaces {
if iface.Flags&net.FlagBroadcast != 0 && iface.Flags&net.FlagUp != 0 {
result = append(result, iface)
}
}
return result, nil
}