Passing settings file contents to construct_reply_packet to remove static IP entries

This commit is contained in:
Dom 2023-11-14 22:19:25 +00:00
parent 7e38f3d04b
commit cbd8f336ab

View File

@ -118,7 +118,7 @@ class Packet():
) )
@staticmethod @staticmethod
def construct_reply_packet(reference_packet, _type, source_ip, topology): def construct_reply_packet(reference_packet, _type, source_ip, topology, settings):
''' '''
Summary: Summary:
Constructs a DHCP packet of type Offer or Ack, modelled from a received packet with some options changed/added. Constructs a DHCP packet of type Offer or Ack, modelled from a received packet with some options changed/added.
@ -142,34 +142,29 @@ class Packet():
reply_packet_object.OP = bytes([0x02]) reply_packet_object.OP = bytes([0x02])
reply_packet_object.YIADDR = bytes([yiaddr[0], yiaddr[1], yiaddr[2], yiaddr[3]]) # Client IP reply_packet_object.YIADDR = bytes([yiaddr[0], yiaddr[1], yiaddr[2], yiaddr[3]]) # Client IP
reply_packet_object.SIADDR = bytes([192, 168, 1, 7]) # Server IP: 192.168.1.7 reply_packet_object.SIADDR = bytes([int(octet) for octet in settings['DHCP']['SERVER_IP'].split('.')]) # Server IP (192.168.0.247)
if _type == 'offer':
packet_type = bytes([53, 1, 2]) # DHCP offer packet
elif _type == 'ack':
packet_type = bytes([53, 1, 5]) # DHCP ack packet
if client_device_os == 'junos': if client_device_os == 'junos':
reply_packet_object.OPTIONS = b"".join( reply_packet_object.OPTIONS = b"".join(
[ [
packet_type, bytes([53, 1, 2 if _type == 'offer' else 5]), # DHCP offer or ack packet
bytes([54, 4, 192, 168, 1, 7]), # Server identifier: 192.168.1.7 bytes([54, 4] + [int(octet) for octet in settings['DHCP']['SERVER_IP'].split('.')]), # Server identifier (192.168.0.247)
bytes([51, 4, 0x00, 0x01, 0x51, 0x80]), # Lease time: 86400 bytes([51, 4, 0x00, 0x01, 0x51, 0x80]), # Lease time: 86400
bytes([1, 4, 255, 255, 255, 254]), # Subnet mask: 255.255.255.254 bytes([1, 4, 255, 255, 255, 254]), # Subnet mask: 255.255.255.254
bytes([3, 4, giaddr[0], giaddr[1], giaddr[2], giaddr[3]]), # Default gateway bytes([3, 4, giaddr[0], giaddr[1], giaddr[2], giaddr[3]]), # Default gateway
bytes([150, 4, 192, 168, 1, 7]), # TFTP server: 192.168.1.7 bytes([150, 4] + [int(octet) for octet in settings['TFTP']['SERVER_IP'].split('.')]), # TFTP server (192.168.0.247)
Packet.construct_junos_suboptions(f'/configs/{client_device_name}.conf'), # Juniper specific suboptions Packet.construct_junos_suboptions(f'/configs/{client_device_name}.conf'), # Juniper specific suboptions
] ]
) )
elif client_device_os == 'cisco_ios': elif client_device_os == 'cisco_ios':
reply_packet_object.OPTIONS = b"".join( reply_packet_object.OPTIONS = b"".join(
[ [
packet_type, bytes([53, 1, 3 if _type == 'offer' else 5]), # DHCP offer or ack packet
bytes([54, 4, 192, 168, 1, 7]), # Server identifier: 192.168.1.7 bytes([54, 4] + [int(octet) for octet in settings['DHCP']['SERVER_IP'].split('.')]), # Server identifier (192.168.0.247)
bytes([51, 4, 0x00, 0x01, 0x51, 0x80]), # Lease time: 86400 bytes([51, 4, 0x00, 0x01, 0x51, 0x80]), # Lease time: 86400
bytes([1, 4, 255, 255, 255, 254]), # Subnet mask: 255.255.255.254 bytes([1, 4, 255, 255, 255, 254]), # Subnet mask: 255.255.255.254
bytes([3, 4, giaddr[0], giaddr[1], giaddr[2], giaddr[3]]), # Default gateway bytes([3, 4, giaddr[0], giaddr[1], giaddr[2], giaddr[3]]), # Default gateway
bytes([150, 4, 192, 168, 1, 7]), # TFTP server: 192.168.1.7 bytes([150, 4] + [int(octet) for octet in settings['TFTP']['SERVER_IP'].split('.')]), # TFTP server (192.168.0.247)
Packet.construct_tlv(67, f'/configs/{client_device_name}.conf'), # Cisco specific bootfile Packet.construct_tlv(67, f'/configs/{client_device_name}.conf'), # Cisco specific bootfile
] ]
) )
@ -233,7 +228,6 @@ class Packet():
option_description_dict[option_number] = option_description option_description_dict[option_number] = option_description
index += 2 + option_length index += 2 + option_length
return option_dict, option_description_dict return option_dict, option_description_dict
@staticmethod @staticmethod
@ -355,7 +349,7 @@ class DHCPServer(object):
if packet_options[53] == 1: if packet_options[53] == 1:
log('Received DHCP discover packet', 'info') log('Received DHCP discover packet', 'info')
offer_packet = Packet.construct_reply_packet(received_packet, 'offer', source_address, topology) offer_packet = Packet.construct_reply_packet(received_packet, 'offer', source_address, topology, self.SETTINGS)
log('Constructing DHCP offer packet...', 'info') log('Constructing DHCP offer packet...', 'info')
packet_data, packet_options, option_descriptions = Packet.examine_packet(offer_packet) packet_data, packet_options, option_descriptions = Packet.examine_packet(offer_packet)
log(f'Offer packet options:', 'info') log(f'Offer packet options:', 'info')
@ -365,7 +359,7 @@ class DHCPServer(object):
elif packet_options[53] == 3: elif packet_options[53] == 3:
log('Received DHCP request packet', 'info') log('Received DHCP request packet', 'info')
ack_packet = Packet.construct_reply_packet(offer_packet, 'ack', source_address, topology) ack_packet = Packet.construct_reply_packet(offer_packet, 'ack', source_address, topology, self.SETTINGS)
log('Constructing DHCP ack packet...', 'info') log('Constructing DHCP ack packet...', 'info')
packet_data, packet_options, option_descriptions = Packet.examine_packet(ack_packet) packet_data, packet_options, option_descriptions = Packet.examine_packet(ack_packet)
log(f'Ack packet options:', 'info') log(f'Ack packet options:', 'info')