-
Notifications
You must be signed in to change notification settings - Fork 2
/
request.go
236 lines (191 loc) · 6.07 KB
/
request.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
package main
import (
"bytes"
"fmt"
"log"
"net"
)
type RequestHandler struct {
header *MessageHeader
options *Options
pool *Pool
}
func NewRequestHandler(message *DHCPMessage, pool *Pool) *RequestHandler {
return &RequestHandler{
pool: pool,
header: message.Header,
options: message.Options,
}
}
func (r *RequestHandler) Handle() *DHCPMessage {
switch r.options.GetByte(OPTION_MESSAGE_TYPE) {
case DHCPDISCOVER:
return r.HandleDiscover()
case DHCPREQUEST:
return r.HandleRequest()
case DHCPRELEASE:
return r.HandleRelease()
default:
log.Printf("Unimplemented op %v", r.header.Op)
return nil
}
}
func (r *RequestHandler) HandleDiscover() *DHCPMessage {
hostname := ""
if option, ok := r.options.Get(OPTION_HOST_NAME); ok {
hostname = string(option.Data)
}
mac := r.header.Mac
log.Printf("DHCPDISCOVER from %v (%s)", mac.String(), hostname)
if lease, ok := r.pool.TouchLeaseByMac(mac); ok {
log.Printf("Have old lease for %v: %v", mac.String(), lease.IP.String())
return r.SendLeaseInfo(lease, DHCPOFFER)
}
lease, err := r.pool.GetNextLease(mac, hostname)
if err != nil {
log.Printf("Could not get a new lease for %v: %v", mac.String(), err)
return nil
}
return r.SendLeaseInfo(lease, DHCPOFFER)
}
func (r *RequestHandler) HandleRequest() *DHCPMessage {
mac := r.header.Mac
log.Printf("DHCPREQUEST from %v for %v", mac.String(), r.header.ClientAddr.String())
var lease *Lease
var ok bool
if lease, ok = r.pool.TouchLeaseByMac(mac); !ok {
log.Printf("Unrecognized lease for %v", mac.String())
return r.SendNAK()
}
// Verify IP matches what is in our lease
if r.header.ClientAddr != lease.IP {
log.Printf("Client IP does not match! %v != %v (expected)", r.header.ClientAddr, lease.IP)
return r.SendNAK()
}
// Need to send DHCPACK
return r.SendLeaseInfo(lease, DHCPACK)
}
func (r *RequestHandler) HandleRelease() *DHCPMessage {
mac := r.header.Mac
log.Printf("DHCPRELEASE from %v for %v", mac.String(), r.header.ClientAddr.String())
var lease *Lease
var ok bool
if lease, ok = r.pool.ReleaseLeaseByMac(mac); !ok {
log.Printf("Unrecognized lease for %v to release", mac.String())
return nil
}
// Verify IP matches what is in our lease
if r.header.ClientAddr != lease.IP {
log.Printf("Client IP does not match! %v != %v (expected)", r.header.ClientAddr, lease.IP)
}
// No response to a DHCPRELEASE
return nil
}
// Share code for DHCPOFFER and DHCPACK
func (r *RequestHandler) SendLeaseInfo(lease *Lease, op byte) *DHCPMessage {
header := &MessageHeader{
Op: BOOT_REPLY,
Hops: 0,
Identifier: r.header.Identifier,
YourAddr: lease.IP,
ServerAddr: r.pool.MyIp,
Mac: r.header.Mac,
}
log.Printf("Sending %s with %v to %v", opNames[op], lease.IP.String(), r.header.Mac.String())
options := NewOptions()
// Message type
options.Set(OPTION_MESSAGE_TYPE, []byte{op})
// Netmask option
options.SetIPs(OPTION_SUBNET, r.pool.Netmask)
// Router (defgw)
if len(r.pool.Router) > 0 {
options.SetIPs(OPTION_ROUTER, r.pool.Router...)
}
// DNS servers
if len(r.pool.Dns) > 0 {
options.SetIPs(OPTION_DNS_SERVER, r.pool.Dns...)
}
// Lease time
options.Set(OPTION_LEASE_TIME, long2bytes(uint32(r.pool.LeaseTime.Seconds())))
// DHCP server
options.SetFixedV4s(OPTION_SERVER_ID, r.pool.MyIp)
return &DHCPMessage{header, options}
}
func (r *RequestHandler) SendNAK() *DHCPMessage {
header := &MessageHeader{
Op: BOOT_REPLY,
Hops: 0,
Identifier: r.header.Identifier,
ServerAddr: r.pool.MyIp,
Mac: r.header.Mac,
}
log.Printf("Sending %s to %v", opNames[DHCPNAK], r.header.Mac.String())
options := NewOptions()
options.Set(OPTION_MESSAGE_TYPE, []byte{DHCPNAK})
// FIXME: we likely need more options
return &DHCPMessage{header, options}
}
//
// Send a dhcp response message to broadcast address
//
func (r *RequestHandler) sendMessageBroadcast(message *DHCPMessage, localSocket *net.UDPConn) {
buf := new(bytes.Buffer)
err := message.Encode(buf)
if err != nil {
log.Printf("Failed encoding payload: %v", err)
return
}
err = r.sendBroadcast(buf.Bytes(), localSocket)
if err != nil {
log.Printf("Failed sending %s payload: %v", opNames[message.Options.GetByte(OPTION_MESSAGE_TYPE)], err)
}
}
func (r *RequestHandler) sendBroadcast(data []byte, localSocket *net.UDPConn) error {
// Quickly ripped from https://github.com/aler9/howto-udp-broadcast-golang
addr, err := net.ResolveUDPAddr("udp4", r.pool.Broadcast.String()+":68")
if err != nil {
return fmt.Errorf("Failed resolving remote: %v", err)
}
// Need to use our original listening socket to maintain source port 67,
// otherwise windows dhcp will not see our responses
_, err = localSocket.WriteTo(data, addr)
if err != nil {
return fmt.Errorf("Failed writing: %v", err)
}
return nil
}
//
// Send a dhcp response message to a unicast address
//
func (r *RequestHandler) sendMessageRelayed(message *DHCPMessage, dest FixedV4, localSocket *net.UDPConn) {
// FIXME: maybe more/fixed header mangling?
message.Header.GatewayAddr = r.header.GatewayAddr
message.Header.Flags = r.header.Flags
r.sendMessageUnicast(message, dest, localSocket)
}
func (r *RequestHandler) sendMessageUnicast(message *DHCPMessage, dest FixedV4, localSocket *net.UDPConn) {
buf := new(bytes.Buffer)
err := message.Encode(buf)
if err != nil {
log.Printf("Failed encoding payload: %v", err)
return
}
err = r.sendUnicast(buf.Bytes(), dest, localSocket)
if err != nil {
log.Printf("Failed sending %s unicast payload: %v", opNames[message.Options.GetByte(OPTION_MESSAGE_TYPE)], err)
}
}
func (r *RequestHandler) sendUnicast(data []byte, dest FixedV4, localSocket *net.UDPConn) error {
// Quickly ripped from https://github.com/aler9/howto-udp-broadcast-golang
addr, err := net.ResolveUDPAddr("udp4", dest.String()+":67")
if err != nil {
return fmt.Errorf("Failed resolving remote: %v", err)
}
// Need to use our original listening socket to maintain source port 67,
// otherwise windows dhcp will not see our responses
_, err = localSocket.WriteTo(data, addr)
if err != nil {
return fmt.Errorf("Failed writing: %v", err)
}
return nil
}