## 3 D2 HCP (Distributed Dynamic Host Configuration Protocol)
### Joining and Departure of Nodes
#### 3.2.1 Node Joining  
The entry of a node to the network implies the need to locate a node acting as a server. Once found, it will facilitate the joining by providing an IP address block and a _Free_IP_Blocks_ table representing the state of all the nodes in the network. Until the node has an assigned IP address, its communication with nodes which might act as servers will be through the MAC layer. The configuration mechanism uses four types of messages in most cases. If no nodes in range with free IP addresses are found six types of messages in total will be used. Figure 1 shows the exchanged message scheme as is explained below:  
1. _SERVER_DISCOVERY:_ The client node wishing to join a network starts the process with a message of this type. It is transmitted by the MAC layer, with the _broadcast_ address as its destination. The message indicates the IP address number which is required (equal to the  
Figure 1: The messages interchanged in the process of a node joining the network. interface number). If the node has more than one network interface, the message is transmitted through all of them, using the ID field, thus the different interfaces are not _confused_ with several nodes. 2. _SERVER_OFFER:_ The network nodes receiving the SERVER_DISCOVERY message reply to this message, also using the MAC layer, in which an IP address number is offered. The number of addresses offered is half of the available range. The SERVER_DISCOVERY message includes a _Count_ field indicating how many attempts have been made by the client. Depending on its value, the server nodes will behave as follows: * _Count_ = 1: The server node will respond with a SERVER_OFFER if enough addresses are available and the fields R (_Ready_) and L (_Local_) have the value 1 (it can assign the addresses provided in the moment, and they are addresses from the node's own block). * _Count_ = 2: The node server will respond with a SERVER_OFFER if the fields can take the value R = 1 and L = 1. If not possible, it will still also respond if it is the case that there are enough addresses and R = 0, L = 1 (the server cannot assign addresses at the moment, but it has them). * _Count_ > 2: If the node has addresses available and is in a state to do so, it will send a SERVER_OFFER with R = 1, L = 1. If it can, will send it with R = 0, L = 1. Finally, if it does not have enough free addresses, it will send the message with the fields R = 1, L = 0 (immediate availability of addresses, but the offered addresses are from another node in the network). 3. _SERVER_POLL:_ After a certain listening time, the client node will have received several SERVER_OFFER messages. If not, it will try again. It will sort received messages using the following criteria: * The servers which are not available are discarded, _i.e._, with R = 0. The SERVER_OFFER with R = 0 is not used to reply with a SERVER_POLL, but they have the function of informing the client that there is a server node in the network, although it cannot provide access to it at this moment. * Priority is given to local addresses: it will prefer messages with the field L = 1. * Finally, it is organized so that the offered addresses are ranked, from highest to lowest. According to this criteria for order preference, it will send a SERVER_POLL message to the first server (via the MAC layer, again) to let it know that the node has chosen this one to assign a free IP address block to it. 4. _IP_RANGE_REQUEST:_ If the addresses provided by the server node were not their own, but they were from a third node in the network, with this message there will be a formal request made to that node. Since there is communication between two nodes already configured correctly, it is performed at the IP layer. 5. _IP_RANGE_RETURN:_ The third network node authorizes the node that sends the message IP_RANGE_REQUEST to assign the address block indicated in this message to client nodes. It is also a message sent by IP. 6.