forked from perlin-network/noise
-
Notifications
You must be signed in to change notification settings - Fork 0
/
addr.go
38 lines (29 loc) · 762 Bytes
/
addr.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
package noise
import (
"net"
)
// ResolveAddress resolves an address using net.ResolveTCPAddress("tcp", (*net.Conn).RemoteAddr()) and nullifies the
// IP if the IP is unspecified or is a loopback address. It then returns the string representation of the address, or
// an error if the resolution of the address fails.
func ResolveAddress(address string) (string, error) {
addr, err := net.ResolveTCPAddr("tcp", address)
if err != nil {
return "", err
}
addr.IP = resolveIP(addr.IP)
return addr.String(), nil
}
func resolveIP(ip net.IP) net.IP {
if ip.IsLoopback() || ip.IsUnspecified() {
return nil
}
return ip
}
func normalizeIP(ip net.IP) string {
ip = resolveIP(ip)
str := ip.String()
if str == "<nil>" {
return ""
}
return str
}