def _getNextDHCPPacket(self, timeout=60, packet_buffer=2048): """ Blocks for up to ``timeout`` seconds while waiting for a packet to arrive; if one does, a thread is spawned to process it. Have a thread blocking on this at all times; restart it immediately after it returns. :param int timeout: The number of seconds to wait before returning. :param int packet_buffer: The size of the buffer to use for receiving packets. :return tuple(2): 0. ``bool``: ``True`` if a DHCP packet was received. 1. :class:`Address <dhcp.Address>` or ``None``: None if the timeout was reached. """ (source_address, data, pxe) = self._network_link.getData(timeout=timeout, packet_buffer=packet_buffer) if data: try: packet = DHCPPacket(data=data) except ValueError: pass else: if packet.isDHCPRequestPacket(): threading.Thread(target=self._handleDHCPRequest, args=(packet, source_address, pxe)).start() elif packet.isDHCPDiscoverPacket(): threading.Thread(target=self._handleDHCPDiscover, args=(packet, source_address, pxe)).start() elif packet.isDHCPInformPacket(): threading.Thread(target=self._handleDHCPInform, args=(packet, source_address, pxe)).start() elif packet.isDHCPReleasePacket(): threading.Thread(target=self._handleDHCPRelease, args=(packet, source_address, pxe)).start() elif packet.isDHCPDeclinePacket(): threading.Thread(target=self._handleDHCPDecline, args=(packet, source_address, pxe)).start() elif packet.isDHCPLeaseQueryPacket(): threading.Thread(target=self._handleDHCPLeaseQuery, args=(packet, source_address, pxe)).start() return (True, source_address) return (False, source_address)
def _getNextDHCPPacket(self, timeout=60): """ Blocks for up to C{timeout} seconds while waiting for a packet to arrive; if one does, a thread is spawned to process it. @type timeout: int @param timeout: The number of seconds to wait before returning. @rtype: tuple(2) @return: (received:bool, (address:basestring, port:int)|None), with received indicating whether a DHCP packet was received or not and the tuple reflecting the source of the received packet, if any. """ active_sockets = None source_address = None if self._pxe_socket: active_sockets = select.select([self._dhcp_socket, self._pxe_socket], [], [], timeout)[0] else: active_sockets = select.select([self._dhcp_socket], [], [], timeout)[0] if active_sockets: active_socket = active_sockets[0] (data, source_address) = active_socket.recvfrom(4096) if data: packet = DHCPPacket(data) if packet.isDHCPPacket(): pxe = active_socket == self._pxe_socket if packet.isDHCPRequestPacket(): threading.Thread(target=self._handleDHCPRequest, args=(packet, source_address, pxe)).start() elif packet.isDHCPDiscoverPacket(): threading.Thread(target=self._handleDHCPDiscover, args=(packet, source_address, pxe)).start() elif packet.isDHCPInformPacket(): threading.Thread(target=self._handleDHCPInform, args=(packet, source_address, pxe)).start() elif packet.isDHCPReleasePacket(): threading.Thread(target=self._handleDHCPRelease, args=(packet, source_address, pxe)).start() elif packet.isDHCPDeclinePacket(): threading.Thread(target=self._handleDHCPDecline, args=(packet, source_address, pxe)).start() elif packet.isDHCPLeaseQueryPacket(): threading.Thread(target=self._handleDHCPLeaseQuery, args=(packet, source_address, pxe)).start() return (True, source_address) return (False, source_address)