-
Notifications
You must be signed in to change notification settings - Fork 1
/
ip_fix.go
99 lines (88 loc) · 1.71 KB
/
ip_fix.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
//
// @project IPRangeTree 2016 - 2019
// @author Dmitry Ponomarev <demdxx@gmail.com> 2016 - 2019
//
package iprangetree
import (
"net"
"github.com/google/btree"
)
// Fixed size IP data type
type ipFix [net.IPv6len + 1]byte
func ip2fix(ip net.IP) (nIP ipFix) {
var nIPCursor = net.IPv6len - 1
var isIPv4 = true
for i := len(ip) - 1; i >= 0; i-- {
nIP[nIPCursor] = ip[i]
if isIPv4 {
switch {
case nIPCursor == 11 || nIPCursor == 10:
if nIP[nIPCursor] != 0xff {
isIPv4 = false
}
case nIPCursor < 10:
if nIP[nIPCursor] != 0 {
isIPv4 = false
}
}
}
nIPCursor--
}
if isIPv4 {
nIP[net.IPv6len] = 1
} else {
nIP[net.IPv6len] = 2
}
return
}
func (ip ipFix) IsIPv4() bool {
return ip[net.IPv6len] == 1
}
func (ip ipFix) IsEmpty() bool {
return ip[net.IPv6len] == 0
}
func (ip *ipFix) Compare(ip2 *ipFix) (result int) {
i := 0
if v1, v2 := ip.IsIPv4(), ip2.IsIPv4(); !v1 && v2 {
return 1
} else if v1 && !v2 {
return -1
} else if v1 {
i = 12
}
for ; i < net.IPv6len; i++ {
if ip[i] < ip2[i] {
return -1
} else if ip[i] > ip2[i] {
return 1
}
}
return 0
}
func (ip ipFix) IP() net.IP {
if ip.IsIPv4() {
return net.IP(ip[12:net.IPv6len])
}
return net.IP(ip[:net.IPv6len])
}
// IPv4 simple format
func (ip ipFix) IPv4() ipV4 {
if ip.IsIPv4() {
return toIPv4(ip[12:net.IPv6len])
}
return undefinedIPv4
}
// Less camparing for btree
func (ip *ipFix) Less(then btree.Item) bool {
switch v := then.(type) {
case *IPItemFix:
return ip.Compare(&v.StartIP) < 0
case *IPItemV4:
return ip.IsIPv4() && ip.IPv4().Compare(v.StartIP) < 0
case ipV4:
return ip.IsIPv4() && ip.IPv4().Compare(v) < 0
case *ipFix:
return ip.Compare(v) < 0
}
return false
}