-
Notifications
You must be signed in to change notification settings - Fork 4.4k
/
ipaddr.go
59 lines (53 loc) · 1.38 KB
/
ipaddr.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
package ipaddr
import (
"fmt"
"net"
"reflect"
"strconv"
)
// FormatAddressPort Helper for net.JoinHostPort that takes int for port
func FormatAddressPort(address string, port int) string {
return net.JoinHostPort(address, strconv.Itoa(port))
}
// IsAny checks if the given ip address is an IPv4 or IPv6 ANY address. ip
// can be either a *net.IP or a string. It panics on another type.
func IsAny(ip interface{}) bool {
return IsAnyV4(ip) || IsAnyV6(ip)
}
// IsAnyV4 checks if the given ip address is an IPv4 ANY address. ip
// can be either a *net.IP or a string. It panics on another type.
func IsAnyV4(ip interface{}) bool {
return iptos(ip) == "0.0.0.0"
}
// IsAnyV6 checks if the given ip address is an IPv6 ANY address. ip
// can be either a *net.IP or a string. It panics on another type.
func IsAnyV6(ip interface{}) bool {
ips := iptos(ip)
return ips == "::" || ips == "[::]"
}
func iptos(ip interface{}) string {
if ip == nil || reflect.TypeOf(ip).Kind() == reflect.Ptr && reflect.ValueOf(ip).IsNil() {
return ""
}
switch x := ip.(type) {
case string:
return x
case *string:
if x == nil {
return ""
}
return *x
case net.IP:
return x.String()
case *net.IP:
return x.String()
case *net.IPAddr:
return x.IP.String()
case *net.TCPAddr:
return x.IP.String()
case *net.UDPAddr:
return x.IP.String()
default:
panic(fmt.Sprintf("invalid type: %T", ip))
}
}