arduino/Arduino — pull request #1244 by valentinalexeev (2013-01-24 13:10:31+00:00)
Title: Feature: implement DHCP options provider and parser

I have implemented a feature to allow Ethernet library user to supply and read DHCP options.

This functionality is very useful when you need to provide run-time configuration to the Arduino board. Since objective of DHCP is "host configuration" it is an ideal choice to use to pass parameters to the board. This commit also allows board to send additional options as part of the request. In my case, for example, it was necessary to provide additional Vendor Class Identifier (option 60) for the DHCP server to answer.

Here is a sample code:
```c
// Define two functions to provide the DHCP options:

/** DHCP custom option parser.
* At the moment will extract DNS domain (option 15) and store it separately).
*
* \warning The code must read() through the option even if it is unknown.
* @param optionType DHCP option type as per standard.
* @param dhcpUdpSocket Reference to UDP socket where to read the data from.
*/
void dhcpOptionParser(const uint8_t optionType, EthernetUDP *dhcpUdpSocket) {
uint8_t opt_len = dhcpUdpSocket->read();

if (optionType == 15) { // domain name
// read the value with dhcpUdpSocket->read()
} else {
while (opt_len--) {
dhcpUdpSocket->read();
}
}
}

/** DHCP option provider.
* Send Vendor Class Identifier (60) option as part of DHCP request.
* @param messageType type of outgoing DHCP message
* @param dhcpUdpSocket socket to write to
*/
void dhcpOptionProvider(const uint8_t messageType, EthernetUDP *dhcpUdpSocket) {
uint8_t buffer[] = {
dhcpClassIdentifier,
0x08,
'M','S','F','T',' ','5','.','0'
};
dhcpUdpSocket->write(buffer, 10);
}

//...

// Initialize the Ethernet library in setup() phase
void setup() {
// ...
Ethernet.begin(mac, (DhcpOptionParser *) &dhcpOptionParser, (DhcpOptionProvider *) &dhcpOptionProvider);
// ...
}
```

Please refer to dhcp-options(5) http://linux.die.net/man/5/dhcp-options chapter "Defining New Options" for an example on how to provide custom options with isc-dhcp server. Other DHCP servers should have similar support.