-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathallocator.go
144 lines (118 loc) · 2.65 KB
/
allocator.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package main
import (
"fmt"
"net"
"sync"
"time"
)
type Client struct {
Name string
MAC net.HardwareAddr
IP net.IP
Expires int64
}
type Allocator struct {
AvailableIPs []net.IP
Allocated []Client
mutex sync.Mutex
}
func (a *Allocator) Clock() {
ticker := time.NewTicker(time.Second)
for {
ct := <-ticker.C
for i, c := range a.Allocated {
if c.Expires < ct.Unix() {
a.mutex.Lock()
a.AvailableIPs = append(a.AvailableIPs, c.IP)
a.Allocated = append(a.Allocated[:i], a.Allocated[i+1:]...)
a.mutex.Unlock()
}
}
}
}
func (a *Allocator) GetAvailableIP(mac net.HardwareAddr) (net.IP, error) {
a.mutex.Lock()
defer a.mutex.Unlock()
for _, c := range a.Allocated {
if c.MAC.String() == mac.String() {
return c.IP, nil
}
}
if len(a.AvailableIPs) == 0 {
return net.IPv4zero, fmt.Errorf("no more available IPs")
}
ip := a.AvailableIPs[0]
a.AvailableIPs = a.AvailableIPs[1:]
a.Allocated = append(a.Allocated, Client{
MAC: mac,
IP: ip,
Expires: time.Now().Add(time.Second * 5).Unix(),
})
return ip, nil
}
func (a *Allocator) Release(mac net.HardwareAddr) {
a.mutex.Lock()
defer a.mutex.Unlock()
for i, c := range a.Allocated {
if c.MAC.String() == mac.String() {
a.Allocated = append(a.Allocated[:i], a.Allocated[i+1:]...)
a.AvailableIPs = append(a.AvailableIPs, c.IP)
return
}
}
}
func (a *Allocator) Renew(mac net.HardwareAddr, Expires int64) {
a.mutex.Lock()
defer a.mutex.Unlock()
for i, c := range a.Allocated {
if c.MAC.String() == mac.String() {
a.Allocated[i].Expires = Expires
return
}
}
}
func (a *Allocator) RARP(mac net.HardwareAddr) (net.IP, error) {
a.mutex.Lock()
defer a.mutex.Unlock()
for _, c := range a.Allocated {
if c.MAC.String() == mac.String() {
return c.IP, nil
}
}
return net.IPv4zero, fmt.Errorf("no IP found for MAC")
}
func (a *Allocator) Allocate(name string, mac net.HardwareAddr, ip net.IP, Expires int64) error {
a.mutex.Lock()
defer a.mutex.Unlock()
if ip.IsUnspecified() {
return fmt.Errorf("IP is unspecified")
}
for _, c := range a.Allocated {
if c.IP.Equal(ip) && c.MAC.String() != mac.String() {
return fmt.Errorf("IP already allocated")
}
}
for i, c := range a.Allocated {
if c.MAC.String() == mac.String() {
a.Allocated[i].Name = name
a.Allocated[i].IP = ip
a.Allocated[i].Expires = Expires
return nil
}
}
client := Client{
Name: name,
MAC: mac,
IP: ip,
Expires: Expires,
}
a.Allocated = append(a.Allocated, client)
return nil
}
func NewAllocator(ips []net.IP) *Allocator {
alloc := Allocator{
AvailableIPs: ips,
Allocated: []Client{},
}
return &alloc
}