Add posssibility to disable debug in dhcp.c
This commit is contained in:
parent
b83c2629b9
commit
ce67dcd097
1 changed files with 21 additions and 14 deletions
|
|
@ -20,6 +20,12 @@
|
||||||
#include <lwip/netif.h>
|
#include <lwip/netif.h>
|
||||||
#include <lwip/api.h>
|
#include <lwip/api.h>
|
||||||
|
|
||||||
|
#if (DHCP_DEBUG == LWIP_DBG_ON)
|
||||||
|
#define debug(s, ...) printf("%s: " s "\n", "DHCP", ## __VA_ARGS__)
|
||||||
|
#else
|
||||||
|
#define debug(s, ...)
|
||||||
|
#endif
|
||||||
|
|
||||||
/* Grow the size of the lwip dhcp_msg struct's options field, as LWIP
|
/* Grow the size of the lwip dhcp_msg struct's options field, as LWIP
|
||||||
defaults to a 68 octet options field for its DHCP client, and most
|
defaults to a 68 octet options field for its DHCP client, and most
|
||||||
full-sized clients send us more than this. */
|
full-sized clients send us more than this. */
|
||||||
|
|
@ -108,7 +114,7 @@ static void dhcpserver_task(void *pxParameter)
|
||||||
|
|
||||||
state->nc = netconn_new (NETCONN_UDP);
|
state->nc = netconn_new (NETCONN_UDP);
|
||||||
if(!state->nc) {
|
if(!state->nc) {
|
||||||
printf("DHCP Server Error: Failed to allocate socket.\r\n");
|
debug("DHCP Server Error: Failed to allocate socket.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -122,7 +128,7 @@ static void dhcpserver_task(void *pxParameter)
|
||||||
/* Receive a DHCP packet */
|
/* Receive a DHCP packet */
|
||||||
err_t err = netconn_recv(state->nc, &netbuf);
|
err_t err = netconn_recv(state->nc, &netbuf);
|
||||||
if(err != ERR_OK) {
|
if(err != ERR_OK) {
|
||||||
printf("DHCP Server Error: Failed to receive DHCP packet. err=%d\r\n", err);
|
debug("DHCP Server Error: Failed to receive DHCP packet. err=%d", err);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -144,7 +150,7 @@ static void dhcpserver_task(void *pxParameter)
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if(netbuf_len(netbuf) >= sizeof(struct dhcp_msg)) {
|
if(netbuf_len(netbuf) >= sizeof(struct dhcp_msg)) {
|
||||||
printf("DHCP Server Warning: Client sent more options than we know how to parse. len=%d\r\n", netbuf_len(netbuf));
|
debug("DHCP Server Warning: Client sent more options than we know how to parse. len=%d", netbuf_len(netbuf));
|
||||||
}
|
}
|
||||||
|
|
||||||
netbuf_copy(netbuf, &received, sizeof(struct dhcp_msg));
|
netbuf_copy(netbuf, &received, sizeof(struct dhcp_msg));
|
||||||
|
|
@ -153,18 +159,19 @@ static void dhcpserver_task(void *pxParameter)
|
||||||
uint8_t *message_type = find_dhcp_option(&received, DHCP_OPTION_MESSAGE_TYPE,
|
uint8_t *message_type = find_dhcp_option(&received, DHCP_OPTION_MESSAGE_TYPE,
|
||||||
DHCP_OPTION_MESSAGE_TYPE_LEN, NULL);
|
DHCP_OPTION_MESSAGE_TYPE_LEN, NULL);
|
||||||
if(!message_type) {
|
if(!message_type) {
|
||||||
printf("DHCP Server Error: No message type field found");
|
debug("DHCP Server Error: No message type field found");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if (DHCP_DEBUG == LWIP_DBG_ON)
|
||||||
printf("State dump. Message type %d\n", *message_type);
|
debug("State dump. Message type %d", *message_type);
|
||||||
for(int i = 0; i < state->max_leases; i++) {
|
for(int i = 0; i < state->max_leases; i++) {
|
||||||
dhcp_lease_t *lease = &state->leases[i];
|
dhcp_lease_t *lease = &state->leases[i];
|
||||||
printf("lease slot %d expiry %d hwaddr %02x:%02x:%02x:%02x:%02x:%02x\r\n", i, lease->expires, lease->hwaddr[0],
|
debug("lease slot %d expiry %d hwaddr %02x:%02x:%02x:%02x:%02x:%02x", i, lease->expires, lease->hwaddr[0],
|
||||||
lease->hwaddr[1], lease->hwaddr[2], lease->hwaddr[3], lease->hwaddr[4],
|
lease->hwaddr[1], lease->hwaddr[2], lease->hwaddr[3], lease->hwaddr[4],
|
||||||
lease->hwaddr[5]);
|
lease->hwaddr[5]);
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
switch(*message_type) {
|
switch(*message_type) {
|
||||||
case DHCP_DISCOVER:
|
case DHCP_DISCOVER:
|
||||||
|
|
@ -176,7 +183,7 @@ static void dhcpserver_task(void *pxParameter)
|
||||||
case DHCP_RELEASE:
|
case DHCP_RELEASE:
|
||||||
handle_dhcp_release(&received);
|
handle_dhcp_release(&received);
|
||||||
default:
|
default:
|
||||||
printf("DHCP Server Error: Unsupported message type %d\r\n", *message_type);
|
debug("DHCP Server Error: Unsupported message type %d", *message_type);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -191,7 +198,7 @@ static void handle_dhcp_discover(struct dhcp_msg *dhcpmsg)
|
||||||
|
|
||||||
dhcp_lease_t *freelease = find_lease_slot(dhcpmsg->chaddr);
|
dhcp_lease_t *freelease = find_lease_slot(dhcpmsg->chaddr);
|
||||||
if(!freelease) {
|
if(!freelease) {
|
||||||
printf("DHCP Server: All leases taken.\r\n");
|
debug("DHCP Server: All leases taken.");
|
||||||
return; /* Nothing available, so do nothing */
|
return; /* Nothing available, so do nothing */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -230,7 +237,7 @@ static void handle_dhcp_request(struct dhcp_msg *dhcpmsg)
|
||||||
} else if(ip_addr_cmp(&requested_ip, IP_ADDR_ANY)) {
|
} else if(ip_addr_cmp(&requested_ip, IP_ADDR_ANY)) {
|
||||||
ip_addr_copy(requested_ip, dhcpmsg->ciaddr);
|
ip_addr_copy(requested_ip, dhcpmsg->ciaddr);
|
||||||
} else {
|
} else {
|
||||||
printf("DHCP Server Error: No requested IP\r\n");
|
debug("DHCP Server Error: No requested IP");
|
||||||
send_dhcp_nak(dhcpmsg);
|
send_dhcp_nak(dhcpmsg);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
@ -240,14 +247,14 @@ static void handle_dhcp_request(struct dhcp_msg *dhcpmsg)
|
||||||
|| ip4_addr2(&requested_ip) != ip4_addr2(&state->first_client_addr)
|
|| ip4_addr2(&requested_ip) != ip4_addr2(&state->first_client_addr)
|
||||||
|| ip4_addr3(&requested_ip) != ip4_addr3(&state->first_client_addr)) {
|
|| ip4_addr3(&requested_ip) != ip4_addr3(&state->first_client_addr)) {
|
||||||
sprintf_ipaddr(&requested_ip, ipbuf);
|
sprintf_ipaddr(&requested_ip, ipbuf);
|
||||||
printf("DHCP Server Error: %s not an allowed IP\r\n", ipbuf);
|
debug("DHCP Server Error: %s not an allowed IP", ipbuf);
|
||||||
send_dhcp_nak(dhcpmsg);
|
send_dhcp_nak(dhcpmsg);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
/* Test the last octet is in the MAXCLIENTS range */
|
/* Test the last octet is in the MAXCLIENTS range */
|
||||||
int16_t octet_offs = ip4_addr4(&requested_ip) - ip4_addr4(&state->first_client_addr);
|
int16_t octet_offs = ip4_addr4(&requested_ip) - ip4_addr4(&state->first_client_addr);
|
||||||
if(octet_offs < 0 || octet_offs >= state->max_leases) {
|
if(octet_offs < 0 || octet_offs >= state->max_leases) {
|
||||||
printf("DHCP Server Error: Address out of range\r\n");
|
debug("DHCP Server Error: Address out of range");
|
||||||
send_dhcp_nak(dhcpmsg);
|
send_dhcp_nak(dhcpmsg);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
@ -255,14 +262,14 @@ static void handle_dhcp_request(struct dhcp_msg *dhcpmsg)
|
||||||
dhcp_lease_t *requested_lease = state->leases + octet_offs;
|
dhcp_lease_t *requested_lease = state->leases + octet_offs;
|
||||||
if(requested_lease->expires != 0 && memcmp(requested_lease->hwaddr, dhcpmsg->chaddr,dhcpmsg->hlen))
|
if(requested_lease->expires != 0 && memcmp(requested_lease->hwaddr, dhcpmsg->chaddr,dhcpmsg->hlen))
|
||||||
{
|
{
|
||||||
printf("DHCP Server Error: Lease for address already taken\r\n");
|
debug("DHCP Server Error: Lease for address already taken");
|
||||||
send_dhcp_nak(dhcpmsg);
|
send_dhcp_nak(dhcpmsg);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
memcpy(requested_lease->hwaddr, dhcpmsg->chaddr, dhcpmsg->hlen);
|
memcpy(requested_lease->hwaddr, dhcpmsg->chaddr, dhcpmsg->hlen);
|
||||||
sprintf_ipaddr(&requested_ip, ipbuf);
|
sprintf_ipaddr(&requested_ip, ipbuf);
|
||||||
printf("DHCP lease addr %s assigned to MAC %02x:%02x:%02x:%02x:%02x:%02x\r\n", ipbuf, requested_lease->hwaddr[0],
|
debug("DHCP lease addr %s assigned to MAC %02x:%02x:%02x:%02x:%02x:%02x", ipbuf, requested_lease->hwaddr[0],
|
||||||
requested_lease->hwaddr[1], requested_lease->hwaddr[2], requested_lease->hwaddr[3], requested_lease->hwaddr[4],
|
requested_lease->hwaddr[1], requested_lease->hwaddr[2], requested_lease->hwaddr[3], requested_lease->hwaddr[4],
|
||||||
requested_lease->hwaddr[5]);
|
requested_lease->hwaddr[5]);
|
||||||
requested_lease->expires = DHCPSERVER_LEASE_TIME * configTICK_RATE_HZ;
|
requested_lease->expires = DHCPSERVER_LEASE_TIME * configTICK_RATE_HZ;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue