The Dynamic Host Configuration Protocol (DHCP) is used for getting IP configuration data from a server.
This is Adam Dunkels' DHCP client implementation on top of his uIP TCP/IP stack — Albrecht Weinert's adaption to weAutSys (and weAutSys — Automation runtime system weAut_01):
Besides the impact on dhcp.c and dhcp.h those improvements implied some changes on other uIP files, too.
Files | |
file | dhcpc.h |
Definitions for the DHCP client. | |
Data Structures | |
struct | dhcpMsg_t |
The DHCP message structure. More... | |
struct | dhcpOption_t |
The structure of a DHCP message option field. More... | |
struct | dhcpState_t |
Structure for DHCP application state. More... | |
Defines | |
#define | BOOTP_BROADCAST 0x8000 |
DHCP message flag value. | |
#define | DHCP_HLEN_ETHERNET 6 |
DHCP message hardware address length. | |
#define | DHCP_HTYPE_ETHERNET 1 |
DHCP message type value. | |
#define | DHCP_OPTION_BROADCAST_ADDR 28 |
DHCP option field code: broadcast address. | |
#define | DHCP_OPTION_CLIENT_NAME 12 |
DHCP option field code: host name. | |
#define | DHCP_OPTION_DEFAULT_TTL 23 |
DHCP option field code: default IP TTL. | |
#define | DHCP_OPTION_DNS_SERVER 6 |
DHCP option field code. | |
#define | DHCP_OPTION_DOMAIN_NAME 15 |
DHCP option field code: domain name. | |
#define | DHCP_OPTION_END 255 |
DHCP option field code: end of all options. | |
#define | DHCP_OPTION_LEASE_TIME 51 |
The address lease time. | |
#define | DHCP_OPTION_MSG_TYPE 53 |
DHCP option field code: message type. | |
#define | DHCP_OPTION_NAME_SERVERS 5 |
DHCP option field code: name server(s) | |
#define | DHCP_OPTION_NTP_SERVERS |
DHCP option field code: time server(s) | |
#define | DHCP_OPTION_REBND_TIME 59 |
DHCP option field code. | |
#define | DHCP_OPTION_RENEW_TIME 58 |
DHCP option field code. | |
#define | DHCP_OPTION_REQ_IPADDR 50 |
DHCP option field code. | |
#define | DHCP_OPTION_REQ_LIST 55 |
DHCP request option field code: Parameter Request List. | |
#define | DHCP_OPTION_ROUTER 3 |
DHCP option field code. | |
#define | DHCP_OPTION_SERVER_ID 54 |
DHCP option field code: DHCH server IP address. | |
#define | DHCP_OPTION_SMTP_SERVERS 69 |
DHCP option field code: SMTP server(s) | |
#define | DHCP_OPTION_SUBNET_MASK 1 |
DHCP option field code. | |
#define | DHCP_OPTION_TIME_OFFSET 2 |
DHCP option field code: time offset. | |
#define | DHCP_OPTION_TIME_SERVERS 4 |
DHCP option field code: time server(s) | |
#define | DHCP_REPLY 2 |
DHCP message operation type. | |
#define | DHCP_REQUEST 1 |
DHCP message operation type. | |
#define | DHCPACK 5 |
DHCP message type. | |
#define | DHCPC_CLIENT_PORT 68 |
Used DHCP client port. | |
#define | DHCPC_SERVER_PORT 67 |
Well known DHCP server port. | |
#define | DHCPDECLINE 4 |
DHCP message type. | |
#define | DHCPDISCOVER 1 |
DHCP message type. | |
#define | DHCPINFORM 8 |
DHCP message type. | |
#define | DHCPNAK 6 |
DHCP message type. | |
#define | DHCPOFFER 2 |
DHCP message type. | |
#define | DHCPRELEASE 7 |
DHCP message type. | |
#define | DHCPREQUEST 3 |
DHCP message type. | |
#define | STATE_CONFIG_RECEIVED 3 |
DHCP state machine state. | |
#define | STATE_INITIAL 0 |
DHCP state machine state. | |
#define | STATE_OFFER_RECEIVED 2 |
DHCP state machine state. | |
#define | STATE_SENDING 1 |
DHCP state machine state. | |
Functions | |
ptfnct_t | dhcpc_appcall (void) |
Handle DHCP server events. | |
void | dhcpc_configured (uint8_t respType, const uint16_t ipAddr[]) |
DHCP success. | |
void | dhcpcGotOption (const struct dhcpOption_t *dhcpOption) |
DHCP option received. | |
void | dhcpInit (void const *mac_addr) |
Initialise the DHCP (client) | |
void | dhcpReset (void) |
Reset the DHCP (client) | |
Variables | |
struct dhcpState_t | dhcpState |
DHCP application state. |
#define DHCP_OPTION_MSG_TYPE 53 |
DHCP option field code: message type.
This is usually (if not always) the first option in a DHCP package. It redundantly gives the message type in form of an option.
The rationale behind is to inform an option parser / option handler in an uniform way about the nature of the package in question.
Especially DHCPACK would mean that all other options are valid and meant to be used by the client.
For the possible options see also RFC2132.
#define DHCP_OPTION_TIME_OFFSET 2 |
DHCP option field code: time offset.
This is the time offset of the client's subnet (location) in seconds relative to UTC. This value can be used to calculate local time from NTP server responses.
#define DHCP_OPTION_TIME_SERVERS 4 |
DHCP option field code: time server(s)
This is a list of 1..n time server addresses with decreasing preference. By DHCP definition those servers use time protocol (RFC 868, port 37) not NTP (RFC 1305, port 123).
See also: DHCP_OPTION_NTP_SERVERS
#define DHCP_OPTION_NAME_SERVERS 5 |
DHCP option field code: name server(s)
This is a list of 1..n name server addresses with decreasing preference.
#define DHCP_OPTION_CLIENT_NAME 12 |
DHCP option field code: host name.
This is to give the client a name by the central organisation.
#define DHCP_OPTION_DOMAIN_NAME 15 |
DHCP option field code: domain name.
This is to give the domain name (sometimes called DNS suffix) that client should use when resolving hostnames via the Domain Name System (DNS). This option can deliver just one sufix, not a list of.
#define DHCP_OPTION_DEFAULT_TTL 23 |
DHCP option field code: default IP TTL.
This is the (default) time to live for outgoing connections.
#define DHCP_OPTION_NTP_SERVERS |
DHCP option field code: time server(s)
This is a list of 1..n NTP server addresses with decreasing preference. Those servers use NTP (RFC 1305, port 123).
See also: DHCP_OPTION_TIME_SERVERS
#define DHCP_OPTION_LEASE_TIME 51 |
The address lease time.
This option is the requested respectively granted lease time for the address given by the DHCP server in units of seconds (unsigned 32 bit, wrong endian).
Time to renew (T1) is 50% of the lease time by default and may be sent by the server as DHCP_OPTION_RENEW_TIME option too.
Time to rebind (T2) is 87.5% (7/8) of the lease time by default and may be sent by the server as DHCP_OPTION_REBND_TIME option too.
#define DHCP_OPTION_SERVER_ID 54 |
DHCP option field code: DHCH server IP address.
This option allows to distinguish several DHCP servers in requests and offers. DHCP option field code
#define DHCP_OPTION_SMTP_SERVERS 69 |
DHCP option field code: SMTP server(s)
This is a list of 1..n server addresses for the Simple Mail Transport Protocol (SMTP) with decreasing preference.
#define DHCP_OPTION_END 255 |
DHCP option field code: end of all options.
This is always the last option in a DHCP package. It is empty and just signals the end of the list. DHCP option field code
void dhcpInit | ( | void const * | mac_addr | ) |
Initialise the DHCP (client)
This function initialises the DHCP structures and state machine (= protothread) like does dhcpReset. Then it is tried to establish connections to DHCP server and client port(s).
The protocol is not started by this function; the work is done by dhcpc_appcall.
mac_addr | pointer to the device's MAC address |
void dhcpReset | ( | void | ) |
Reset the DHCP (client)
This function resets all DHCP state to initial. In contrast to dhcpInit no trial to connect to DHCP ports is made and the protocol is not (not even indirectly) started.
ptfnct_t dhcpc_appcall | ( | void | ) |
Handle DHCP server events.
This function handles DHCP events. And it is a (as a protothread) the DHCP client state machine. It has to be called in the udp_appcall in a manner like
const uint16_t remPort = convert16endian(uip_udp_conn->rport); if(remPort == DHCPC_SERVER_PORT) { // DHCP protocol dhcpc_appcall(); return; } // DHCP protocol
void dhcpcGotOption | ( | const struct dhcpOption_t * | dhcpOption | ) |
DHCP option received.
This (callback) signals an option send by the DHCP server. Its up to the implementing application or system software to utilise (or ignore) the respective values.
dhcpOption | the option (one of the options) received |
void dhcpc_configured | ( | uint8_t | respType, |
const uint16_t | ipAddr[] | ||
) |
DHCP success.
This (callback) signals a successful walk through the DHCP protocol steps ended by acknowledge.
respType | at present always DHCPACK; (no callback in other cases yet) |
ipAddr | the assigned / leased IP address (in case of success) |