-
Notifications
You must be signed in to change notification settings - Fork 56
/
ipaddr.go
51 lines (43 loc) · 1.12 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
package goVirtualHost
import (
"errors"
"net"
)
var unknownIPVersion = errors.New("unknown IP version")
func isPrivateIPv4(netIPv4 net.IP) bool {
return netIPv4[0] == 10 ||
(netIPv4[0] == 172 && netIPv4[1]&0xf0 == 16) ||
(netIPv4[0] == 192 && netIPv4[1] == 168)
}
func isPrivateIPv6(netIPv6 net.IP) bool {
return netIPv6[0]&0xfe == 0xfc
}
func newIPAddr(netIP net.IP) (*ipAddr, error) {
var version int
var isNonPrivate bool
if netIPv4 := netIP.To4(); netIPv4 != nil {
version = ip4ver
isNonPrivate = !isPrivateIPv4(netIPv4)
} else if netIPv6 := netIP.To16(); netIPv6 != nil {
version = ip6ver
isNonPrivate = !isPrivateIPv6(netIPv6)
} else {
return nil, unknownIPVersion
}
instance := &ipAddr{
netIP: netIP,
version: version,
isGlobalUnicast: netIP.IsGlobalUnicast(),
isLinkLocalUnicast: netIP.IsLinkLocalUnicast(),
isNonPrivate: isNonPrivate,
isNonLoopback: !netIP.IsLoopback(),
}
return instance, nil
}
func (addr *ipAddr) String() string {
if addr.version == ip6ver {
return "[" + addr.netIP.String() + "]"
} else {
return addr.netIP.String()
}
}