Framework overview

libpydhcpserver is fundamentally a framework on which a DHCP server may be built. Its types may be of value to developers working with DHCP in Python, but some will be crazy enough to need to build a server of their own and this is a good place to start.

For reference, consider studying staticDHCPd, a complete server built on top of libpydhcpserver.

API: all you need to know

The core operation of a DHCP server is relatively straightforward: a request packet is received and analysed, then a response packet is emitted. Between the types libpydhcpserver provides (which include an object-oriented packet interface) and the DHCPServer class described below, all that’s left for you to do is customise the analysis part.

Constants

dhcp.IP_UNSPECIFIED_FILTER = (IPv4('0.0.0.0'), IPv4('255.255.255.255'), None)

A tuple of addresses that reflect non-unicast targets.

Named Tuples

dhcp.Address

An inet layer-3 address.

dhcp.ip

An IPv4 address

dhcp.port

A numeric port value.

Classes

class dhcp.DHCPServer(server_address, server_port, client_port, proxy_port=None, response_interface=None, response_interface_qtags=None)[source]

Sets up the DHCP network infrastructure.

Parameters:
  • server_address (IPv4) – The IP address on which to run the DHCP service.
  • port (int) – The port on which DHCP servers and relays listen in this network.
  • client_port (int) – The port on which DHCP clients listen in this network.
  • proxy_port (int) – The port on which ProxyDHCP servers listen for in this network; None to disable.
  • response_interface (str) – The interface on which to provide raw packet support, like "eth0", or None if not requested. '-' enables automatic resolution based on server_address.
  • response_interface_qtags (sequence) – Any qtags to insert into raw packets, in order of appearance. Definitions take the following form: (pcp:0-7, dei:bool, vid:1-4094)
Raises Exception:
 

A problem occurred during setup.

_getNextDHCPPacket(timeout=60, packet_buffer=2048)[source]

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.

Parameters:
  • timeout (int) – The number of seconds to wait before returning.
  • packet_buffer (int) – The size of the buffer to use for receiving packets.
Return tuple(2):
 

(DHCP-packet-received:bool, Address or None on timeout)

_handleDHCPDecline(packet, source_address, port)[source]

Processes a DECLINE packet.

Override this with your own logic to handle DECLINEs.

Parameters:
  • packet (DHCPPacket) – The packet to be processed.
  • source_address (Address) – The address from which the request was received.
  • port (int) – The port on which the packet was received.
_handleDHCPDiscover(packet, source_address, port)[source]

Processes a DISCOVER packet.

Override this with your own logic to handle DISCOVERs.

Parameters:
  • packet (DHCPPacket) – The packet to be processed.
  • source_address (Address) – The address from which the request was received.
  • port (int) – The port on which the packet was received.
_handleDHCPInform(packet, source_address, port)[source]

Processes an INFORM packet.

Override this with your own logic to handle INFORMs.

Parameters:
  • packet (DHCPPacket) – The packet to be processed.
  • source_address (Address) – The address from which the request was received.
  • port (int) – The port on which the packet was received.
_handleDHCPLeaseQuery(packet, source_address, port)[source]

Processes a LEASEQUERY packet.

Override this with your own logic to handle LEASEQUERYs.

Parameters:
  • packet (DHCPPacket) – The packet to be processed.
  • source_address (Address) – The address from which the request was received.
  • port (int) – The port on which the packet was received.
_handleDHCPRelease(packet, source_address, port)[source]

Processes a RELEASE packet.

Override this with your own logic to handle RELEASEs.

Parameters:
  • packet (DHCPPacket) – The packet to be processed.
  • source_address (Address) – The address from which the request was received.
  • port (int) – The port on which the packet was received.
_handleDHCPRequest(packet, source_address, port)[source]

Processes a REQUEST packet.

Override this with your own logic to handle REQUESTs.

Parameters:
  • packet (DHCPPacket) – The packet to be processed.
  • source_address (Address) – The address from which the request was received.
  • port (int) – The port on which the packet was received.
_sendDHCPPacket(packet, source_address, port)[source]

Encodes and sends a DHCP packet to its destination.

Important: during this process, the packet may be modified, but will be restored to its initial state by the time this method returns. If any threadsafing is required, it must be handled in calling logic.

Parameters:
  • packet (DHCPPacket) – The packet to be processed.
  • source_address (Address) – The address from which the request was received.
  • port (int) – The port on which the packet was received.
Return int:

The number of bytes transmitted.

Raises Exception:
 

A problem occurred during serialisation or transmission.

Table Of Contents

Previous topic

Constants