libtins  4.0
Public Types | Public Member Functions | Static Public Member Functions | Static Public Attributes | List of all members
Tins::DHCP Class Reference

Represents the DHCP PDU. More...

#include <dhcp.h>

Inheritance diagram for Tins::DHCP:
Tins::BootP Tins::PDU

Public Types

enum  Flags {
  DISCOVER = 1, OFFER = 2, REQUEST = 3, DECLINE = 4,
  ACK = 5, NAK = 6, RELEASE = 7, INFORM = 8
}
 
enum  OptionTypes {
  PAD, SUBNET_MASK, TIME_OFFSET, ROUTERS,
  TIME_SERVERS, NAME_SERVERS, DOMAIN_NAME_SERVERS, LOG_SERVERS,
  COOKIE_SERVERS, LPR_SERVERS, IMPRESS_SERVERS, RESOURCE_LOCATION_SERVERS,
  HOST_NAME, BOOT_SIZE, MERIT_DUMP, DOMAIN_NAME,
  SWAP_SERVER, ROOT_PATH, EXTENSIONS_PATH, IP_FORWARDING,
  NON_LOCAL_SOURCE_ROUTING, POLICY_FILTER, MAX_DGRAM_REASSEMBLY, DEFAULT_IP_TTL,
  PATH_MTU_AGING_TIMEOUT, PATH_MTU_PLATEAU_TABLE, INTERFACE_MTU, ALL_SUBNETS_LOCAL,
  BROADCAST_ADDRESS, PERFORM_MASK_DISCOVERY, MASK_SUPPLIER, ROUTER_DISCOVERY,
  ROUTER_SOLICITATION_ADDRESS, STATIC_ROUTES, TRAILER_ENCAPSULATION, ARP_CACHE_TIMEOUT,
  IEEE802_3_ENCAPSULATION, DEFAULT_TCP_TTL, TCP_KEEPALIVE_INTERVAL, TCP_KEEPALIVE_GARBAGE,
  NIS_DOMAIN, NIS_SERVERS, NTP_SERVERS, VENDOR_ENCAPSULATED_OPTIONS,
  NETBIOS_NAME_SERVERS, NETBIOS_DD_SERVER, NETBIOS_NODE_TYPE, NETBIOS_SCOPE,
  FONT_SERVERS, X_DISPLAY_MANAGER, DHCP_REQUESTED_ADDRESS, DHCP_LEASE_TIME,
  DHCP_OPTION_OVERLOAD, DHCP_MESSAGE_TYPE, DHCP_SERVER_IDENTIFIER, DHCP_PARAMETER_REQUEST_LIST,
  DHCP_MESSAGE, DHCP_MAX_MESSAGE_SIZE, DHCP_RENEWAL_TIME, DHCP_REBINDING_TIME,
  VENDOR_CLASS_IDENTIFIER, DHCP_CLIENT_IDENTIFIER, NWIP_DOMAIN_NAME, NWIP_SUBOPTIONS,
  USER_CLASS = 77, FQDN = 81, DHCP_AGENT_OPTIONS = 82, SUBNET_SELECTION = 118,
  AUTHENTICATE = 210, END = 255
}
 DHCP options enum.
 
typedef PDUOption< uint8_t, DHCPoption
 
typedef std::vector< optionoptions_type
 
- Public Types inherited from Tins::BootP
enum  OpCodes { BOOTREQUEST = 1, BOOTREPLY = 2 }
 Enum which contains the different opcodes BootP messages.
 
typedef IPv4Address ipaddress_type
 
typedef HWAddress< 16 > chaddr_type
 
typedef std::vector< uint8_t > vend_type
 
- Public Types inherited from Tins::PDU
enum  endian_type { BE, LE }
 
enum  PDUType {
  RAW, ETHERNET_II, IEEE802_3, DOT3 = IEEE802_3,
  RADIOTAP, DOT11, DOT11_ACK, DOT11_ASSOC_REQ,
  DOT11_ASSOC_RESP, DOT11_AUTH, DOT11_BEACON, DOT11_BLOCK_ACK,
  DOT11_BLOCK_ACK_REQ, DOT11_CF_END, DOT11_DATA, DOT11_CONTROL,
  DOT11_DEAUTH, DOT11_DIASSOC, DOT11_END_CF_ACK, DOT11_MANAGEMENT,
  DOT11_PROBE_REQ, DOT11_PROBE_RESP, DOT11_PS_POLL, DOT11_REASSOC_REQ,
  DOT11_REASSOC_RESP, DOT11_RTS, DOT11_QOS_DATA, LLC,
  SNAP, IP, ARP, TCP,
  UDP, ICMP, BOOTP, DHCP,
  EAPOL, RC4EAPOL, RSNEAPOL, DNS,
  LOOPBACK, IPv6, ICMPv6, SLL,
  DHCPv6, DOT1Q, PPPOE, STP,
  PPI, IPSEC_AH, IPSEC_ESP, PKTAP,
  MPLS, UNKNOWN = 999, USER_DEFINED_PDU = 1000
}
 Enum which identifies each type of PDU. More...
 
typedef byte_array serialization_type
 

Public Member Functions

 DHCP ()
 Creates an instance of DHCP. More...
 
 DHCP (const uint8_t *buffer, uint32_t total_sz)
 Constructs a DHCP object from a buffer. More...
 
void add_option (const option &opt)
 Adds a new option to this DHCP PDU. More...
 
void add_option (option &&opt)
 Adds a new option to this DHCP PDU. More...
 
bool remove_option (OptionTypes type)
 Removes a DHCP option. More...
 
const optionsearch_option (OptionTypes opt) const
 Searchs for an option that matchs the given flag. More...
 
void type (Flags type)
 Adds a type option to the option list. More...
 
void end ()
 Adds an end option to the option list. More...
 
void server_identifier (ipaddress_type ip)
 Adds a server identifier option. More...
 
void lease_time (uint32_t time)
 Adds an IP address lease time option. More...
 
void renewal_time (uint32_t time)
 Adds a lease renewal time option. More...
 
void rebind_time (uint32_t time)
 Adds a rebind time option. More...
 
void subnet_mask (ipaddress_type mask)
 Adds a subnet mask option. More...
 
void routers (const std::vector< ipaddress_type > &routers)
 Adds a routers option. More...
 
void domain_name_servers (const std::vector< ipaddress_type > &dns)
 Adds a domain name servers option. More...
 
void broadcast (ipaddress_type addr)
 Adds a broadcast address option. More...
 
void requested_ip (ipaddress_type addr)
 Adds a requested address option. More...
 
void domain_name (const std::string &name)
 Adds a domain name option. More...
 
void hostname (const std::string &name)
 Adds a hostname option. More...
 
uint8_t type () const
 Searchs for a type option. More...
 
ipaddress_type server_identifier () const
 Searchs for a server identifier option. More...
 
uint32_t lease_time () const
 Searchs for a lease time option. More...
 
uint32_t renewal_time () const
 Searchs for a lease renewal time option. More...
 
uint32_t rebind_time () const
 Searchs for a rebind time option. More...
 
ipaddress_type subnet_mask () const
 Searchs for a subnet mask option. More...
 
std::vector< ipaddress_typerouters () const
 Searchs for a routers option. More...
 
std::vector< ipaddress_typedomain_name_servers () const
 Searchs for a dns option. More...
 
ipaddress_type broadcast () const
 Searchs for a broadcast option. More...
 
ipaddress_type requested_ip () const
 Searchs for a requested option. More...
 
std::string domain_name () const
 Searchs for a domain name option. More...
 
std::string hostname () const
 Searchs for a hostname option. More...
 
const options_type options () const
 Getter for the options list. More...
 
PDUType pdu_type () const
 Getter for the PDU's type. More...
 
uint32_t header_size () const
 Getter for the header size. More...
 
DHCPclone () const
 
- Public Member Functions inherited from Tins::BootP
 BootP ()
 Creates an instance of BootP. More...
 
 BootP (const uint8_t *buffer, uint32_t total_sz, uint32_t vend_field_size=64)
 Constructs a BootP object from a buffer . More...
 
uint8_t opcode () const
 Getter for the opcode field. More...
 
uint8_t htype () const
 Getter for the htype field. More...
 
uint8_t hlen () const
 Getter for the hlen field. More...
 
uint8_t hops () const
 Getter for the hops field. More...
 
uint32_t xid () const
 Getter for the xid field. More...
 
uint16_t secs () const
 Getter for the secs field. More...
 
uint16_t padding () const
 Getter for the padding field. More...
 
ipaddress_type ciaddr () const
 Getter for the ciaddr field. More...
 
ipaddress_type yiaddr () const
 Getter for the yiaddr field. More...
 
ipaddress_type siaddr () const
 Getter for the siaddr field. More...
 
ipaddress_type giaddr () const
 Getter for the giaddr field. More...
 
chaddr_type chaddr () const
 Getter for the chaddr field. More...
 
const uint8_t * sname () const
 Getter for the sname field. More...
 
const uint8_t * file () const
 Getter for the file field. More...
 
const vend_typevend () const
 Getter for the vend field. More...
 
void opcode (uint8_t code)
 Setter for the opcode field. More...
 
void htype (uint8_t type)
 Setter for the hardware type field. More...
 
void hlen (uint8_t length)
 Setter for the hlen field. More...
 
void hops (uint8_t count)
 Setter for the hops field. More...
 
void xid (uint32_t identifier)
 Setter for the xid field. More...
 
void secs (uint16_t value)
 Setter for the secs field. More...
 
void padding (uint16_t value)
 Setter for the padding field. More...
 
void ciaddr (ipaddress_type address)
 Setter for the ciaddr field. More...
 
void yiaddr (ipaddress_type address)
 Setter for the yiaddr field. More...
 
void siaddr (ipaddress_type address)
 Setter for the siaddr field. More...
 
void giaddr (ipaddress_type address)
 Setter for the giaddr field. More...
 
template<size_t n>
void chaddr (const HWAddress< n > &new_chaddr)
 Setter for the chaddr field. The new_chaddr pointer must be at least BOOTP::hlen() bytes long. More...
 
void sname (const uint8_t *new_sname)
 Setter for the sname field. More...
 
void file (const uint8_t *new_file)
 Setter for the file field. More...
 
void vend (const vend_type &newvend_)
 Setter for the vend field. More...
 
bool matches_response (const uint8_t *ptr, uint32_t total_sz) const
 Check whether ptr points to a valid response for this PDU. More...
 
- Public Member Functions inherited from Tins::PDU
 PDU ()
 Default constructor.
 
 PDU (PDU &&rhs) TINS_NOEXCEPT
 Move constructor. More...
 
PDUoperator= (PDU &&rhs) TINS_NOEXCEPT
 Move assignment operator. More...
 
virtual ~PDU ()
 PDU destructor. More...
 
virtual uint32_t trailer_size () const
 Trailer's size. More...
 
uint32_t size () const
 The whole chain of PDU's size, including this one. More...
 
PDUinner_pdu () const
 Getter for the inner PDU. More...
 
PDUparent_pdu () const
 
PDUrelease_inner_pdu ()
 Releases the inner PDU. More...
 
void inner_pdu (PDU *next_pdu)
 Sets the child PDU. More...
 
void inner_pdu (const PDU &next_pdu)
 Sets the child PDU. More...
 
serialization_type serialize ()
 Serializes the whole chain of PDU's, including this one. More...
 
template<typename T >
T * find_pdu (PDUType type=T::pdu_flag)
 Finds and returns the first PDU that matches the given flag. More...
 
template<typename T >
const T * find_pdu (PDUType type=T::pdu_flag) const
 Finds and returns the first PDU that matches the given flag. More...
 
template<typename T >
T & rfind_pdu (PDUType type=T::pdu_flag)
 Finds and returns the first PDU that matches the given flag. More...
 
template<typename T >
const T & rfind_pdu (PDUType type=T::pdu_flag) const
 Finds and returns the first PDU that matches the given flag. More...
 
virtual void send (PacketSender &sender, const NetworkInterface &iface)
 Send the stack of PDUs through a PacketSender. More...
 
virtual PDUrecv_response (PacketSender &sender, const NetworkInterface &iface)
 Receives a matching response for this packet. More...
 
virtual bool matches_flag (PDUType flag) const
 Check whether this PDU matches the specified flag. More...
 

Static Public Member Functions

static metadata extract_metadata (const uint8_t *buffer, uint32_t total_sz)
 Extracts metadata for this protocol based on the buffer provided. More...
 

Static Public Attributes

static const PDU::PDUType pdu_flag = PDU::DHCP
 
- Static Public Attributes inherited from Tins::BootP
static const PDU::PDUType pdu_flag = PDU::BOOTP
 This PDU's flag.
 
- Static Public Attributes inherited from Tins::PDU
static const endian_type endianness = BE
 

Additional Inherited Members

- Protected Member Functions inherited from Tins::BootP
vend_typevend ()
 Getter for the vend field. More...
 
- Protected Member Functions inherited from Tins::PDU
 PDU (const PDU &other)
 Copy constructor.
 
PDUoperator= (const PDU &other)
 Copy assignment operator.
 
void copy_inner_pdu (const PDU &pdu)
 Copy other PDU's inner PDU(if any). More...
 
virtual void prepare_for_serialize ()
 Prepares this PDU for serialization. More...
 
void serialize (uint8_t *buffer, uint32_t total_sz)
 Serializes this PDU and propagates this action to child PDUs. More...
 
- Protected Attributes inherited from Tins::BootP
TINS_BEGIN_PACK struct Tins::BootP::bootp_header TINS_END_PACK
 

Detailed Description

Represents the DHCP PDU.

This class represents a DHCP PDU. It contains helpers methods which make it easy to set/get specific option values.

Note that when adding options, the "End" option is not added automatically, so you will have to add it yourself.

Options can be retrieved easily from DHCP PDUs:

// Sniff a packet from somewhere
DHCP dhcp = get_dhcp_from_somewhere();
// This retrieves the Domain Name Servers option and converts
// it to a std::vector<IPv4Address>. Note that if this option
// is not present, an option_not_found exception is thrown.
for(const auto& address : dhcp.domain_name_servers()) {
// address is an ip
}

Member Typedef Documentation

typedef PDUOption<uint8_t, DHCP> Tins::DHCP::option

The DHCP option type.

typedef std::vector<option> Tins::DHCP::options_type

The type used to store the DHCP options.

Member Enumeration Documentation

DHCP flags.

Constructor & Destructor Documentation

Tins::DHCP::DHCP ( )

Creates an instance of DHCP.

This sets the hwtype and hlen fields to match the ethernet type and length.

Tins::DHCP::DHCP ( const uint8_t *  buffer,
uint32_t  total_sz 
)

Constructs a DHCP object from a buffer.

If there is not enough size for a BootP header, or any of the TLV options contains an invalid size field, then a malformed_packet exception is thrown.

Parameters
bufferThe buffer from which this PDU will be constructed.
total_szThe total size of the buffer.

Member Function Documentation

void Tins::DHCP::add_option ( const option opt)

Adds a new option to this DHCP PDU.

Parameters
optThe option to be added.
void Tins::DHCP::add_option ( option &&  opt)
inline

Adds a new option to this DHCP PDU.

The option is move-constructed.

Parameters
optThe option to be added.
void Tins::DHCP::broadcast ( ipaddress_type  addr)

Adds a broadcast address option.

The new option is appended at the end of the list.

Parameters
addrThe broadcast address.
DHCP::ipaddress_type Tins::DHCP::broadcast ( ) const

Searchs for a broadcast option.

If the option is not found, an option_not_found exception is thrown.

Returns
ipaddress_type Containing the broadcast address.
DHCP* Tins::DHCP::clone ( ) const
inlinevirtual
See also
PDU::clone

Reimplemented from Tins::BootP.

void Tins::DHCP::domain_name ( const std::string &  name)

Adds a domain name option.

The new option is appended at the end of the list.

Parameters
nameThe domain name.
string Tins::DHCP::domain_name ( ) const

Searchs for a domain name option.

If the option is not found, an option_not_found exception is thrown.

Returns
std::string Containing the domain name.
void Tins::DHCP::domain_name_servers ( const std::vector< ipaddress_type > &  dns)

Adds a domain name servers option.

The new option is appended at the end of the list.

Parameters
dnsA list of ip addresses.
vector< DHCP::ipaddress_type > Tins::DHCP::domain_name_servers ( ) const

Searchs for a dns option.

If the option is not found, an option_not_found exception is thrown.

Returns
The list of DNS servers provided.
void Tins::DHCP::end ( )

Adds an end option to the option list.

The new option is appended at the end of the list.

The END option is not added automatically. You should explicitly add it at the end of the DHCP options for the PDU to be standard-compliant.

PDU::metadata Tins::DHCP::extract_metadata ( const uint8_t *  buffer,
uint32_t  total_sz 
)
static

Extracts metadata for this protocol based on the buffer provided.

Parameters
bufferPointer to a buffer
total_szSize of the buffer pointed by buffer
uint32_t Tins::DHCP::header_size ( ) const
virtual

Getter for the header size.

Returns
Returns the BOOTP header size.
See also
PDU::header_size

Reimplemented from Tins::BootP.

void Tins::DHCP::hostname ( const std::string &  name)

Adds a hostname option.

The new option is appended at the end of the list.

Parameters
nameThe hostname.
string Tins::DHCP::hostname ( ) const

Searchs for a hostname option.

If the option is not found, an option_not_found exception is thrown.

Returns
std::string Containing the hostname.
void Tins::DHCP::lease_time ( uint32_t  time)

Adds an IP address lease time option.

The new option is appended at the end of the list.

Parameters
timeThe lease time.
uint32_t Tins::DHCP::lease_time ( ) const

Searchs for a lease time option.

If the option is not found, an option_not_found exception is thrown.

Returns
uint32_t Containing the lease time.
const options_type Tins::DHCP::options ( ) const
inline

Getter for the options list.

Returns
The option list.
PDUType Tins::DHCP::pdu_type ( ) const
inlinevirtual

Getter for the PDU's type.

See also
PDU::pdu_type

Reimplemented from Tins::BootP.

void Tins::DHCP::rebind_time ( uint32_t  time)

Adds a rebind time option.

The new option is appended at the end of the list.

Parameters
timeThe lease rebind time.
uint32_t Tins::DHCP::rebind_time ( ) const

Searchs for a rebind time option.

If the option is not found, an option_not_found exception is thrown.

Returns
uint32_t Containing the rebind time.
bool Tins::DHCP::remove_option ( OptionTypes  type)

Removes a DHCP option.

If there are multiple options of the given type, only the first one will be removed.

Parameters
typeThe type of the option to be removed.
Returns
true if the option was removed, false otherwise.
void Tins::DHCP::renewal_time ( uint32_t  time)

Adds a lease renewal time option.

The new option is appended at the end of the list.

Parameters
timeThe lease renew time.
uint32_t Tins::DHCP::renewal_time ( ) const

Searchs for a lease renewal time option.

If the option is not found, an option_not_found exception is thrown.

Returns
uint32_t Containing the renewal time.
void Tins::DHCP::requested_ip ( ipaddress_type  addr)

Adds a requested address option.

The new option is appended at the end of the list.

Parameters
addrThe requested address.
DHCP::ipaddress_type Tins::DHCP::requested_ip ( ) const

Searchs for a requested option.

If the option is not found, an option_not_found exception is thrown.

Returns
ipaddress_type Containing the requested IP address.
void Tins::DHCP::routers ( const std::vector< ipaddress_type > &  routers)

Adds a routers option.

The new option is appended at the end of the list.

Parameters
routersA list of ip addresses.
vector< DHCP::ipaddress_type > Tins::DHCP::routers ( ) const

Searchs for a routers option.

If the option is not found, an option_not_found exception is thrown.

Returns
std::vector<ipaddress_type> Containing the routers option data.
const DHCP::option * Tins::DHCP::search_option ( OptionTypes  opt) const

Searchs for an option that matchs the given flag.

Parameters
opt_flagThe flag to be searched.
Returns
A pointer to the option, or 0 if it was not found.
void Tins::DHCP::server_identifier ( ipaddress_type  ip)

Adds a server identifier option.

The new option is appended at the end of the list.

Parameters
ipThe server's IP address.
DHCP::ipaddress_type Tins::DHCP::server_identifier ( ) const

Searchs for a server identifier option.

If the option is not found, an option_not_found exception is thrown.

Returns
ipaddress_type Containing the server identifier.
void Tins::DHCP::subnet_mask ( ipaddress_type  mask)

Adds a subnet mask option.

The new option is appended at the end of the list.

Parameters
maskThe subnet mask.
DHCP::ipaddress_type Tins::DHCP::subnet_mask ( ) const

Searchs for a subnet mask option.

If the option is not found, an option_not_found exception is thrown.

Returns
ipaddress_type Containing the subnet mask.
void Tins::DHCP::type ( Flags  type)

Adds a type option to the option list.

The new option is appended at the end of the list.

Parameters
typeThe type of this DHCP PDU.
uint8_t Tins::DHCP::type ( ) const

Searchs for a type option.

If the option is not found, an option_not_found exception is thrown.

Returns
uint8_t containing the type option.

Member Data Documentation

const PDU::PDUType Tins::DHCP::pdu_flag = PDU::DHCP
static

This PDU's flag.


The documentation for this class was generated from the following files: