-
Notifications
You must be signed in to change notification settings - Fork 4
/
ippool.go
93 lines (78 loc) · 2.01 KB
/
ippool.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
package vpnlib
import (
"errors"
"fmt"
"net"
"sync"
)
const (
subnetMaxSize = 30
subnetMinSize = 16
)
// This is a pool of IPs that can be allocated to clients.
type IPPool struct {
sync.Mutex
ips map[string]bool
serverIp string
subnetSize uint8
}
// NewIPPool creates a new IP pool based on the provided CIDR.
// This pool will be used to allocate IPs to clients. (think DHCP)
func NewIPPool(cidr string) (*IPPool, error) {
// First check the size of the subnet provided
_, ipnet, err := net.ParseCIDR(cidr)
if err != nil {
return nil, fmt.Errorf("invalid cidr range: %v", err)
}
netsize, _ := ipnet.Mask.Size()
if netsize > subnetMaxSize {
return nil, fmt.Errorf("invalid cidr range, must have at least 2 usable addresses (i.e. /30 or less), got %d", netsize)
}
if netsize < subnetMinSize {
return nil, fmt.Errorf("invalid cidr range, --vpn-subnet size to large. Cannot be more than a /%d, got %s", subnetMinSize, cidr)
}
usableIps, subnetSize, err := cidrToUsableIPs(cidr)
if err != nil {
return nil, fmt.Errorf("failed to parse cidr: %v", err)
}
serverIp := usableIps[0]
ips := make(map[string]bool)
for _, ip := range usableIps[1:] {
ips[ip] = true
}
return &IPPool{
ips: ips,
serverIp: serverIp,
subnetSize: subnetSize,
}, nil
}
// GetServerIp returns the server ip
func (p *IPPool) GetServerIp() string {
return p.serverIp
}
// GetSubnetSize returns the subnet size
func (p *IPPool) GetSubnetSize() uint8 {
return p.subnetSize
}
// Allocate allocates an available IP address from the pool.
// This will find a free IP address and mark it as used.
func (p *IPPool) Allocate() (string, error) {
p.Lock()
defer p.Unlock()
for ip, available := range p.ips {
if available {
p.ips[ip] = false
return ip, nil
}
}
return "", errors.New("IP Pool exhausted")
}
// Release releases an IP address back to the pool.
// This will mark the IP address as available.
func (p *IPPool) Release(ip string) {
p.Lock()
defer p.Unlock()
if _, ok := p.ips[ip]; ok {
p.ips[ip] = true
}
}