def HandleDhcpRequest(self, packet): mac = MAC.from_list(packet.GetHardwareAddress()) request_ip = self._get_requested_ip_from_packet(packet) or \ IP.from_list(packet.GetOption('yiaddr')) logging.debug("REQUEST: %s requested %s", str(mac), str(request_ip)) if sum(packet.GetOption('giaddr')): dest_relay_or_gateway = str(IP.from_list(packet.GetOption('giaddr'))) else: dest_relay_or_gateway = "255.255.255.255" lease_time = 30000 entry_options = self._calculate_entry_options(str(mac)) ipv4_range_collection = self._get_ipv4_range_collection(entry_options) try: if not self.ip_lease_manager.was_last_leased_to(request_ip, mac): logging.debug("REQUEST: ERROR: %s requested ip not leased to him: %s", str(mac), str(request_ip)) packet.TransformToDhcpNackPacket() logging.debug("REQUEST: Sent DENY to %s", str(mac)) self.SendDhcpPacketTo(packet, dest_relay_or_gateway, 68) return self.ip_lease_manager.lease_ip_address(request_ip, mac, lease_time) except LeaseError as e: logging.debug("REQUEST: ERROR: ", str(e)) return self._set_packet_options(packet, entry_options) packet.SetOption('yiaddr', list(request_ip)) packet.TransformToDhcpAckPacket() logging.debug("REQUEST: Sent ACK to %s", str(mac)) self.SendDhcpPacketTo(packet, dest_relay_or_gateway, 68)
def HandleDhcpInform(self, packet): mac = MAC.from_list(packet.GetHardwareAddress()) logging.debug("GOT: INFORM from " + 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) client_lease = self.ip_lease_manager.get_lease(mac=mac) if not client_lease: return None ip = IPv4.from_str(client_lease.ip_str) packet.SetOption('yiaddr', list(ip)) packet.TransformToDhcpAckPacket() dest_relay_or_gateway = None if sum(packet.GetOption('giaddr')): dest_relay_or_gateway = str(IP.from_list(packet.GetOption('giaddr'))) else: dest_relay_or_gateway = "255.255.255.255" logging.debug("SENT: ACK") self.SendDhcpPacketTo(packet, dest_relay_or_gateway, 68)
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 HandleDhcpRelease(self, packet): mac = MAC.from_list(packet.GetHardwareAddress()) logging.debug("RELEASE: from %s", str(mac)) self.ip_lease_manager.delete_lease(mac=mac)