forked from espressif/esp-idf
lwip: format dhcpserver.c
This commit is contained in:
@@ -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* option_arg = NULL;
|
||||
switch (op_id){
|
||||
void *option_arg = NULL;
|
||||
|
||||
switch (op_id) {
|
||||
case IP_ADDRESS_LEASE_TIME:
|
||||
if (opt_len == sizeof(dhcps_time_t))
|
||||
if (opt_len == sizeof(dhcps_time_t)) {
|
||||
option_arg = &dhcps_lease_time;
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case REQUESTED_IP_ADDRESS:
|
||||
if (opt_len == sizeof(dhcps_lease_t))
|
||||
if (opt_len == sizeof(dhcps_lease_t)) {
|
||||
option_arg = &dhcps_poll;
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case ROUTER_SOLICITATION_ADDRESS:
|
||||
if (opt_len == sizeof(dhcps_offer_t))
|
||||
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)
|
||||
|
||||
if (*phead == NULL) {
|
||||
*phead = pinsert;
|
||||
else {
|
||||
} else {
|
||||
plist = *phead;
|
||||
pdhcps_node = pinsert->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;
|
||||
*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,23 +184,25 @@ 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){
|
||||
|
||||
if (plist == NULL) {
|
||||
*phead = NULL;
|
||||
} else {
|
||||
if (plist == pdelete){
|
||||
if (plist == pdelete) {
|
||||
*phead = plist->pnext;
|
||||
pdelete->pnext = NULL;
|
||||
} else {
|
||||
while (plist != NULL) {
|
||||
if (plist->pnext == pdelete){
|
||||
if (plist->pnext == pdelete) {
|
||||
plist->pnext = pdelete->pnext;
|
||||
pdelete->pnext = NULL;
|
||||
}
|
||||
|
||||
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
|
||||
* 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;
|
||||
@@ -212,11 +229,11 @@ 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;
|
||||
|
||||
ipadd.addr = *( (u32_t *) &server_address);
|
||||
ipadd.addr = *((u32_t *) &server_address);
|
||||
|
||||
#ifdef USE_CLASS_B_NET
|
||||
*optptr++ = DHCP_OPTION_SUBNET_MASK;
|
||||
@@ -243,48 +260,48 @@ static u8_t* add_offer_options(u8_t *optptr)
|
||||
|
||||
*optptr++ = DHCP_OPTION_SERVER_ID;
|
||||
*optptr++ = 4;
|
||||
*optptr++ = ip4_addr1( &ipadd);
|
||||
*optptr++ = ip4_addr2( &ipadd);
|
||||
*optptr++ = ip4_addr3( &ipadd);
|
||||
*optptr++ = ip4_addr4( &ipadd);
|
||||
*optptr++ = ip4_addr1(&ipadd);
|
||||
*optptr++ = ip4_addr2(&ipadd);
|
||||
*optptr++ = ip4_addr3(&ipadd);
|
||||
*optptr++ = ip4_addr4(&ipadd);
|
||||
|
||||
if (dhcps_router_enabled(dhcps_offer)){
|
||||
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));
|
||||
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++ = 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++ = 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++ = 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++ = ip4_addr1(&ipadd);
|
||||
*optptr++ = ip4_addr2(&ipadd);
|
||||
*optptr++ = ip4_addr3(&ipadd);
|
||||
*optptr++ = 255;
|
||||
#endif
|
||||
|
||||
@@ -321,7 +338,7 @@ 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;
|
||||
@@ -338,7 +355,7 @@ static void create_msg(struct dhcps_msg *m)
|
||||
ip4_addr_t client;
|
||||
|
||||
|
||||
client.addr = *( (uint32_t *) &client_address);
|
||||
client.addr = *((uint32_t *) &client_address);
|
||||
|
||||
m->op = DHCP_REPLY;
|
||||
|
||||
@@ -381,7 +398,7 @@ static void send_offer(struct dhcps_msg *m)
|
||||
u8_t *end;
|
||||
struct pbuf *p, *q;
|
||||
u8_t *data;
|
||||
u16_t cnt=0;
|
||||
u16_t cnt = 0;
|
||||
u16_t i;
|
||||
err_t SendOffer_err_t;
|
||||
create_msg(m);
|
||||
@@ -394,7 +411,8 @@ static void send_offer(struct dhcps_msg *m)
|
||||
#if DHCPS_DEBUG
|
||||
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");
|
||||
@@ -402,22 +420,25 @@ static void send_offer(struct dhcps_msg *m)
|
||||
DHCPS_LOG("dhcps: send_offer>>p->len = %d\n", p->len);
|
||||
#endif
|
||||
q = p;
|
||||
while(q != NULL){
|
||||
|
||||
while (q != NULL) {
|
||||
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++];
|
||||
#if DHCPS_DEBUG
|
||||
DHCPS_LOG("%02x ",data[i]);
|
||||
if((i+1)%16 == 0){
|
||||
DHCPS_LOG("%02x ", data[i]);
|
||||
|
||||
if ((i + 1) % 16 == 0) {
|
||||
DHCPS_LOG("\n");
|
||||
}
|
||||
|
||||
#endif
|
||||
}
|
||||
|
||||
q = q->next;
|
||||
}
|
||||
}else{
|
||||
} else {
|
||||
|
||||
#if DHCPS_DEBUG
|
||||
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);
|
||||
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
|
||||
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");
|
||||
#endif
|
||||
@@ -450,7 +472,7 @@ static void send_nak(struct dhcps_msg *m)
|
||||
u8_t *end;
|
||||
struct pbuf *p, *q;
|
||||
u8_t *data;
|
||||
u16_t cnt=0;
|
||||
u16_t cnt = 0;
|
||||
u16_t i;
|
||||
err_t SendNak_err_t;
|
||||
create_msg(m);
|
||||
@@ -462,7 +484,8 @@ static void send_nak(struct dhcps_msg *m)
|
||||
#if DHCPS_DEBUG
|
||||
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");
|
||||
@@ -470,22 +493,25 @@ static void send_nak(struct dhcps_msg *m)
|
||||
DHCPS_LOG("dhcps: send_nak>>p->len = %d\n", p->len);
|
||||
#endif
|
||||
q = p;
|
||||
while(q != NULL){
|
||||
|
||||
while (q != NULL) {
|
||||
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++];
|
||||
#if DHCPS_DEBUG
|
||||
DHCPS_LOG("%02x ",data[i]);
|
||||
if((i+1)%16 == 0){
|
||||
DHCPS_LOG("%02x ", data[i]);
|
||||
|
||||
if ((i + 1) % 16 == 0) {
|
||||
DHCPS_LOG("\n");
|
||||
}
|
||||
|
||||
#endif
|
||||
}
|
||||
|
||||
q = q->next;
|
||||
}
|
||||
}else{
|
||||
} else {
|
||||
|
||||
#if DHCPS_DEBUG
|
||||
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);
|
||||
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
|
||||
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 (p->ref != 0) {
|
||||
#if DHCPS_DEBUG
|
||||
DHCPS_LOG("udhcp: send_nak>>free pbuf\n");
|
||||
#endif
|
||||
@@ -518,7 +545,7 @@ static void send_ack(struct dhcps_msg *m)
|
||||
u8_t *end;
|
||||
struct pbuf *p, *q;
|
||||
u8_t *data;
|
||||
u16_t cnt=0;
|
||||
u16_t cnt = 0;
|
||||
u16_t i;
|
||||
err_t SendAck_err_t;
|
||||
create_msg(m);
|
||||
@@ -531,7 +558,8 @@ static void send_ack(struct dhcps_msg *m)
|
||||
#if DHCPS_DEBUG
|
||||
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");
|
||||
@@ -539,22 +567,25 @@ static void send_ack(struct dhcps_msg *m)
|
||||
DHCPS_LOG("dhcps: send_ack>>p->len = %d\n", p->len);
|
||||
#endif
|
||||
q = p;
|
||||
while(q != NULL){
|
||||
|
||||
while (q != NULL) {
|
||||
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++];
|
||||
#if DHCPS_DEBUG
|
||||
DHCPS_LOG("%02x ",data[i]);
|
||||
if((i+1)%16 == 0){
|
||||
DHCPS_LOG("%02x ", data[i]);
|
||||
|
||||
if ((i + 1) % 16 == 0) {
|
||||
DHCPS_LOG("\n");
|
||||
}
|
||||
|
||||
#endif
|
||||
}
|
||||
|
||||
q = q->next;
|
||||
}
|
||||
}else{
|
||||
} else {
|
||||
|
||||
#if DHCPS_DEBUG
|
||||
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);
|
||||
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
|
||||
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");
|
||||
#endif
|
||||
@@ -590,7 +621,7 @@ static u8_t parse_options(u8_t *optptr, s16_t len)
|
||||
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;
|
||||
@@ -601,6 +632,7 @@ static u8_t parse_options(u8_t *optptr, s16_t len)
|
||||
#if DHCPS_DEBUG
|
||||
DHCPS_LOG("dhcps: (s16_t)*optptr = %d\n", (s16_t)*optptr);
|
||||
#endif
|
||||
|
||||
switch ((s16_t) *optptr) {
|
||||
|
||||
case DHCP_OPTION_MSG_TYPE: //53
|
||||
@@ -608,33 +640,34 @@ static u8_t parse_options(u8_t *optptr, s16_t len)
|
||||
break;
|
||||
|
||||
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
|
||||
DHCPS_LOG("dhcps: DHCP_OPTION_REQ_IPADDR = 0 ok\n");
|
||||
#endif
|
||||
s.state = DHCPS_STATE_ACK;
|
||||
}else {
|
||||
} else {
|
||||
#if DHCPS_DEBUG
|
||||
DHCPS_LOG("dhcps: DHCP_OPTION_REQ_IPADDR != 0 err\n");
|
||||
#endif
|
||||
s.state = DHCPS_STATE_NAK;
|
||||
}
|
||||
|
||||
break;
|
||||
case DHCP_OPTION_END:
|
||||
{
|
||||
|
||||
case DHCP_OPTION_END: {
|
||||
is_dhcp_parse_end = true;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
if(is_dhcp_parse_end){
|
||||
if (is_dhcp_parse_end) {
|
||||
break;
|
||||
}
|
||||
|
||||
optptr += optptr[1] + 2;
|
||||
}
|
||||
|
||||
switch (type){
|
||||
switch (type) {
|
||||
|
||||
case DHCPDISCOVER://1
|
||||
s.state = DHCPS_STATE_OFFER;
|
||||
@@ -644,16 +677,18 @@ static u8_t parse_options(u8_t *optptr, s16_t len)
|
||||
break;
|
||||
|
||||
case DHCPREQUEST://3
|
||||
if ( !(s.state == DHCPS_STATE_ACK || s.state == DHCPS_STATE_NAK) ) {
|
||||
if(renew == true) {
|
||||
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");
|
||||
#endif
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case DHCPDECLINE://4
|
||||
@@ -670,6 +705,7 @@ static u8_t parse_options(u8_t *optptr, s16_t len)
|
||||
#endif
|
||||
break;
|
||||
}
|
||||
|
||||
#if DHCPS_DEBUG
|
||||
DHCPS_LOG("dhcps: return s.state = %d\n", s.state);
|
||||
#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)
|
||||
{
|
||||
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);
|
||||
#endif
|
||||
@@ -701,18 +737,20 @@ static s16_t parse_msg(struct dhcps_msg *m, u16_t len)
|
||||
client_address.addr = client_address_plus.addr;
|
||||
renew = false;
|
||||
|
||||
if (plist != NULL){
|
||||
for (pback_node = plist; pback_node != NULL;pback_node = pback_node->pnext) {
|
||||
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->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){
|
||||
} 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);
|
||||
@@ -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) {
|
||||
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));
|
||||
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 = (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);
|
||||
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 {
|
||||
@@ -762,10 +802,11 @@ static s16_t parse_msg(struct dhcps_msg *m, u16_t len)
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
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;
|
||||
}
|
||||
@@ -774,14 +815,15 @@ static s16_t parse_msg(struct dhcps_msg *m, u16_t len)
|
||||
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);
|
||||
if (ret == DHCPS_STATE_RELEASE) {
|
||||
if (pnode != NULL) {
|
||||
node_remove_from_list(&plist, pnode);
|
||||
free(pnode);
|
||||
pnode = NULL;
|
||||
}
|
||||
@@ -790,7 +832,8 @@ static s16_t parse_msg(struct dhcps_msg *m, u16_t len)
|
||||
free(pdhcps_pool);
|
||||
pdhcps_pool = NULL;
|
||||
}
|
||||
memset(&client_address,0x0,sizeof(client_address));
|
||||
|
||||
memset(&client_address, 0x0, sizeof(client_address));
|
||||
}
|
||||
|
||||
#if DHCPS_DEBUG
|
||||
@@ -799,6 +842,7 @@ static s16_t parse_msg(struct dhcps_msg *m, u16_t len)
|
||||
#endif
|
||||
return ret;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -821,22 +865,26 @@ static void handle_dhcp(void *arg,
|
||||
struct dhcps_msg *pmsg_dhcps = NULL;
|
||||
s16_t tlen;
|
||||
u16_t i;
|
||||
u16_t dhcps_msg_cnt=0;
|
||||
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");
|
||||
#endif
|
||||
if (p==NULL) return;
|
||||
|
||||
if (p == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
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);
|
||||
return;
|
||||
}
|
||||
|
||||
p_dhcps_msg = (u8_t *)pmsg_dhcps;
|
||||
tlen = p->tot_len;
|
||||
data = p->payload;
|
||||
@@ -846,31 +894,36 @@ static void handle_dhcp(void *arg,
|
||||
DHCPS_LOG("dhcps: handle_dhcp-> p->len = %d\n", p->len);
|
||||
#endif
|
||||
|
||||
for(i=0; i<p->len; i++){
|
||||
for (i = 0; i < p->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("%02x ", data[i]);
|
||||
|
||||
if ((i + 1) % 16 == 0) {
|
||||
DHCPS_LOG("\n");
|
||||
}
|
||||
|
||||
#endif
|
||||
}
|
||||
|
||||
if(p->next != NULL) {
|
||||
if (p->next != NULL) {
|
||||
#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);
|
||||
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; i<p->next->len; i++){
|
||||
|
||||
for (i = 0; i < p->next->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("%02x ", data[i]);
|
||||
|
||||
if ((i + 1) % 16 == 0) {
|
||||
DHCPS_LOG("\n");
|
||||
}
|
||||
|
||||
#endif
|
||||
}
|
||||
}
|
||||
@@ -879,28 +932,32 @@ static void handle_dhcp(void *arg,
|
||||
DHCPS_LOG("dhcps: handle_dhcp-> parse_msg(p)\n");
|
||||
#endif
|
||||
|
||||
switch(parse_msg(pmsg_dhcps, tlen - 240)) {
|
||||
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
|
||||
@@ -917,19 +974,22 @@ static void handle_dhcp(void *arg,
|
||||
*******************************************************************************/
|
||||
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 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;
|
||||
@@ -941,16 +1001,18 @@ static void dhcps_poll_set(u32_t ip)
|
||||
local_ip = softap_ip = htonl(ip);
|
||||
softap_ip &= 0xFFFFFF00;
|
||||
local_ip &= 0xFF;
|
||||
if (local_ip >= 0x80)
|
||||
|
||||
if (local_ip >= 0x80) {
|
||||
local_ip -= DHCPS_MAX_LEASE;
|
||||
else
|
||||
} 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);
|
||||
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)
|
||||
{
|
||||
struct netif * apnetif =netif;
|
||||
struct netif *apnetif = netif;
|
||||
|
||||
|
||||
if(apnetif->dhcps_pcb != NULL) {
|
||||
if (apnetif->dhcps_pcb != NULL) {
|
||||
udp_remove(apnetif->dhcps_pcb);
|
||||
}
|
||||
|
||||
pcb_dhcps = udp_new();
|
||||
if (pcb_dhcps == NULL || info ==NULL) {
|
||||
|
||||
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 );
|
||||
dhcps_poll_set(server_address.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
|
||||
* 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;
|
||||
@@ -1041,16 +1108,20 @@ static void kill_oldest_dhcps_pool(void)
|
||||
p = pre->pnext;
|
||||
minpre = pre;
|
||||
minp = p;
|
||||
while (p != NULL){
|
||||
|
||||
while (p != NULL) {
|
||||
pdhcps_pool = p->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;
|
||||
minpre = pre;
|
||||
}
|
||||
|
||||
pre = p;
|
||||
p = p->pnext;
|
||||
}
|
||||
|
||||
minpre->pnext = minp->pnext;
|
||||
free(minp->pnode);
|
||||
minp->pnode = NULL;
|
||||
@@ -1071,13 +1142,15 @@ void dhcps_coarse_tmr(void)
|
||||
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){
|
||||
|
||||
if (pdhcps_pool->lease_timer == 0) {
|
||||
pback_node = pnode;
|
||||
pnode = pback_node->pnext;
|
||||
node_remove_from_list(&plist,pback_node);
|
||||
node_remove_from_list(&plist, pback_node);
|
||||
free(pback_node->pnode);
|
||||
pback_node->pnode = NULL;
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
/******************************************************************************
|
||||
@@ -1105,9 +1179,10 @@ bool dhcp_search_ip_on_mac(u8_t *mac, ip4_addr_t *ip)
|
||||
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;
|
||||
|
Reference in New Issue
Block a user