📄 dhcp.c
字号:
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE, ("dhcp_discover: sendto(DISCOVER, IP_ADDR_BROADCAST, DHCP_SERVER_PORT)\n"));
udp_sendto(dhcp->pcb, dhcp->p_out, IP_ADDR_BROADCAST, DHCP_SERVER_PORT);
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE, ("dhcp_discover: deleting()ing\n"));
dhcp_delete_request(netif);
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE | DBG_STATE, ("dhcp_discover: SELECTING\n"));
dhcp_set_state(dhcp, DHCP_SELECTING);
} else {
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE | 2, ("dhcp_discover: could not allocate DHCP request\n"));
}
dhcp->tries++;
msecs = dhcp->tries < 4 ? (dhcp->tries + 1) * 1000 : 10 * 1000;
dhcp->request_timeout = (msecs + DHCP_FINE_TIMER_MSECS - 1) / DHCP_FINE_TIMER_MSECS;
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE | DBG_STATE, ("dhcp_discover(): set request timeout %u msecs\n", msecs));
return result;
}
/**
* Bind the interface to the offered IP address.
*
* @param netif network interface to bind to the offered address
*/
static void dhcp_bind(struct netif *netif)
{
struct dhcp *dhcp = netif->dhcp;
struct ip_addr sn_mask, gw_addr;
LWIP_ASSERT("dhcp_bind: netif != NULL", netif != NULL);
LWIP_ASSERT("dhcp_bind: dhcp != NULL", dhcp != NULL);
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE | 3, ("dhcp_bind(netif=%p) %c%c%u\n", netif, netif->name[0], netif->name[1], netif->num));
/* temporary DHCP lease? */
if (dhcp->offered_t1_renew != 0xffffffffUL) {
/* set renewal period timer */
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE, ("dhcp_bind(): t1 renewal timer %lu secs\n", dhcp->offered_t1_renew));
dhcp->t1_timeout = (dhcp->offered_t1_renew + DHCP_COARSE_TIMER_SECS / 2) / DHCP_COARSE_TIMER_SECS;
if (dhcp->t1_timeout == 0) dhcp->t1_timeout = 1;
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE | DBG_STATE, ("dhcp_bind(): set request timeout %u msecs\n", dhcp->offered_t1_renew*1000));
}
/* set renewal period timer */
if (dhcp->offered_t2_rebind != 0xffffffffUL) {
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE, ("dhcp_bind(): t2 rebind timer %lu secs\n", dhcp->offered_t2_rebind));
dhcp->t2_timeout = (dhcp->offered_t2_rebind + DHCP_COARSE_TIMER_SECS / 2) / DHCP_COARSE_TIMER_SECS;
if (dhcp->t2_timeout == 0) dhcp->t2_timeout = 1;
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE | DBG_STATE, ("dhcp_bind(): set request timeout %u msecs\n", dhcp->offered_t2_rebind*1000));
}
/* copy offered network mask */
ip_addr_set(&sn_mask, &dhcp->offered_sn_mask);
/* subnet mask not given? */
/* TODO: this is not a valid check. what if the network mask is 0? */
if (sn_mask.addr == 0) {
/* choose a safe subnet mask given the network class */
u8_t first_octet = ip4_addr1(&sn_mask);
if (first_octet <= 127) sn_mask.addr = htonl(0xff000000);
else if (first_octet >= 192) sn_mask.addr = htonl(0xffffff00);
else sn_mask.addr = htonl(0xffff0000);
}
ip_addr_set(&gw_addr, &dhcp->offered_gw_addr);
/* gateway address not given? */
if (gw_addr.addr == 0) {
/* copy network address */
gw_addr.addr = (dhcp->offered_ip_addr.addr & sn_mask.addr);
/* use first host address on network as gateway */
gw_addr.addr |= htonl(0x00000001);
}
LWIP_DEBUGF(DHCP_DEBUG | DBG_STATE, ("dhcp_bind(): IP: 0x%08lx\n", dhcp->offered_ip_addr.addr));
netif_set_ipaddr(netif, &dhcp->offered_ip_addr);
LWIP_DEBUGF(DHCP_DEBUG | DBG_STATE, ("dhcp_bind(): SN: 0x%08lx\n", sn_mask.addr));
netif_set_netmask(netif, &sn_mask);
LWIP_DEBUGF(DHCP_DEBUG | DBG_STATE, ("dhcp_bind(): GW: 0x%08lx\n", gw_addr.addr));
netif_set_gw(netif, &gw_addr);
/* netif is now bound to DHCP leased address */
dhcp_set_state(dhcp, DHCP_BOUND);
}
/**
* Renew an existing DHCP lease at the involved DHCP server.
*
* @param netif network interface which must renew its lease
*/
err_t dhcp_renew(struct netif *netif)
{
struct dhcp *dhcp = netif->dhcp;
err_t result;
u16_t msecs;
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE | 3, ("dhcp_renew()\n"));
dhcp_set_state(dhcp, DHCP_RENEWING);
/* create and initialize the DHCP message header */
result = dhcp_create_request(netif);
if (result == ERR_OK) {
dhcp_option(dhcp, DHCP_OPTION_MESSAGE_TYPE, DHCP_OPTION_MESSAGE_TYPE_LEN);
dhcp_option_byte(dhcp, DHCP_REQUEST);
dhcp_option(dhcp, DHCP_OPTION_MAX_MSG_SIZE, DHCP_OPTION_MAX_MSG_SIZE_LEN);
/* TODO: use netif->mtu in some way */
dhcp_option_short(dhcp, 576);
#if 0
dhcp_option(dhcp, DHCP_OPTION_REQUESTED_IP, 4);
dhcp_option_long(dhcp, ntohl(dhcp->offered_ip_addr.addr));
#endif
#if 0
dhcp_option(dhcp, DHCP_OPTION_SERVER_ID, 4);
dhcp_option_long(dhcp, ntohl(dhcp->server_ip_addr.addr));
#endif
/* append DHCP message trailer */
dhcp_option_trailer(dhcp);
pbuf_realloc(dhcp->p_out, sizeof(struct dhcp_msg) - DHCP_OPTIONS_LEN + dhcp->options_out_len);
udp_bind(dhcp->pcb, IP_ADDR_ANY, DHCP_CLIENT_PORT);
udp_connect(dhcp->pcb, &dhcp->server_ip_addr, DHCP_SERVER_PORT);
udp_send(dhcp->pcb, dhcp->p_out);
dhcp_delete_request(netif);
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE | DBG_STATE, ("dhcp_renew: RENEWING\n"));
} else {
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE | 2, ("dhcp_renew: could not allocate DHCP request\n"));
}
dhcp->tries++;
/* back-off on retries, but to a maximum of 20 seconds */
msecs = dhcp->tries < 10 ? dhcp->tries * 2000 : 20 * 1000;
dhcp->request_timeout = (msecs + DHCP_FINE_TIMER_MSECS - 1) / DHCP_FINE_TIMER_MSECS;
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE | DBG_STATE, ("dhcp_renew(): set request timeout %u msecs\n", msecs));
return result;
}
/**
* Rebind with a DHCP server for an existing DHCP lease.
*
* @param netif network interface which must rebind with a DHCP server
*/
static err_t dhcp_rebind(struct netif *netif)
{
struct dhcp *dhcp = netif->dhcp;
err_t result;
u16_t msecs;
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE | DBG_STATE, ("dhcp_rebind()\n"));
dhcp_set_state(dhcp, DHCP_REBINDING);
/* create and initialize the DHCP message header */
result = dhcp_create_request(netif);
if (result == ERR_OK)
{
dhcp_option(dhcp, DHCP_OPTION_MESSAGE_TYPE, DHCP_OPTION_MESSAGE_TYPE_LEN);
dhcp_option_byte(dhcp, DHCP_REQUEST);
dhcp_option(dhcp, DHCP_OPTION_MAX_MSG_SIZE, DHCP_OPTION_MAX_MSG_SIZE_LEN);
dhcp_option_short(dhcp, 576);
#if 0
dhcp_option(dhcp, DHCP_OPTION_REQUESTED_IP, 4);
dhcp_option_long(dhcp, ntohl(dhcp->offered_ip_addr.addr));
dhcp_option(dhcp, DHCP_OPTION_SERVER_ID, 4);
dhcp_option_long(dhcp, ntohl(dhcp->server_ip_addr.addr));
#endif
dhcp_option_trailer(dhcp);
pbuf_realloc(dhcp->p_out, sizeof(struct dhcp_msg) - DHCP_OPTIONS_LEN + dhcp->options_out_len);
/* set remote IP association to any DHCP server */
udp_bind(dhcp->pcb, IP_ADDR_ANY, DHCP_CLIENT_PORT);
udp_connect(dhcp->pcb, IP_ADDR_ANY, DHCP_SERVER_PORT);
/* broadcast to server */
udp_sendto(dhcp->pcb, dhcp->p_out, IP_ADDR_BROADCAST, DHCP_SERVER_PORT);
dhcp_delete_request(netif);
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE | DBG_STATE, ("dhcp_rebind: REBINDING\n"));
} else {
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE | 2, ("dhcp_rebind: could not allocate DHCP request\n"));
}
dhcp->tries++;
msecs = dhcp->tries < 10 ? dhcp->tries * 1000 : 10 * 1000;
dhcp->request_timeout = (msecs + DHCP_FINE_TIMER_MSECS - 1) / DHCP_FINE_TIMER_MSECS;
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE | DBG_STATE, ("dhcp_rebind(): set request timeout %u msecs\n", msecs));
return result;
}
/**
* Release a DHCP lease.
*
* @param netif network interface which must release its lease
*/
static err_t dhcp_release(struct netif *netif)
{
struct dhcp *dhcp = netif->dhcp;
err_t result;
u16_t msecs;
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE | 3, ("dhcp_release()\n"));
/* idle DHCP client */
dhcp_set_state(dhcp, DHCP_OFF);
/* create and initialize the DHCP message header */
result = dhcp_create_request(netif);
if (result == ERR_OK) {
dhcp_option(dhcp, DHCP_OPTION_MESSAGE_TYPE, DHCP_OPTION_MESSAGE_TYPE_LEN);
dhcp_option_byte(dhcp, DHCP_RELEASE);
dhcp_option_trailer(dhcp);
pbuf_realloc(dhcp->p_out, sizeof(struct dhcp_msg) - DHCP_OPTIONS_LEN + dhcp->options_out_len);
udp_bind(dhcp->pcb, IP_ADDR_ANY, DHCP_CLIENT_PORT);
udp_connect(dhcp->pcb, &dhcp->server_ip_addr, DHCP_SERVER_PORT);
udp_send(dhcp->pcb, dhcp->p_out);
dhcp_delete_request(netif);
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE | DBG_STATE, ("dhcp_release: RELEASED, DHCP_OFF\n"));
} else {
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE | 2, ("dhcp_release: could not allocate DHCP request\n"));
}
dhcp->tries++;
msecs = dhcp->tries < 10 ? dhcp->tries * 1000 : 10 * 1000;
dhcp->request_timeout = (msecs + DHCP_FINE_TIMER_MSECS - 1) / DHCP_FINE_TIMER_MSECS;
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE | DBG_STATE, ("dhcp_release(): set request timeout %u msecs\n", msecs));
/* remove IP address from interface */
netif_set_ipaddr(netif, IP_ADDR_ANY);
netif_set_gw(netif, IP_ADDR_ANY);
netif_set_netmask(netif, IP_ADDR_ANY);
/* TODO: netif_down(netif); */
return result;
}
/**
* Remove the DHCP client from the interface.
*
* @param netif The network interface to stop DHCP on
*/
void dhcp_stop(struct netif *netif)
{
struct dhcp *dhcp = netif->dhcp;
LWIP_ASSERT("dhcp_stop: netif != NULL", netif != NULL);
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE | 3, ("dhcp_stop()\n"));
/* netif is DHCP configured? */
if (dhcp != NULL)
{
if (dhcp->pcb != NULL)
{
udp_remove(dhcp->pcb);
dhcp->pcb = NULL;
}
if (dhcp->p != NULL)
{
pbuf_free(dhcp->p);
dhcp->p = NULL;
}
/* free unfolded reply */
dhcp_free_reply(dhcp);
mem_free((void *)dhcp);
netif->dhcp = NULL;
}
}
/*
* Set the DHCP state of a DHCP client.
*
* If the state changed, reset the number of tries.
*
* TODO: we might also want to reset the timeout here?
*/
static void dhcp_set_state(struct dhcp *dhcp, unsigned char new_state)
{
if (new_state != dhcp->state)
{
dhcp->state = new_state;
dhcp->tries = 0;
}
}
/*
* Concatenate an option type and length field to the outgoing
* DHCP message.
*
*/
static void dhcp_option(struct dhcp *dhcp, u8_t option_type, u8_t option_len)
{
LWIP_ASSERT("dhcp_option_short: dhcp->options_out_len + 2 + option_len <= DHCP_OPTIONS_LEN", dhcp->options_out_len + 2 + option_len <= DHCP_OPTIONS_LEN);
dhcp->msg_out->options[dhcp->options_out_len++] = option_type;
dhcp->msg_out->options[dhcp->options_out_len++] = option_len;
}
/*
* Concatenate a single byte to the outgoing DHCP message.
*
*/
static void dhcp_option_byte(struct dhcp *dhcp, u8_t value)
{
LWIP_ASSERT("dhcp_option_short: dhcp->options_out_len < DHCP_OPTIONS_LEN", dhcp->options_out_len < DHCP_OPTIONS_LEN);
dhcp->msg_out->options[dhcp->options_out_len++] = value;
}
static void dhcp_option_short(struct dhcp *dhcp, u16_t value)
{
LWIP_ASSERT("dhcp_option_short: dhcp->options_out_len + 2 <= DHCP_OPTIONS_LEN", dhcp->options_out_len + 2 <= DHCP_OPTIONS_LEN);
dhcp->msg_out->options[dhcp->options_out_len++] = (value & 0xff00U) >> 8;
dhcp->msg_out->options[dhcp->options_out_len++] = value & 0x00ffU;
}
static void dhcp_option_long(struct dhcp *dhcp, u32_t value)
{
LWIP_ASSERT("dhcp_option_long: dhcp->options_out_len + 4 <= DHCP_OPTIONS_LEN", dhcp->options_out_len + 4 <= DHCP_OPTIONS_LEN);
dhcp->msg_out->options[dhcp->options_out_len++] = (value & 0xff000000UL) >> 24;
dhcp->msg_out->options[dhcp->options_out_len++] = (value & 0x00ff0000UL) >> 16;
dhcp->msg_out->options[dhcp->options_out_len++] = (value & 0x0000ff00UL) >> 8;
dhcp->msg_out->options[dhcp->options_out_len++] = (value & 0x000000ffUL);
}
/**
* Extract the DHCP message and the DHCP options.
*
* Extract the DHCP message and the DHCP options, each into a contiguous
* piece of memory. As a DHCP message is variable sized by its options,
* and also allows overriding some fields for options, the easy approach
* is to first unfold the options into a conitguous piece of memory, and
* use that further on.
*
*/
static err_t dhcp_unfold_reply(struct dhcp *dhcp)
{
struct pbuf *p = dhcp->p;
u8_t *ptr;
u16_t i;
u16_t j = 0;
LWIP_ASSERT("dhcp->p != NULL", dhcp->p != NULL);
/* free any left-overs from previous unfolds */
dhcp_free_reply(dhcp);
/* options present? */
if (dhcp->p->tot_len > (sizeof(struct dhcp_msg) - DHCP_OPTIONS_LEN))
{
dhcp->options_in_len = dhcp->p->tot_len - (sizeof(struct dhcp_msg) - DHCP_OPTIONS_LEN);
dhcp->options_in = mem_malloc(dhcp->options_in_len);
if (dhcp->options_in == NULL)
{
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE | 2, ("dhcp_unfold_reply(): could not allocate dhcp->options\n"));
return ERR_MEM;
}
}
dhcp->msg_in = mem_malloc(sizeof(struct dhcp_msg) - DHCP_OPTIONS_LEN);
if (dhcp->msg_in == NULL)
{
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE | 2, ("dhcp_unfold_reply(): could not allocate dhcp->msg_in\n"));
mem_free((void *)dhcp->options_in);
dhcp->options_in = NULL;
return ERR_MEM;
}
ptr = (u8_t *)dhcp->msg_in;
/* proceed through struct dhcp_msg */
for (i = 0; i < sizeof(struct dhcp_msg) - DHCP_OPTIONS_LEN; i++)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -