forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
net.go
50 lines (46 loc) · 1.21 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
package common
import (
"fmt"
"net"
)
// LocalIpAddrs finds the IP addresses of the hosts on which
// the shipper currently runs on.
func LocalIpAddrs() ([]net.IP, error) {
var localAddrs = []net.IP{}
addrs, err := net.InterfaceAddrs()
if err != nil {
return []net.IP{}, err
}
for _, addr := range addrs {
if ipnet, ok := addr.(*net.IPNet); ok {
localAddrs = append(localAddrs, ipnet.IP)
}
}
return localAddrs, nil
}
// LocalIpAddrs finds the IP addresses of the hosts on which
// the shipper currently runs on and returns them as an array of
// strings.
func LocalIpAddrsAsStrings(include_loopbacks bool) ([]string, error) {
var localAddrsStrings = []string{}
var err error
ipaddrs, err := LocalIpAddrs()
if err != nil {
return []string{}, err
}
for _, ipaddr := range ipaddrs {
if include_loopbacks || !ipaddr.IsLoopback() {
localAddrsStrings = append(localAddrsStrings, ipaddr.String())
}
}
return localAddrsStrings, err
}
// IsLoopback check if a particular IP notation corresponds
// to a loopback interface.
func IsLoopback(ip_str string) (bool, error) {
ip := net.ParseIP(ip_str)
if ip == nil {
return false, fmt.Errorf("Wrong IP format %s", ip_str)
}
return ip.IsLoopback(), nil
}