forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
net.go
33 lines (29 loc) · 740 Bytes
/
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
package main
import (
"net"
)
func LocalAddrs() ([]string, error) {
var localAddrs = []string{}
addrs, err := net.InterfaceAddrs()
if err != nil {
return nil, err
}
for _, addr := range addrs {
// a bit wtf'ish.. Don't know how to do this otherwise
ip, _, err := net.ParseCIDR(addr.String())
if ip.IsLoopback() {
continue
}
if err == nil && ip != nil {
localAddrs = append(localAddrs, ip.String())
}
}
return localAddrs, nil
}
func IsLoopback(ip_str string) (bool, error) {
ip := net.ParseIP(ip_str)
if ip == nil {
return false, MsgError("Wrong IP format %s", ip_str)
}
return ip.IsLoopback(), nil
}