Library dhcp

Implement a Dynamic Host Configuration Protocol (DHCP) client.

DHCP, defined in rfc2132 and rfc2131, is a protocol for hosts to automatically configure themselves on a network (that is, obtain an ip address). This library, which have a trivial one-function interface, can send out DHCP packets of many types and parse the responses.

Author:

  • Ron Bowes

Source: https://svn.nmap.org/nmap/nselib/dhcp.lua

Functions

dhcp_build (request_type, ip_address, mac_address, options, request_options, overrides, lease_time, transaction_id)

Builds a DHCP packet

dhcp_parse (data, transaction_id)

Parse a DHCP packet (either a request or a response) and return the results as a table.

make_request (target, request_type, ip_address, mac_address, options, request_options, overrides, lease_time)

Build and send any kind of DHCP packet, and parse the response. This is the only interface to the DHCP library, and should be the only one necessary.

Functions

dhcp_build (request_type, ip_address, mac_address, options, request_options, overrides, lease_time, transaction_id)

Builds a DHCP packet

Parameters

request_type
The type of request as an integer (use the request_types table at the top of this file).
ip_address
Your ip address (as a dotted-decimal string). This tells the DHCP server where to send the response. Setting it to "255.255.255.255" or "0.0.0.0" is generally acceptable (if not, host.ip_src can work).
mac_address
Your mac address (as a string up to 16 bytes) where the server will send the response. Like ip_address, setting to the broadcast address (FF:FF:FF:FF:FF:FF) is common (host.mac_addr_src works).
options
[optional] A table of additional request options where each option is a table containing the following fields: * number - The option number * type - The option type ("string" or "ip") * value - The option value
request_options
[optional] The options to request from the server, as an array of integers. For the acceptable options, see the actions table above or have a look at rfc2132. Some DHCP servers (such as my Linksys WRT54g) will ignore this list and send whichever information it wants. Default: all options marked as 'default' in the actions table above are requested (the typical interesting ones) if no verbosity is given. If any level of verbosity is on, get all types.
overrides
[optional] A table of overrides. If a field in the table matches a field in the DHCP packet (see rfc2131 section 2 for a list of possible fields), the value in the table will be sent instead of the default value.
lease_time
[optional] The lease time used when requesting an IP. Default: none.
transaction_id
The identity of the transaction.

Return values:

  1. status (true or false)
  2. The parsed response, as a table.
dhcp_parse (data, transaction_id)

Parse a DHCP packet (either a request or a response) and return the results as a table.

The table at the top of this function (actions) defines the name of each field, as laid out in rfc2132, and the function that parses it.

In theory, this should be able to parse any valid DHCP packet.

Parameters

data
The DHCP packet data. Any padding at the end of the packet will be ignored (by default, DHCP packets are padded with \x00 bytes).
transaction_id
 
make_request (target, request_type, ip_address, mac_address, options, request_options, overrides, lease_time)

Build and send any kind of DHCP packet, and parse the response. This is the only interface to the DHCP library, and should be the only one necessary.

All DHCP packet have the same structure, but different fields. It is therefore easy to build any of the possible request types:

  • DHCPDISCOVER
  • DHCPOFFER
  • DHCPREQUEST
  • DHCPDECLINE
  • DHCPACK
  • DHCPNAK
  • DHCPRELEASE
  • DHCPINFORM

Although these will all build a valid packet with any option, and the default options (that can be overridden with the overrides argument) won't necessarily work with every request type. If you're going to build some DHCP code on your own, I recommend reading rfc2131.

Parameters

target
 
request_type
The type of request as an integer (use the request_types table at the top of this file).
ip_address
Your ip address (as a dotted-decimal string). This tells the DHCP server where to send the response. Setting it to "255.255.255.255" or "0.0.0.0" is generally acceptable (if not, host.ip_src can work).
mac_address
Your mac address (as a string up to 16 bytes) where the server will send the response. Like ip_address, setting to the broadcast address (FF:FF:FF:FF:FF:FF) is common (host.mac_addr_src works).
options
[optional] A table of additional request options where each option is a table containing the following fields: * number - The option number * type - The option type ("string" or "ip") * value - The option value
request_options
[optional] The options to request from the server, as an array of integers. For the acceptable options, see the actions table above or have a look at rfc2132. Some DHCP servers (such as my Linksys WRT54g) will ignore this list and send whichever information it wants. Default: all options marked as 'default' in the actions table above are requested (the typical interesting ones) if no verbosity is given. If any level of verbosity is on, get all types.
overrides
[optional] A table of overrides. If a field in the table matches a field in the DHCP packet (see rfc2131 section 2 for a list of possible fields), the value in the table will be sent instead of the default value.
lease_time
[optional] The lease time used when requesting an IP. Default: none.

Return values:

  1. status (true or false)
  2. The parsed response, as a table.