Fixed bug for dhcp server support CIDR

Closes https://github.com/espressif/esp-idf/issues/10024

Closes https://github.com/espressif/esp-idf/issues/10559
This commit is contained in:
xueyunfei 2023-02-22 10:46:41 +08:00
parent 778aeae99e
commit 4f0732cf05
2 changed files with 6 additions and 28 deletions

View File

@ -65,37 +65,14 @@
#define IS_INVALID_SUBNET_MASK(x) (((x-1) | x) != 0xFFFFFFFF)
/* Notes:
* 1. Class a address range 0.0.0.0~127.255.255.255.
* 2. Class b address range 128.0.0.0~191.255.255.255.
* 3. Class c address range 192.0.0.0~223.255.255.255.
* CIDR eliminates the traditional Class A, Class B and Class C addresses.
*/
#define IS_VALID_CLASSA_SUBNET_MASK(mask) (mask >= 0xFF000000 && mask <= 0xFFFE0000)
#define IS_VALID_CLASSB_SUBNET_MASK(mask) (mask >= 0xFFFF0000 && mask <= 0xFFFFFE00)
#define IS_VALID_CLASSC_SUBNET_MASK(mask) (mask >= 0xFFFFFF00 && mask <= 0xFFFFFFFC)
#define IP_CLASS_HOST_NUM(mask) (0xffffffff & ~mask)
#define DHCP_CHECK_SUBNET_MASK_IP(mask, ip) \
#define DHCP_CHECK_SUBNET_MASK_IP(mask) \
do { \
if (IS_INVALID_SUBNET_MASK(mask)) { \
DHCPS_LOG("dhcps: Illegal subnet mask.\n"); \
return ERR_ARG; \
} else { \
if (IP_CLASSA(ip)) { \
if(!IS_VALID_CLASSA_SUBNET_MASK(mask)) { \
DHCPS_LOG("dhcps: The subnet mask does not match the A address.\n"); \
return ERR_ARG; \
} \
} else if (IP_CLASSB(ip)) { \
if(!IS_VALID_CLASSB_SUBNET_MASK(mask)) { \
DHCPS_LOG("dhcps: The subnet mask does not match the B address.\n"); \
return ERR_ARG; \
} \
} else if (IP_CLASSC(ip)) { \
if(!IS_VALID_CLASSC_SUBNET_MASK(mask)) { \
DHCPS_LOG("dhcps: The subnet mask does not match the C address.\n"); \
return ERR_ARG; \
} \
} \
} \
} while (0)
@ -1272,7 +1249,7 @@ err_t dhcps_start(dhcps_t *dhcps, struct netif *netif, ip4_addr_t ip)
IP4_ADDR(&dhcps->broadcast_dhcps, 255, 255, 255, 255);
dhcps->server_address.addr = ip.addr;
DHCP_CHECK_SUBNET_MASK_IP(htonl(dhcps->dhcps_mask.addr), htonl(dhcps->server_address.addr));
DHCP_CHECK_SUBNET_MASK_IP(htonl(dhcps->dhcps_mask.addr));
dhcps_poll_set(dhcps, dhcps->server_address.addr);
dhcps->client_address_plus.addr = dhcps->dhcps_poll.start_ip.addr;

View File

@ -205,9 +205,10 @@ TEST(lwip, dhcp_server_start_stop_localhost)
// Class C: IP: 192.168.1.1, Mask: 255.255.255.0
dhcps_test_net_classes(0xC0A80101, 0xFFFFFF00, true);
// Class A: IP: 127.0.0.1, with Class C Mask: 255.255.255.0
// Class A: IP: 127.0.0.1, with inaccurate Mask: 255.248.255.0
// expect dhcps_start() to fail
dhcps_test_net_classes(0x7f000001, 0xFFFFFF00, false);
dhcps_test_net_classes(0x7f000001, 0xFFF8FF00, false);
}