-
Notifications
You must be signed in to change notification settings - Fork 0
/
dhcp.go
111 lines (98 loc) · 3.2 KB
/
dhcp.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
package main
import (
"math/rand"
"net"
"time"
dhcp "github.com/krolaw/dhcp4"
log "github.com/sirupsen/logrus"
)
type lease struct {
nic string // Client's CHAddr
expiry time.Time // When the lease expires
}
type dhcpServerHandler 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 newDHCPServer() *dhcpServerHandler {
return &dhcpServerHandler{
ip: net.ParseIP(DHCP_SERVER_ADDR),
start: net.ParseIP(DHCP_SERVER_LEASE_START_ADDR),
leaseDuration: DHCP_LEASE_DURATION * time.Minute,
leases: make(map[int]lease, DHCP_LEASE_COUNT),
leaseRange: DHCP_LEASE_RANGE,
options: dhcp.Options{
dhcp.OptionTFTPServerName: []byte(TFTP_SERVER_ADDR),
dhcp.OptionBootFileName: []byte(PXELINUX_LOADER),
dhcp.OptionDomainNameServer: []byte(DNS_SERVER_ADDR),
},
}
}
func (h *dhcpServerHandler) 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
}
func (h *dhcpServerHandler) ServeDHCP(p dhcp.Packet, msgType dhcp.MessageType, options dhcp.Options) (d dhcp.Packet) {
logger := log.New()
rl := NewLogger(logger.Writer())
rl.Info(msgType)
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
}
// Check if DHCP server implements "Requested IP Address" option
// RFC: http://www.freesoft.org/CIE/RFC/2131/23.htm
reqIP := net.IP(options[dhcp.OptionRequestedIPAddress])
if reqIP == nil {
reqIP = net.IP(p.CIAddr())
}
if len(reqIP) == 4 && !reqIP.Equal(net.IPv4zero) {
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, reqIP, h.leaseDuration,
h.options.SelectOrder(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
}
}
case dhcp.Inform:
return dhcp.ReplyPacket(p, dhcp.ACK, h.ip, nil, 0, nil)
}
return nil
}