Skip to content

Net_DHCP

Stephane Carrez edited this page Nov 24, 2016 · 3 revisions

DHCP client

DHCP Client

The DHCP client can be used to configure the IPv4 network stack by using the DHCP protocol (RFC 2131). The DHCP client uses a UDP socket on port 68 to send and receive DHCP messages. The DHCP client state is maintained by two procedures which are called asynchronously: the Process and Receive procedures. The Process procedure is responsible for sending requests to the DHCP server and to manage the timeouts used for the retransmissions, renewal and lease expiration. On its hand, the Receive procedure is called by the UDP socket layer when a DHCP packet is received. These two procedures are typically called from different tasks.

To make the implementation simple and ready to use, the DHCP client uses a pre-defined configuration that should meet most requirements. The DHCP client asks for the following DHCP options:

  • Option 1: Subnetmask
  • Option 3: Router
  • Option 6: Domain name server
  • Option 12: Hostname
  • Option 15: Domain name
  • Option 26: Interface MTU size
  • Option 28: Brodcast address
  • Option 42: NTP server
  • Option 72: WWW server
  • Option 51: Lease time
  • Option 58: Renew time
  • Option 59: Rebind time

It sends the following options to help the server identify the client:

  • Option 60: Vendor class identifier, the string "Ada Embedded Network" is sent.
  • Option 61: Client identifier, the Ethernet address is used as identifier.

Initialization

To use the client, one will have to declare a global aliased DHCP client instance (the aliased is necessary as the UDP socket layer needs to get an access to it):

C : aliased Net.DHCP.Client;

The DHCP client instance must then be initialized after the network interface is initialized. The Initialize procedure needs an access to the interface instance.

C.Initialize (Ifnet'Access);

The initialization only binds the UDP socket to the port 68 and prepares the DHCP state machine. At this stage, no DHCP packet is sent yet but the UDP socket is now able to receive them.

Processing

The Process procedure must be called either by a main task or by a dedicated task to send the DHCP requests and maintain the DHCP state machine. Each time this procedure is called, it looks whether some DHCP processing must be done and it computes a delay that indicates the maximum time to wait before the next call. It is safe to call the Process procedure more often than required. The operation will perform different tasks depending on the DHCP state:

In the STATE_INIT state, it records the begining of the DHCP discovering state, switches to the STATE_SELECTING and sends the first DHCP discover packet.

When the DHCP state machine is in the STATE_SELECTING state, it continues to send the DHCP discover packet taking into account the backoff timeout.

In the STATE_REQUESTING state, it sends the DHCP request packet to the server.

In the STATE_BOUND state, it configures the interface if it is not yet configured and it then waits for the DHCP lease renewal. If the DHCP lease must be renewed, it switches to the STATE_RENEWING state.

The DHCP client does not use any task to give you the freedom on how you want to integrate the DHCP client in your application. The Process procedure may be integrated in a loop similar to the loop below:

declare
   Dhcp_Timeout : Ada.Real_Time.Time_Span;
begin
   loop
      C.Process (Dhcp_Timeout);
      delay until Ada.Real_Time.Clock + Dhcp_Timeout;
   end loop;
end;

This loop may be part of a dedicated task for the DHCP client but it may also be part of another task that could also handle other network house keeping (such as ARP management).

Interface Configuration

Once in the STATE_BOUND, the interface configuration is done by the Process procedure that calls the Bind procedure with the DHCP received options. This procedure configures the interface IP, netmask, gateway, MTU and DNS. It is possible to override this procedure in an application to be notified and extract other information from the received DHCP options. In that case, it is still important to call the overriden procedure so that the interface and network stack is correctly configured.


Generated by Dynamo from net-dhcp.ads

Clone this wiki locally