-
Notifications
You must be signed in to change notification settings - Fork 127
/
dns_ip_pool.go
81 lines (67 loc) · 1.53 KB
/
dns_ip_pool.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
//
// date : 2016-09-24
// author: xjdrew
//
package kone
import (
"hash/adler32"
"net"
"github.com/xjdrew/kone/tcpip"
)
const DnsIPPoolMaxSpace = 0x3ffff // 4*65535
type DnsIPPool struct {
base uint32
space uint32
flags []bool
}
func (pool *DnsIPPool) Capacity() int {
return int(pool.space)
}
func (pool *DnsIPPool) Contains(ip net.IP) bool {
index := tcpip.ConvertIPv4ToUint32(ip) - pool.base
return index < pool.space
}
func (pool *DnsIPPool) Release(ip net.IP) {
index := tcpip.ConvertIPv4ToUint32(ip) - pool.base
if index < pool.space {
pool.flags[index] = false
}
}
// use tips as a hint to find a stable index
func (pool *DnsIPPool) Alloc(tips string) net.IP {
index := adler32.Checksum([]byte(tips)) % pool.space
if pool.flags[index] {
logger.Debugf("[dns] %s is not in main index: %d", tips, index)
for i, used := range pool.flags {
if !used {
index = uint32(i)
break
}
}
}
if pool.flags[index] {
return nil
}
pool.flags[index] = true
return tcpip.ConvertUint32ToIPv4(pool.base + index)
}
func NewDnsIPPool(ip net.IP, subnet *net.IPNet) *DnsIPPool {
base := tcpip.ConvertIPv4ToUint32(subnet.IP) + 1
max := base + ^tcpip.ConvertIPv4ToUint32(net.IP(subnet.Mask))
// space should not over 0x3ffff
space := max - base
if space > DnsIPPoolMaxSpace {
space = DnsIPPoolMaxSpace
}
flags := make([]bool, space)
// ip is used by tun
index := tcpip.ConvertIPv4ToUint32(ip) - base
if index < space {
flags[index] = true
}
return &DnsIPPool{
base: base,
space: space,
flags: flags,
}
}