Microchip® Advanced Software Framework

dhcp.h File Reference
#include "lwip/opt.h"
#include "lwip/netif.h"
#include "lwip/udp.h"

Data Structures

struct  dhcp
 
struct  dhcp_msg
 minimum set of fields of any DHCP message More...
 

Macros

#define DHCP_ACK   5
 
#define DHCP_AUTOIP_COOP_STATE_OFF   0
 AUTOIP cooperation flags. More...
 
#define DHCP_AUTOIP_COOP_STATE_ON   1
 
#define DHCP_BACKING_OFF   12
 not yet implemented #define DHCP_RELEASING 11 More...
 
#define DHCP_BOOTREPLY   2
 
#define DHCP_BOOTREQUEST   1
 
#define DHCP_BOUND   10
 
#define DHCP_CHADDR_LEN   16U
 
#define DHCP_CHADDR_OFS   28
 
#define DHCP_CHECKING   8
 
#define DHCP_CIADDR_OFS   12
 
#define DHCP_CLIENT_PORT   68
 
#define DHCP_COARSE_TIMER_MSECS   (DHCP_COARSE_TIMER_SECS * 1000UL)
 period (in milliseconds) of the application calling dhcp_coarse_tmr() More...
 
#define DHCP_COARSE_TIMER_SECS   60
 period (in seconds) of the application calling dhcp_coarse_tmr() More...
 
#define DHCP_COOKIE_OFS   DHCP_MSG_LEN
 
#define DHCP_DECLINE   4
 
#define DHCP_DISCOVER   1
 DHCP message types. More...
 
#define DHCP_FILE_LEN   128U
 
#define DHCP_FILE_OFS   108
 
#define DHCP_FINE_TIMER_MSECS   500
 period (in milliseconds) of the application calling dhcp_fine_tmr() More...
 
#define DHCP_FLAGS_OFS   10
 
#define DHCP_GIADDR_OFS   24
 
#define DHCP_HLEN_OFS   2
 
#define DHCP_HOPS_OFS   3
 
#define DHCP_HTYPE_ETH   1
 DHCP hardware type, currently only ethernet is supported. More...
 
#define DHCP_HTYPE_OFS   1
 
#define DHCP_INFORM   8
 
#define DHCP_INIT   2
 
#define DHCP_MAGIC_COOKIE   0x63825363UL
 
#define DHCP_MIN_OPTIONS_LEN   68U
 
#define DHCP_MSG_LEN   236
 
#define DHCP_NAK   6
 
#define DHCP_OFF   0
 DHCP client states. More...
 
#define DHCP_OFFER   2
 
#define DHCP_OP_OFS   0
 DHCP message item offsets and length. More...
 
#define DHCP_OPTION_BOOTFILE   67
 
#define DHCP_OPTION_BROADCAST   28
 
#define DHCP_OPTION_CLIENT_ID   61
 
#define DHCP_OPTION_DNS_SERVER   6
 
#define DHCP_OPTION_END   255
 
#define DHCP_OPTION_HOSTNAME   12
 
#define DHCP_OPTION_IP_TTL   23
 
#define DHCP_OPTION_LEASE_TIME   51 /* RFC 2132 9.2, time in seconds, in 4 bytes */
 
#define DHCP_OPTION_MAX_MSG_SIZE   57 /* RFC 2132 9.10, message size accepted >= 576 */
 
#define DHCP_OPTION_MAX_MSG_SIZE_LEN   2
 
#define DHCP_OPTION_MESSAGE_TYPE   53 /* RFC 2132 9.6, important for DHCP */
 
#define DHCP_OPTION_MESSAGE_TYPE_LEN   1
 
#define DHCP_OPTION_MTU   26
 
#define DHCP_OPTION_OVERLOAD   52 /* RFC2132 9.3, use file and/or sname field for options */
 
#define DHCP_OPTION_PAD   0
 BootP options. More...
 
#define DHCP_OPTION_PARAMETER_REQUEST_LIST   55 /* RFC 2132 9.8, requested option types */
 
#define DHCP_OPTION_REQUESTED_IP   50 /* RFC 2132 9.1, requested IP address */
 DHCP options. More...
 
#define DHCP_OPTION_ROUTER   3
 
#define DHCP_OPTION_SERVER_ID   54 /* RFC 2132 9.7, server IP address */
 
#define DHCP_OPTION_SUBNET_MASK   1 /* RFC 2132 3.3 */
 
#define DHCP_OPTION_T1   58 /* T1 renewal time */
 
#define DHCP_OPTION_T2   59 /* T2 rebinding time */
 
#define DHCP_OPTION_TCP_TTL   37
 
#define DHCP_OPTION_TFTP_SERVERNAME   66
 
#define DHCP_OPTION_US   60
 
#define DHCP_OPTIONS_LEN   DHCP_MIN_OPTIONS_LEN
 make sure user does not configure this too small More...
 
#define DHCP_OPTIONS_OFS   (DHCP_MSG_LEN + 4)
 
#define DHCP_OVERLOAD_FILE   1
 
#define DHCP_OVERLOAD_NONE   0
 possible combinations of overloading the file and sname fields with options More...
 
#define DHCP_OVERLOAD_SNAME   2
 
#define DHCP_OVERLOAD_SNAME_FILE   3
 
#define DHCP_REBINDING   4
 
#define DHCP_REBOOTING   3
 
#define DHCP_RELEASE   7
 
#define dhcp_remove_struct(netif)   do { (netif)->dhcp = NULL; } while(0)
 Remove a struct dhcp previously set to the netif using dhcp_set_struct() More...
 
#define DHCP_RENEWING   5
 
#define DHCP_REQUEST   3
 
#define DHCP_REQUESTING   1
 
#define DHCP_SECS_OFS   8
 
#define DHCP_SELECTING   6
 
#define DHCP_SERVER_PORT   67
 
#define DHCP_SIADDR_OFS   20
 
#define DHCP_SNAME_LEN   64U
 
#define DHCP_SNAME_OFS   44
 
#define DHCP_XID_OFS   4
 
#define DHCP_YIADDR_OFS   16
 

Functions

void dhcp_arp_reply (struct netif *netif, ip_addr_t *addr)
 if enabled, check whether the offered IP address is not in use, using ARP More...
 
void dhcp_cleanup (struct netif *netif)
 Removes a struct dhcp from a netif. More...
 
void dhcp_coarse_tmr (void)
 to be called every minute More...
 
void dhcp_fine_tmr (void)
 to be called every half second More...
 
void dhcp_inform (struct netif *netif)
 inform server of our manual IP address More...
 
void dhcp_network_changed (struct netif *netif)
 Handle a possible change in the network configuration. More...
 
err_t dhcp_release (struct netif *netif)
 release the DHCP lease, usually called before dhcp_stop() More...
 
err_t dhcp_renew (struct netif *netif)
 enforce early lease renewal (not needed normally) More...
 
PACK_STRUCT_END void dhcp_set_struct (struct netif *netif, struct dhcp *dhcp)
 Set a statically allocated struct dhcp to work with. More...
 
err_t dhcp_start (struct netif *netif)
 start DHCP configuration More...
 
void dhcp_stop (struct netif *netif)
 stop DHCP configuration More...
 

Variables

PACK_STRUCT_BEGIN struct dhcp_msg PACK_STRUCT_STRUCT
 

#define DHCP_ACK   5
#define DHCP_AUTOIP_COOP_STATE_OFF   0

AUTOIP cooperation flags.

Referenced by dhcp_bind(), dhcp_discover(), dhcp_network_changed(), and dhcp_stop().

#define DHCP_AUTOIP_COOP_STATE_ON   1
#define DHCP_BACKING_OFF   12

not yet implemented #define DHCP_RELEASING 11

Referenced by dhcp_decline(), dhcp_handle_nak(), and dhcp_timeout().

#define DHCP_BOOTREPLY   2

Referenced by dhcp_recv(), and lwip_dhcp_server_fn().

#define DHCP_BOOTREQUEST   1

Referenced by dhcp_create_msg().

#define DHCP_BOUND   10
#define DHCP_CHADDR_LEN   16U

Referenced by dhcp_create_msg(), and dhcp_recv().

#define DHCP_CHADDR_OFS   28

Referenced by lwip_dhcp_server_fn().

#define DHCP_CHECKING   8
#define DHCP_CIADDR_OFS   12
#define DHCP_CLIENT_PORT   68
#define DHCP_COARSE_TIMER_MSECS   (DHCP_COARSE_TIMER_SECS * 1000UL)

period (in milliseconds) of the application calling dhcp_coarse_tmr()

Referenced by dhcp_timer_coarse(), and sys_timeouts_init().

#define DHCP_COARSE_TIMER_SECS   60

period (in seconds) of the application calling dhcp_coarse_tmr()

Referenced by dhcp_bind().

#define DHCP_COOKIE_OFS   DHCP_MSG_LEN

Referenced by lwip_dhcp_server_fn().

#define DHCP_DECLINE   4

Referenced by dhcp_create_msg(), and dhcp_decline().

#define DHCP_DISCOVER   1

DHCP message types.

Referenced by dhcp_discover(), and lwip_dhcp_server_fn().

#define DHCP_FILE_LEN   128U
#define DHCP_FILE_OFS   108

Referenced by dhcp_parse_reply().

#define DHCP_FINE_TIMER_MSECS   500
#define DHCP_FLAGS_OFS   10

Referenced by lwip_dhcp_server_fn().

#define DHCP_GIADDR_OFS   24
#define DHCP_HLEN_OFS   2
#define DHCP_HOPS_OFS   3
#define DHCP_HTYPE_ETH   1

DHCP hardware type, currently only ethernet is supported.

Referenced by dhcp_create_msg().

#define DHCP_HTYPE_OFS   1
#define DHCP_INFORM   8
#define DHCP_INIT   2

Referenced by dhcp_start().

#define DHCP_MAGIC_COOKIE   0x63825363UL
#define DHCP_MIN_OPTIONS_LEN   68U

Referenced by dhcp_option_trailer().

#define DHCP_MSG_LEN   236
#define DHCP_NAK   6

Referenced by dhcp_recv(), and lwip_dhcp_server_fn().

#define DHCP_OFF   0

DHCP client states.

Referenced by dhcp_network_changed(), dhcp_release(), and dhcp_stop().

#define DHCP_OFFER   2

Referenced by dhcp_recv(), and lwip_dhcp_server_fn().

#define DHCP_OP_OFS   0

DHCP message item offsets and length.

Referenced by lwip_dhcp_server_fn().

#define DHCP_OPTION_BOOTFILE   67
#define DHCP_OPTION_BROADCAST   28

Referenced by dhcp_discover(), and dhcp_select().

#define DHCP_OPTION_CLIENT_ID   61
#define DHCP_OPTION_DNS_SERVER   6
#define DHCP_OPTION_END   255
#define DHCP_OPTION_HOSTNAME   12
#define DHCP_OPTION_IP_TTL   23
#define DHCP_OPTION_LEASE_TIME   51 /* RFC 2132 9.2, time in seconds, in 4 bytes */
#define DHCP_OPTION_MAX_MSG_SIZE   57 /* RFC 2132 9.10, message size accepted >= 576 */
#define DHCP_OPTION_MAX_MSG_SIZE_LEN   2
#define DHCP_OPTION_MESSAGE_TYPE   53 /* RFC 2132 9.6, important for DHCP */
#define DHCP_OPTION_MESSAGE_TYPE_LEN   1

Referenced by dhcp_create_msg().

#define DHCP_OPTION_MTU   26
#define DHCP_OPTION_OVERLOAD   52 /* RFC2132 9.3, use file and/or sname field for options */

Referenced by dhcp_parse_reply().

#define DHCP_OPTION_PAD   0

BootP options.

Referenced by dhcp_parse_reply().

#define DHCP_OPTION_PARAMETER_REQUEST_LIST   55 /* RFC 2132 9.8, requested option types */

Referenced by dhcp_discover(), and dhcp_select().

#define DHCP_OPTION_REQUESTED_IP   50 /* RFC 2132 9.1, requested IP address */
#define DHCP_OPTION_ROUTER   3
#define DHCP_OPTION_SERVER_ID   54 /* RFC 2132 9.7, server IP address */
#define DHCP_OPTION_SUBNET_MASK   1 /* RFC 2132 3.3 */
#define DHCP_OPTION_T1   58 /* T1 renewal time */
#define DHCP_OPTION_T2   59 /* T2 rebinding time */
#define DHCP_OPTION_TCP_TTL   37
#define DHCP_OPTION_TFTP_SERVERNAME   66
#define DHCP_OPTION_US   60
#define DHCP_OPTIONS_LEN   DHCP_MIN_OPTIONS_LEN

make sure user does not configure this too small

allow this to be configured in lwipopts.h, but not too small set this to be sufficient for your options in outgoing DHCP msgs

Referenced by dhcp_create_msg(), dhcp_decline(), dhcp_discover(), dhcp_inform(), dhcp_option(), dhcp_option_byte(), dhcp_option_long(), dhcp_option_short(), dhcp_option_trailer(), dhcp_rebind(), dhcp_reboot(), dhcp_release(), dhcp_renew(), and dhcp_select().

#define DHCP_OPTIONS_OFS   (DHCP_MSG_LEN + 4)
#define DHCP_OVERLOAD_FILE   1

Referenced by dhcp_parse_reply().

#define DHCP_OVERLOAD_NONE   0

possible combinations of overloading the file and sname fields with options

#define DHCP_OVERLOAD_SNAME   2

Referenced by dhcp_parse_reply().

#define DHCP_OVERLOAD_SNAME_FILE   3

Referenced by dhcp_parse_reply().

#define DHCP_REBINDING   4
#define DHCP_REBOOTING   3
#define DHCP_RELEASE   7

Referenced by dhcp_release().

#define dhcp_remove_struct (   netif)    do { (netif)->dhcp = NULL; } while(0)

Remove a struct dhcp previously set to the netif using dhcp_set_struct()

#define DHCP_REQUESTING   1
#define DHCP_SECS_OFS   8

Referenced by lwip_dhcp_server_fn().

#define DHCP_SELECTING   6
#define DHCP_SIADDR_OFS   20

Referenced by lwip_dhcp_server_fn().

#define DHCP_SNAME_LEN   64U
#define DHCP_SNAME_OFS   44

Referenced by dhcp_parse_reply().

#define DHCP_XID_OFS   4
#define DHCP_YIADDR_OFS   16

Referenced by lwip_dhcp_server_fn().

void dhcp_arp_reply ( struct netif netif,
ip_addr_t addr 
)

if enabled, check whether the offered IP address is not in use, using ARP

if enabled, check whether the offered IP address is not in use, using ARP

Parameters
netifthe network interface on which the reply was received
addrThe IP address we received a reply from

References netif::dhcp, DHCP_CHECKING, DHCP_DEBUG, dhcp_decline(), ip4_addr_get_u32, ip_addr_cmp, LWIP_DBG_LEVEL_WARNING, LWIP_DBG_STATE, LWIP_DBG_TRACE, LWIP_DEBUGF, LWIP_ERROR(), NULL, dhcp::offered_ip_addr, dhcp::state, and X32_F.

Referenced by etharp_arp_input().

void dhcp_cleanup ( struct netif netif)

Removes a struct dhcp from a netif.

ATTENTION: Only use this when not using dhcp_set_struct() to allocate the struct dhcp since the memory is passed back to the heap.

Parameters
netifthe netif from which to remove the struct dhcp

References netif::dhcp, LWIP_ASSERT, mem_free, and NULL.

void dhcp_coarse_tmr ( void  )
void dhcp_fine_tmr ( void  )

to be called every half second

to be called every half second

A DHCP server is expected to respond within a short period of time. This timer checks whether an outstanding DHCP request is timed out.

References netif::dhcp, DHCP_DEBUG, dhcp_timeout(), LWIP_DBG_STATE, LWIP_DBG_TRACE, LWIP_DEBUGF, netif_list, netif::next, NULL, and dhcp::request_timeout.

Referenced by dhcp_timer_fine().

void dhcp_inform ( struct netif netif)

inform server of our manual IP address

inform server of our manual IP address

This informs DHCP servers of our fixed IP address configuration by sending an INFORM message. It does not involve DHCP address configuration, it is just here to be nice to the network.

Parameters
netifThe lwIP network interface

References netif::dhcp, DHCP_CLIENT_PORT, dhcp_create_msg(), DHCP_DEBUG, dhcp_delete_msg(), DHCP_INFORM, DHCP_MAX_MSG_LEN, dhcp_option(), DHCP_OPTION_MAX_MSG_SIZE, DHCP_OPTION_MAX_MSG_SIZE_LEN, dhcp_option_short(), dhcp_option_trailer(), DHCP_OPTIONS_LEN, DHCP_SERVER_PORT, dhcp_set_state(), ERR_OK, IP_ADDR_ANY, IP_ADDR_BROADCAST, ip_set_option, LWIP_DBG_LEVEL_SERIOUS, LWIP_DBG_STATE, LWIP_DBG_TRACE, LWIP_DEBUGF, LWIP_ERROR(), NULL, dhcp::options_out_len, dhcp::p_out, pbuf_realloc(), dhcp::pcb, SOF_BROADCAST, udp_bind(), udp_new(), udp_remove(), and udp_sendto_if().

void dhcp_network_changed ( struct netif netif)

Handle a possible change in the network configuration.

This enters the REBOOTING state to verify that the currently bound address is still valid.

References autoip_stop(), netif::dhcp, DHCP_AUTOIP_COOP_STATE_OFF, DHCP_AUTOIP_COOP_STATE_ON, DHCP_BOUND, dhcp_discover(), DHCP_OFF, DHCP_REBINDING, dhcp_reboot(), DHCP_REBOOTING, DHCP_RENEWING, netif_set_down(), dhcp::state, and dhcp::tries.

Referenced by netif_set_link_up().

PACK_STRUCT_END void dhcp_set_struct ( struct netif netif,
struct dhcp dhcp 
)

Set a statically allocated struct dhcp to work with.

Using this prevents dhcp_start to allocate it using mem_malloc.

Parameters
netifthe netif for which to set the struct dhcp
dhcp(uninitialised) dhcp struct allocated by the application

References netif::dhcp, LWIP_ASSERT, and NULL.

err_t dhcp_start ( struct netif netif)

start DHCP configuration

start DHCP configuration

If no DHCP client instance was attached to this interface, a new client is created first. If a DHCP client instance was already present, it restarts negotiation.

Parameters
netifThe lwIP network interface
Returns
lwIP error code
  • ERR_OK - No error
  • ERR_MEM - Out of memory

References netif::dhcp, DHCP_CLIENT_PORT, DHCP_DEBUG, dhcp_discover(), DHCP_INIT, DHCP_MAX_MSG_LEN_MIN_REQUIRED, dhcp_recv(), DHCP_SERVER_PORT, dhcp_set_state(), dhcp_stop(), ERR_ARG, ERR_MEM, ERR_OK, netif::flags, IP_ADDR_ANY, ip_set_option, LWIP_ASSERT, LWIP_DBG_STATE, LWIP_DBG_TRACE, LWIP_DEBUGF, LWIP_ERROR(), mem_malloc, netif::mtu, netif::name, NETIF_FLAG_DHCP, NETIF_FLAG_ETHARP, netif_is_link_up, NULL, netif::num, SOF_BROADCAST, U16_F, udp_bind(), udp_connect(), udp_new(), udp_recv(), and udp_remove().

Referenced by net_interface_up_imp(), and net_set_mode().

void dhcp_stop ( struct netif netif)

stop DHCP configuration

stop DHCP configuration

Parameters
netifThe network interface to stop DHCP on

References autoip_stop(), netif::dhcp, DHCP_AUTOIP_COOP_STATE_OFF, DHCP_AUTOIP_COOP_STATE_ON, DHCP_DEBUG, DHCP_OFF, dhcp_set_state(), netif::flags, LWIP_ASSERT, LWIP_DBG_TRACE, LWIP_DEBUGF, LWIP_ERROR(), NETIF_FLAG_DHCP, NULL, and udp_remove().

Referenced by dhcp_start(), net_interface_down_imp(), and net_set_mode().

PACK_STRUCT_BEGIN struct dhcp_msg PACK_STRUCT_STRUCT