lwip: format dhcpserver.c

This commit is contained in:
Wu Jian Gang
2016-09-01 19:05:50 +08:00
parent 1f4af47911
commit 87977b92f3

View File

@@ -101,23 +101,34 @@ 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 *dhcps_option_info(u8_t op_id, u32_t opt_len)
{ {
void* option_arg = NULL; void *option_arg = NULL;
switch (op_id){
switch (op_id) {
case IP_ADDRESS_LEASE_TIME: case IP_ADDRESS_LEASE_TIME:
if (opt_len == sizeof(dhcps_time_t)) if (opt_len == sizeof(dhcps_time_t)) {
option_arg = &dhcps_lease_time; option_arg = &dhcps_lease_time;
}
break; break;
case REQUESTED_IP_ADDRESS: case REQUESTED_IP_ADDRESS:
if (opt_len == sizeof(dhcps_lease_t)) if (opt_len == sizeof(dhcps_lease_t)) {
option_arg = &dhcps_poll; option_arg = &dhcps_poll;
}
break; break;
case ROUTER_SOLICITATION_ADDRESS: case ROUTER_SOLICITATION_ADDRESS:
if (opt_len == sizeof(dhcps_offer_t)) if (opt_len == sizeof(dhcps_offer_t)) {
option_arg = &dhcps_offer; option_arg = &dhcps_offer;
}
break; break;
default: default:
break; break;
} }
return option_arg; 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 * pinsert -- the insert node of the list
* Returns : none * 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; list_node *plist = NULL;
struct dhcps_pool *pdhcps_pool = NULL; struct dhcps_pool *pdhcps_pool = NULL;
struct dhcps_pool *pdhcps_node = NULL; struct dhcps_pool *pdhcps_node = NULL;
if (*phead == NULL)
if (*phead == NULL) {
*phead = pinsert; *phead = pinsert;
else { } else {
plist = *phead; plist = *phead;
pdhcps_node = pinsert->pnode; pdhcps_node = pinsert->pnode;
pdhcps_pool = plist->pnode; pdhcps_pool = plist->pnode;
if(pdhcps_node->ip.addr < pdhcps_pool->ip.addr) { if (pdhcps_node->ip.addr < pdhcps_pool->ip.addr) {
pinsert->pnext = plist; pinsert->pnext = plist;
*phead = pinsert; *phead = pinsert;
} else { } else {
while (plist->pnext != NULL) { while (plist->pnext != NULL) {
pdhcps_pool = plist->pnext->pnode; pdhcps_pool = plist->pnext->pnode;
if (pdhcps_node->ip.addr < pdhcps_pool->ip.addr) { if (pdhcps_node->ip.addr < pdhcps_pool->ip.addr) {
pinsert->pnext = plist->pnext; pinsert->pnext = plist->pnext;
plist->pnext = pinsert; plist->pnext = pinsert;
break; break;
} }
plist = plist->pnext; plist = plist->pnext;
} }
if(plist->pnext == NULL) { if (plist->pnext == NULL) {
plist->pnext = pinsert; plist->pnext = pinsert;
} }
} }
} }
// pinsert->pnext = NULL; // pinsert->pnext = NULL;
} }
@@ -169,23 +184,25 @@ void node_insert_to_list(list_node **phead, list_node* pinsert)
* pdelete -- the remove node of the list * pdelete -- the remove node of the list
* Returns : none * 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; list_node *plist = NULL;
plist = *phead; plist = *phead;
if (plist == NULL){
if (plist == NULL) {
*phead = NULL; *phead = NULL;
} else { } else {
if (plist == pdelete){ if (plist == pdelete) {
*phead = plist->pnext; *phead = plist->pnext;
pdelete->pnext = NULL; pdelete->pnext = NULL;
} else { } else {
while (plist != NULL) { while (plist != NULL) {
if (plist->pnext == pdelete){ if (plist->pnext == pdelete) {
plist->pnext = pdelete->pnext; plist->pnext = pdelete->pnext;
pdelete->pnext = NULL; pdelete->pnext = NULL;
} }
plist = plist->pnext; plist = plist->pnext;
} }
} }
@@ -198,7 +215,7 @@ void node_remove_from_list(list_node **phead, list_node* pdelete)
* Parameters : optptr -- the addr of DHCP message option * Parameters : optptr -- the addr of DHCP message option
* Returns : 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++ = DHCP_OPTION_MSG_TYPE;
*optptr++ = 1; *optptr++ = 1;
@@ -212,11 +229,11 @@ static u8_t* add_msg_type(u8_t *optptr, u8_t type)
* Parameters : optptr -- the addr of DHCP message option * Parameters : optptr -- the addr of DHCP message option
* Returns : 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 #ifdef USE_CLASS_B_NET
*optptr++ = DHCP_OPTION_SUBNET_MASK; *optptr++ = DHCP_OPTION_SUBNET_MASK;
@@ -243,48 +260,48 @@ static u8_t* add_offer_options(u8_t *optptr)
*optptr++ = DHCP_OPTION_SERVER_ID; *optptr++ = DHCP_OPTION_SERVER_ID;
*optptr++ = 4; *optptr++ = 4;
*optptr++ = ip4_addr1( &ipadd); *optptr++ = ip4_addr1(&ipadd);
*optptr++ = ip4_addr2( &ipadd); *optptr++ = ip4_addr2(&ipadd);
*optptr++ = ip4_addr3( &ipadd); *optptr++ = ip4_addr3(&ipadd);
*optptr++ = ip4_addr4( &ipadd); *optptr++ = ip4_addr4(&ipadd);
if (dhcps_router_enabled(dhcps_offer)){ if (dhcps_router_enabled(dhcps_offer)) {
struct ip_info if_ip; struct ip_info if_ip;
//bzero(&if_ip, sizeof(struct ip_info)); //bzero(&if_ip, sizeof(struct ip_info));
memset(&if_ip ,0x00, sizeof(struct ip_info)); memset(&if_ip , 0x00, sizeof(struct ip_info));
tcpip_adapter_get_ip_info(WIFI_IF_AP, &if_ip); tcpip_adapter_get_ip_info(WIFI_IF_AP, &if_ip);
*optptr++ = DHCP_OPTION_ROUTER; *optptr++ = DHCP_OPTION_ROUTER;
*optptr++ = 4; *optptr++ = 4;
*optptr++ = ip4_addr1( &if_ip.gw); *optptr++ = ip4_addr1(&if_ip.gw);
*optptr++ = ip4_addr2( &if_ip.gw); *optptr++ = ip4_addr2(&if_ip.gw);
*optptr++ = ip4_addr3( &if_ip.gw); *optptr++ = ip4_addr3(&if_ip.gw);
*optptr++ = ip4_addr4( &if_ip.gw); *optptr++ = ip4_addr4(&if_ip.gw);
} }
#ifdef USE_DNS #ifdef USE_DNS
*optptr++ = DHCP_OPTION_DNS_SERVER; *optptr++ = DHCP_OPTION_DNS_SERVER;
*optptr++ = 4; *optptr++ = 4;
*optptr++ = ip4_addr1( &ipadd); *optptr++ = ip4_addr1(&ipadd);
*optptr++ = ip4_addr2( &ipadd); *optptr++ = ip4_addr2(&ipadd);
*optptr++ = ip4_addr3( &ipadd); *optptr++ = ip4_addr3(&ipadd);
*optptr++ = ip4_addr4( &ipadd); *optptr++ = ip4_addr4(&ipadd);
#endif #endif
#ifdef CLASS_B_NET #ifdef CLASS_B_NET
*optptr++ = DHCP_OPTION_BROADCAST_ADDRESS; *optptr++ = DHCP_OPTION_BROADCAST_ADDRESS;
*optptr++ = 4; *optptr++ = 4;
*optptr++ = ip4_addr1( &ipadd); *optptr++ = ip4_addr1(&ipadd);
*optptr++ = 255; *optptr++ = 255;
*optptr++ = 255; *optptr++ = 255;
*optptr++ = 255; *optptr++ = 255;
#else #else
*optptr++ = DHCP_OPTION_BROADCAST_ADDRESS; *optptr++ = DHCP_OPTION_BROADCAST_ADDRESS;
*optptr++ = 4; *optptr++ = 4;
*optptr++ = ip4_addr1( &ipadd); *optptr++ = ip4_addr1(&ipadd);
*optptr++ = ip4_addr2( &ipadd); *optptr++ = ip4_addr2(&ipadd);
*optptr++ = ip4_addr3( &ipadd); *optptr++ = ip4_addr3(&ipadd);
*optptr++ = 255; *optptr++ = 255;
#endif #endif
@@ -321,7 +338,7 @@ static u8_t* add_offer_options(u8_t *optptr)
* Parameters : optptr -- the addr of DHCP message option * Parameters : optptr -- the addr of DHCP message option
* Returns : 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; *optptr++ = DHCP_OPTION_END;
return optptr; return optptr;
@@ -338,7 +355,7 @@ 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;
@@ -381,7 +398,7 @@ static void send_offer(struct dhcps_msg *m)
u8_t *end; u8_t *end;
struct pbuf *p, *q; struct pbuf *p, *q;
u8_t *data; u8_t *data;
u16_t cnt=0; u16_t cnt = 0;
u16_t i; u16_t i;
err_t SendOffer_err_t; err_t SendOffer_err_t;
create_msg(m); create_msg(m);
@@ -394,7 +411,8 @@ static void send_offer(struct dhcps_msg *m)
#if DHCPS_DEBUG #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 #endif
if(p != NULL){
if (p != NULL) {
#if DHCPS_DEBUG #if DHCPS_DEBUG
DHCPS_LOG("dhcps: send_offer>>pbuf_alloc succeed\n"); DHCPS_LOG("dhcps: send_offer>>pbuf_alloc succeed\n");
@@ -402,22 +420,25 @@ static void send_offer(struct dhcps_msg *m)
DHCPS_LOG("dhcps: send_offer>>p->len = %d\n", p->len); DHCPS_LOG("dhcps: send_offer>>p->len = %d\n", p->len);
#endif #endif
q = p; q = p;
while(q != NULL){
while (q != NULL) {
data = (u8_t *)q->payload; data = (u8_t *)q->payload;
for(i=0; i<q->len; i++)
{ for (i = 0; i < q->len; i++) {
data[i] = ((u8_t *) m)[cnt++]; data[i] = ((u8_t *) m)[cnt++];
#if DHCPS_DEBUG #if DHCPS_DEBUG
DHCPS_LOG("%02x ",data[i]); DHCPS_LOG("%02x ", data[i]);
if((i+1)%16 == 0){
if ((i + 1) % 16 == 0) {
DHCPS_LOG("\n"); DHCPS_LOG("\n");
} }
#endif #endif
} }
q = q->next; q = q->next;
} }
}else{ } else {
#if DHCPS_DEBUG #if DHCPS_DEBUG
DHCPS_LOG("dhcps: send_offer>>pbuf_alloc failed\n"); DHCPS_LOG("dhcps: send_offer>>pbuf_alloc failed\n");
@@ -427,11 +448,12 @@ static void send_offer(struct dhcps_msg *m)
ip_addr_t ip_temp = IPADDR4_INIT(0x0); ip_addr_t ip_temp = IPADDR4_INIT(0x0);
ip4_addr_set(ip_2_ip4(&ip_temp), &broadcast_dhcps); ip4_addr_set(ip_2_ip4(&ip_temp), &broadcast_dhcps);
SendOffer_err_t = udp_sendto( pcb_dhcps, p, &ip_temp, DHCPS_CLIENT_PORT ); SendOffer_err_t = udp_sendto(pcb_dhcps, p, &ip_temp, DHCPS_CLIENT_PORT);
#if DHCPS_DEBUG #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 #endif
if(p->ref != 0){
if (p->ref != 0) {
#if DHCPS_DEBUG #if DHCPS_DEBUG
DHCPS_LOG("udhcp: send_offer>>free pbuf\n"); DHCPS_LOG("udhcp: send_offer>>free pbuf\n");
#endif #endif
@@ -450,7 +472,7 @@ static void send_nak(struct dhcps_msg *m)
u8_t *end; u8_t *end;
struct pbuf *p, *q; struct pbuf *p, *q;
u8_t *data; u8_t *data;
u16_t cnt=0; u16_t cnt = 0;
u16_t i; u16_t i;
err_t SendNak_err_t; err_t SendNak_err_t;
create_msg(m); create_msg(m);
@@ -462,7 +484,8 @@ static void send_nak(struct dhcps_msg *m)
#if DHCPS_DEBUG #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 #endif
if(p != NULL){
if (p != NULL) {
#if DHCPS_DEBUG #if DHCPS_DEBUG
DHCPS_LOG("dhcps: send_nak>>pbuf_alloc succeed\n"); DHCPS_LOG("dhcps: send_nak>>pbuf_alloc succeed\n");
@@ -470,22 +493,25 @@ static void send_nak(struct dhcps_msg *m)
DHCPS_LOG("dhcps: send_nak>>p->len = %d\n", p->len); DHCPS_LOG("dhcps: send_nak>>p->len = %d\n", p->len);
#endif #endif
q = p; q = p;
while(q != NULL){
while (q != NULL) {
data = (u8_t *)q->payload; data = (u8_t *)q->payload;
for(i=0; i<q->len; i++)
{ for (i = 0; i < q->len; i++) {
data[i] = ((u8_t *) m)[cnt++]; data[i] = ((u8_t *) m)[cnt++];
#if DHCPS_DEBUG #if DHCPS_DEBUG
DHCPS_LOG("%02x ",data[i]); DHCPS_LOG("%02x ", data[i]);
if((i+1)%16 == 0){
if ((i + 1) % 16 == 0) {
DHCPS_LOG("\n"); DHCPS_LOG("\n");
} }
#endif #endif
} }
q = q->next; q = q->next;
} }
}else{ } else {
#if DHCPS_DEBUG #if DHCPS_DEBUG
DHCPS_LOG("dhcps: send_nak>>pbuf_alloc failed\n"); DHCPS_LOG("dhcps: send_nak>>pbuf_alloc failed\n");
@@ -495,11 +521,12 @@ static void send_nak(struct dhcps_msg *m)
ip_addr_t ip_temp = IPADDR4_INIT(0x0); ip_addr_t ip_temp = IPADDR4_INIT(0x0);
ip4_addr_set(ip_2_ip4(&ip_temp), &broadcast_dhcps); ip4_addr_set(ip_2_ip4(&ip_temp), &broadcast_dhcps);
SendNak_err_t = udp_sendto( pcb_dhcps, p, &ip_temp, DHCPS_CLIENT_PORT ); SendNak_err_t = udp_sendto(pcb_dhcps, p, &ip_temp, DHCPS_CLIENT_PORT);
#if DHCPS_DEBUG #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 #endif
if(p->ref != 0){
if (p->ref != 0) {
#if DHCPS_DEBUG #if DHCPS_DEBUG
DHCPS_LOG("udhcp: send_nak>>free pbuf\n"); DHCPS_LOG("udhcp: send_nak>>free pbuf\n");
#endif #endif
@@ -518,7 +545,7 @@ static void send_ack(struct dhcps_msg *m)
u8_t *end; u8_t *end;
struct pbuf *p, *q; struct pbuf *p, *q;
u8_t *data; u8_t *data;
u16_t cnt=0; u16_t cnt = 0;
u16_t i; u16_t i;
err_t SendAck_err_t; err_t SendAck_err_t;
create_msg(m); create_msg(m);
@@ -531,7 +558,8 @@ static void send_ack(struct dhcps_msg *m)
#if DHCPS_DEBUG #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 #endif
if(p != NULL){
if (p != NULL) {
#if DHCPS_DEBUG #if DHCPS_DEBUG
DHCPS_LOG("dhcps: send_ack>>pbuf_alloc succeed\n"); DHCPS_LOG("dhcps: send_ack>>pbuf_alloc succeed\n");
@@ -539,22 +567,25 @@ static void send_ack(struct dhcps_msg *m)
DHCPS_LOG("dhcps: send_ack>>p->len = %d\n", p->len); DHCPS_LOG("dhcps: send_ack>>p->len = %d\n", p->len);
#endif #endif
q = p; q = p;
while(q != NULL){
while (q != NULL) {
data = (u8_t *)q->payload; data = (u8_t *)q->payload;
for(i=0; i<q->len; i++)
{ for (i = 0; i < q->len; i++) {
data[i] = ((u8_t *) m)[cnt++]; data[i] = ((u8_t *) m)[cnt++];
#if DHCPS_DEBUG #if DHCPS_DEBUG
DHCPS_LOG("%02x ",data[i]); DHCPS_LOG("%02x ", data[i]);
if((i+1)%16 == 0){
if ((i + 1) % 16 == 0) {
DHCPS_LOG("\n"); DHCPS_LOG("\n");
} }
#endif #endif
} }
q = q->next; q = q->next;
} }
}else{ } else {
#if DHCPS_DEBUG #if DHCPS_DEBUG
DHCPS_LOG("dhcps: send_ack>>pbuf_alloc failed\n"); DHCPS_LOG("dhcps: send_ack>>pbuf_alloc failed\n");
@@ -564,12 +595,12 @@ static void send_ack(struct dhcps_msg *m)
ip_addr_t ip_temp = IPADDR4_INIT(0x0); ip_addr_t ip_temp = IPADDR4_INIT(0x0);
ip4_addr_set(ip_2_ip4(&ip_temp), &broadcast_dhcps); ip4_addr_set(ip_2_ip4(&ip_temp), &broadcast_dhcps);
SendAck_err_t = udp_sendto( pcb_dhcps, p, &ip_temp, DHCPS_CLIENT_PORT ); SendAck_err_t = udp_sendto(pcb_dhcps, p, &ip_temp, DHCPS_CLIENT_PORT);
#if DHCPS_DEBUG #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 #endif
if(p->ref != 0){ if (p->ref != 0) {
#if DHCPS_DEBUG #if DHCPS_DEBUG
DHCPS_LOG("udhcp: send_ack>>free pbuf\n"); DHCPS_LOG("udhcp: send_ack>>free pbuf\n");
#endif #endif
@@ -590,7 +621,7 @@ static u8_t parse_options(u8_t *optptr, s16_t len)
bool is_dhcp_parse_end = false; bool is_dhcp_parse_end = false;
struct dhcps_state s; struct dhcps_state s;
client.addr = *( (uint32_t *) &client_address); client.addr = *((uint32_t *) &client_address);
u8_t *end = optptr + len; u8_t *end = optptr + len;
u16_t type = 0; u16_t type = 0;
@@ -601,6 +632,7 @@ static u8_t parse_options(u8_t *optptr, s16_t len)
#if DHCPS_DEBUG #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 #endif
switch ((s16_t) *optptr) { switch ((s16_t) *optptr) {
case DHCP_OPTION_MSG_TYPE: //53 case DHCP_OPTION_MSG_TYPE: //53
@@ -608,33 +640,34 @@ static u8_t parse_options(u8_t *optptr, s16_t len)
break; break;
case DHCP_OPTION_REQ_IPADDR://50 case DHCP_OPTION_REQ_IPADDR://50
if( memcmp( (char *) &client.addr, (char *) optptr+2,4)==0 ) { if (memcmp((char *) &client.addr, (char *) optptr + 2, 4) == 0) {
#if DHCPS_DEBUG #if DHCPS_DEBUG
DHCPS_LOG("dhcps: DHCP_OPTION_REQ_IPADDR = 0 ok\n"); DHCPS_LOG("dhcps: DHCP_OPTION_REQ_IPADDR = 0 ok\n");
#endif #endif
s.state = DHCPS_STATE_ACK; s.state = DHCPS_STATE_ACK;
}else { } else {
#if DHCPS_DEBUG #if DHCPS_DEBUG
DHCPS_LOG("dhcps: DHCP_OPTION_REQ_IPADDR != 0 err\n"); DHCPS_LOG("dhcps: DHCP_OPTION_REQ_IPADDR != 0 err\n");
#endif #endif
s.state = DHCPS_STATE_NAK; s.state = DHCPS_STATE_NAK;
} }
break; break;
case DHCP_OPTION_END:
{ case DHCP_OPTION_END: {
is_dhcp_parse_end = true; is_dhcp_parse_end = true;
} }
break; break;
} }
if(is_dhcp_parse_end){ if (is_dhcp_parse_end) {
break; break;
} }
optptr += optptr[1] + 2; optptr += optptr[1] + 2;
} }
switch (type){ switch (type) {
case DHCPDISCOVER://1 case DHCPDISCOVER://1
s.state = DHCPS_STATE_OFFER; s.state = DHCPS_STATE_OFFER;
@@ -644,16 +677,18 @@ static u8_t parse_options(u8_t *optptr, s16_t len)
break; break;
case DHCPREQUEST://3 case DHCPREQUEST://3
if ( !(s.state == DHCPS_STATE_ACK || s.state == DHCPS_STATE_NAK) ) { if (!(s.state == DHCPS_STATE_ACK || s.state == DHCPS_STATE_NAK)) {
if(renew == true) { if (renew == true) {
s.state = DHCPS_STATE_ACK; s.state = DHCPS_STATE_ACK;
} else { } else {
s.state = DHCPS_STATE_NAK; s.state = DHCPS_STATE_NAK;
} }
#if DHCPS_DEBUG #if DHCPS_DEBUG
DHCPS_LOG("dhcps: DHCPD_STATE_NAK\n"); DHCPS_LOG("dhcps: DHCPD_STATE_NAK\n");
#endif #endif
} }
break; break;
case DHCPDECLINE://4 case DHCPDECLINE://4
@@ -670,6 +705,7 @@ static u8_t parse_options(u8_t *optptr, s16_t len)
#endif #endif
break; break;
} }
#if DHCPS_DEBUG #if DHCPS_DEBUG
DHCPS_LOG("dhcps: return s.state = %d\n", s.state); DHCPS_LOG("dhcps: return s.state = %d\n", s.state);
#endif #endif
@@ -685,7 +721,7 @@ static u8_t parse_options(u8_t *optptr, s16_t len)
*******************************************************************************/ *******************************************************************************/
static s16_t parse_msg(struct dhcps_msg *m, u16_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 #if DHCPS_DEBUG
DHCPS_LOG("dhcps: len = %d\n", len); DHCPS_LOG("dhcps: len = %d\n", len);
#endif #endif
@@ -701,18 +737,20 @@ static s16_t parse_msg(struct dhcps_msg *m, u16_t len)
client_address.addr = client_address_plus.addr; client_address.addr = client_address_plus.addr;
renew = false; renew = false;
if (plist != NULL){ if (plist != NULL) {
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; pdhcps_pool = pback_node->pnode;
if (memcmp(pdhcps_pool->mac, m->chaddr, sizeof(pdhcps_pool->mac)) == 0){
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) { if (memcmp(&pdhcps_pool->ip.addr, m->ciaddr, sizeof(pdhcps_pool->ip.addr)) == 0) {
renew = true; renew = true;
} }
client_address.addr = pdhcps_pool->ip.addr; client_address.addr = pdhcps_pool->ip.addr;
pdhcps_pool->lease_timer = dhcps_lease_time; pdhcps_pool->lease_timer = dhcps_lease_time;
pnode = pback_node; pnode = pback_node;
goto POOL_CHECK; goto POOL_CHECK;
} else if (pdhcps_pool->ip.addr == client_address_plus.addr){ } else if (pdhcps_pool->ip.addr == client_address_plus.addr) {
addr_tmp.addr = htonl(client_address_plus.addr); addr_tmp.addr = htonl(client_address_plus.addr);
addr_tmp.addr++; addr_tmp.addr++;
client_address_plus.addr = htonl(addr_tmp.addr); client_address_plus.addr = htonl(addr_tmp.addr);
@@ -736,23 +774,25 @@ static s16_t parse_msg(struct dhcps_msg *m, u16_t len)
if (client_address_plus.addr > dhcps_poll.end_ip.addr) { if (client_address_plus.addr > dhcps_poll.end_ip.addr) {
client_address.addr = first_address.addr; client_address.addr = first_address.addr;
} }
if (client_address.addr > dhcps_poll.end_ip.addr) { if (client_address.addr > dhcps_poll.end_ip.addr) {
client_address_plus.addr = dhcps_poll.start_ip.addr; client_address_plus.addr = dhcps_poll.start_ip.addr;
pdhcps_pool = NULL; pdhcps_pool = NULL;
pnode = NULL; pnode = NULL;
} else { } else {
pdhcps_pool = (struct dhcps_pool *)malloc(sizeof(struct dhcps_pool)); pdhcps_pool = (struct dhcps_pool *)malloc(sizeof(struct dhcps_pool));
memset(pdhcps_pool ,0x00 ,sizeof(struct dhcps_pool)); memset(pdhcps_pool , 0x00 , sizeof(struct dhcps_pool));
pdhcps_pool->ip.addr = client_address.addr; pdhcps_pool->ip.addr = client_address.addr;
memcpy(pdhcps_pool->mac, m->chaddr, sizeof(pdhcps_pool->mac)); memcpy(pdhcps_pool->mac, m->chaddr, sizeof(pdhcps_pool->mac));
pdhcps_pool->lease_timer = dhcps_lease_time; pdhcps_pool->lease_timer = dhcps_lease_time;
pnode = (list_node *)malloc(sizeof(list_node )); pnode = (list_node *)malloc(sizeof(list_node));
memset(pnode ,0x00 ,sizeof(list_node)); memset(pnode , 0x00 , sizeof(list_node));
pnode->pnode = pdhcps_pool; pnode->pnode = pdhcps_pool;
pnode->pnext = NULL; pnode->pnext = NULL;
node_insert_to_list(&plist,pnode); node_insert_to_list(&plist, pnode);
if (client_address.addr == dhcps_poll.end_ip.addr) { if (client_address.addr == dhcps_poll.end_ip.addr) {
client_address_plus.addr = dhcps_poll.start_ip.addr; client_address_plus.addr = dhcps_poll.start_ip.addr;
} else { } else {
@@ -762,10 +802,11 @@ static s16_t parse_msg(struct dhcps_msg *m, u16_t len)
} }
} }
POOL_CHECK: POOL_CHECK:
if ((client_address.addr > dhcps_poll.end_ip.addr) || (ip4_addr_isany(&client_address))){
if(pnode != NULL) { if ((client_address.addr > dhcps_poll.end_ip.addr) || (ip4_addr_isany(&client_address))) {
node_remove_from_list(&plist,pnode); if (pnode != NULL) {
node_remove_from_list(&plist, pnode);
free(pnode); free(pnode);
pnode = NULL; pnode = NULL;
} }
@@ -774,14 +815,15 @@ static s16_t parse_msg(struct dhcps_msg *m, u16_t len)
free(pdhcps_pool); free(pdhcps_pool);
pdhcps_pool = NULL; pdhcps_pool = NULL;
} }
return 4; return 4;
} }
s16_t ret = parse_options(&m->options[4], len);; s16_t ret = parse_options(&m->options[4], len);;
if(ret == DHCPS_STATE_RELEASE) { if (ret == DHCPS_STATE_RELEASE) {
if(pnode != NULL) { if (pnode != NULL) {
node_remove_from_list(&plist,pnode); node_remove_from_list(&plist, pnode);
free(pnode); free(pnode);
pnode = NULL; pnode = NULL;
} }
@@ -790,7 +832,8 @@ static s16_t parse_msg(struct dhcps_msg *m, u16_t len)
free(pdhcps_pool); free(pdhcps_pool);
pdhcps_pool = NULL; pdhcps_pool = NULL;
} }
memset(&client_address,0x0,sizeof(client_address));
memset(&client_address, 0x0, sizeof(client_address));
} }
#if DHCPS_DEBUG #if DHCPS_DEBUG
@@ -799,6 +842,7 @@ static s16_t parse_msg(struct dhcps_msg *m, u16_t len)
#endif #endif
return ret; return ret;
} }
return 0; return 0;
} }
@@ -821,22 +865,26 @@ static void handle_dhcp(void *arg,
struct dhcps_msg *pmsg_dhcps = NULL; struct dhcps_msg *pmsg_dhcps = NULL;
s16_t tlen; s16_t tlen;
u16_t i; u16_t i;
u16_t dhcps_msg_cnt=0; u16_t dhcps_msg_cnt = 0;
u8_t *p_dhcps_msg = NULL; u8_t *p_dhcps_msg = NULL;
u8_t *data; u8_t *data;
#if DHCPS_DEBUG #if DHCPS_DEBUG
DHCPS_LOG("dhcps: handle_dhcp-> receive a packet\n"); DHCPS_LOG("dhcps: handle_dhcp-> receive a packet\n");
#endif #endif
if (p==NULL) return;
if (p == NULL) {
return;
}
pmsg_dhcps = (struct dhcps_msg *)malloc(sizeof(struct dhcps_msg)); pmsg_dhcps = (struct dhcps_msg *)malloc(sizeof(struct dhcps_msg));
memset(pmsg_dhcps ,0x00 ,sizeof(struct dhcps_msg)); memset(pmsg_dhcps , 0x00 , sizeof(struct dhcps_msg));
if (NULL == pmsg_dhcps){ if (NULL == pmsg_dhcps) {
pbuf_free(p); pbuf_free(p);
return; return;
} }
p_dhcps_msg = (u8_t *)pmsg_dhcps; p_dhcps_msg = (u8_t *)pmsg_dhcps;
tlen = p->tot_len; tlen = p->tot_len;
data = p->payload; data = p->payload;
@@ -846,31 +894,36 @@ static void handle_dhcp(void *arg,
DHCPS_LOG("dhcps: handle_dhcp-> p->len = %d\n", p->len); DHCPS_LOG("dhcps: handle_dhcp-> p->len = %d\n", p->len);
#endif #endif
for(i=0; i<p->len; i++){ for (i = 0; i < p->len; i++) {
p_dhcps_msg[dhcps_msg_cnt++] = data[i]; p_dhcps_msg[dhcps_msg_cnt++] = data[i];
#if DHCPS_DEBUG #if DHCPS_DEBUG
DHCPS_LOG("%02x ",data[i]); DHCPS_LOG("%02x ", data[i]);
if((i+1)%16 == 0){
if ((i + 1) % 16 == 0) {
DHCPS_LOG("\n"); DHCPS_LOG("\n");
} }
#endif #endif
} }
if(p->next != NULL) { if (p->next != NULL) {
#if DHCPS_DEBUG #if DHCPS_DEBUG
DHCPS_LOG("dhcps: handle_dhcp-> p->next != NULL\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->tot_len = %d\n", p->next->tot_len);
DHCPS_LOG("dhcps: handle_dhcp-> p->next->len = %d\n",p->next->len); DHCPS_LOG("dhcps: handle_dhcp-> p->next->len = %d\n", p->next->len);
#endif #endif
data = p->next->payload; data = p->next->payload;
for(i=0; i<p->next->len; i++){
for (i = 0; i < p->next->len; i++) {
p_dhcps_msg[dhcps_msg_cnt++] = data[i]; p_dhcps_msg[dhcps_msg_cnt++] = data[i];
#if DHCPS_DEBUG #if DHCPS_DEBUG
DHCPS_LOG("%02x ",data[i]); DHCPS_LOG("%02x ", data[i]);
if((i+1)%16 == 0){
if ((i + 1) % 16 == 0) {
DHCPS_LOG("\n"); DHCPS_LOG("\n");
} }
#endif #endif
} }
} }
@@ -879,28 +932,32 @@ static void handle_dhcp(void *arg,
DHCPS_LOG("dhcps: handle_dhcp-> parse_msg(p)\n"); DHCPS_LOG("dhcps: handle_dhcp-> parse_msg(p)\n");
#endif #endif
switch(parse_msg(pmsg_dhcps, tlen - 240)) { switch (parse_msg(pmsg_dhcps, tlen - 240)) {
case DHCPS_STATE_OFFER://1 case DHCPS_STATE_OFFER://1
#if DHCPS_DEBUG #if DHCPS_DEBUG
DHCPS_LOG("dhcps: handle_dhcp-> DHCPD_STATE_OFFER\n"); DHCPS_LOG("dhcps: handle_dhcp-> DHCPD_STATE_OFFER\n");
#endif #endif
send_offer(pmsg_dhcps); send_offer(pmsg_dhcps);
break; break;
case DHCPS_STATE_ACK://3 case DHCPS_STATE_ACK://3
#if DHCPS_DEBUG #if DHCPS_DEBUG
DHCPS_LOG("dhcps: handle_dhcp-> DHCPD_STATE_ACK\n"); DHCPS_LOG("dhcps: handle_dhcp-> DHCPD_STATE_ACK\n");
#endif #endif
send_ack(pmsg_dhcps); send_ack(pmsg_dhcps);
break; break;
case DHCPS_STATE_NAK://4 case DHCPS_STATE_NAK://4
#if DHCPS_DEBUG #if DHCPS_DEBUG
DHCPS_LOG("dhcps: handle_dhcp-> DHCPD_STATE_NAK\n"); DHCPS_LOG("dhcps: handle_dhcp-> DHCPD_STATE_NAK\n");
#endif #endif
send_nak(pmsg_dhcps); send_nak(pmsg_dhcps);
break; break;
default : default :
break; break;
} }
#if DHCPS_DEBUG #if DHCPS_DEBUG
DHCPS_LOG("dhcps: handle_dhcp-> pbuf_free(p)\n"); DHCPS_LOG("dhcps: handle_dhcp-> pbuf_free(p)\n");
#endif #endif
@@ -917,19 +974,22 @@ static void handle_dhcp(void *arg,
*******************************************************************************/ *******************************************************************************/
static void dhcps_poll_set(u32_t ip) static void dhcps_poll_set(u32_t ip)
{ {
u32_t softap_ip = 0,local_ip = 0; u32_t softap_ip = 0, local_ip = 0;
u32_t start_ip = 0; u32_t start_ip = 0;
u32_t end_ip = 0; u32_t end_ip = 0;
if (dhcps_poll.enable == true) { if (dhcps_poll.enable == true) {
softap_ip = htonl(ip); softap_ip = htonl(ip);
start_ip = htonl(dhcps_poll.start_ip.addr); start_ip = htonl(dhcps_poll.start_ip.addr);
end_ip = htonl(dhcps_poll.end_ip.addr); end_ip = htonl(dhcps_poll.end_ip.addr);
/*config ip information can't contain local ip*/ /*config ip information can't contain local ip*/
if ((start_ip <= softap_ip) && (softap_ip <= end_ip)) { if ((start_ip <= softap_ip) && (softap_ip <= end_ip)) {
dhcps_poll.enable = false; dhcps_poll.enable = false;
} else { } else {
/*config ip information must be in the same segment as the local ip*/ /*config ip information must be in the same segment as the local ip*/
softap_ip >>= 8; softap_ip >>= 8;
if (((start_ip >> 8 != softap_ip) || (end_ip >> 8 != softap_ip)) if (((start_ip >> 8 != softap_ip) || (end_ip >> 8 != softap_ip))
|| (end_ip - start_ip > DHCPS_MAX_LEASE)) { || (end_ip - start_ip > DHCPS_MAX_LEASE)) {
dhcps_poll.enable = false; dhcps_poll.enable = false;
@@ -941,16 +1001,18 @@ static void dhcps_poll_set(u32_t ip)
local_ip = softap_ip = htonl(ip); local_ip = softap_ip = htonl(ip);
softap_ip &= 0xFFFFFF00; softap_ip &= 0xFFFFFF00;
local_ip &= 0xFF; local_ip &= 0xFF;
if (local_ip >= 0x80)
if (local_ip >= 0x80) {
local_ip -= DHCPS_MAX_LEASE; local_ip -= DHCPS_MAX_LEASE;
else } else {
local_ip ++; local_ip ++;
}
bzero(&dhcps_poll, sizeof(dhcps_poll)); bzero(&dhcps_poll, sizeof(dhcps_poll));
dhcps_poll.start_ip.addr = softap_ip | local_ip; dhcps_poll.start_ip.addr = softap_ip | local_ip;
dhcps_poll.end_ip.addr = softap_ip | (local_ip + DHCPS_MAX_LEASE - 1); 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.start_ip.addr = htonl(dhcps_poll.start_ip.addr);
dhcps_poll.end_ip.addr= htonl(dhcps_poll.end_ip.addr); dhcps_poll.end_ip.addr = htonl(dhcps_poll.end_ip.addr);
} }
} }
@@ -964,22 +1026,25 @@ static void dhcps_poll_set(u32_t ip)
*******************************************************************************/ *******************************************************************************/
void dhcps_start(struct netif *netif, struct ip_info *info) void dhcps_start(struct netif *netif, struct ip_info *info)
{ {
struct netif * apnetif =netif; struct netif *apnetif = netif;
if(apnetif->dhcps_pcb != NULL) { if (apnetif->dhcps_pcb != NULL) {
udp_remove(apnetif->dhcps_pcb); udp_remove(apnetif->dhcps_pcb);
} }
pcb_dhcps = udp_new(); pcb_dhcps = udp_new();
if (pcb_dhcps == NULL || info ==NULL) {
if (pcb_dhcps == NULL || info == NULL) {
printf("dhcps_start(): could not obtain pcb\n"); printf("dhcps_start(): could not obtain pcb\n");
} }
apnetif->dhcps_pcb = pcb_dhcps; apnetif->dhcps_pcb = pcb_dhcps;
IP4_ADDR(&broadcast_dhcps, 255, 255, 255, 255); IP4_ADDR(&broadcast_dhcps, 255, 255, 255, 255);
server_address = info->ip; server_address = info->ip;
dhcps_poll_set( server_address.addr ); dhcps_poll_set(server_address.addr);
client_address_plus.addr = dhcps_poll.start_ip.addr; client_address_plus.addr = dhcps_poll.start_ip.addr;
@@ -997,17 +1062,18 @@ void dhcps_start(struct netif *netif, struct ip_info *info)
* Parameters : netif -- The current netif addr * Parameters : netif -- The current netif addr
* Returns : none * Returns : none
*******************************************************************************/ *******************************************************************************/
void dhcps_stop(struct netif *netif ) void dhcps_stop(struct netif *netif)
{ {
struct netif * apnetif = netif; struct netif *apnetif = netif;
if(apnetif == NULL)
{ if (apnetif == NULL) {
printf("dhcps_stop: apnetif == NULL\n"); printf("dhcps_stop: apnetif == NULL\n");
return; return;
} }
udp_disconnect(pcb_dhcps); udp_disconnect(pcb_dhcps);
if(apnetif->dhcps_pcb != NULL) { if (apnetif->dhcps_pcb != NULL) {
udp_remove(apnetif->dhcps_pcb); udp_remove(apnetif->dhcps_pcb);
apnetif->dhcps_pcb = NULL; apnetif->dhcps_pcb = NULL;
} }
@@ -1015,6 +1081,7 @@ void dhcps_stop(struct netif *netif )
list_node *pnode = NULL; list_node *pnode = NULL;
list_node *pback_node = NULL; list_node *pback_node = NULL;
pnode = plist; pnode = plist;
while (pnode != NULL) { while (pnode != NULL) {
pback_node = pnode; pback_node = pnode;
pnode = pback_node->pnext; pnode = pback_node->pnext;
@@ -1041,16 +1108,20 @@ static void kill_oldest_dhcps_pool(void)
p = pre->pnext; p = pre->pnext;
minpre = pre; minpre = pre;
minp = p; minp = p;
while (p != NULL){
while (p != NULL) {
pdhcps_pool = p->pnode; pdhcps_pool = p->pnode;
pmin_pool = minp->pnode; pmin_pool = minp->pnode;
if (pdhcps_pool->lease_timer < pmin_pool->lease_timer){
if (pdhcps_pool->lease_timer < pmin_pool->lease_timer) {
minp = p; minp = p;
minpre = pre; minpre = pre;
} }
pre = p; pre = p;
p = p->pnext; p = p->pnext;
} }
minpre->pnext = minp->pnext; minpre->pnext = minp->pnext;
free(minp->pnode); free(minp->pnode);
minp->pnode = NULL; minp->pnode = NULL;
@@ -1071,13 +1142,15 @@ void dhcps_coarse_tmr(void)
list_node *pnode = NULL; list_node *pnode = NULL;
struct dhcps_pool *pdhcps_pool = NULL; struct dhcps_pool *pdhcps_pool = NULL;
pnode = plist; pnode = plist;
while (pnode != NULL) { while (pnode != NULL) {
pdhcps_pool = pnode->pnode; pdhcps_pool = pnode->pnode;
pdhcps_pool->lease_timer --; pdhcps_pool->lease_timer --;
if (pdhcps_pool->lease_timer == 0){
if (pdhcps_pool->lease_timer == 0) {
pback_node = pnode; pback_node = pnode;
pnode = pback_node->pnext; pnode = pback_node->pnext;
node_remove_from_list(&plist,pback_node); node_remove_from_list(&plist, pback_node);
free(pback_node->pnode); free(pback_node->pnode);
pback_node->pnode = NULL; pback_node->pnode = NULL;
free(pback_node); free(pback_node);
@@ -1088,8 +1161,9 @@ void dhcps_coarse_tmr(void)
} }
} }
if (num_dhcps_pool >= MAX_STATION_NUM) if (num_dhcps_pool >= MAX_STATION_NUM) {
kill_oldest_dhcps_pool(); kill_oldest_dhcps_pool();
}
} }
/****************************************************************************** /******************************************************************************
@@ -1105,9 +1179,10 @@ bool dhcp_search_ip_on_mac(u8_t *mac, ip4_addr_t *ip)
list_node *pback_node = NULL; list_node *pback_node = NULL;
bool ret = false; 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; 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)); memcpy(&ip->addr, &pdhcps_pool->ip.addr, sizeof(pdhcps_pool->ip.addr));
ret = true; ret = true;
break; break;