forked from galthaus/ocb-dhcp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.go
215 lines (181 loc) · 5.05 KB
/
data.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package main
import (
"errors"
"log"
"net"
"net/http"
"sync"
dhcp "github.com/krolaw/dhcp4"
)
type DataTracker struct {
sync.Mutex `json:"-"`
store LoadSaver `json:"-"`
Subnets map[string]*Subnet // subnet -> SubnetData
}
func NewDataTracker(store LoadSaver) *DataTracker {
return &DataTracker{
Subnets: make(map[string]*Subnet),
store: store,
}
}
func (dt *DataTracker) FindBoundIP(mac net.HardwareAddr) *Subnet {
for _, s := range dt.Subnets {
for _, b := range s.Bindings {
if b.Mac == mac.String() {
return s
}
}
}
return nil
}
func (dt *DataTracker) FindSubnet(ip net.IP) *Subnet {
for _, s := range dt.Subnets {
if s.Subnet.Contains(ip) {
return s
}
}
return nil
}
func (dt *DataTracker) AddSubnet(s *Subnet) (error, int) {
lsubnet := dt.Subnets[s.Name]
if lsubnet != nil {
return errors.New("Already exists"), http.StatusConflict
}
// Make sure subnet doesn't overlap into other spaces.
if dt.subnetsOverlap(s) {
return errors.New("Subnet overlaps with existing subnet"), http.StatusBadRequest
}
dt.Subnets[s.Name] = s
dt.save_data()
return nil, http.StatusOK
}
func (dt *DataTracker) RemoveSubnet(subnetName string) (error, int) {
lsubnet := dt.Subnets[subnetName]
if lsubnet == nil {
return errors.New("Not Found"), http.StatusNotFound
}
delete(dt.Subnets, subnetName)
dt.save_data()
return nil, http.StatusOK
}
func (dt *DataTracker) ReplaceSubnet(subnetName string, subnet *Subnet) (error, int) {
lsubnet := dt.Subnets[subnetName]
if lsubnet == nil {
return errors.New("Not Found"), http.StatusNotFound
}
// Take Leases and Bindings from old to new if nets match
subnet.Leases = lsubnet.Leases
subnet.Bindings = lsubnet.Bindings
// XXX: One day we should handle if active/reserved change.
subnet.ActiveBits = lsubnet.ActiveBits
delete(dt.Subnets, lsubnet.Name)
// Make sure subnet doesn't overlap into other spaces.
if dt.subnetsOverlap(subnet) {
// Put the original back
dt.Subnets[lsubnet.Name] = lsubnet
return errors.New("Subnet overlaps with existing subnet"), http.StatusBadRequest
}
dt.Subnets[subnet.Name] = subnet
dt.save_data()
return nil, http.StatusOK
}
// HACK BECAUSE IPNet doesn't marshall/unmarshall
type MyIPNet struct {
*net.IPNet
}
func (ipnet MyIPNet) MarshalText() ([]byte, error) {
return []byte(ipnet.String()), nil
}
// UnmarshalText implements the encoding.TextUnmarshaler interfacee.
// The IP address is expected in a form accepted by ParseIP.
func (ipnet *MyIPNet) UnmarshalText(text []byte) error {
if len(text) == 0 {
return errors.New("Empty MyIPNet")
}
s := string(text)
_, newnet, err := net.ParseCIDR(s)
if err != nil {
return &net.ParseError{"NetIP address", s}
}
*ipnet = MyIPNet{
&net.IPNet{IP: newnet.IP, Mask: newnet.Mask},
}
return nil
}
/*
* Data storage/retrieval functions
*/
func (dt *DataTracker) load_data() {
if err := dt.store.Load(dt); err != nil {
log.Panicf("Unable to load data from backing store: %s", err)
}
}
func (dt *DataTracker) save_data() {
if err := dt.store.Save(dt); err != nil {
log.Panicf("Unable to save data to backing store: %s", err)
}
}
func (dt *DataTracker) subnetsOverlap(subnet *Subnet) bool {
for _, es := range dt.Subnets {
if es.Subnet.Contains(subnet.Subnet.IP) {
return true
}
if subnet.Subnet.Contains(es.Subnet.IP) {
return true
}
}
return false
}
func (dt *DataTracker) AddBinding(subnetName string, binding Binding) (error, int) {
lsubnet := dt.Subnets[subnetName]
if lsubnet == nil {
return errors.New("Not Found"), http.StatusNotFound
}
// If existing, clear the reservation for IP
b := lsubnet.Bindings[binding.Mac]
if b != nil {
if dhcp.IPInRange(lsubnet.ActiveStart, lsubnet.ActiveEnd, b.Ip) {
lsubnet.ActiveBits.Clear(uint(dhcp.IPRange(lsubnet.ActiveStart, b.Ip) - 1))
}
}
// Reserve the IP if in Active range
if dhcp.IPInRange(lsubnet.ActiveStart, lsubnet.ActiveEnd, binding.Ip) {
lsubnet.ActiveBits.Set(uint(dhcp.IPRange(lsubnet.ActiveStart, binding.Ip) - 1))
}
lsubnet.Bindings[binding.Mac] = &binding
dt.save_data()
return nil, http.StatusOK
}
func (dt *DataTracker) DeleteBinding(subnetName, mac string) (error, int) {
lsubnet := dt.Subnets[subnetName]
if lsubnet == nil {
return errors.New("Subnet Not Found"), http.StatusNotFound
}
b := lsubnet.Bindings[mac]
if b == nil {
return errors.New("Binding Not Found"), http.StatusNotFound
}
if dhcp.IPInRange(lsubnet.ActiveStart, lsubnet.ActiveEnd, b.Ip) {
lsubnet.ActiveBits.Clear(uint(dhcp.IPRange(lsubnet.ActiveStart, b.Ip) - 1))
}
delete(lsubnet.Bindings, mac)
dt.save_data()
return nil, http.StatusOK
}
func (dt *DataTracker) SetNextServer(subnetName string, ip net.IP, nextServer NextServer) (error, int) {
lsubnet := dt.Subnets[subnetName]
if lsubnet == nil {
return errors.New("Not Found"), http.StatusNotFound
}
save_me := false
for _, v := range lsubnet.Bindings {
if v.Ip.Equal(ip) && (v.NextServer == nil || *v.NextServer != nextServer.Server) {
save_me = true
v.NextServer = &nextServer.Server
}
}
if save_me {
dt.save_data()
}
return nil, http.StatusOK
}