def HandleDhcpDiscover(self, packet): mac = MAC.from_list(packet.GetHardwareAddress()) logging.debug("DISCOVER: %s", str(mac)) entry_options = self._calculate_entry_options(str(mac)) ipv4_range_collection = self._get_ipv4_range_collection(entry_options) self._set_packet_options(packet, entry_options) backend_ip = entry_options.get('yiaddr', None) requested_ip = self._get_requested_ip_from_packet(packet) ip = None if backend_ip: ip = IPv4.from_list(backend_ip) self.ip_lease_manager.reallocate_ip_address(ip) logging.debug("DISCOVER: %s gets static ip: %s", str(mac), str(ip)) else: ip = self.ip_lease_manager.allocate_ip_address(ipv4_range_collection, mac, requested_ip=requested_ip) logging.debug("DISCOVER: %s requested ip %s, giving %s", str(mac), str(requested_ip), str(ip)) packet.SetOption('yiaddr', list(ip)) packet.TransformToDhcpOfferPacket() logging.debug("DISCOVER: Sent OFFER to %s", str(mac)) self.SendDhcpPacketTo(packet, "255.255.255.255", 68)
def _get_requested_ip_from_packet(self, packet): for attr in ('ciaddr', 'request_ip_address'): if sum(packet.GetOption(attr)): requested_ip = IPv4.from_list(packet.GetOption(attr)) return requested_ip return None