This repository has been archived by the owner on Oct 20, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qDHCPv4.go
650 lines (579 loc) · 20.6 KB
/
qDHCPv4.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
package main
import(
"fmt"
//"strings"
//"bytes"
"log"
"net"
//"math/rand"
"time"
dhcp "github.com/krolaw/dhcp4"
)
/*
type lease struct {
nic string // Client's CHAddr
expiry time.Time // When the lease expires
}
type DHCPHandler struct {
ip net.IP // Server IP to use
options dhcp.Options // Options to send to DHCP Clients
start net.IP // Start of IP range to distribute
leaseRange int // Number of IPs to distribute (starting from start)
leaseDuration time.Duration // Lease period
leases map[int]lease // Map to keep track of leases
}
func (h *DHCPHandler) ServeDHCP(p dhcp.Packet, msgType dhcp.MessageType, options dhcp.Options) (d dhcp.Packet) {
switch msgType {
case dhcp.Discover:
free, nic := -1, p.CHAddr().String()
for i, v := range h.leases { // Find previous lease
if v.nic == nic {
free = i
goto reply
}
}
if free = h.freeLease(); free == -1 {
return
}
reply:
return dhcp.ReplyPacket(p, dhcp.Offer, h.ip, dhcp.IPAdd(h.start, free), h.leaseDuration,
h.options.SelectOrderOrAll(options[dhcp.OptionParameterRequestList]))
case dhcp.Request:
if server, ok := options[dhcp.OptionServerIdentifier]; ok && !net.IP(server).Equal(h.ip) {
return nil // Message not for this dhcp server
}
if reqIP := net.IP(options[dhcp.OptionRequestedIPAddress]); len(reqIP) == 4 {
if leaseNum := dhcp.IPRange(h.start, reqIP) - 1; leaseNum >= 0 && leaseNum < h.leaseRange {
if l, exists := h.leases[leaseNum]; !exists || l.nic == p.CHAddr().String() {
h.leases[leaseNum] = lease{nic: p.CHAddr().String(), expiry: time.Now().Add(h.leaseDuration)}
return dhcp.ReplyPacket(p, dhcp.ACK, h.ip, net.IP(options[dhcp.OptionRequestedIPAddress]), h.leaseDuration,
h.options.SelectOrderOrAll(options[dhcp.OptionParameterRequestList]))
}
}
}
return dhcp.ReplyPacket(p, dhcp.NAK, h.ip, nil, 0, nil)
case dhcp.Release, dhcp.Decline:
nic := p.CHAddr().String()
for i, v := range h.leases {
if v.nic == nic {
delete(h.leases, i)
break
}
}
}
return nil
}
func (h *DHCPHandler) freeLease() int {
now := time.Now()
b := rand.Intn(h.leaseRange) // Try random first
for _, v := range [][]int{[]int{b, h.leaseRange}, []int{0, b}} {
for i := v[0]; i < v[1]; i++ {
if l, ok := h.leases[i]; !ok || l.expiry.Before(now) {
return i
}
}
}
return -1
}
// Example using DHCP with a single network interface device
func ExampleHandler() {
serverIP := net.IP{172, 30, 0, 1}
handler := &DHCPHandler{
ip: serverIP,
leaseDuration: 2 * time.Hour,
start: net.IP{172, 30, 0, 2},
leaseRange: 50,
leases: make(map[int]lease, 10),
options: dhcp.Options{
dhcp.OptionSubnetMask: []byte{255, 255, 240, 0},
dhcp.OptionRouter: []byte(serverIP), // Presuming Server is also your router
dhcp.OptionDomainNameServer: []byte(serverIP), // Presuming Server is also your DNS server
},
}
//log.Fatal(dhcp.ListenAndServe(handler))
// log.Fatal(dhcp.ListenAndServeIf("eth0",handler)) // Select interface on multi interface device
}
*/
// http://support.microsoft.com/kb/169289/ru
// https://ru.wikipedia.org/wiki/DHCP#.D0.9F.D1.80.D0.B8.D0.BC.D0.B5.D1.80_.D0.BF.D1.80.D0.BE.D1.86.D0.B5.D1.81.D1.81.D0.B0_.D0.BF.D0.BE.D0.BB.D1.83.D1.87.D0.B5.D0.BD.D0.B8.D1.8F_.D0.B0.D0.B4.D1.80.D0.B5.D1.81.D0.B0
// Host Name:
//android-a70378b9bf61c919
//android-99d3a92f5cb0d61e
//iPhone-Elena
//iPad-Andrey
//Windows-Phone
// Tests:
// http://blog.thecybershadow.net/2013/01/10/dhcp-test-client/
// http://www.opennet.ru/prog/info/3604.shtml
// http://www.ingmarverheij.com/microsoft-vendor-specific-dhcp-options-explained-and-demystified/
func main() {
fmt.Printf("Hello world!");
dhcp.ListenAndServe(func() dhcp.Handler {
//all IP - array + linked queue for last free
//MAC - map
type MAC48 [6]byte;
const (
IP_Range = 1024;
)
var (
START_IP = net.IP{194,188,32,1};
SERVER_IP = net.IP{194,188,64,28};
)
type Stage byte;
const (
IP_Free Stage = iota;
IP_Reserved;
IP_NotFree;
)
type queueIP struct{
ip net.IP;
mac MAC48;
//expiry time.Time;
stage Stage; //Only for light freeIPTest()
nextFreeIP *queueIP;
beforeFreeIP *queueIP;
};
var IP[IP_Range] queueIP;
var lastFreeIP *queueIP;
var endFreeIP *queueIP;
var endReserveIP *queueIP;
{//Init IP
IP[len(IP)-1].nextFreeIP = nil;
IP[len(IP)-1].beforeFreeIP = &IP[len(IP)-2];
IP[len(IP)-1].stage = IP_Free;
IP[len(IP)-1].ip = dhcp.IPAdd(START_IP, len(IP)-1);
for i := len(IP) - 2; i > 0; i-- {
IP[i].nextFreeIP = &IP[i+1];
IP[i].beforeFreeIP = &IP[i-1];
IP[i].stage = IP_Free;
IP[i].ip = dhcp.IPAdd(START_IP, i);
}
IP[0].nextFreeIP = &IP[1];
IP[0].beforeFreeIP = nil;
IP[0].stage = IP_Free;
IP[0].ip = START_IP;
lastFreeIP = &IP[0];
endFreeIP = &IP[len(IP)-1];
endReserveIP = nil;
}//Init IP end
var offsetToIP = func(offset int) *queueIP{
return &IP[offset];
};
var getLastFreeIP = func() (freeIP *queueIP){
{//FreeIP
if lastFreeIP == nil { return nil; }
if lastFreeIP.nextFreeIP != nil { lastFreeIP.nextFreeIP.beforeFreeIP = nil; }
freeIP = lastFreeIP;
lastFreeIP = lastFreeIP.nextFreeIP;
if lastFreeIP == nil { endFreeIP = nil; }
freeIP.nextFreeIP = nil;
}
{//ReserveIP
freeIP.nextFreeIP = nil;
freeIP.beforeFreeIP = endReserveIP;
if endReserveIP != nil { endReserveIP.nextFreeIP = freeIP; }
endReserveIP = freeIP;
freeIP.stage = IP_Reserved;
}
return;
};
var getFreeIP = func(offset int) (freeIP *queueIP){
{//FreeIP
if IP[offset].beforeFreeIP != nil { IP[offset].beforeFreeIP.nextFreeIP = IP[offset].nextFreeIP; }
if IP[offset].nextFreeIP != nil { IP[offset].nextFreeIP.beforeFreeIP = IP[offset].beforeFreeIP; }
if &IP[offset] == endFreeIP { endFreeIP = IP[offset].beforeFreeIP; }
if &IP[offset] == lastFreeIP { lastFreeIP = IP[offset].nextFreeIP; }
IP[offset].nextFreeIP = nil;
IP[offset].beforeFreeIP = nil;
}
freeIP = &IP[offset];
{//ReserveIP
freeIP.nextFreeIP = nil;
freeIP.beforeFreeIP = endReserveIP;
if endReserveIP != nil { endReserveIP.nextFreeIP = freeIP; }
endReserveIP = freeIP;
freeIP.stage = IP_Reserved;
}
return;
};
var getReserveIP = func(offset int) (freeIP *queueIP){
{//ReserveIP
if IP[offset].beforeFreeIP != nil { IP[offset].beforeFreeIP.nextFreeIP = IP[offset].nextFreeIP; }
if IP[offset].nextFreeIP != nil { IP[offset].nextFreeIP.beforeFreeIP = IP[offset].beforeFreeIP; }
if &IP[offset] == endReserveIP { endReserveIP = IP[offset].beforeFreeIP; }
IP[offset].nextFreeIP = nil;
IP[offset].beforeFreeIP = nil;
}
freeIP = &IP[offset];
freeIP.stage = IP_NotFree;
return;
};
var retNotFreeIP = func(freeIP *queueIP) {
freeIP.nextFreeIP = nil;
freeIP.beforeFreeIP = endFreeIP;
if endFreeIP != nil { endFreeIP.nextFreeIP = freeIP;
} else { lastFreeIP = freeIP; }
endFreeIP = freeIP;
freeIP.stage = IP_Free;
};
var freeIPTest = func(offset int) Stage{
// Dead code:
//switch{
// case (IP[offset].nextFreeIP == nil && IP[offset].beforeFreeIP == nil) &&
// (IP[offset] != lastFreeIP /*|| IP[offset] != endFreeIP*/) &&
// (IP[offset] != endReserveIP): return 2;
// case IP[offset].reserved: return 1;
// default: return 0;
//}
return IP[offset].stage;
};
var clientMAC = map[MAC48] *queueIP {};
var safeSetMAC = func(offset int, MAC MAC48){
if clientMAC[MAC] != nil && clientMAC[MAC] != &IP[offset] {
if IP[offset].stage == IP_Reserved {
getReserveIP(offset);
}
retNotFreeIP(&IP[offset]);
}
clientMAC[MAC] = &IP[offset];
IP[offset].mac = MAC;
};
var setMAC = func(freeIP *queueIP, MAC MAC48){
clientMAC[MAC] = freeIP;
freeIP.mac = MAC;
};
var deleteMAC = func(MAC MAC48){
clientMAC[MAC].mac = MAC48{0,0,0,0,0,0};
delete(clientMAC, MAC);
};
return dhcp.Handler{
ServeDHCP: func(req dhcp.Packet, msgType dhcp.MessageType, options dhcp.Options) dhcp.Packet {
return (map[dhcp.MessageType] func() dhcp.Packet{
dhcp.Discover: func() dhcp.Packet{
/*
switch android, iPhone, PC, ...
*/
//req.
/*var leasesMobileIP = map[string] string{
"iPhone":"Apple",
"":"noname",
};
var leasesMobileMAC = map[string] string{
"iPhone":"Apple",
"":"noname",
};
var leasesPC;
log.Println(leases[string(bytes.SplitN(options[dhcp.OptionHostName],[]byte{'-'},2)[0])]);*/
var reqMAC MAC48; copy(reqMAC[:], req.CHAddr());
var reqIP = net.IP(options[dhcp.OptionRequestedIPAddress]);
if tr:=req.CIAddr(); reqIP == nil && !tr.Equal(net.IP{0,0,0,0}) { reqIP = tr; }
if reqIP!=nil { if r:=dhcp.IPRange(START_IP, reqIP)-1; !(r>=0 && r<IP_Range) { reqIP=nil; } }
switch{
//first
case reqIP == nil && clientMAC[reqMAC] == nil:
var record = getLastFreeIP(); //TODO: if record == nil
setMAC(record, reqMAC);
return dhcp.ReplyPacket(req, dhcp.Offer, SERVER_IP, record.ip, time.Hour, nil);
//double first
case reqIP == nil && clientMAC[reqMAC] != nil && clientMAC[reqMAC].stage == IP_Reserved:
return dhcp.ReplyPacket(req, dhcp.Offer, SERVER_IP, clientMAC[reqMAC].ip, time.Hour, nil);
//old my friend
case reqIP == nil && clientMAC[reqMAC] != nil && clientMAC[reqMAC].stage == IP_NotFree:
//change state to IP_Reserved
if getFreeIP(dhcp.IPRange(START_IP, clientMAC[reqMAC].ip)-1) != clientMAC[reqMAC] { panic("Bug#657438310"); } //TODO: Check
return dhcp.ReplyPacket(req, dhcp.Offer, SERVER_IP, clientMAC[reqMAC].ip, time.Hour, nil);
//detect error in this program
case reqIP == nil && clientMAC[reqMAC] != nil && clientMAC[reqMAC].stage == IP_Free:
panic("Bug#5464985");
return nil;
case reqIP != nil && clientMAC[reqMAC] == nil:
var offset = dhcp.IPRange(START_IP, reqIP)-1;
switch freeIPTest(offset) {
//use user IP
case IP_Free:
var record = getFreeIP(offset);
setMAC(record, reqMAC);
return dhcp.ReplyPacket(req, dhcp.Offer, SERVER_IP, record.ip, time.Hour, nil);
//try send another ip
case IP_Reserved, IP_NotFree:
var record = getLastFreeIP();
setMAC(record, reqMAC);
return dhcp.ReplyPacket(req, dhcp.Offer, SERVER_IP, record.ip, time.Hour, nil);
}
case reqIP != nil && clientMAC[reqMAC] != nil:
var offsetReqIP = dhcp.IPRange(START_IP, reqIP)-1;
if offsetToIP(offsetReqIP).mac == reqMAC {
switch freeIPTest(offsetReqIP){
//double
case IP_Reserved:
return dhcp.ReplyPacket(req, dhcp.Offer, SERVER_IP, reqIP, time.Hour, nil);
//my friend
case IP_NotFree:
//change state to IP_Reserved
if getFreeIP(dhcp.IPRange(START_IP, reqIP)-1) != clientMAC[reqMAC] { panic("Bug#657438310+"); } //TODO: Check
return dhcp.ReplyPacket(req, dhcp.Offer, SERVER_IP, reqIP, time.Hour, nil);
//detect error in this program
case IP_Free:
panic("Bug#5464985+");
return nil;
}
}else{
switch freeIPTest(offsetReqIP){
//move to user IP
case IP_Free:
safeSetMAC(offsetReqIP, reqMAC);
var record = getFreeIP(offsetReqIP);
return dhcp.ReplyPacket(req, dhcp.Offer, SERVER_IP, record.ip, time.Hour, nil);
case IP_Reserved, IP_NotFree:
switch clientMAC[reqMAC].stage{
//double sclerosis
case IP_Reserved:
return dhcp.ReplyPacket(req, dhcp.Offer, SERVER_IP, clientMAC[reqMAC].ip, time.Hour, nil);
//my sclerosis friend
case IP_NotFree:
//change state to IP_Reserved
if getFreeIP(dhcp.IPRange(START_IP, clientMAC[reqMAC].ip)-1) != clientMAC[reqMAC] { panic("Bug#657438310-"); } //TODO: Check
return dhcp.ReplyPacket(req, dhcp.Offer, SERVER_IP, clientMAC[reqMAC].ip, time.Hour, nil);
//detect error in this program
case IP_Free:
panic("Bug#5464985-");
return nil;
}
}
}
}
/*return dhcp.ReplyPacket(req, dhcp.Offer, SERVER_IP, clientMAC[reqMAC],
h.options.SelectOrderOrAll(options[dhcp.OptionParameterRequestList]), nil);*/
return nil;
},
dhcp.Request: func() dhcp.Packet{
var reqMAC MAC48; copy(reqMAC[:], req.CHAddr());
var reqIP = net.IP(options[dhcp.OptionRequestedIPAddress]);
var reqServerIP = net.IP(options[dhcp.OptionServerIdentifier]);
if tr:=req.CIAddr(); reqIP == nil && !tr.Equal(net.IP{0,0,0,0}) { reqIP = tr; }
//very buggy client
if reqServerIP == nil && reqIP == nil { return nil; }
//try correct
if reqServerIP == nil { reqServerIP = SERVER_IP; }
if reqIP == nil {
if reqIP = clientMAC[reqMAC].ip; reqIP == nil { return nil; } //correct fail
}
var reqIPInRange bool; {
r:=dhcp.IPRange(START_IP, reqIP)-1;
reqIPInRange = r>=0 && r<IP_Range;
}
var serversIPEqual = reqServerIP.Equal(SERVER_IP);
switch {
case serversIPEqual && reqIPInRange && clientMAC[reqMAC] != nil:
var offsetReqIP = dhcp.IPRange(START_IP, reqIP)-1;
if offsetToIP(offsetReqIP).mac == reqMAC { //TODO: test speed "offsetToIP(offsetReqIP)==clientMAC[reqMAC]" && "clientMAC[reqMAC].ip.Equal(reqIP)"
switch freeIPTest(offsetReqIP){
case IP_Reserved:
//change state to IP_NotFree
if getReserveIP(offsetReqIP) != clientMAC[reqMAC] { panic("Bug#+545448411000512+"); } //TODO: Check
return dhcp.ReplyPacket(req, dhcp.ACK, SERVER_IP, reqIP, time.Hour, nil);
case IP_NotFree:
return dhcp.ReplyPacket(req, dhcp.ACK, SERVER_IP, reqIP, time.Hour, nil);
//detect error in this program
case IP_Free:
panic("Bug#+5464985+");
return nil;
}
}else{
switch freeIPTest(offsetReqIP){
//move to user IP
case IP_Free:
safeSetMAC(offsetReqIP, reqMAC);
var record = getFreeIP(offsetReqIP);
record = getReserveIP(offsetReqIP);
return dhcp.ReplyPacket(req, dhcp.ACK, SERVER_IP, record.ip, time.Hour, nil);
case IP_Reserved, IP_NotFree:
switch clientMAC[reqMAC].stage {
case IP_Reserved:
//delete user
retNotFreeIP(getReserveIP(dhcp.IPRange(START_IP, clientMAC[reqMAC].ip)-1));
deleteMAC(reqMAC);
return dhcp.ReplyPacket(req, dhcp.NAK, SERVER_IP, nil, 0, nil);
case IP_NotFree:
//delete user
retNotFreeIP(clientMAC[reqMAC]);
deleteMAC(reqMAC);
return dhcp.ReplyPacket(req, dhcp.NAK, SERVER_IP, nil, 0, nil);
//detect error in this program
case IP_Free:
panic("Bug#-5464985-");
return nil;
}
}
}
case serversIPEqual && reqIPInRange && clientMAC[reqMAC] == nil:
var offset = dhcp.IPRange(START_IP, reqIP)-1;
switch freeIPTest(offset) {
//use user IP
case IP_Free:
var record = getFreeIP(offset);
record = getReserveIP(offset);
setMAC(record, reqMAC);
return dhcp.ReplyPacket(req, dhcp.ACK, SERVER_IP, record.ip, time.Hour, nil);
//user broken off
case IP_Reserved, IP_NotFree:
return dhcp.ReplyPacket(req, dhcp.NAK, SERVER_IP, nil, 0, nil);
}
case serversIPEqual && !reqIPInRange && clientMAC[reqMAC] != nil:
switch clientMAC[reqMAC].stage {
case IP_Reserved:
//delete user
retNotFreeIP(getReserveIP(dhcp.IPRange(START_IP, clientMAC[reqMAC].ip)-1));
deleteMAC(reqMAC);
return dhcp.ReplyPacket(req, dhcp.NAK, SERVER_IP, nil, 0, nil);
case IP_NotFree:
//delete user
retNotFreeIP(clientMAC[reqMAC]);
deleteMAC(reqMAC);
return dhcp.ReplyPacket(req, dhcp.NAK, SERVER_IP, nil, 0, nil);
//detect error in this program
case IP_Free:
panic("Bug#--5464985--");
return nil;
}
case serversIPEqual && !reqIPInRange && clientMAC[reqMAC] == nil:
return dhcp.ReplyPacket(req, dhcp.NAK, SERVER_IP, nil, 0, nil);
case !serversIPEqual && reqIPInRange && clientMAC[reqMAC] != nil:
var offsetReqIP = dhcp.IPRange(START_IP, reqIP)-1;
if offsetToIP(offsetReqIP).mac == reqMAC { //TODO: test speed "offsetToIP(offsetReqIP)==clientMAC[reqMAC]" && "clientMAC[reqMAC].ip.Equal(reqIP)"
switch freeIPTest(offsetReqIP){
case IP_Reserved:
//change state to IP_NotFree
if getReserveIP(offsetReqIP) != clientMAC[reqMAC] { panic("Bug#++545448411000512++"); } //TODO: Check
return nil;
case IP_NotFree:
return nil;
//detect error in this program
case IP_Free:
panic("Bug#++5464985++");
return nil;
}
}else{
switch freeIPTest(offsetReqIP){
//move to user IP
case IP_Free:
safeSetMAC(offsetReqIP, reqMAC);
/*var record =*/ getFreeIP(offsetReqIP);
/*record =*/ getReserveIP(offsetReqIP);
return nil;
case IP_Reserved, IP_NotFree:
switch clientMAC[reqMAC].stage {
case IP_Reserved:
//delete user
retNotFreeIP(getReserveIP(dhcp.IPRange(START_IP, clientMAC[reqMAC].ip)-1));
deleteMAC(reqMAC);
return nil;
case IP_NotFree:
//delete user
retNotFreeIP(clientMAC[reqMAC]);
deleteMAC(reqMAC);
return nil;
//detect error in this program
case IP_Free:
panic("Bug#---5464985---");
return nil;
}
}
}
case !serversIPEqual && reqIPInRange && clientMAC[reqMAC] == nil:
var offset = dhcp.IPRange(START_IP, reqIP)-1;
switch freeIPTest(offset) {
//use user IP
case IP_Free:
var record = getFreeIP(offset);
record = getReserveIP(offset);
setMAC(record, reqMAC);
return nil;
//user broken off
case IP_Reserved, IP_NotFree:
return nil;
}
case !serversIPEqual && !reqIPInRange && clientMAC[reqMAC] != nil:
switch clientMAC[reqMAC].stage {
case IP_Reserved:
//delete user
retNotFreeIP(getReserveIP(dhcp.IPRange(START_IP, clientMAC[reqMAC].ip)-1));
deleteMAC(reqMAC);
return nil;
case IP_NotFree:
//delete user
retNotFreeIP(clientMAC[reqMAC]);
deleteMAC(reqMAC);
return nil;
//detect error in this program
case IP_Free:
panic("Bug#--5464985--");
return nil;
}
case !serversIPEqual && !reqIPInRange && clientMAC[reqMAC] == nil:
return nil;
}
return nil;
},
dhcp.Decline: func() dhcp.Packet{
var reqMAC MAC48; copy(reqMAC[:], req.CHAddr());
if clientMAC[reqMAC] != nil {
log.Println("IP conflict (Decline msg) - IP: " + clientMAC[reqMAC].ip.String() + " MAC: " + net.HardwareAddr(reqMAC[:]).String());
switch clientMAC[reqMAC].stage {
case IP_Reserved:
//delete user
retNotFreeIP(getReserveIP(dhcp.IPRange(START_IP, clientMAC[reqMAC].ip)-1));
deleteMAC(reqMAC);
return nil;
case IP_NotFree:
//delete user
retNotFreeIP(clientMAC[reqMAC]);
deleteMAC(reqMAC);
return nil;
//detect error in this program
case IP_Free:
panic("Bug#!--5464985--!");
return nil;
}
}
return nil;
},
dhcp.Release: func() dhcp.Packet{
var reqMAC MAC48; copy(reqMAC[:], req.CHAddr());
var reqIP = req.CIAddr();
if reqIP.Equal(net.IP{0,0,0,0}) { reqIP = nil; }
if clientMAC[reqMAC] != nil && !(reqIP != nil && !clientMAC[reqMAC].ip.Equal(reqIP)) {
switch clientMAC[reqMAC].stage {
case IP_Reserved:
//delete user
retNotFreeIP(getReserveIP(dhcp.IPRange(START_IP, clientMAC[reqMAC].ip)-1));
deleteMAC(reqMAC);
return nil;
case IP_NotFree:
//delete user
retNotFreeIP(clientMAC[reqMAC]);
deleteMAC(reqMAC);
return nil;
//detect error in this program
case IP_Free:
panic("Bug#!!--5464985--!!");
return nil;
}
}
return nil;
},
dhcp.Inform: func() dhcp.Packet{
var reqMAC MAC48; copy(reqMAC[:], req.CHAddr());
var reqIP = req.CIAddr();
if clientMAC[reqMAC].ip.Equal(reqIP) { // + "clientMAC[reqMAC] != nil" :-)
return dhcp.ReplyPacket(req, dhcp.ACK, SERVER_IP, nil, 0, nil);
}else{
log.Println("Interesting (Inform msg)...");
}
return nil;
},
})[msgType]();
},
};
}());
}