-
Notifications
You must be signed in to change notification settings - Fork 1
/
server_helpers.py
225 lines (185 loc) · 7.71 KB
/
server_helpers.py
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
import queue
import struct
import threading
import time
from _socket import inet_aton
from listener import options, shortpack, macpack, ReadBootProtocolPacket
class DelayWorker(object):
def __init__(self):
self.closed = False
self.queue = queue.Queue()
self.thread = threading.Thread(target=self._delay_response_thread)
self.thread.start()
def _delay_response_thread(self):
while not self.closed:
if self.closed:
break
try:
p = self.queue.get(timeout=1)
t, func, args, kw = p
now = time.time()
if now < t:
time.sleep(0.01)
self.queue.put(p)
else:
func(*args, **kw)
except queue.Empty:
continue
def do_after(self, seconds, func, args=(), kw=None):
if kw is None:
kw = {}
self.queue.put((time.time() + seconds, func, args, kw))
def close(self):
self.closed = True
class Transaction(object):
def __init__(self, server):
self.server = server
self.configuration = server.configuration
self.packets = []
self.done_time = time.time() + self.configuration.length_of_transaction
self.done = False
self.do_after = self.server.delay_worker.do_after
def is_done(self):
return self.done or self.done_time < time.time()
def close(self):
self.done = True
def check_auth(self, packet):
return self.server.auth.check_auth(packet.client_mac_address)
def get_configuration(self, packet):
if self.check_auth(packet):
return self.server.post_auth_configuration
else:
return self.server.pre_auth_configuration
def receive(self, packet):
# packet from client <-> packet.message_type == 1
if packet.message_type == 1 and packet.dhcp_message_type == 'DHCPDISCOVER':
self.do_after(self.configuration.dhcp_offer_after_seconds,
self.received_dhcp_discover, (packet,), )
elif packet.message_type == 1 and packet.dhcp_message_type == 'DHCPREQUEST':
self.do_after(self.configuration.dhcp_acknowledge_after_seconds,
self.received_dhcp_request, (packet,), )
elif packet.message_type == 1 and packet.dhcp_message_type == 'DHCPINFORM':
self.received_dhcp_inform(packet)
else:
return False
return True
def received_dhcp_discover(self, discovery):
if self.is_done():
return
self.configuration.debug('discover:\n {}'.format(str(discovery).replace('\n', '\n\t')))
self.send_offer(discovery)
def send_offer(self, discovery):
offer = WriteBootProtocolPacket(self.get_configuration(discovery))
offer.parameter_order = discovery.parameter_request_list
offer.your_ip_address = self.server.get_ip_address(discovery)
# offer.client_ip_address =
offer.transaction_id = discovery.transaction_id
# offer.next_server_ip_address =
offer.relay_agent_ip_address = discovery.relay_agent_ip_address
offer.client_mac_address = discovery.client_mac_address
offer.client_ip_address = discovery.client_ip_address or '0.0.0.0'
offer.bootp_flags = discovery.bootp_flags
offer.dhcp_message_type = 'DHCPOFFER'
offer.client_identifier = discovery.client_mac_address
self.server.broadcast(offer)
def received_dhcp_request(self, request):
if self.is_done():
return
self.server.client_has_chosen(request)
self.acknowledge(request)
self.close()
def acknowledge(self, request):
ack = WriteBootProtocolPacket(self.get_configuration(request))
ack.parameter_order = request.parameter_request_list
ack.transaction_id = request.transaction_id
# ack.next_server_ip_address =
ack.bootp_flags = request.bootp_flags
ack.relay_agent_ip_address = request.relay_agent_ip_address
mac = request.client_mac_address
ack.client_mac_address = mac
# requested_ip_address = request.requested_ip_address
ack.client_ip_address = request.client_ip_address or '0.0.0.0'
ack.your_ip_address = self.server.get_ip_address(request)
ack.dhcp_message_type = 'DHCPACK'
self.server.broadcast(ack)
def received_dhcp_inform(self, inform):
self.close()
self.server.client_has_chosen(inform)
class WriteBootProtocolPacket(object):
message_type = 2 # 1 for client -> server 2 for server -> client
hardware_type = 1
hardware_address_length = 6
hops = 0
transaction_id = None
seconds_elapsed = 0
bootp_flags = 0 # unicast
client_ip_address = '0.0.0.0'
your_ip_address = '0.0.0.0'
next_server_ip_address = '0.0.0.0'
relay_agent_ip_address = '0.0.0.0'
client_mac_address = None
magic_cookie = '99.130.83.99'
parameter_order = []
def __init__(self, configuration):
for i in range(256):
names = ['option_{}'.format(i)]
if i < len(options) and hasattr(configuration, options[i][0]):
names.append(options[i][0])
for name in names:
if hasattr(configuration, name):
setattr(self, name, getattr(configuration, name))
def to_bytes(self):
result = bytearray(236)
result[0] = self.message_type
result[1] = self.hardware_type
result[2] = self.hardware_address_length
result[3] = self.hops
result[4:8] = struct.pack('>I', self.transaction_id)
result[8:10] = shortpack(self.seconds_elapsed)
result[10:12] = shortpack(self.bootp_flags)
result[12:16] = inet_aton(self.client_ip_address)
result[16:20] = inet_aton(self.your_ip_address)
result[20:24] = inet_aton(self.next_server_ip_address)
result[24:28] = inet_aton(self.relay_agent_ip_address)
result[28:28 + self.hardware_address_length] = macpack(self.client_mac_address)
result += inet_aton(self.magic_cookie)
for option in self.options:
value = self.get_option(option)
# print(option, value)
if value is None:
continue
result += bytes([option, len(value)]) + value
result += bytes([255])
return bytes(result)
def get_option(self, option):
if option < len(options) and hasattr(self, options[option][0]):
value = getattr(self, options[option][0])
elif hasattr(self, 'option_{}'.format(option)):
value = getattr(self, 'option_{}'.format(option))
else:
return None
function = options[option][2]
if function and value is not None:
value = function(value)
return value
@property
def options(self):
done = list()
# fulfill wishes
for option in self.parameter_order:
if option < len(options) and hasattr(self, options[option][0]) or hasattr(self, 'option_{}'.format(option)):
# this may break with the specification because we must try to fulfill the wishes
if option not in done:
done.append(option)
# add my stuff
for option, o in enumerate(options):
if o[0] and hasattr(self, o[0]):
if option not in done:
done.append(option)
for option in range(256):
if hasattr(self, 'option_{}'.format(option)):
if option not in done:
done.append(option)
return done
def __str__(self):
return str(ReadBootProtocolPacket(self.to_bytes()))