ex_dhcp v0.1.5 ExDhcp.Options.Macro View Source

This module provides methods that facilitate custom encoding and decoding strategies for DHCP packet options. ExDhcp.Options.Basic provides basic parameter encoding; the full DHCP specification provides for additional, proprietary, and custom options encoding.

For example, PXE (Preboot eXecution Environment) uses an additional set of options to transmit booting information to the client. These are implemented in ExDhcp.Options.Pxe for demonstration.

If you need to implement additional options parsing use this module, include it as an option in the use ExDhcp directive along with ExDhcp.Options.Basic (unless you want to override it):

use ExDhcp, dhcp_options: [MyParser, ExDhcp.Options.Basic]

Standard and Custom Parsing

Pass the options/1 macro a keyword list of parameter names and datatypes. The options parser will then search for a module parameter corresponding to the keyword, and assign that integer as the options parameter. The type will indicate either a standard type codec or an atom for a custom codec.

The following standard types are implemented:

NameErlang Term TypeBinary Representation
:ip{a, b, c, d}4 octets
:iplistlist({a, b, c, d})Nx4 octets
:stringbinaryvariable octets
:integerinteger4 octet (32 bit) integer
:shortinteger2 octet (16 bit) integer
:byteinteger1 octet (8 bit) integer
:booleanbooleanone octet, either 1 or 0

In the case of a custom codec, you must implement two functions:

  • encode_<atom_value>/1:   The encoder should take a raw binary and convert it to an appropriate erlang term representing the atom.

  • decode_<atom_value>/1:   The decoder should take an erlang term and convert it to an appropriate binary to be packed into the DHCP packet.

options/1 will append a relevant table of encoders/decoders into your module documentation as a feature.

Here is an example implementation of a parser:


defmodule MyParser do

  import ExDhcp.Options.Macro

  @behaviour ExDhcp.Options.Api

  @option_1  123
  @option_2  124
  @option_3  125

  options option_1: :integer
          option_2: :string
          option_3: :option_3

  def decode_option_3(binary) do
    # code_to_decode_option_3
    result_erlang_term
  end

  def encode_option_3(source_erlang_term) do
    # code_to_encode_option_3
    result_binary
  end

end

Refer to ExDhcp.Options.Basic and ExDhcp.Options.Pxe source codes as an additional reference.

Learn more about PXE here: Wikipedia

Link to this section Summary

Functions

Generates code for codecs based on a list of atom / type keys.

Link to this section Functions

Link to this macro

options(options_list) View Source (macro)

Generates code for codecs based on a list of atom / type keys.

See ExDhcp.Options.Macro for details and strategies for using this macro.