DHCPv4 - Dynamic Host Configuration Protocol

pcapkit.protocols.application.dhcpv4.protocol holds DHCPv4, the extractor for Dynamic Host Configuration Protocol (DHCP) messages [RFC 2131], laid out as protocol gives. Its last field, options, opens with the magic cookie, and the sname and file fields hold options too where the Option Overload option says so (RFC 2131 Section 4.1).

Added in version 1.10.0.

class pcapkit.protocols.application.dhcpv4.protocol.DHCPv4(file, length=None, /, *, layer=None, protocol=None, context=None)[source]

Bases: DHCP[DHCPv4, DHCP]

This class implements the Dynamic Host Configuration Protocol.

A message is a DHCP one where its options carry the DHCP Message Type option (RFC 2131 Section 3), so this class refuses one without it, on both paths; BOOTP reads it. Its options decode as DHCP gives, in the options field, then in the file and sname fields the Option Overload option there overloads, in that order.

Added in version 1.10.0.

Properties

property name: Literal['Dynamic Host Configuration Protocol']

Name of current protocol.

property message_type: MessageType | None

DHCP message type, or None where its option keeps its octets.

Methods

classmethod id()[source]

Index ID of the protocol.

Returns:

RFC 2131 names it plain DHCP, as RFC 792 names ICMPv4’s.

Return type:

tuple[Literal['DHCP']]

read(length=None, **kwargs)[source]

Read Dynamic Host Configuration Protocol.

Structure of a DHCP message [RFC 2131 Section 2]:

 0                   1                   2                   3
 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+---------------+---------------+---------------+---------------+
|     op (1)    |   htype (1)   |   hlen (1)    |   hops (1)    |
+---------------+---------------+---------------+---------------+
|                            xid (4)                            |
+-------------------------------+-------------------------------+
|           secs (2)            |           flags (2)           |
+-------------------------------+-------------------------------+
|                          ciaddr  (4)                          |
+---------------------------------------------------------------+
|                          yiaddr  (4)                          |
+---------------------------------------------------------------+
|                          siaddr  (4)                          |
+---------------------------------------------------------------+
|                          giaddr  (4)                          |
+---------------------------------------------------------------+
|                          chaddr  (16)                         |
+---------------------------------------------------------------+
|                          sname   (64)                         |
+---------------------------------------------------------------+
|                          file    (128)                        |
+---------------------------------------------------------------+
|                          options (variable)                   |
+---------------------------------------------------------------+

An option that runs past its field, as a capture cut leaves one, keeps its octets as the field’s padding, and the options field records the cut as its header schema would.

Parameters:
  • length (int | None) – Length of packet data.

  • **kwargs (Any) – Arbitrary keyword arguments.

Return type:

DHCPv4

Returns:

Parsed packet data.

Raises:

ProtocolError – If no magic cookie opens the options field, or the options carry no DHCP Message Type option whole before any option runs past its field.

make(*, op=Opcode.BOOTREQUEST, htype=Hardware.Ethernet, hlen=None, hops=0, xid=0, secs=0, flags=0, ciaddr='0.0.0.0', yiaddr='0.0.0.0', siaddr='0.0.0.0', giaddr='0.0.0.0', chaddr=b'', chaddr_padding=None, sname=b'', file=b'', options=(), padding=b'', payload=b'', **kwargs)[source]

Make (construct) packet data.

Parameters:
  • op (Opcode | int) – Message op code.

  • htype (Hardware | int) – Hardware address type.

  • hlen (int | None) – Hardware address length; that of chaddr if None.

  • hops (int) – Hops.

  • xid (int) – Transaction ID.

  • secs (int) – Seconds elapsed since the client began.

  • flags (Mapping[str, Any] | int) – Flags, as their data model or a mapping of its keys, or the two octets as an integer.

  • ciaddr (IPv4Address | str | bytes | int) – Client IP address.

  • yiaddr (IPv4Address | str | bytes | int) – Your (client) IP address.

  • siaddr (IPv4Address | str | bytes | int) – Next server IP address.

  • giaddr (IPv4Address | str | bytes | int) – Relay agent IP address.

  • chaddr (str | bytes) – Client hardware address: a MAC address for an Ethernet or IEEE 802 type, or its octets.

  • chaddr_padding (bytes | None) – Octets of chaddr after the address; zeros filling the field if None.

  • sname (bytes | Mapping[str, Any]) – Server host name, which NULs fill out, or the options it holds, as OverloadedField holds them.

  • file (bytes | Mapping[str, Any]) – Boot file name, which NULs fill out, or the options it holds, likewise.

  • options (Iterable[Mapping[str, Any]]) – Options, each a mapping as Option holds it, written after the magic cookie.

  • padding (bytes) – Octets after the End option, or with none, the option the field ends inside.

  • payload (bytes | ProtocolBase | Schema) – Octets after the message.

  • **kwargs (Any) – Arbitrary keyword arguments.

Return type:

DHCP

Returns:

Constructed packet data.

Raises:

ProtocolError – If a field does not fit its width; if an option is malformed; if sname or file holds options where the Option Overload option does not say so, or text where it does; or if the options carry no DHCP Message Type option.

Utilities

classmethod _make_data(data)[source]

Create key-value pairs from data for protocol construction.

Parameters:

data (DHCPv4) – protocol data

Return type:

dict[str, Any]

Returns:

Key-value pairs for protocol construction.

Data Models

class pcapkit.protocols.application.dhcpv4.data.DHCPv4(op, htype, hlen, hops, xid, secs, flags, ciaddr, yiaddr, siaddr, giaddr, chaddr, chaddr_padding, sname, file, magic_cookie, options, padding)[source]

Bases: Protocol

Data model for a DHCP message (RFC 2131 Section 2).

op: Opcode

Message op code.

htype: Hardware

Hardware address type.

hlen: int

Hardware address length.

hops: int

Hops.

xid: int

Transaction ID.

secs: int

Seconds elapsed since the client began.

flags: Flags

Flags.

ciaddr: IPv4Address

Client IP address.

yiaddr: IPv4Address

Your (client) IP address.

siaddr: IPv4Address

Next server IP address.

giaddr: IPv4Address

Relay agent IP address.

chaddr: str | bytes

Client hardware address, its first hlen octets; a MAC address for an Ethernet or IEEE 802 type of six octets.

chaddr_padding: bytes

The octets of chaddr after the address.

sname: bytes | OverloadedField

Server host name, its trailing NULs removed, or the options it holds where the Option Overload option says so.

file: bytes | OverloadedField

Boot file name, its trailing NULs removed, or the options it holds where the Option Overload option says so.

The magic cookie opening the options field.

options: tuple[Option, ...]

Options.

padding: bytes

Octets after the End option, or from the option the field ends inside.