diff --git a/dhcp/midasd.py b/dhcp/midasd.py index c0d1c1e..ba92ba0 100644 --- a/dhcp/midasd.py +++ b/dhcp/midasd.py @@ -118,7 +118,7 @@ class Packet(): ) @staticmethod - def construct_reply_packet(reference_packet, _type, source_ip, topology): + def construct_reply_packet(reference_packet, _type, source_ip, topology, settings): ''' Summary: 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.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 - - if _type == 'offer': - packet_type = bytes([53, 1, 2]) # DHCP offer packet - elif _type == 'ack': - packet_type = bytes([53, 1, 5]) # DHCP ack packet + reply_packet_object.SIADDR = bytes([int(octet) for octet in settings['DHCP']['SERVER_IP'].split('.')]) # Server IP (192.168.0.247) if client_device_os == 'junos': reply_packet_object.OPTIONS = b"".join( [ - packet_type, - bytes([54, 4, 192, 168, 1, 7]), # Server identifier: 192.168.1.7 + bytes([53, 1, 2 if _type == 'offer' else 5]), # DHCP offer or ack packet + 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([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([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 ] ) elif client_device_os == 'cisco_ios': reply_packet_object.OPTIONS = b"".join( [ - packet_type, - bytes([54, 4, 192, 168, 1, 7]), # Server identifier: 192.168.1.7 + bytes([53, 1, 3 if _type == 'offer' else 5]), # DHCP offer or ack packet + 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([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([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 ] ) @@ -233,7 +228,6 @@ class Packet(): option_description_dict[option_number] = option_description index += 2 + option_length - return option_dict, option_description_dict @staticmethod @@ -355,7 +349,7 @@ class DHCPServer(object): if packet_options[53] == 1: 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') packet_data, packet_options, option_descriptions = Packet.examine_packet(offer_packet) log(f'Offer packet options:', 'info') @@ -365,7 +359,7 @@ class DHCPServer(object): elif packet_options[53] == 3: 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') packet_data, packet_options, option_descriptions = Packet.examine_packet(ack_packet) log(f'Ack packet options:', 'info')