DHCP - Dynamic Host Configuration Protocol

pcapkit.protocols.application.dhcp.protocol holds DHCP, the abstract base of the Bootstrap Protocol (BOOTP) [RFC 951, RFC 1542] and the Dynamic Host Configuration Protocol (DHCP) [RFC 2131], whose messages share one layout:

Octets

Bits

Name

Description

0

0

dhcp.op

Message Op Code

1

8

dhcp.htype

Hardware Address Type

2

16

dhcp.hlen

Hardware Address Length

3

24

dhcp.hops

Hops

4

32

dhcp.xid

Transaction ID

8

64

dhcp.secs

Seconds Elapsed

10

80

dhcp.flags

Flags

12

96

dhcp.ciaddr

Client IP Address

16

128

dhcp.yiaddr

Your (Client) IP Address

20

160

dhcp.siaddr

Next Server IP Address

24

192

dhcp.giaddr

Relay Agent IP Address

28

224

dhcp.chaddr

Client Hardware Address

44

352

dhcp.sname

Server Host Name

108

864

dhcp.file

Boot File Name

236

1888

BOOTP’s vend or DHCP’s options

Each is a member of its own:

Message

Class

Specification

BOOTP

BOOTP

RFC 951, RFC 1542, and the vendor extensions of RFC 2132 sections 3 to 8

DHCP

DHCPv4

RFC 2131, and the options of RFC 2132 and its successors

A DHCP message is the one whose options carry the DHCP Message Type option (RFC 2131 Section 3, RFC 1534 Section 2); no field gives a version. UDP ports 67 and 68 carry either, so UDP.__proto__ binds this base and it picks the member, by _classify().

Added in version 1.10.0.

class pcapkit.protocols.application.dhcp.protocol.DHCP[_PT: Data, _ST: Schema](file, length=None, /, *, layer=None, protocol=None, context=None)[source]

Bases: Application[_PT, _ST], Generic[_PT, _ST]

This class implements the BOOTP and DHCP family.

It is abstract, as STP is, and never gives an instance of this class. Octets give the member _classify() picks; no field names a member, since the family has no version, so fields are refused here and a member builds itself; from_data() reads the data model’s class. See __new__().

The options either member decodes are those __option__ registers, each exactly as its specification defines it; a BOOTP message decodes the vendor extensions alone (VENDOR_EXTENSIONS). An option whose data its decoder refuses, and every instance of an option the message carries more than once, which RFC 3396 Section 7 concatenates before any is read, keep their octets; decoding the concatenation is #3200.

The data each option carries:

Option

Data Model

Subnet Mask, Swap Server, Broadcast Address, Router Solicitation Address, Requested IP Address, Server Identifier, Subnet Selection

Address

Router and the other server lists of RFC 2132, Mobile IP Home Agent

Addresses

Time Offset, Boot File Size, the timers, TTLs and sizes, IPv6-Only Preferred

Number

The on/off options of RFC 2132, Auto-Configure

Boolean

Host Name and the other text options of RFC 2132, PCode, TCode, Captive-Portal

Text

Policy Filter

PolicyFilter

Path MTU Plateau Table

Sizes

Static Route

StaticRoutes

NetBIOS over TCP/IP Node Type

NodeType

Option Overload

Overload

DHCP Message Type

MessageType

Parameter Request List

ParameterList

Client-identifier

ClientIdentifier

User Class (RFC 3004)

UserClass

Client FQDN (RFC 4702)

ClientFQDN

Relay Agent Information (RFC 3046)

RelayAgentInformation

Client System Architecture (RFC 4578)

ArchitectureTypes

Client Network Interface Identifier (RFC 4578)

ClientNDI

Client Machine Identifier (RFC 4578)

MachineIdentifier

Domain Search (RFC 3397)

DomainSearch

Classless Static Route (RFC 3442)

ClasslessStaticRoutes

Vendor-Identifying Vendor Class (RFC 3925)

VendorClass

Vendor-Identifying Vendor-Specific Information (RFC 3925)

VendorSpecific

Any other option, the opaque Vendor Specific Information and Vendor Class Identifier of RFC 2132 among them, keeps its octets.

Note

The subpackage does not track the dispatch tier. The family is dispatched from UDP.__proto__ at ports 67 and 68; it configures a host rather than relaying its packets, so it is a user of the stack, as RFC 1122 Section 1.1.3 places booting.

Defaults

__option__: DefaultDict[Option | int, str | tuple[Callable[[Option, bytes], OptionData | bytes], Callable[[Option, Mapping[str, Any]], bytes]]]

Option code to method mapping, which both members read through. A str value names the methods _read_opt_${name} and _make_opt_${name}, falling back to _read_opt_unknown() and _make_opt_unknown(); any other value is a (parser, constructor) pair of callables.

Properties

property length: int

Header length of current protocol, i.e. the whole message.

property alias: str

Acronym of current protocol, 'BOOTP' or 'DHCP'.

property info_name: Literal['dhcp']

Key name of the info dict.

Methods

classmethod id()[source]

Index ID of the protocol.

RFC 2131 names its protocol plain DHCP, so the family’s own name doubles as the canonical name of DHCPv4, the shape ICMP.id takes.

Return type:

tuple[Literal['DHCP'], Literal['BOOTP']]

Returns:

Index ID of the protocol, the family name and BOOTP’s; each member narrows it to its own canonical name.

classmethod from_data(data, /, **kwargs)[source]

Create protocol instance from data.

Parameters:
Return type:

Self

Returns:

Protocol instance; on this class, an instance of the member whose data model data is.

Raises:

ProtocolError – If data is no member’s data model, on this class. A dict is refused, since no key picks the member.

classmethod register_option(code, meth)[source]

Register an option parser.

Parameters:
Warns:

RegistryWarning – If a different entry is already registered for code; it is overwritten. Handing back the entry already stored, or one equal to it, is a silent no-op.

Data Models

classmethod __index__()[source]

Numeral registry index of the protocol.

Return type:

int

Returns:

Numeral registry index of the protocol in IANA service names and port numbers: port 67, the port of UDP.bootps as an int, which every member inherits (The Registry Index).

__length_hint__()[source]

Return an estimated length for the object, the header and the magic cookie.

Return type:

int

static __new__(cls, file=NO_VALUE, length=None, /, **kwargs)[source]

Create the member a parse of this class describes.

Octets give the member _classify() picks, whose __init__ then runs with the same arguments; fields are refused, as no field names a member. A subclass of this class is created as itself.

Parameters:
  • file (IO[bytes] | bytes | None | NoValueType) – Source packet stream, read here without being consumed; omitted to construct.

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

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

Return type:

DHCP

Returns:

An uninitialised instance of the member.

Raises:

Utilities

_read_opt_unknown(code, data)[source]

Read an option with no decoder registered: its raw octets.

Parameters:
  • code (Option) – Option code.

  • data (bytes) – Option data.

Return type:

bytes

Returns:

data itself.

_read_opt_address(code, data)[source]

Read an option carrying one IPv4 address, four octets (RFC 2132).

Parameters:
  • code (Option) – Option code.

  • data (bytes) – Option data.

Return type:

Address

Returns:

Parsed option data.

Raises:

ProtocolError – If data is not four octets.

_read_opt_time_offset(code, data)[source]

Read the Time Offset option, a signed 32-bit integer (RFC 2132 Section 3.4).

Parameters:
  • code (Option) – Option code.

  • data (bytes) – Option data.

Return type:

Number

Returns:

Parsed option data.

Raises:

ProtocolError – If data is not four octets.

_read_opt_addresses(code, data)[source]

Read an option carrying one IPv4 address or more (RFC 2132).

Parameters:
  • code (Option) – Option code.

  • data (bytes) – Option data.

Return type:

Addresses

Returns:

Parsed option data.

Raises:

ProtocolError – If data is empty or not a multiple of four octets.

_read_opt_text(code, data)[source]

Read an option carrying ASCII text, one octet or more (RFC 2132 Section 2).

Parameters:
  • code (Option) – Option code.

  • data (bytes) – Option data.

Return type:

Text

Returns:

Parsed option data.

Raises:

ProtocolError – If data is empty or not ASCII.

_read_opt_uint16(code, data)[source]

Read an option carrying a 16-bit unsigned integer (RFC 2132).

Parameters:
  • code (Option) – Option code.

  • data (bytes) – Option data.

Return type:

Number

Returns:

Parsed option data.

Raises:

ProtocolError – If data is not two octets, or the value is below the least MINIMUMS gives the option.

_read_opt_boolean(code, data)[source]

Read an option carrying an octet of 0 or 1 (RFC 2132, RFC 2563).

Parameters:
  • code (Option) – Option code.

  • data (bytes) – Option data.

Return type:

Boolean

Returns:

Parsed option data.

Raises:

ProtocolError – If data is not one octet of 0 or 1.

_read_opt_policy_filter(code, data)[source]

Read the Policy Filter option (RFC 2132 Section 4.3).

Parameters:
  • code (Option) – Option code.

  • data (bytes) – Option data.

Return type:

PolicyFilter

Returns:

Parsed option data.

Raises:

ProtocolError – If data is empty or not a multiple of eight octets.

_read_opt_uint8(code, data)[source]

Read an option carrying an 8-bit unsigned integer (RFC 2132).

Parameters:
  • code (Option) – Option code.

  • data (bytes) – Option data.

Return type:

Number

Returns:

Parsed option data.

Raises:

ProtocolError – If data is not one octet, or the value is below the least MINIMUMS gives the option.

_read_opt_uint32(code, data)[source]

Read an option carrying a 32-bit unsigned integer (RFC 2132, RFC 8925).

Parameters:
  • code (Option) – Option code.

  • data (bytes) – Option data.

Return type:

Number

Returns:

Parsed option data.

Raises:

ProtocolError – If data is not four octets.

_read_opt_mtu_plateau(code, data)[source]

Read the Path MTU Plateau Table option (RFC 2132 Section 4.7).

Parameters:
  • code (Option) – Option code.

  • data (bytes) – Option data.

Return type:

Sizes

Returns:

Parsed option data.

Raises:

ProtocolError – If data is empty or not a multiple of two octets, or a size is below 68.

_read_opt_static_route(code, data)[source]

Read the Static Route option (RFC 2132 Section 5.8).

Parameters:
  • code (Option) – Option code.

  • data (bytes) – Option data.

Return type:

StaticRoutes

Returns:

Parsed option data.

Raises:

ProtocolError – If data is empty or not a multiple of eight octets.

_read_opt_node_type(code, data)[source]

Read the NetBIOS over TCP/IP Node Type option (RFC 2132 Section 8.7).

Parameters:
  • code (Option) – Option code.

  • data (bytes) – Option data.

Return type:

NodeType

Returns:

Parsed option data.

Raises:

ProtocolError – If data is not one octet of 0x1, 0x2, 0x4 or 0x8.

_read_opt_overload(code, data)[source]

Read the Option Overload option (RFC 2132 Section 9.3).

Parameters:
  • code (Option) – Option code.

  • data (bytes) – Option data.

Return type:

Overload

Returns:

Parsed option data.

Raises:

ProtocolError – If data is not one octet of 1, 2 or 3.

_read_opt_message_type(code, data)[source]

Read the DHCP Message Type option (RFC 2132 Section 9.6).

Parameters:
  • code (Option) – Option code.

  • data (bytes) – Option data.

Return type:

MessageType

Returns:

Parsed option data.

Raises:

ProtocolError – If data is not one octet.

_read_opt_parameter_list(code, data)[source]

Read the Parameter Request List option (RFC 2132 Section 9.8).

Parameters:
  • code (Option) – Option code.

  • data (bytes) – Option data.

Return type:

ParameterList

Returns:

Parsed option data.

Raises:

ProtocolError – If data is empty.

_read_opt_client_id(code, data)[source]

Read the Client-identifier option (RFC 2132 Section 9.14).

Parameters:
  • code (Option) – Option code.

  • data (bytes) – Option data.

Return type:

ClientIdentifier

Returns:

Parsed option data.

Raises:

ProtocolError – If data is shorter than two octets.

_read_opt_home_agent(code, data)[source]

Read the Mobile IP Home Agent option, of zero addresses or more (RFC 2132 Section 8.10).

Parameters:
  • code (Option) – Option code.

  • data (bytes) – Option data.

Return type:

Addresses

Returns:

Parsed option data.

Raises:

ProtocolError – If data is not a multiple of four octets.

_read_opt_user_class(code, data)[source]

Read the User Class option (RFC 3004 Section 4).

Parameters:
  • code (Option) – Option code.

  • data (bytes) – Option data.

Return type:

UserClass

Returns:

Parsed option data.

Raises:

ProtocolError – If data is empty, an instance is empty, or one runs past data.

_read_opt_client_fqdn(code, data)[source]

Read the Client FQDN option (RFC 4702 Section 2).

The domain name is in canonical wire format, uncompressed, where the E flag is set, and may be partial, without the root label; else it is ASCII text, as RFC 4702 Section 2.3.1 keeps for older clients.

Parameters:
  • code (Option) – Option code.

  • data (bytes) – Option data.

Return type:

ClientFQDN

Returns:

Parsed option data.

Raises:

ProtocolError – If data is shorter than three octets, or the domain name is malformed.

_read_opt_relay_agent(code, data)[source]

Read the Relay Agent Information option (RFC 3046 Section 2.0).

Each sub-option’s data is kept as octets, its format being its own specification’s.

Parameters:
  • code (Option) – Option code.

  • data (bytes) – Option data.

Return type:

RelayAgentInformation

Returns:

Parsed option data.

Raises:

ProtocolError – If data is empty, or a sub-option runs past it.

_read_opt_client_system(code, data)[source]

Read the Client System Architecture Type option (RFC 4578 Section 2.1).

Parameters:
  • code (Option) – Option code.

  • data (bytes) – Option data.

Return type:

ArchitectureTypes

Returns:

Parsed option data.

Raises:

ProtocolError – If data is empty or not a multiple of two octets.

_read_opt_client_ndi(code, data)[source]

Read the Client Network Interface Identifier option (RFC 4578 Section 2.2).

Parameters:
  • code (Option) – Option code.

  • data (bytes) – Option data.

Return type:

ClientNDI

Returns:

Parsed option data.

Raises:

ProtocolError – If data is not three octets.

_read_opt_machine_id(code, data)[source]

Read the Client Machine Identifier option (RFC 4578 Section 2.3).

Parameters:
  • code (Option) – Option code.

  • data (bytes) – Option data.

Return type:

MachineIdentifier

Returns:

Parsed option data.

Raises:

ProtocolError – If data is not 17 octets.

Read the Domain Search option (RFC 3397 Section 2).

Its names are written as RFC 1035 Section 3.1 writes them, and a compression pointer is an offset into the option’s data.

Parameters:
  • code (Option) – Option code.

  • data (bytes) – Option data.

Return type:

DomainSearch

Returns:

Parsed option data.

Raises:

ProtocolError – If data is empty or a name is malformed.

_read_opt_classless_route(code, data)[source]

Read the Classless Static Route option (RFC 3442 Section 2).

Each route is its prefix length, the significant octets of its destination and its router.

Parameters:
  • code (Option) – Option code.

  • data (bytes) – Option data.

Return type:

ClasslessStaticRoutes

Returns:

Parsed option data.

Raises:

ProtocolError – If data is shorter than five octets, a prefix length exceeds 32, a route runs past data, or a destination sets a bit past its prefix.

_read_opt_vendor_class(code, data)[source]

Read the Vendor-Identifying Vendor Class option (RFC 3925 Section 3).

Parameters:
  • code (Option) – Option code.

  • data (bytes) – Option data.

Return type:

VendorClass

Returns:

Parsed option data.

Raises:

ProtocolError – If data is empty, or a vendor’s data or an item of it runs past its end.

_read_opt_vendor_specific(code, data)[source]

Read the Vendor-Identifying Vendor-Specific Information option (RFC 3925 Section 4).

Parameters:
  • code (Option) – Option code.

  • data (bytes) – Option data.

Return type:

VendorSpecific

Returns:

Parsed option data.

Raises:

ProtocolError – If data is empty, or a vendor’s data or a sub-option of it runs past its end.

_make_opt_unknown(code, data)[source]

Make an option with no constructor registered.

Parameters:
  • code (Option) – Option code.

  • data (Mapping[str, Any]) – Option fields; none can be written.

Return type:

bytes

Returns:

Empty option data, for no fields.

Raises:

ProtocolError – If data holds a field.

_make_opt_address(code, data)[source]

Make an option carrying one IPv4 address.

Parameters:
Return type:

bytes

Returns:

Option data.

_make_opt_time_offset(code, data)[source]

Make the Time Offset option.

Parameters:
  • code (Option) – Option code.

  • data (Mapping[str, Any]) – Option fields, i.e. value, in seconds.

Return type:

bytes

Returns:

Option data.

_make_opt_addresses(code, data)[source]

Make an option carrying one IPv4 address or more.

Parameters:
Return type:

bytes

Returns:

Option data.

Raises:

ProtocolError – If addresses is empty.

_make_opt_text(code, data)[source]

Make an option carrying ASCII text.

Parameters:
Return type:

bytes

Returns:

Option data.

Raises:

ProtocolError – If text is empty.

_make_opt_uint16(code, data)[source]

Make an option carrying a 16-bit unsigned integer.

Parameters:
Return type:

bytes

Returns:

Option data.

_make_opt_boolean(code, data)[source]

Make an option carrying an octet of 0 or 1.

Parameters:
Return type:

bytes

Returns:

Option data.

_make_opt_policy_filter(code, data)[source]

Make the Policy Filter option.

Parameters:
  • code (Option) – Option code.

  • data (Mapping[str, Any]) – Option fields, i.e. filters, each an address and a mask.

Return type:

bytes

Returns:

Option data.

_make_opt_uint8(code, data)[source]

Make an option carrying an 8-bit unsigned integer.

Parameters:
Return type:

bytes

Returns:

Option data.

_make_opt_uint32(code, data)[source]

Make an option carrying a 32-bit unsigned integer.

Parameters:
Return type:

bytes

Returns:

Option data.

_make_opt_mtu_plateau(code, data)[source]

Make the Path MTU Plateau Table option.

Parameters:
Return type:

bytes

Returns:

Option data.

Raises:

ProtocolError – If sizes is empty or a size is below 68.

_make_opt_static_route(code, data)[source]

Make the Static Route option.

Parameters:
  • code (Option) – Option code.

  • data (Mapping[str, Any]) – Option fields, i.e. routes, each a destination and a router.

Return type:

bytes

Returns:

Option data.

_make_opt_node_type(code, data)[source]

Make the NetBIOS over TCP/IP Node Type option.

Parameters:
Return type:

bytes

Returns:

Option data.

Raises:

ProtocolError – If node_type is none of 0x1, 0x2, 0x4 and 0x8.

_make_opt_overload(code, data)[source]

Make the Option Overload option.

Parameters:
  • code (Option) – Option code.

  • data (Mapping[str, Any]) – Option fields, i.e. file and sname.

Return type:

bytes

Returns:

Option data.

Raises:

ProtocolError – If neither field is overloaded.

_make_opt_message_type(code, data)[source]

Make the DHCP Message Type option.

Parameters:
Return type:

bytes

Returns:

Option data.

_make_opt_parameter_list(code, data)[source]

Make the Parameter Request List option.

Parameters:
Return type:

bytes

Returns:

Option data.

Raises:

ProtocolError – If codes is empty.

_make_opt_client_id(code, data)[source]

Make the Client-identifier option.

Parameters:
  • code (Option) – Option code.

  • data (Mapping[str, Any]) – Option fields, i.e. type and identifier.

Return type:

bytes

Returns:

Option data.

Raises:

ProtocolError – If identifier is empty.

_make_opt_home_agent(code, data)[source]

Make the Mobile IP Home Agent option.

Parameters:
  • code (Option) – Option code.

  • data (Mapping[str, Any]) – Option fields, i.e. addresses, which may be none.

Return type:

bytes

Returns:

Option data.

_make_opt_user_class(code, data)[source]

Make the User Class option.

Parameters:
Return type:

bytes

Returns:

Option data.

Raises:

ProtocolError – If classes is empty, or holds an empty instance.

_make_opt_client_fqdn(code, data)[source]

Make the Client FQDN option.

Parameters:
  • code (Option) – Option code.

  • data (Mapping[str, Any]) – Option fields, i.e. flags, rcode1, rcode2 and name, the last written in canonical wire format where the E flag is set.

Return type:

bytes

Returns:

Option data.

_make_opt_relay_agent(code, data)[source]

Make the Relay Agent Information option.

Parameters:
  • code (Option) – Option code.

  • data (Mapping[str, Any]) – Option fields, i.e. suboptions, each a code, a data and, when it is not that of data, a length.

Return type:

bytes

Returns:

Option data.

Raises:

ProtocolError – If suboptions is empty, or a sub-option is no mapping or its data is no octets.

_make_opt_client_system(code, data)[source]

Make the Client System Architecture Type option.

Parameters:
Return type:

bytes

Returns:

Option data.

Raises:

ProtocolError – If types is empty.

_make_opt_client_ndi(code, data)[source]

Make the Client Network Interface Identifier option.

Parameters:
  • code (Option) – Option code.

  • data (Mapping[str, Any]) – Option fields, i.e. type, major and minor.

Return type:

bytes

Returns:

Option data.

_make_opt_machine_id(code, data)[source]

Make the Client Machine Identifier option.

Parameters:
  • code (Option) – Option code.

  • data (Mapping[str, Any]) – Option fields, i.e. type and the 16-octet identifier.

Return type:

bytes

Returns:

Option data.

Make the Domain Search option.

Parameters:
  • code (Option) – Option code.

  • data (Mapping[str, Any]) – Option fields, i.e. names: each a DomainName, which writes the compression it records, or its presentation form, written in full.

Return type:

bytes

Returns:

Option data.

Raises:

ProtocolError – If names is empty, or a name is malformed.

_make_opt_classless_route(code, data)[source]

Make the Classless Static Route option.

Parameters:
  • code (Option) – Option code.

  • data (Mapping[str, Any]) – Option fields, i.e. routes, each a destination network and a router.

Return type:

bytes

Returns:

Option data.

Raises:

ProtocolError – If routes is empty, or a destination is no IPv4 network.

_make_opt_vendor_class(code, data)[source]

Make the Vendor-Identifying Vendor Class option.

Parameters:
  • code (Option) – Option code.

  • data (Mapping[str, Any]) – Option fields, i.e. vendors, each an enterprise number and its classes.

Return type:

bytes

Returns:

Option data.

_make_opt_vendor_specific(code, data)[source]

Make the Vendor-Identifying Vendor-Specific Information option.

Parameters:
  • code (Option) – Option code.

  • data (Mapping[str, Any]) – Option fields, i.e. vendors, each an enterprise number and its options, each a code and a data.

Return type:

bytes

Returns:

Option data.

classmethod _construction_member(fields)[source]

The class from_fields() and a call given no file construct.

Parameters:

fields (dict[str, Any]) – Construction keywords.

Return type:

Type[Self]

Returns:

The class itself, which must be a member.

Raises:

ProtocolError – On this class, since no field names the member.

classmethod _classify(data)[source]

Pick the member a message is.

A message whose options carry the DHCP Message Type option is a DHCP message (RFC 1534 Section 2): the options field, opening with the magic cookie, then the file and sname fields an Option Overload option there says hold options (RFC 2131 Section 4.1), read in that order. The option counts where it is whole before any option runs past its field, as a capture cut leaves one; any other message is a BOOTP message.

Parameters:

data (bytes) – The message, its octets alone.

Return type:

Type[DHCP]

Returns:

DHCPv4 or BOOTP.

static _split_options(area)[source]

Split an option area into its options (RFC 2132 Section 2).

The Pad and End options are one octet; every other option is its code, its length octet and that many octets of data. The area ends at the End option, where its octets do, or at an option that runs past them.

Parameters:

area (bytes) – The option area, after any magic cookie.

Return type:

tuple[tuple[Tuple[int, bytes | None], ...], int, bool]

Returns:

The whole options, each its code and data, the latter None for the Pad and End options; the offset past the End option, of the option that runs past the area, or the area’s length; and whether an option runs past the area.

static _overloaded_fields(entries)[source]

The fields an Option Overload option says hold options, in the order they are read (RFC 2131 Section 4.1).

Parameters:

entries (Iterable[Tuple[int, bytes | None]]) – The options of the options field.

Return type:

tuple[tuple[int, int], ...]

Returns:

The offsets of the file field, then of the sname field, in the message, each where the one Option Overload option, of a valid value, says it holds options.

_read_dhcp_header()[source]

Read the fields a BOOTP and a DHCP message share, op through chaddr.

Return type:

dict[str, Any]

Returns:

The fields, as their data models’ keys; length is set.

_read_dhcp_options(entries, counts, codes=None)[source]

Decode the options of an area.

Each option’s data is read by the parser __option__ registers for its code, and kept as raw octets where that parser rejects it, where codes leaves the code out, or where counts has the code more than once, as RFC 3396 Section 7 concatenates such instances.

Parameters:
Return type:

tuple[Option, ...]

Returns:

The options, in order.

static _count_options(*areas)[source]

Count the instances of each option code in the message.

Parameters:

*areas (Iterable[Tuple[int, bytes | None]]) – The options of each area that holds options.

Return type:

Counter[int]

Returns:

How many times each code other than Pad and End occurs.

_make_dhcp_header(*, op, htype, hlen, hops, xid, secs, flags, ciaddr, yiaddr, siaddr, giaddr, chaddr, chaddr_padding)[source]

Make the fields a BOOTP and a DHCP message share, op through chaddr.

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.

  • 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, or its octets.

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

Return type:

dict[str, Any]

Returns:

The schema’s keyword arguments.

Raises:

ProtocolError – If a field does not fit its width, or the address and its padding are not 16 octets.

_make_dhcp_field(value, *, size, name)[source]

Make the sname or file field from its text.

Parameters:
  • value (bytes) – The text, which NULs fill out to size octets.

  • size (int) – Octets in the field.

  • name (str) – Name of the field, for the error message.

Return type:

bytes

Returns:

The field’s octets.

Raises:

ProtocolError – If value is no octets, or longer than size.

_make_dhcp_options(options, padding, *, codes=None, size=None)[source]

Make an option area.

Parameters:
  • options (Iterable[Mapping[str, Any]]) – Each option’s code, length and data, as Option holds them; data is raw octets, a mapping of the fields of the code’s data model, or None for the Pad and End options, and length is computed when not given.

  • padding (bytes) – Octets after the End option, or with none, an option the area ends inside, as _split_options() reads one.

  • codes (frozenset[int] | None) – The codes a mapping may build, or None for all.

  • size (int | None) – Octets the area fills, for an overloaded sname or file, or None for the options field.

Return type:

bytes

Returns:

The area’s octets.

Raises:

ProtocolError – If an option is malformed; if its data is a mapping for a code codes leaves out, or that options holds more than once; if padding follows no End option yet is no option the area ends inside, which for size has to run to its end; or if the area does not fit size.

_read_dhcp_addresses(code, data)[source]

Read a run of IPv4 addresses.

Parameters:
  • code (Option) – Option code.

  • data (bytes) – The addresses’ octets.

Return type:

tuple[IPv4Address, ...]

Returns:

The addresses, in order.

Raises:

ProtocolError – If data is not a multiple of four octets.

_read_dhcp_text(code, data)[source]

Read ASCII text.

Parameters:
  • code (Option) – Option code.

  • data (bytes) – The text’s octets.

Return type:

str

Returns:

The text.

Raises:

ProtocolError – If data is not ASCII.

_read_dhcp_uint(code, data, *, size)[source]

Read an unsigned integer, no less than the least its option takes.

Parameters:
  • code (Option) – Option code.

  • data (bytes) – The integer’s octets.

  • size (int) – Octets in the integer.

Return type:

int

Returns:

The integer.

Raises:

ProtocolError – If data is not size octets, or the value is below the least MINIMUMS gives the option.

_read_dhcp_pairs(code, data)[source]

Read a run of IPv4 address pairs.

Parameters:
  • code (Option) – Option code.

  • data (bytes) – The pairs’ octets.

Return type:

tuple[IPv4Address, ...]

Returns:

The addresses, in order, two per pair.

Raises:

ProtocolError – If data is empty or not a multiple of eight octets.

_read_hardware_address(htype, data)[source]

Read a hardware address.

Parameters:
  • htype (Hardware | int) – Hardware type.

  • data (bytes) – The address’s octets.

Return type:

str | bytes

Returns:

A MAC address for an Ethernet or IEEE 802 type of six octets, the octets otherwise.

_read_dhcp_vectors(code, data)[source]

Read a run of vectors, each one octet of length and that many octets.

Parameters:
  • code (Option) – Option code.

  • data (bytes) – The vectors’ octets.

Return type:

tuple[bytes, ...]

Returns:

The vectors, in order.

Raises:

ProtocolError – If a vector runs past data.

_read_fqdn_name(code, data)[source]

Read the domain name of the Client FQDN option, in canonical wire format.

Parameters:
  • code (Option) – Option code.

  • data (bytes) – The name’s octets.

Return type:

str

Returns:

The name in presentation form, ending in . where the root label ends it, i.e. where it is fully qualified.

Raises:

ProtocolError – If a label is longer than 63 octets or runs past data, or octets follow the root label.

_read_dhcp_vendors(code, data)[source]

Read the per-vendor data of a Vendor-Identifying option (RFC 3925).

Parameters:
  • code (Option) – Option code.

  • data (bytes) – Option data.

Return type:

tuple[tuple[int, bytes], ...]

Returns:

Each vendor’s enterprise number and data.

Raises:

ProtocolError – If data is empty, or a vendor’s data runs past it.

_make_dhcp_address(code, value)[source]

Make an IPv4 address.

Parameters:
  • code (Option) – Option code.

  • value (Any) – The address.

Return type:

bytes

Returns:

Its four octets.

_make_dhcp_text(code, value)[source]

Make ASCII text.

Parameters:
  • code (Option) – Option code.

  • value (Any) – The text.

Return type:

bytes

Returns:

Its octets.

Raises:

ProtocolError – If value is no ASCII str.

_make_dhcp_uint(code, value, *, size)[source]

Make an unsigned integer, no less than the least its option takes.

Parameters:
  • code (Option) – Option code.

  • value (Any) – The integer.

  • size (int) – Octets in the integer.

Return type:

bytes

Returns:

Its octets.

Raises:

ProtocolError – If value does not fit, or is below the least MINIMUMS gives the option.

_make_dhcp_pairs(code, pairs, keys)[source]

Make a run of IPv4 address pairs.

Parameters:
Return type:

bytes

Returns:

The pairs’ octets.

Raises:

ProtocolError – If pairs is empty or a pair is no mapping.

_make_hardware_address(htype, value, *, name)[source]

Make a hardware address.

Parameters:
  • htype (Hardware | int) – Hardware type.

  • value (Any) – A MAC address for an Ethernet or IEEE 802 type, or the octets.

  • name (str) – Name of the address, for the error message.

Return type:

bytes

Returns:

The address’s octets.

Raises:

ProtocolError – If value is neither.

_make_dhcp_vectors(code, vectors)[source]

Make a run of vectors, each one octet of length and that many octets.

Parameters:
Return type:

bytes

Returns:

Their octets.

Raises:

ProtocolError – If a vector is no octets or longer than 255.

_make_fqdn_name(code, value)[source]

Make the domain name of the Client FQDN option, in canonical wire format.

Parameters:
  • code (Option) – Option code.

  • value (Any) – The name in presentation form, ending in . where it is fully qualified.

Return type:

bytes

Returns:

The name’s octets, the root label ending it where it is fully qualified.

Raises:

ProtocolError – If value is no str, or is malformed.

_make_dhcp_vendors(code, vendors, body)[source]

Make the per-vendor data of a Vendor-Identifying option (RFC 3925).

Parameters:
Return type:

bytes

Returns:

Option data.

Raises:

ProtocolError – If vendors is empty, a vendor is no mapping, or its data is longer than 255 octets.

Header Schemas

class pcapkit.protocols.application.dhcp.schema.DHCP(op=NO_VALUE, htype=NO_VALUE, hlen=NO_VALUE, hops=NO_VALUE, xid=NO_VALUE, secs=NO_VALUE, flags=NO_VALUE, ciaddr=NO_VALUE, yiaddr=NO_VALUE, siaddr=NO_VALUE, giaddr=NO_VALUE, chaddr=NO_VALUE, sname=NO_VALUE, file=NO_VALUE, options=NO_VALUE, payload=NO_VALUE, *, __packet__=None, **kwargs)[source]

Bases: Schema

Header schema for a BOOTP or DHCP message.

op: int = <UInt8Field op>

Message op code.

htype: int = <UInt8Field htype>

Hardware address type.

hlen: int = <UInt8Field hlen>

Hardware address length.

hops: int = <UInt8Field hops>

Hops.

xid: int = <UInt32Field xid>

Transaction ID.

secs: int = <UInt16Field secs>

Seconds elapsed since the client began.

flags: Flags = <BitField flags>

Flags (RFC 1542 Section 3.1.1).

ciaddr: IPv4Address = <IPv4AddressField ciaddr>

Client IP address.

yiaddr: IPv4Address = <IPv4AddressField yiaddr>

Your (client) IP address.

siaddr: IPv4Address = <IPv4AddressField siaddr>

Next server IP address.

giaddr: IPv4Address = <IPv4AddressField giaddr>

Relay agent IP address.

chaddr: bytes = <BytesField chaddr>

Client hardware address.

sname: bytes = <BytesField sname>

Server host name.

file: bytes = <BytesField file>

Boot file name.

options: bytes = <BytesField options>

BOOTP’s vendor area or DHCP’s options, undecoded.

payload: bytes = <PayloadField payload>

Octets after the message, which it does not define.

Type Stubs

class pcapkit.protocols.application.dhcp.schema.Flags[source]

Bases: TypedDict

BOOTP and DHCP header flags.

broadcast: int

Broadcast flag.

reserved: int

Remaining bits, which must be zero.

Auxiliary Functions

pcapkit.protocols.application.dhcp.schema.dhcp_area_length(packet)[source]

Length of DHCP.options.

Parameters:

packet (dict[str, Any]) – Packet data.

Return type:

int

Returns:

The rest of the data; packing, where __length__ is unset, the area as given.

Data Models

class pcapkit.protocols.application.dhcp.data.Flags(broadcast, reserved)[source]

Bases: Data

Data model for the header flags (RFC 1542 Section 3.1.1, RFC 2131 Section 2).

broadcast: bool

Broadcast flag, the leftmost bit.

reserved: int

The other 15 bits, which must be zero, as captured.

class pcapkit.protocols.application.dhcp.data.Option(code, length, data)[source]

Bases: Data

Data model for an option (RFC 2132 Section 2).

data holds the decoded data where the option’s parser registers a decoder for code and reads it exactly, and the raw octets otherwise.

code: Option

Option code.

length: int | None

Length octet, or None for the Pad and End options, which have none.

data: OptionData | bytes | None

Data, decoded or as raw octets; None for the Pad and End options.

class pcapkit.protocols.application.dhcp.data.OverloadedField(options, padding)[source]

Bases: Data

Data model for an sname or file field holding options (RFC 2131 Section 4.1, RFC 2132 Section 9.3).

options: tuple[Option, ...]

The options, from the first octet of the field.

padding: bytes

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

class pcapkit.protocols.application.dhcp.data.DomainName(name, labels, literal, pointer)[source]

Bases: Data

Data model for a domain name (RFC 1035 Section 3.1).

literal and pointer record how the name was written, so that it is rebuilt with the compression it was read with (RFC 1035 Section 4.1.4).

name: str

Name in presentation form, absolute, e.g. example.com..

labels: tuple[bytes, ...]

Every label of the name, the root’s empty label left out.

literal: int

How many of labels are written in full where the name is.

pointer: int | None

Offset of the compression pointer ending the name, or None where the root label ends it.

Option Data

class pcapkit.protocols.application.dhcp.data.OptionData(*args, **kwargs)[source]

Bases: Data

Data model for the decoded data of an option.

class pcapkit.protocols.application.dhcp.data.Address(address)[source]

Bases: OptionData

Data model for an option carrying one IPv4 address.

address: IPv4Address

The address.

class pcapkit.protocols.application.dhcp.data.Addresses(addresses)[source]

Bases: OptionData

Data model for an option carrying a list of IPv4 addresses.

addresses: tuple[IPv4Address, ...]

The addresses, in order of preference.

class pcapkit.protocols.application.dhcp.data.Number(value)[source]

Bases: OptionData

Data model for an option carrying one integer.

value: int

The integer, signed for the Time Offset option (RFC 2132 Section 3.4).

class pcapkit.protocols.application.dhcp.data.Boolean(value)[source]

Bases: OptionData

Data model for an option carrying an octet of 0 or 1.

value: bool

Whether the octet is 1.

class pcapkit.protocols.application.dhcp.data.Text(text)[source]

Bases: OptionData

Data model for an option carrying ASCII text (RFC 2132 Section 2).

text: str

The text, with any trailing NUL kept.

class pcapkit.protocols.application.dhcp.data.Sizes(sizes)[source]

Bases: OptionData

Data model for the Path MTU Plateau Table option (RFC 2132 Section 4.7).

sizes: tuple[int, ...]

MTU sizes, smallest first.

class pcapkit.protocols.application.dhcp.data.AddressMask(address, mask)[source]

Bases: Data

Data model for an address and mask pair of the Policy Filter option.

address: IPv4Address

Destination address.

mask: IPv4Address

Its mask.

class pcapkit.protocols.application.dhcp.data.PolicyFilter(filters)[source]

Bases: OptionData

Data model for the Policy Filter option (RFC 2132 Section 4.3).

filters: tuple[AddressMask, ...]

Address and mask pairs.

class pcapkit.protocols.application.dhcp.data.Route(destination, router)[source]

Bases: Data

Data model for a destination and router pair of the Static Route option.

destination: IPv4Address

Destination address.

router: IPv4Address

Router.

class pcapkit.protocols.application.dhcp.data.StaticRoutes(routes)[source]

Bases: OptionData

Data model for the Static Route option (RFC 2132 Section 5.8).

routes: tuple[Route, ...]

Destination and router pairs, in order of priority.

class pcapkit.protocols.application.dhcp.data.NodeType(node_type)[source]

Bases: OptionData

Data model for the NetBIOS over TCP/IP Node Type option (RFC 2132 Section 8.7).

node_type: int

Node type, 0x1 for a B-node, 0x2 P-node, 0x4 M-node or 0x8 H-node.

class pcapkit.protocols.application.dhcp.data.Overload(file, sname)[source]

Bases: OptionData

Data model for the Option Overload option (RFC 2132 Section 9.3).

file: bool

Whether the file field holds options, value 1 or 3.

sname: bool

Whether the sname field holds options, value 2 or 3.

class pcapkit.protocols.application.dhcp.data.MessageType(type)[source]

Bases: OptionData

Data model for the DHCP Message Type option (RFC 2132 Section 9.6).

type: MessageType

Message type.

class pcapkit.protocols.application.dhcp.data.ParameterList(codes)[source]

Bases: OptionData

Data model for the Parameter Request List option (RFC 2132 Section 9.8).

codes: tuple[Option, ...]

Option codes requested, in order of preference.

class pcapkit.protocols.application.dhcp.data.ClientIdentifier(type, identifier)[source]

Bases: OptionData

Data model for the Client-identifier option (RFC 2132 Section 9.14).

type: Hardware

Hardware type (RFC 1700), or 0 for an identifier other than a hardware address.

identifier: str | bytes

Identifier; for an Ethernet type and six octets, a MAC address.

class pcapkit.protocols.application.dhcp.data.UserClass(classes)[source]

Bases: OptionData

Data model for the User Class option (RFC 3004 Section 4).

classes: tuple[bytes, ...]

Each instance of user class data.

class pcapkit.protocols.application.dhcp.data.FQDNFlags(n, e, o, s, mbz)[source]

Bases: Data

Data model for the flags of the Client FQDN option (RFC 4702 Section 2.1).

n: bool

N flag, set where the server should perform no DNS update.

e: bool

E flag, set where the name is in canonical wire format, not ASCII.

o: bool

O flag, set where the server has overridden the client’s S flag.

s: bool

S flag, set where the server should perform the A RR update.

mbz: int

MBZ, the four high bits, which must be zero, as captured.

class pcapkit.protocols.application.dhcp.data.ClientFQDN(flags, rcode1, rcode2, name)[source]

Bases: OptionData

Data model for the Client FQDN option (RFC 4702 Section 2).

flags: FQDNFlags

Flags.

rcode1: int

RCODE1, deprecated.

rcode2: int

RCODE2, deprecated.

name: str

Domain name, in presentation form where the E flag sets canonical wire format, ending in . where it is fully qualified, and else its ASCII text (RFC 4702 Section 2.3).

class pcapkit.protocols.application.dhcp.data.RelayAgentSubOption(code, length, data)[source]

Bases: Data

Data model for a sub-option of the Relay Agent Information option.

code: RelayAgentSubOption

Sub-option code.

length: int

Length octet.

data: bytes

Sub-option data.

class pcapkit.protocols.application.dhcp.data.RelayAgentInformation(suboptions)[source]

Bases: OptionData

Data model for the Relay Agent Information option (RFC 3046 Section 2.0).

suboptions: tuple[RelayAgentSubOption, ...]

Sub-options, in order.

class pcapkit.protocols.application.dhcp.data.ArchitectureTypes(types)[source]

Bases: OptionData

Data model for the Client System Architecture Type option (RFC 4578 Section 2.1).

types: tuple[int, ...]

Processor architecture types, in order of preference.

class pcapkit.protocols.application.dhcp.data.ClientNDI(type, major, minor)[source]

Bases: OptionData

Data model for the Client Network Interface Identifier option (RFC 4578 Section 2.2).

type: int

Interface type, 1 for UNDI.

major: int

Major revision.

minor: int

Minor revision.

class pcapkit.protocols.application.dhcp.data.MachineIdentifier(type, identifier)[source]

Bases: OptionData

Data model for the Client Machine Identifier option (RFC 4578 Section 2.3).

type: int

Identifier type, 0 for a GUID.

identifier: bytes

The 16-octet identifier.

class pcapkit.protocols.application.dhcp.data.DomainSearch(names)[source]

Bases: OptionData

Data model for the Domain Search option (RFC 3397 Section 2).

names: tuple[DomainName, ...]

Domain names, in order.

class pcapkit.protocols.application.dhcp.data.ClasslessRoute(destination, router)[source]

Bases: Data

Data model for a route of the Classless Static Route option.

destination: IPv4Network

Destination network.

router: IPv4Address

Router.

class pcapkit.protocols.application.dhcp.data.ClasslessStaticRoutes(routes)[source]

Bases: OptionData

Data model for the Classless Static Route option (RFC 3442 Section 2).

routes: tuple[ClasslessRoute, ...]

Routes, in order.

class pcapkit.protocols.application.dhcp.data.VendorClassEntry(enterprise, classes)[source]

Bases: Data

Data model for one vendor’s data in the Vendor-Identifying Vendor Class option.

enterprise: int

IANA Private Enterprise Number.

classes: tuple[bytes, ...]

Each item of the vendor class data.

class pcapkit.protocols.application.dhcp.data.VendorClass(vendors)[source]

Bases: OptionData

Data model for the Vendor-Identifying Vendor Class option (RFC 3925 Section 3).

vendors: tuple[VendorClassEntry, ...]

Each vendor’s data.

class pcapkit.protocols.application.dhcp.data.VendorOption(code, data)[source]

Bases: Data

Data model for a sub-option of the Vendor-Identifying Vendor-Specific Information option.

code: int

Sub-option code, the vendor’s own.

data: bytes

Sub-option data.

class pcapkit.protocols.application.dhcp.data.VendorOptions(enterprise, options)[source]

Bases: Data

Data model for one vendor’s data in the Vendor-Identifying Vendor-Specific Information option.

enterprise: int

IANA Private Enterprise Number.

options: tuple[VendorOption, ...]

The vendor’s sub-options.

class pcapkit.protocols.application.dhcp.data.VendorSpecific(vendors)[source]

Bases: OptionData

Data model for the Vendor-Identifying Vendor-Specific Information option (RFC 3925 Section 4).

vendors: tuple[VendorOptions, ...]

Each vendor’s data.

Constants

pcapkit.protocols.application.dhcp.const.HEADER_LENGTH = 236

Length of the fixed header, op through file (RFC 2131 Section 2).

pcapkit.protocols.application.dhcp.const.CHADDR_LENGTH = 16

Length of the chaddr field.

pcapkit.protocols.application.dhcp.const.SNAME_LENGTH = 64

Length of the sname field.

pcapkit.protocols.application.dhcp.const.FILE_LENGTH = 128

Length of the file field.

The magic cookie, 99.130.83.99, opening an option area (RFC 2132 Section 2).

pcapkit.protocols.application.dhcp.const.MINIMUMS = {<Option.Max_DG_Assembly: 22>: 576, <Option.Default_IP_TTL: 23>: 1, <Option.MTU_Interface: 26>: 68, <Option.Default_TCP_TTL: 37>: 1, <Option.DHCP_Max_Msg_Size: 57>: 576}

The least value an integer option takes, where RFC 2132 gives one.