From: Wu Jian Gang Date: Thu, 1 Sep 2016 11:05:50 +0000 (+0800) Subject: lwip: format dhcpserver.c X-Git-Tag: v0.9~69^2~5^2 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=87977b92f3e12cfca74cf2e4dea87dc8d60b26fc;p=esp-idf lwip: format dhcpserver.c --- diff --git a/components/lwip/apps/dhcpserver.c b/components/lwip/apps/dhcpserver.c index 82cd17815d..8abf7ca7c6 100644 --- a/components/lwip/apps/dhcpserver.c +++ b/components/lwip/apps/dhcpserver.c @@ -101,24 +101,35 @@ static dhcps_time_t dhcps_lease_time = DHCPS_LEASE_TIME_DEF; //minute *******************************************************************************/ void *dhcps_option_info(u8_t op_id, u32_t opt_len) { - void* option_arg = NULL; - switch (op_id){ - case IP_ADDRESS_LEASE_TIME: - if (opt_len == sizeof(dhcps_time_t)) - option_arg = &dhcps_lease_time; - break; - case REQUESTED_IP_ADDRESS: - if (opt_len == sizeof(dhcps_lease_t)) - option_arg = &dhcps_poll; - break; - case ROUTER_SOLICITATION_ADDRESS: - if (opt_len == sizeof(dhcps_offer_t)) - option_arg = &dhcps_offer; - break; - default: - break; - } - return option_arg; + void *option_arg = NULL; + + switch (op_id) { + case IP_ADDRESS_LEASE_TIME: + if (opt_len == sizeof(dhcps_time_t)) { + option_arg = &dhcps_lease_time; + } + + break; + + case REQUESTED_IP_ADDRESS: + if (opt_len == sizeof(dhcps_lease_t)) { + option_arg = &dhcps_poll; + } + + break; + + case ROUTER_SOLICITATION_ADDRESS: + if (opt_len == sizeof(dhcps_offer_t)) { + option_arg = &dhcps_offer; + } + + break; + + default: + break; + } + + return option_arg; } /****************************************************************************** @@ -128,37 +139,41 @@ void *dhcps_option_info(u8_t op_id, u32_t opt_len) * pinsert -- the insert node of the list * Returns : none *******************************************************************************/ -void node_insert_to_list(list_node **phead, list_node* pinsert) +void node_insert_to_list(list_node **phead, list_node *pinsert) { - list_node *plist = NULL; - struct dhcps_pool *pdhcps_pool = NULL; - struct dhcps_pool *pdhcps_node = NULL; - if (*phead == NULL) - *phead = pinsert; - else { - plist = *phead; - pdhcps_node = pinsert->pnode; - pdhcps_pool = plist->pnode; - - if(pdhcps_node->ip.addr < pdhcps_pool->ip.addr) { - pinsert->pnext = plist; - *phead = pinsert; - } else { + list_node *plist = NULL; + struct dhcps_pool *pdhcps_pool = NULL; + struct dhcps_pool *pdhcps_node = NULL; + + if (*phead == NULL) { + *phead = pinsert; + } else { + plist = *phead; + pdhcps_node = pinsert->pnode; + pdhcps_pool = plist->pnode; + + if (pdhcps_node->ip.addr < pdhcps_pool->ip.addr) { + pinsert->pnext = plist; + *phead = pinsert; + } else { while (plist->pnext != NULL) { pdhcps_pool = plist->pnext->pnode; + if (pdhcps_node->ip.addr < pdhcps_pool->ip.addr) { pinsert->pnext = plist->pnext; plist->pnext = pinsert; break; } + plist = plist->pnext; } - if(plist->pnext == NULL) { + if (plist->pnext == NULL) { plist->pnext = pinsert; } - } - } + } + } + // pinsert->pnext = NULL; } @@ -169,27 +184,29 @@ void node_insert_to_list(list_node **phead, list_node* pinsert) * pdelete -- the remove node of the list * Returns : none *******************************************************************************/ -void node_remove_from_list(list_node **phead, list_node* pdelete) +void node_remove_from_list(list_node **phead, list_node *pdelete) { - list_node *plist = NULL; - - plist = *phead; - if (plist == NULL){ - *phead = NULL; - } else { - if (plist == pdelete){ - *phead = plist->pnext; - pdelete->pnext = NULL; - } else { - while (plist != NULL) { - if (plist->pnext == pdelete){ - plist->pnext = pdelete->pnext; - pdelete->pnext = NULL; - } - plist = plist->pnext; - } - } - } + list_node *plist = NULL; + + plist = *phead; + + if (plist == NULL) { + *phead = NULL; + } else { + if (plist == pdelete) { + *phead = plist->pnext; + pdelete->pnext = NULL; + } else { + while (plist != NULL) { + if (plist->pnext == pdelete) { + plist->pnext = pdelete->pnext; + pdelete->pnext = NULL; + } + + plist = plist->pnext; + } + } + } } /****************************************************************************** @@ -198,12 +215,12 @@ void node_remove_from_list(list_node **phead, list_node* pdelete) * Parameters : optptr -- the addr of DHCP message option * Returns : the addr of DHCP message option *******************************************************************************/ -static u8_t* add_msg_type(u8_t *optptr, u8_t type) +static u8_t *add_msg_type(u8_t *optptr, u8_t type) { - *optptr++ = DHCP_OPTION_MSG_TYPE; - *optptr++ = 1; - *optptr++ = type; - return optptr; + *optptr++ = DHCP_OPTION_MSG_TYPE; + *optptr++ = 1; + *optptr++ = type; + return optptr; } /****************************************************************************** @@ -212,107 +229,107 @@ static u8_t* add_msg_type(u8_t *optptr, u8_t type) * Parameters : optptr -- the addr of DHCP message option * Returns : the addr of DHCP message option *******************************************************************************/ -static u8_t* add_offer_options(u8_t *optptr) +static u8_t *add_offer_options(u8_t *optptr) { - ip4_addr_t ipadd; + ip4_addr_t ipadd; - ipadd.addr = *( (u32_t *) &server_address); + ipadd.addr = *((u32_t *) &server_address); #ifdef USE_CLASS_B_NET - *optptr++ = DHCP_OPTION_SUBNET_MASK; - *optptr++ = 4; //length - *optptr++ = 255; - *optptr++ = 240; - *optptr++ = 0; - *optptr++ = 0; + *optptr++ = DHCP_OPTION_SUBNET_MASK; + *optptr++ = 4; //length + *optptr++ = 255; + *optptr++ = 240; + *optptr++ = 0; + *optptr++ = 0; #else - *optptr++ = DHCP_OPTION_SUBNET_MASK; - *optptr++ = 4; - *optptr++ = 255; - *optptr++ = 255; - *optptr++ = 255; - *optptr++ = 0; + *optptr++ = DHCP_OPTION_SUBNET_MASK; + *optptr++ = 4; + *optptr++ = 255; + *optptr++ = 255; + *optptr++ = 255; + *optptr++ = 0; #endif - *optptr++ = DHCP_OPTION_LEASE_TIME; - *optptr++ = 4; - *optptr++ = ((dhcps_lease_time * 60) >> 24) & 0xFF; - *optptr++ = ((dhcps_lease_time * 60) >> 16) & 0xFF; - *optptr++ = ((dhcps_lease_time * 60) >> 8) & 0xFF; - *optptr++ = ((dhcps_lease_time * 60) >> 0) & 0xFF; - - *optptr++ = DHCP_OPTION_SERVER_ID; - *optptr++ = 4; - *optptr++ = ip4_addr1( &ipadd); - *optptr++ = ip4_addr2( &ipadd); - *optptr++ = ip4_addr3( &ipadd); - *optptr++ = ip4_addr4( &ipadd); - - if (dhcps_router_enabled(dhcps_offer)){ - struct ip_info if_ip; - //bzero(&if_ip, sizeof(struct ip_info)); - memset(&if_ip ,0x00, sizeof(struct ip_info)); - - tcpip_adapter_get_ip_info(WIFI_IF_AP, &if_ip); - - *optptr++ = DHCP_OPTION_ROUTER; - *optptr++ = 4; - *optptr++ = ip4_addr1( &if_ip.gw); - *optptr++ = ip4_addr2( &if_ip.gw); - *optptr++ = ip4_addr3( &if_ip.gw); - *optptr++ = ip4_addr4( &if_ip.gw); - } + *optptr++ = DHCP_OPTION_LEASE_TIME; + *optptr++ = 4; + *optptr++ = ((dhcps_lease_time * 60) >> 24) & 0xFF; + *optptr++ = ((dhcps_lease_time * 60) >> 16) & 0xFF; + *optptr++ = ((dhcps_lease_time * 60) >> 8) & 0xFF; + *optptr++ = ((dhcps_lease_time * 60) >> 0) & 0xFF; + + *optptr++ = DHCP_OPTION_SERVER_ID; + *optptr++ = 4; + *optptr++ = ip4_addr1(&ipadd); + *optptr++ = ip4_addr2(&ipadd); + *optptr++ = ip4_addr3(&ipadd); + *optptr++ = ip4_addr4(&ipadd); + + if (dhcps_router_enabled(dhcps_offer)) { + struct ip_info if_ip; + //bzero(&if_ip, sizeof(struct ip_info)); + memset(&if_ip , 0x00, sizeof(struct ip_info)); + + tcpip_adapter_get_ip_info(WIFI_IF_AP, &if_ip); + + *optptr++ = DHCP_OPTION_ROUTER; + *optptr++ = 4; + *optptr++ = ip4_addr1(&if_ip.gw); + *optptr++ = ip4_addr2(&if_ip.gw); + *optptr++ = ip4_addr3(&if_ip.gw); + *optptr++ = ip4_addr4(&if_ip.gw); + } #ifdef USE_DNS - *optptr++ = DHCP_OPTION_DNS_SERVER; - *optptr++ = 4; - *optptr++ = ip4_addr1( &ipadd); - *optptr++ = ip4_addr2( &ipadd); - *optptr++ = ip4_addr3( &ipadd); - *optptr++ = ip4_addr4( &ipadd); + *optptr++ = DHCP_OPTION_DNS_SERVER; + *optptr++ = 4; + *optptr++ = ip4_addr1(&ipadd); + *optptr++ = ip4_addr2(&ipadd); + *optptr++ = ip4_addr3(&ipadd); + *optptr++ = ip4_addr4(&ipadd); #endif #ifdef CLASS_B_NET - *optptr++ = DHCP_OPTION_BROADCAST_ADDRESS; - *optptr++ = 4; - *optptr++ = ip4_addr1( &ipadd); - *optptr++ = 255; - *optptr++ = 255; - *optptr++ = 255; + *optptr++ = DHCP_OPTION_BROADCAST_ADDRESS; + *optptr++ = 4; + *optptr++ = ip4_addr1(&ipadd); + *optptr++ = 255; + *optptr++ = 255; + *optptr++ = 255; #else - *optptr++ = DHCP_OPTION_BROADCAST_ADDRESS; - *optptr++ = 4; - *optptr++ = ip4_addr1( &ipadd); - *optptr++ = ip4_addr2( &ipadd); - *optptr++ = ip4_addr3( &ipadd); - *optptr++ = 255; + *optptr++ = DHCP_OPTION_BROADCAST_ADDRESS; + *optptr++ = 4; + *optptr++ = ip4_addr1(&ipadd); + *optptr++ = ip4_addr2(&ipadd); + *optptr++ = ip4_addr3(&ipadd); + *optptr++ = 255; #endif - *optptr++ = DHCP_OPTION_INTERFACE_MTU; - *optptr++ = 2; + *optptr++ = DHCP_OPTION_INTERFACE_MTU; + *optptr++ = 2; #ifdef CLASS_B_NET - *optptr++ = 0x05; - *optptr++ = 0xdc; + *optptr++ = 0x05; + *optptr++ = 0xdc; #else - *optptr++ = 0x02; - *optptr++ = 0x40; + *optptr++ = 0x02; + *optptr++ = 0x40; #endif - *optptr++ = DHCP_OPTION_PERFORM_ROUTER_DISCOVERY; - *optptr++ = 1; - *optptr++ = 0x00; + *optptr++ = DHCP_OPTION_PERFORM_ROUTER_DISCOVERY; + *optptr++ = 1; + *optptr++ = 0x00; - *optptr++ = 43; - *optptr++ = 6; + *optptr++ = 43; + *optptr++ = 6; - *optptr++ = 0x01; - *optptr++ = 4; - *optptr++ = 0x00; - *optptr++ = 0x00; - *optptr++ = 0x00; - *optptr++ = 0x02; + *optptr++ = 0x01; + *optptr++ = 4; + *optptr++ = 0x00; + *optptr++ = 0x00; + *optptr++ = 0x00; + *optptr++ = 0x02; - return optptr; + return optptr; } /****************************************************************************** @@ -321,10 +338,10 @@ static u8_t* add_offer_options(u8_t *optptr) * Parameters : optptr -- the addr of DHCP message option * Returns : the addr of DHCP message option *******************************************************************************/ -static u8_t* add_end(u8_t *optptr) +static u8_t *add_end(u8_t *optptr) { - *optptr++ = DHCP_OPTION_END; - return optptr; + *optptr++ = DHCP_OPTION_END; + return optptr; } /****************************************************************************** @@ -335,39 +352,39 @@ static u8_t* add_end(u8_t *optptr) *******************************************************************************/ static void create_msg(struct dhcps_msg *m) { - ip4_addr_t client; + ip4_addr_t client; - client.addr = *( (uint32_t *) &client_address); + client.addr = *((uint32_t *) &client_address); - m->op = DHCP_REPLY; + m->op = DHCP_REPLY; - m->htype = DHCP_HTYPE_ETHERNET; + m->htype = DHCP_HTYPE_ETHERNET; - m->hlen = 6; + m->hlen = 6; - m->hops = 0; + m->hops = 0; // os_memcpy((char *) xid, (char *) m->xid, sizeof(m->xid)); - m->secs = 0; - m->flags = htons(BOOTP_BROADCAST); + m->secs = 0; + m->flags = htons(BOOTP_BROADCAST); + + memcpy((char *) m->yiaddr, (char *) &client.addr, sizeof(m->yiaddr)); - memcpy((char *) m->yiaddr, (char *) &client.addr, sizeof(m->yiaddr)); + memset((char *) m->ciaddr, 0, sizeof(m->ciaddr)); - memset((char *) m->ciaddr, 0, sizeof(m->ciaddr)); + memset((char *) m->siaddr, 0, sizeof(m->siaddr)); - memset((char *) m->siaddr, 0, sizeof(m->siaddr)); + memset((char *) m->giaddr, 0, sizeof(m->giaddr)); - memset((char *) m->giaddr, 0, sizeof(m->giaddr)); + memset((char *) m->sname, 0, sizeof(m->sname)); - memset((char *) m->sname, 0, sizeof(m->sname)); + memset((char *) m->file, 0, sizeof(m->file)); - memset((char *) m->file, 0, sizeof(m->file)); + memset((char *) m->options, 0, sizeof(m->options)); - memset((char *) m->options, 0, sizeof(m->options)); - - u32_t magic_cookie_temp = magic_cookie; + u32_t magic_cookie_temp = magic_cookie; - memcpy((char *) m->options, &magic_cookie_temp, sizeof(magic_cookie_temp)); + memcpy((char *) m->options, &magic_cookie_temp, sizeof(magic_cookie_temp)); } /****************************************************************************** @@ -378,65 +395,70 @@ static void create_msg(struct dhcps_msg *m) *******************************************************************************/ static void send_offer(struct dhcps_msg *m) { - u8_t *end; - struct pbuf *p, *q; - u8_t *data; - u16_t cnt=0; - u16_t i; - err_t SendOffer_err_t; - create_msg(m); - - end = add_msg_type(&m->options[4], DHCPOFFER); - end = add_offer_options(end); - end = add_end(end); - - p = pbuf_alloc(PBUF_TRANSPORT, sizeof(struct dhcps_msg), PBUF_RAM); + u8_t *end; + struct pbuf *p, *q; + u8_t *data; + u16_t cnt = 0; + u16_t i; + err_t SendOffer_err_t; + create_msg(m); + + end = add_msg_type(&m->options[4], DHCPOFFER); + end = add_offer_options(end); + end = add_end(end); + + p = pbuf_alloc(PBUF_TRANSPORT, sizeof(struct dhcps_msg), PBUF_RAM); #if DHCPS_DEBUG - DHCPS_LOG("udhcp: send_offer>>p->ref = %d\n", p->ref); + DHCPS_LOG("udhcp: send_offer>>p->ref = %d\n", p->ref); #endif - if(p != NULL){ - + + if (p != NULL) { + #if DHCPS_DEBUG - DHCPS_LOG("dhcps: send_offer>>pbuf_alloc succeed\n"); - DHCPS_LOG("dhcps: send_offer>>p->tot_len = %d\n", p->tot_len); - DHCPS_LOG("dhcps: send_offer>>p->len = %d\n", p->len); + DHCPS_LOG("dhcps: send_offer>>pbuf_alloc succeed\n"); + DHCPS_LOG("dhcps: send_offer>>p->tot_len = %d\n", p->tot_len); + DHCPS_LOG("dhcps: send_offer>>p->len = %d\n", p->len); #endif - q = p; - while(q != NULL){ - data = (u8_t *)q->payload; - for(i=0; ilen; i++) - { - data[i] = ((u8_t *) m)[cnt++]; + q = p; + + while (q != NULL) { + data = (u8_t *)q->payload; + + for (i = 0; i < q->len; i++) { + data[i] = ((u8_t *) m)[cnt++]; #if DHCPS_DEBUG - DHCPS_LOG("%02x ",data[i]); - if((i+1)%16 == 0){ - DHCPS_LOG("\n"); - } + DHCPS_LOG("%02x ", data[i]); + + if ((i + 1) % 16 == 0) { + DHCPS_LOG("\n"); + } + #endif - } + } + + q = q->next; + } + } else { - q = q->next; - } - }else{ - #if DHCPS_DEBUG - DHCPS_LOG("dhcps: send_offer>>pbuf_alloc failed\n"); + DHCPS_LOG("dhcps: send_offer>>pbuf_alloc failed\n"); #endif - return; - } - - ip_addr_t ip_temp = IPADDR4_INIT(0x0); - ip4_addr_set(ip_2_ip4(&ip_temp), &broadcast_dhcps); - SendOffer_err_t = udp_sendto( pcb_dhcps, p, &ip_temp, DHCPS_CLIENT_PORT ); + return; + } + + ip_addr_t ip_temp = IPADDR4_INIT(0x0); + ip4_addr_set(ip_2_ip4(&ip_temp), &broadcast_dhcps); + SendOffer_err_t = udp_sendto(pcb_dhcps, p, &ip_temp, DHCPS_CLIENT_PORT); #if DHCPS_DEBUG - DHCPS_LOG("dhcps: send_offer>>udp_sendto result %x\n",SendOffer_err_t); + DHCPS_LOG("dhcps: send_offer>>udp_sendto result %x\n", SendOffer_err_t); #endif - if(p->ref != 0){ + + if (p->ref != 0) { #if DHCPS_DEBUG - DHCPS_LOG("udhcp: send_offer>>free pbuf\n"); + DHCPS_LOG("udhcp: send_offer>>free pbuf\n"); #endif - pbuf_free(p); - } + pbuf_free(p); + } } /****************************************************************************** @@ -447,64 +469,69 @@ static void send_offer(struct dhcps_msg *m) *******************************************************************************/ static void send_nak(struct dhcps_msg *m) { - u8_t *end; - struct pbuf *p, *q; - u8_t *data; - u16_t cnt=0; - u16_t i; - err_t SendNak_err_t; - create_msg(m); - - end = add_msg_type(&m->options[4], DHCPNAK); - end = add_end(end); - - p = pbuf_alloc(PBUF_TRANSPORT, sizeof(struct dhcps_msg), PBUF_RAM); + u8_t *end; + struct pbuf *p, *q; + u8_t *data; + u16_t cnt = 0; + u16_t i; + err_t SendNak_err_t; + create_msg(m); + + end = add_msg_type(&m->options[4], DHCPNAK); + end = add_end(end); + + p = pbuf_alloc(PBUF_TRANSPORT, sizeof(struct dhcps_msg), PBUF_RAM); #if DHCPS_DEBUG - DHCPS_LOG("udhcp: send_nak>>p->ref = %d\n", p->ref); + DHCPS_LOG("udhcp: send_nak>>p->ref = %d\n", p->ref); #endif - if(p != NULL){ - + + if (p != NULL) { + #if DHCPS_DEBUG - DHCPS_LOG("dhcps: send_nak>>pbuf_alloc succeed\n"); - DHCPS_LOG("dhcps: send_nak>>p->tot_len = %d\n", p->tot_len); - DHCPS_LOG("dhcps: send_nak>>p->len = %d\n", p->len); + DHCPS_LOG("dhcps: send_nak>>pbuf_alloc succeed\n"); + DHCPS_LOG("dhcps: send_nak>>p->tot_len = %d\n", p->tot_len); + DHCPS_LOG("dhcps: send_nak>>p->len = %d\n", p->len); #endif - q = p; - while(q != NULL){ - data = (u8_t *)q->payload; - for(i=0; ilen; i++) - { - data[i] = ((u8_t *) m)[cnt++]; -#if DHCPS_DEBUG - DHCPS_LOG("%02x ",data[i]); - if((i+1)%16 == 0){ - DHCPS_LOG("\n"); - } + q = p; + + while (q != NULL) { + data = (u8_t *)q->payload; + + for (i = 0; i < q->len; i++) { + data[i] = ((u8_t *) m)[cnt++]; +#if DHCPS_DEBUG + DHCPS_LOG("%02x ", data[i]); + + if ((i + 1) % 16 == 0) { + DHCPS_LOG("\n"); + } + #endif - } + } + + q = q->next; + } + } else { - q = q->next; - } - }else{ - #if DHCPS_DEBUG - DHCPS_LOG("dhcps: send_nak>>pbuf_alloc failed\n"); + DHCPS_LOG("dhcps: send_nak>>pbuf_alloc failed\n"); #endif - return; - } + return; + } - ip_addr_t ip_temp = IPADDR4_INIT(0x0); - ip4_addr_set(ip_2_ip4(&ip_temp), &broadcast_dhcps); - SendNak_err_t = udp_sendto( pcb_dhcps, p, &ip_temp, DHCPS_CLIENT_PORT ); + ip_addr_t ip_temp = IPADDR4_INIT(0x0); + ip4_addr_set(ip_2_ip4(&ip_temp), &broadcast_dhcps); + SendNak_err_t = udp_sendto(pcb_dhcps, p, &ip_temp, DHCPS_CLIENT_PORT); #if DHCPS_DEBUG - DHCPS_LOG("dhcps: send_nak>>udp_sendto result %x\n",SendNak_err_t); + DHCPS_LOG("dhcps: send_nak>>udp_sendto result %x\n", SendNak_err_t); #endif - if(p->ref != 0){ -#if DHCPS_DEBUG - DHCPS_LOG("udhcp: send_nak>>free pbuf\n"); + + if (p->ref != 0) { +#if DHCPS_DEBUG + DHCPS_LOG("udhcp: send_nak>>free pbuf\n"); #endif - pbuf_free(p); - } + pbuf_free(p); + } } /****************************************************************************** @@ -515,66 +542,70 @@ static void send_nak(struct dhcps_msg *m) *******************************************************************************/ static void send_ack(struct dhcps_msg *m) { - u8_t *end; - struct pbuf *p, *q; - u8_t *data; - u16_t cnt=0; - u16_t i; - err_t SendAck_err_t; - create_msg(m); - - end = add_msg_type(&m->options[4], DHCPACK); - end = add_offer_options(end); - end = add_end(end); - - p = pbuf_alloc(PBUF_TRANSPORT, sizeof(struct dhcps_msg), PBUF_RAM); + u8_t *end; + struct pbuf *p, *q; + u8_t *data; + u16_t cnt = 0; + u16_t i; + err_t SendAck_err_t; + create_msg(m); + + end = add_msg_type(&m->options[4], DHCPACK); + end = add_offer_options(end); + end = add_end(end); + + p = pbuf_alloc(PBUF_TRANSPORT, sizeof(struct dhcps_msg), PBUF_RAM); #if DHCPS_DEBUG - DHCPS_LOG("udhcp: send_ack>>p->ref = %d\n", p->ref); + DHCPS_LOG("udhcp: send_ack>>p->ref = %d\n", p->ref); #endif - if(p != NULL){ - + + if (p != NULL) { + #if DHCPS_DEBUG - DHCPS_LOG("dhcps: send_ack>>pbuf_alloc succeed\n"); - DHCPS_LOG("dhcps: send_ack>>p->tot_len = %d\n", p->tot_len); - DHCPS_LOG("dhcps: send_ack>>p->len = %d\n", p->len); + DHCPS_LOG("dhcps: send_ack>>pbuf_alloc succeed\n"); + DHCPS_LOG("dhcps: send_ack>>p->tot_len = %d\n", p->tot_len); + DHCPS_LOG("dhcps: send_ack>>p->len = %d\n", p->len); #endif - q = p; - while(q != NULL){ - data = (u8_t *)q->payload; - for(i=0; ilen; i++) - { - data[i] = ((u8_t *) m)[cnt++]; -#if DHCPS_DEBUG - DHCPS_LOG("%02x ",data[i]); - if((i+1)%16 == 0){ - DHCPS_LOG("\n"); - } + q = p; + + while (q != NULL) { + data = (u8_t *)q->payload; + + for (i = 0; i < q->len; i++) { + data[i] = ((u8_t *) m)[cnt++]; +#if DHCPS_DEBUG + DHCPS_LOG("%02x ", data[i]); + + if ((i + 1) % 16 == 0) { + DHCPS_LOG("\n"); + } + #endif - } + } + + q = q->next; + } + } else { - q = q->next; - } - }else{ - #if DHCPS_DEBUG - DHCPS_LOG("dhcps: send_ack>>pbuf_alloc failed\n"); + DHCPS_LOG("dhcps: send_ack>>pbuf_alloc failed\n"); #endif - return; - } + return; + } - ip_addr_t ip_temp = IPADDR4_INIT(0x0); - ip4_addr_set(ip_2_ip4(&ip_temp), &broadcast_dhcps); - SendAck_err_t = udp_sendto( pcb_dhcps, p, &ip_temp, DHCPS_CLIENT_PORT ); + ip_addr_t ip_temp = IPADDR4_INIT(0x0); + ip4_addr_set(ip_2_ip4(&ip_temp), &broadcast_dhcps); + SendAck_err_t = udp_sendto(pcb_dhcps, p, &ip_temp, DHCPS_CLIENT_PORT); #if DHCPS_DEBUG - DHCPS_LOG("dhcps: send_ack>>udp_sendto result %x\n",SendAck_err_t); + DHCPS_LOG("dhcps: send_ack>>udp_sendto result %x\n", SendAck_err_t); #endif - - if(p->ref != 0){ + + if (p->ref != 0) { #if DHCPS_DEBUG - DHCPS_LOG("udhcp: send_ack>>free pbuf\n"); + DHCPS_LOG("udhcp: send_ack>>free pbuf\n"); #endif - pbuf_free(p); - } + pbuf_free(p); + } } /****************************************************************************** @@ -586,94 +617,99 @@ static void send_ack(struct dhcps_msg *m) *******************************************************************************/ static u8_t parse_options(u8_t *optptr, s16_t len) { - ip4_addr_t client; - bool is_dhcp_parse_end = false; - struct dhcps_state s; + ip4_addr_t client; + bool is_dhcp_parse_end = false; + struct dhcps_state s; - client.addr = *( (uint32_t *) &client_address); + client.addr = *((uint32_t *) &client_address); - u8_t *end = optptr + len; - u16_t type = 0; + u8_t *end = optptr + len; + u16_t type = 0; - s.state = DHCPS_STATE_IDLE; + s.state = DHCPS_STATE_IDLE; - while (optptr < end) { + while (optptr < end) { #if DHCPS_DEBUG - DHCPS_LOG("dhcps: (s16_t)*optptr = %d\n", (s16_t)*optptr); + DHCPS_LOG("dhcps: (s16_t)*optptr = %d\n", (s16_t)*optptr); #endif - switch ((s16_t) *optptr) { - case DHCP_OPTION_MSG_TYPE: //53 - type = *(optptr + 2); - break; + switch ((s16_t) *optptr) { + + case DHCP_OPTION_MSG_TYPE: //53 + type = *(optptr + 2); + break; - case DHCP_OPTION_REQ_IPADDR://50 - if( memcmp( (char *) &client.addr, (char *) optptr+2,4)==0 ) { + case DHCP_OPTION_REQ_IPADDR://50 + if (memcmp((char *) &client.addr, (char *) optptr + 2, 4) == 0) { #if DHCPS_DEBUG - DHCPS_LOG("dhcps: DHCP_OPTION_REQ_IPADDR = 0 ok\n"); + DHCPS_LOG("dhcps: DHCP_OPTION_REQ_IPADDR = 0 ok\n"); #endif - s.state = DHCPS_STATE_ACK; - }else { + s.state = DHCPS_STATE_ACK; + } else { #if DHCPS_DEBUG - DHCPS_LOG("dhcps: DHCP_OPTION_REQ_IPADDR != 0 err\n"); + DHCPS_LOG("dhcps: DHCP_OPTION_REQ_IPADDR != 0 err\n"); #endif - s.state = DHCPS_STATE_NAK; - } - break; - case DHCP_OPTION_END: - { - is_dhcp_parse_end = true; - } - break; - } + s.state = DHCPS_STATE_NAK; + } + + break; - if(is_dhcp_parse_end){ - break; - } + case DHCP_OPTION_END: { + is_dhcp_parse_end = true; + } + break; + } - optptr += optptr[1] + 2; + if (is_dhcp_parse_end) { + break; } - switch (type){ - - case DHCPDISCOVER://1 - s.state = DHCPS_STATE_OFFER; + optptr += optptr[1] + 2; + } + + switch (type) { + + case DHCPDISCOVER://1 + s.state = DHCPS_STATE_OFFER; #if DHCPS_DEBUG - DHCPS_LOG("dhcps: DHCPD_STATE_OFFER\n"); + DHCPS_LOG("dhcps: DHCPD_STATE_OFFER\n"); #endif - break; + break; + + case DHCPREQUEST://3 + if (!(s.state == DHCPS_STATE_ACK || s.state == DHCPS_STATE_NAK)) { + if (renew == true) { + s.state = DHCPS_STATE_ACK; + } else { + s.state = DHCPS_STATE_NAK; + } - case DHCPREQUEST://3 - if ( !(s.state == DHCPS_STATE_ACK || s.state == DHCPS_STATE_NAK) ) { - if(renew == true) { - s.state = DHCPS_STATE_ACK; - } else { - s.state = DHCPS_STATE_NAK; - } #if DHCPS_DEBUG - DHCPS_LOG("dhcps: DHCPD_STATE_NAK\n"); + DHCPS_LOG("dhcps: DHCPD_STATE_NAK\n"); #endif - } - break; + } - case DHCPDECLINE://4 - s.state = DHCPS_STATE_IDLE; + break; + + case DHCPDECLINE://4 + s.state = DHCPS_STATE_IDLE; #if DHCPS_DEBUG - DHCPS_LOG("dhcps: DHCPD_STATE_IDLE\n"); + DHCPS_LOG("dhcps: DHCPD_STATE_IDLE\n"); #endif - break; + break; - case DHCPRELEASE://7 - s.state = DHCPS_STATE_RELEASE; + case DHCPRELEASE://7 + s.state = DHCPS_STATE_RELEASE; #if DHCPS_DEBUG - DHCPS_LOG("dhcps: DHCPD_STATE_IDLE\n"); + DHCPS_LOG("dhcps: DHCPD_STATE_IDLE\n"); #endif - break; - } + break; + } + #if DHCPS_DEBUG - DHCPS_LOG("dhcps: return s.state = %d\n", s.state); + DHCPS_LOG("dhcps: return s.state = %d\n", s.state); #endif - return s.state; + return s.state; } /****************************************************************************** @@ -685,121 +721,129 @@ static u8_t parse_options(u8_t *optptr, s16_t len) *******************************************************************************/ static s16_t parse_msg(struct dhcps_msg *m, u16_t len) { - if(memcmp((char *)m->options,&magic_cookie,sizeof(magic_cookie)) == 0){ + if (memcmp((char *)m->options, &magic_cookie, sizeof(magic_cookie)) == 0) { #if DHCPS_DEBUG - DHCPS_LOG("dhcps: len = %d\n", len); + DHCPS_LOG("dhcps: len = %d\n", len); #endif - ip4_addr_t addr_tmp; - - struct dhcps_pool *pdhcps_pool = NULL; - list_node *pnode = NULL; - list_node *pback_node = NULL; - ip4_addr_t first_address; - bool flag = false; - - first_address.addr = dhcps_poll.start_ip.addr; - client_address.addr = client_address_plus.addr; - renew = false; - - if (plist != NULL){ - for (pback_node = plist; pback_node != NULL;pback_node = pback_node->pnext) { - pdhcps_pool = pback_node->pnode; - if (memcmp(pdhcps_pool->mac, m->chaddr, sizeof(pdhcps_pool->mac)) == 0){ - if (memcmp(&pdhcps_pool->ip.addr, m->ciaddr, sizeof(pdhcps_pool->ip.addr)) == 0) { - renew = true; - } - client_address.addr = pdhcps_pool->ip.addr; - pdhcps_pool->lease_timer = dhcps_lease_time; - pnode = pback_node; - goto POOL_CHECK; - } else if (pdhcps_pool->ip.addr == client_address_plus.addr){ - addr_tmp.addr = htonl(client_address_plus.addr); - addr_tmp.addr++; - client_address_plus.addr = htonl(addr_tmp.addr); - client_address.addr = client_address_plus.addr; - } - - if (flag == false) { // search the fisrt unused ip - if (first_address.addr < pdhcps_pool->ip.addr) { - flag = true; - } else { - addr_tmp.addr = htonl(first_address.addr); - addr_tmp.addr++; - first_address.addr = htonl(addr_tmp.addr); - } - } - } - } else { - client_address.addr = dhcps_poll.start_ip.addr; - } - - if (client_address_plus.addr > dhcps_poll.end_ip.addr) { - client_address.addr = first_address.addr; - } - if (client_address.addr > dhcps_poll.end_ip.addr) { - client_address_plus.addr = dhcps_poll.start_ip.addr; - pdhcps_pool = NULL; - pnode = NULL; - } else { - pdhcps_pool = (struct dhcps_pool *)malloc(sizeof(struct dhcps_pool)); - memset(pdhcps_pool ,0x00 ,sizeof(struct dhcps_pool)); - - pdhcps_pool->ip.addr = client_address.addr; - memcpy(pdhcps_pool->mac, m->chaddr, sizeof(pdhcps_pool->mac)); - pdhcps_pool->lease_timer = dhcps_lease_time; - pnode = (list_node *)malloc(sizeof(list_node )); - memset(pnode ,0x00 ,sizeof(list_node)); - - pnode->pnode = pdhcps_pool; - pnode->pnext = NULL; - node_insert_to_list(&plist,pnode); - if (client_address.addr == dhcps_poll.end_ip.addr) { - client_address_plus.addr = dhcps_poll.start_ip.addr; - } else { - addr_tmp.addr = htonl(client_address.addr); - addr_tmp.addr++; - client_address_plus.addr = htonl(addr_tmp.addr); - } - } - - POOL_CHECK: - if ((client_address.addr > dhcps_poll.end_ip.addr) || (ip4_addr_isany(&client_address))){ - if(pnode != NULL) { - node_remove_from_list(&plist,pnode); - free(pnode); - pnode = NULL; - } - - if (pdhcps_pool != NULL) { - free(pdhcps_pool); - pdhcps_pool = NULL; - } - return 4; - } - - s16_t ret = parse_options(&m->options[4], len);; - - if(ret == DHCPS_STATE_RELEASE) { - if(pnode != NULL) { - node_remove_from_list(&plist,pnode); - free(pnode); - pnode = NULL; - } - - if (pdhcps_pool != NULL) { - free(pdhcps_pool); - pdhcps_pool = NULL; - } - memset(&client_address,0x0,sizeof(client_address)); - } + ip4_addr_t addr_tmp; + + struct dhcps_pool *pdhcps_pool = NULL; + list_node *pnode = NULL; + list_node *pback_node = NULL; + ip4_addr_t first_address; + bool flag = false; + + first_address.addr = dhcps_poll.start_ip.addr; + client_address.addr = client_address_plus.addr; + renew = false; + + if (plist != NULL) { + for (pback_node = plist; pback_node != NULL; pback_node = pback_node->pnext) { + pdhcps_pool = pback_node->pnode; + + if (memcmp(pdhcps_pool->mac, m->chaddr, sizeof(pdhcps_pool->mac)) == 0) { + if (memcmp(&pdhcps_pool->ip.addr, m->ciaddr, sizeof(pdhcps_pool->ip.addr)) == 0) { + renew = true; + } + + client_address.addr = pdhcps_pool->ip.addr; + pdhcps_pool->lease_timer = dhcps_lease_time; + pnode = pback_node; + goto POOL_CHECK; + } else if (pdhcps_pool->ip.addr == client_address_plus.addr) { + addr_tmp.addr = htonl(client_address_plus.addr); + addr_tmp.addr++; + client_address_plus.addr = htonl(addr_tmp.addr); + client_address.addr = client_address_plus.addr; + } + + if (flag == false) { // search the fisrt unused ip + if (first_address.addr < pdhcps_pool->ip.addr) { + flag = true; + } else { + addr_tmp.addr = htonl(first_address.addr); + addr_tmp.addr++; + first_address.addr = htonl(addr_tmp.addr); + } + } + } + } else { + client_address.addr = dhcps_poll.start_ip.addr; + } + + if (client_address_plus.addr > dhcps_poll.end_ip.addr) { + client_address.addr = first_address.addr; + } + + if (client_address.addr > dhcps_poll.end_ip.addr) { + client_address_plus.addr = dhcps_poll.start_ip.addr; + pdhcps_pool = NULL; + pnode = NULL; + } else { + pdhcps_pool = (struct dhcps_pool *)malloc(sizeof(struct dhcps_pool)); + memset(pdhcps_pool , 0x00 , sizeof(struct dhcps_pool)); + + pdhcps_pool->ip.addr = client_address.addr; + memcpy(pdhcps_pool->mac, m->chaddr, sizeof(pdhcps_pool->mac)); + pdhcps_pool->lease_timer = dhcps_lease_time; + pnode = (list_node *)malloc(sizeof(list_node)); + memset(pnode , 0x00 , sizeof(list_node)); + + pnode->pnode = pdhcps_pool; + pnode->pnext = NULL; + node_insert_to_list(&plist, pnode); + + if (client_address.addr == dhcps_poll.end_ip.addr) { + client_address_plus.addr = dhcps_poll.start_ip.addr; + } else { + addr_tmp.addr = htonl(client_address.addr); + addr_tmp.addr++; + client_address_plus.addr = htonl(addr_tmp.addr); + } + } + +POOL_CHECK: + + if ((client_address.addr > dhcps_poll.end_ip.addr) || (ip4_addr_isany(&client_address))) { + if (pnode != NULL) { + node_remove_from_list(&plist, pnode); + free(pnode); + pnode = NULL; + } + + if (pdhcps_pool != NULL) { + free(pdhcps_pool); + pdhcps_pool = NULL; + } + + return 4; + } + + s16_t ret = parse_options(&m->options[4], len);; + + if (ret == DHCPS_STATE_RELEASE) { + if (pnode != NULL) { + node_remove_from_list(&plist, pnode); + free(pnode); + pnode = NULL; + } + + if (pdhcps_pool != NULL) { + free(pdhcps_pool); + pdhcps_pool = NULL; + } + + memset(&client_address, 0x0, sizeof(client_address)); + } #if DHCPS_DEBUG - DHCPS_LOG("dhcps: xid changed\n"); - DHCPS_LOG("dhcps: client_address.addr = %x\n", client_address.addr); + DHCPS_LOG("dhcps: xid changed\n"); + DHCPS_LOG("dhcps: client_address.addr = %x\n", client_address.addr); #endif - return ret; - } - return 0; + return ret; + } + + return 0; } /****************************************************************************** @@ -813,100 +857,113 @@ static s16_t parse_msg(struct dhcps_msg *m, u16_t len) * Returns : none *******************************************************************************/ static void handle_dhcp(void *arg, - struct udp_pcb *pcb, - struct pbuf *p, - const ip_addr_t *addr, - u16_t port) + struct udp_pcb *pcb, + struct pbuf *p, + const ip_addr_t *addr, + u16_t port) { - struct dhcps_msg *pmsg_dhcps = NULL; - s16_t tlen; - u16_t i; - u16_t dhcps_msg_cnt=0; - u8_t *p_dhcps_msg = NULL; - u8_t *data; + struct dhcps_msg *pmsg_dhcps = NULL; + s16_t tlen; + u16_t i; + u16_t dhcps_msg_cnt = 0; + u8_t *p_dhcps_msg = NULL; + u8_t *data; #if DHCPS_DEBUG - DHCPS_LOG("dhcps: handle_dhcp-> receive a packet\n"); + DHCPS_LOG("dhcps: handle_dhcp-> receive a packet\n"); #endif - if (p==NULL) return; - - pmsg_dhcps = (struct dhcps_msg *)malloc(sizeof(struct dhcps_msg)); - memset(pmsg_dhcps ,0x00 ,sizeof(struct dhcps_msg)); - - if (NULL == pmsg_dhcps){ - pbuf_free(p); - return; - } - p_dhcps_msg = (u8_t *)pmsg_dhcps; - tlen = p->tot_len; - data = p->payload; + + if (p == NULL) { + return; + } + + pmsg_dhcps = (struct dhcps_msg *)malloc(sizeof(struct dhcps_msg)); + memset(pmsg_dhcps , 0x00 , sizeof(struct dhcps_msg)); + + if (NULL == pmsg_dhcps) { + pbuf_free(p); + return; + } + + p_dhcps_msg = (u8_t *)pmsg_dhcps; + tlen = p->tot_len; + data = p->payload; #if DHCPS_DEBUG - DHCPS_LOG("dhcps: handle_dhcp-> p->tot_len = %d\n", tlen); - DHCPS_LOG("dhcps: handle_dhcp-> p->len = %d\n", p->len); -#endif - - for(i=0; ilen; i++){ - p_dhcps_msg[dhcps_msg_cnt++] = data[i]; -#if DHCPS_DEBUG - DHCPS_LOG("%02x ",data[i]); - if((i+1)%16 == 0){ - DHCPS_LOG("\n"); - } + DHCPS_LOG("dhcps: handle_dhcp-> p->tot_len = %d\n", tlen); + DHCPS_LOG("dhcps: handle_dhcp-> p->len = %d\n", p->len); #endif - } - - if(p->next != NULL) { + + for (i = 0; i < p->len; i++) { + p_dhcps_msg[dhcps_msg_cnt++] = data[i]; #if DHCPS_DEBUG - DHCPS_LOG("dhcps: handle_dhcp-> p->next != NULL\n"); - DHCPS_LOG("dhcps: handle_dhcp-> p->next->tot_len = %d\n",p->next->tot_len); - DHCPS_LOG("dhcps: handle_dhcp-> p->next->len = %d\n",p->next->len); -#endif - - data = p->next->payload; - for(i=0; inext->len; i++){ - p_dhcps_msg[dhcps_msg_cnt++] = data[i]; -#if DHCPS_DEBUG - DHCPS_LOG("%02x ",data[i]); - if((i+1)%16 == 0){ - DHCPS_LOG("\n"); - } + DHCPS_LOG("%02x ", data[i]); + + if ((i + 1) % 16 == 0) { + DHCPS_LOG("\n"); + } + #endif - } - } + } + if (p->next != NULL) { #if DHCPS_DEBUG - DHCPS_LOG("dhcps: handle_dhcp-> parse_msg(p)\n"); + DHCPS_LOG("dhcps: handle_dhcp-> p->next != NULL\n"); + DHCPS_LOG("dhcps: handle_dhcp-> p->next->tot_len = %d\n", p->next->tot_len); + DHCPS_LOG("dhcps: handle_dhcp-> p->next->len = %d\n", p->next->len); #endif - - switch(parse_msg(pmsg_dhcps, tlen - 240)) { - case DHCPS_STATE_OFFER://1 -#if DHCPS_DEBUG - DHCPS_LOG("dhcps: handle_dhcp-> DHCPD_STATE_OFFER\n"); -#endif - send_offer(pmsg_dhcps); - break; - case DHCPS_STATE_ACK://3 + + data = p->next->payload; + + for (i = 0; i < p->next->len; i++) { + p_dhcps_msg[dhcps_msg_cnt++] = data[i]; #if DHCPS_DEBUG - DHCPS_LOG("dhcps: handle_dhcp-> DHCPD_STATE_ACK\n"); -#endif - send_ack(pmsg_dhcps); - break; - case DHCPS_STATE_NAK://4 -#if DHCPS_DEBUG - DHCPS_LOG("dhcps: handle_dhcp-> DHCPD_STATE_NAK\n"); + DHCPS_LOG("%02x ", data[i]); + + if ((i + 1) % 16 == 0) { + DHCPS_LOG("\n"); + } + #endif - send_nak(pmsg_dhcps); - break; - default : - break; } + } + #if DHCPS_DEBUG - DHCPS_LOG("dhcps: handle_dhcp-> pbuf_free(p)\n"); + DHCPS_LOG("dhcps: handle_dhcp-> parse_msg(p)\n"); #endif - pbuf_free(p); - free(pmsg_dhcps); - pmsg_dhcps = NULL; + + switch (parse_msg(pmsg_dhcps, tlen - 240)) { + case DHCPS_STATE_OFFER://1 +#if DHCPS_DEBUG + DHCPS_LOG("dhcps: handle_dhcp-> DHCPD_STATE_OFFER\n"); +#endif + send_offer(pmsg_dhcps); + break; + + case DHCPS_STATE_ACK://3 +#if DHCPS_DEBUG + DHCPS_LOG("dhcps: handle_dhcp-> DHCPD_STATE_ACK\n"); +#endif + send_ack(pmsg_dhcps); + break; + + case DHCPS_STATE_NAK://4 +#if DHCPS_DEBUG + DHCPS_LOG("dhcps: handle_dhcp-> DHCPD_STATE_NAK\n"); +#endif + send_nak(pmsg_dhcps); + break; + + default : + break; + } + +#if DHCPS_DEBUG + DHCPS_LOG("dhcps: handle_dhcp-> pbuf_free(p)\n"); +#endif + pbuf_free(p); + free(pmsg_dhcps); + pmsg_dhcps = NULL; } /****************************************************************************** @@ -917,41 +974,46 @@ static void handle_dhcp(void *arg, *******************************************************************************/ static void dhcps_poll_set(u32_t ip) { - u32_t softap_ip = 0,local_ip = 0; - u32_t start_ip = 0; - u32_t end_ip = 0; - if (dhcps_poll.enable == true) { - softap_ip = htonl(ip); - start_ip = htonl(dhcps_poll.start_ip.addr); - end_ip = htonl(dhcps_poll.end_ip.addr); - /*config ip information can't contain local ip*/ - if ((start_ip <= softap_ip) && (softap_ip <= end_ip)) { - dhcps_poll.enable = false; - } else { - /*config ip information must be in the same segment as the local ip*/ - softap_ip >>= 8; - if (((start_ip >> 8 != softap_ip) || (end_ip >> 8 != softap_ip)) - || (end_ip - start_ip > DHCPS_MAX_LEASE)) { - dhcps_poll.enable = false; - } - } - } - - if (dhcps_poll.enable == false) { - local_ip = softap_ip = htonl(ip); - softap_ip &= 0xFFFFFF00; - local_ip &= 0xFF; - if (local_ip >= 0x80) - local_ip -= DHCPS_MAX_LEASE; - else - local_ip ++; - - bzero(&dhcps_poll, sizeof(dhcps_poll)); - dhcps_poll.start_ip.addr = softap_ip | local_ip; - dhcps_poll.end_ip.addr = softap_ip | (local_ip + DHCPS_MAX_LEASE - 1); - dhcps_poll.start_ip.addr = htonl(dhcps_poll.start_ip.addr); - dhcps_poll.end_ip.addr= htonl(dhcps_poll.end_ip.addr); - } + u32_t softap_ip = 0, local_ip = 0; + u32_t start_ip = 0; + u32_t end_ip = 0; + + if (dhcps_poll.enable == true) { + softap_ip = htonl(ip); + start_ip = htonl(dhcps_poll.start_ip.addr); + end_ip = htonl(dhcps_poll.end_ip.addr); + + /*config ip information can't contain local ip*/ + if ((start_ip <= softap_ip) && (softap_ip <= end_ip)) { + dhcps_poll.enable = false; + } else { + /*config ip information must be in the same segment as the local ip*/ + softap_ip >>= 8; + + if (((start_ip >> 8 != softap_ip) || (end_ip >> 8 != softap_ip)) + || (end_ip - start_ip > DHCPS_MAX_LEASE)) { + dhcps_poll.enable = false; + } + } + } + + if (dhcps_poll.enable == false) { + local_ip = softap_ip = htonl(ip); + softap_ip &= 0xFFFFFF00; + local_ip &= 0xFF; + + if (local_ip >= 0x80) { + local_ip -= DHCPS_MAX_LEASE; + } else { + local_ip ++; + } + + bzero(&dhcps_poll, sizeof(dhcps_poll)); + dhcps_poll.start_ip.addr = softap_ip | local_ip; + dhcps_poll.end_ip.addr = softap_ip | (local_ip + DHCPS_MAX_LEASE - 1); + dhcps_poll.start_ip.addr = htonl(dhcps_poll.start_ip.addr); + dhcps_poll.end_ip.addr = htonl(dhcps_poll.end_ip.addr); + } } @@ -964,29 +1026,32 @@ static void dhcps_poll_set(u32_t ip) *******************************************************************************/ void dhcps_start(struct netif *netif, struct ip_info *info) { - struct netif * apnetif =netif; - - - if(apnetif->dhcps_pcb != NULL) { - udp_remove(apnetif->dhcps_pcb); - } - pcb_dhcps = udp_new(); - if (pcb_dhcps == NULL || info ==NULL) { - printf("dhcps_start(): could not obtain pcb\n"); - } - apnetif->dhcps_pcb = pcb_dhcps; - - IP4_ADDR(&broadcast_dhcps, 255, 255, 255, 255); - - server_address = info->ip; - dhcps_poll_set( server_address.addr ); - - client_address_plus.addr = dhcps_poll.start_ip.addr; - - udp_bind(pcb_dhcps, IP_ADDR_ANY, DHCPS_SERVER_PORT); - udp_recv(pcb_dhcps, handle_dhcp, NULL); + struct netif *apnetif = netif; + + + if (apnetif->dhcps_pcb != NULL) { + udp_remove(apnetif->dhcps_pcb); + } + + pcb_dhcps = udp_new(); + + if (pcb_dhcps == NULL || info == NULL) { + printf("dhcps_start(): could not obtain pcb\n"); + } + + apnetif->dhcps_pcb = pcb_dhcps; + + IP4_ADDR(&broadcast_dhcps, 255, 255, 255, 255); + + server_address = info->ip; + dhcps_poll_set(server_address.addr); + + client_address_plus.addr = dhcps_poll.start_ip.addr; + + udp_bind(pcb_dhcps, IP_ADDR_ANY, DHCPS_SERVER_PORT); + udp_recv(pcb_dhcps, handle_dhcp, NULL); #if DHCPS_DEBUG - DHCPS_LOG("dhcps:dhcps_start->udp_recv function Set a receive callback handle_dhcp for UDP_PCB pcb_dhcps\n"); + DHCPS_LOG("dhcps:dhcps_start->udp_recv function Set a receive callback handle_dhcp for UDP_PCB pcb_dhcps\n"); #endif } @@ -997,17 +1062,18 @@ void dhcps_start(struct netif *netif, struct ip_info *info) * Parameters : netif -- The current netif addr * Returns : none *******************************************************************************/ -void dhcps_stop(struct netif *netif ) +void dhcps_stop(struct netif *netif) { - struct netif * apnetif = netif; - if(apnetif == NULL) - { + struct netif *apnetif = netif; + + if (apnetif == NULL) { printf("dhcps_stop: apnetif == NULL\n"); return; } + udp_disconnect(pcb_dhcps); - if(apnetif->dhcps_pcb != NULL) { + if (apnetif->dhcps_pcb != NULL) { udp_remove(apnetif->dhcps_pcb); apnetif->dhcps_pcb = NULL; } @@ -1015,6 +1081,7 @@ void dhcps_stop(struct netif *netif ) list_node *pnode = NULL; list_node *pback_node = NULL; pnode = plist; + while (pnode != NULL) { pback_node = pnode; pnode = pback_node->pnext; @@ -1034,28 +1101,32 @@ void dhcps_stop(struct netif *netif ) *******************************************************************************/ static void kill_oldest_dhcps_pool(void) { - list_node *pre = NULL, *p = NULL; - list_node *minpre = NULL, *minp = NULL; - struct dhcps_pool *pdhcps_pool = NULL, *pmin_pool = NULL; - pre = plist; - p = pre->pnext; - minpre = pre; - minp = p; - while (p != NULL){ - pdhcps_pool = p->pnode; - pmin_pool = minp->pnode; - if (pdhcps_pool->lease_timer < pmin_pool->lease_timer){ - minp = p; - minpre = pre; - } - pre = p; - p = p->pnext; - } - minpre->pnext = minp->pnext; - free(minp->pnode); - minp->pnode = NULL; - free(minp); - minp = NULL; + list_node *pre = NULL, *p = NULL; + list_node *minpre = NULL, *minp = NULL; + struct dhcps_pool *pdhcps_pool = NULL, *pmin_pool = NULL; + pre = plist; + p = pre->pnext; + minpre = pre; + minp = p; + + while (p != NULL) { + pdhcps_pool = p->pnode; + pmin_pool = minp->pnode; + + if (pdhcps_pool->lease_timer < pmin_pool->lease_timer) { + minp = p; + minpre = pre; + } + + pre = p; + p = p->pnext; + } + + minpre->pnext = minp->pnext; + free(minp->pnode); + minp->pnode = NULL; + free(minp); + minp = NULL; } /****************************************************************************** @@ -1066,30 +1137,33 @@ static void kill_oldest_dhcps_pool(void) *******************************************************************************/ void dhcps_coarse_tmr(void) { - u8_t num_dhcps_pool = 0; - list_node *pback_node = NULL; - list_node *pnode = NULL; - struct dhcps_pool *pdhcps_pool = NULL; - pnode = plist; - while (pnode != NULL) { - pdhcps_pool = pnode->pnode; - pdhcps_pool->lease_timer --; - if (pdhcps_pool->lease_timer == 0){ - pback_node = pnode; - pnode = pback_node->pnext; - node_remove_from_list(&plist,pback_node); - free(pback_node->pnode); - pback_node->pnode = NULL; - free(pback_node); - pback_node = NULL; - } else { - pnode = pnode ->pnext; - num_dhcps_pool ++; - } - } - - if (num_dhcps_pool >= MAX_STATION_NUM) - kill_oldest_dhcps_pool(); + u8_t num_dhcps_pool = 0; + list_node *pback_node = NULL; + list_node *pnode = NULL; + struct dhcps_pool *pdhcps_pool = NULL; + pnode = plist; + + while (pnode != NULL) { + pdhcps_pool = pnode->pnode; + pdhcps_pool->lease_timer --; + + if (pdhcps_pool->lease_timer == 0) { + pback_node = pnode; + pnode = pback_node->pnext; + node_remove_from_list(&plist, pback_node); + free(pback_node->pnode); + pback_node->pnode = NULL; + free(pback_node); + pback_node = NULL; + } else { + pnode = pnode ->pnext; + num_dhcps_pool ++; + } + } + + if (num_dhcps_pool >= MAX_STATION_NUM) { + kill_oldest_dhcps_pool(); + } } /****************************************************************************** @@ -1104,16 +1178,17 @@ bool dhcp_search_ip_on_mac(u8_t *mac, ip4_addr_t *ip) struct dhcps_pool *pdhcps_pool = NULL; list_node *pback_node = NULL; bool ret = false; - - for (pback_node = plist; pback_node != NULL;pback_node = pback_node->pnext) { + + for (pback_node = plist; pback_node != NULL; pback_node = pback_node->pnext) { pdhcps_pool = pback_node->pnode; - if (memcmp(pdhcps_pool->mac, mac, sizeof(pdhcps_pool->mac)) == 0){ + + if (memcmp(pdhcps_pool->mac, mac, sizeof(pdhcps_pool->mac)) == 0) { memcpy(&ip->addr, &pdhcps_pool->ip.addr, sizeof(pdhcps_pool->ip.addr)); ret = true; break; } } - + return ret; } #endif