mirror of
https://github.com/espressif/esp-idf.git
synced 2024-10-05 20:47:46 -04:00
dhcp server:bugfix softap excedes the range of subnet
This commit is contained in:
parent
f3012849e8
commit
1ae1723d23
@ -63,6 +63,42 @@
|
||||
#define DHCPS_DEBUG 0
|
||||
#define DHCPS_LOG printf
|
||||
|
||||
#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.
|
||||
*/
|
||||
#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) \
|
||||
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)
|
||||
|
||||
#define MAX_STATION_NUM CONFIG_LWIP_DHCPS_MAX_STATION_NUM
|
||||
|
||||
#define DHCPS_STATE_OFFER 1
|
||||
@ -1141,23 +1177,25 @@ static void handle_dhcp(void *arg,
|
||||
*******************************************************************************/
|
||||
static void dhcps_poll_set(dhcps_t *dhcps, u32_t ip)
|
||||
{
|
||||
u32_t softap_ip = 0, local_ip = 0;
|
||||
u32_t server_ip = 0, local_ip = 0;
|
||||
u32_t start_ip = 0;
|
||||
u32_t end_ip = 0;
|
||||
u32_t temp_local_ip = 0;
|
||||
u32_t host_num = 0;
|
||||
dhcps_lease_t *dhcps_poll = &dhcps->dhcps_poll;
|
||||
if (dhcps_poll->enable == true) {
|
||||
softap_ip = htonl(ip);
|
||||
server_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)) {
|
||||
if ((start_ip <= server_ip) && (server_ip <= end_ip)) {
|
||||
dhcps_poll->enable = false;
|
||||
} else {
|
||||
/*config ip information must be in the same segment as the local ip*/
|
||||
softap_ip >>= 8;
|
||||
server_ip >>= 8;
|
||||
|
||||
if (((start_ip >> 8 != softap_ip) || (end_ip >> 8 != softap_ip))
|
||||
if (((start_ip >> 8 != server_ip) || (end_ip >> 8 != server_ip))
|
||||
|| (end_ip - start_ip > DHCPS_MAX_LEASE)) {
|
||||
dhcps_poll->enable = false;
|
||||
}
|
||||
@ -1165,19 +1203,24 @@ static void dhcps_poll_set(dhcps_t *dhcps, u32_t ip)
|
||||
}
|
||||
|
||||
if (dhcps_poll->enable == false) {
|
||||
local_ip = softap_ip = htonl(ip);
|
||||
softap_ip &= 0xFFFFFF00;
|
||||
local_ip &= 0xFF;
|
||||
local_ip = server_ip = htonl(ip);
|
||||
server_ip &= 0xFFFFFF00;
|
||||
temp_local_ip = local_ip &= 0xFF;
|
||||
|
||||
if (local_ip >= 0x80) {
|
||||
local_ip -= DHCPS_MAX_LEASE;
|
||||
temp_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);
|
||||
host_num = IP_CLASS_HOST_NUM(htonl(dhcps->dhcps_mask.addr));
|
||||
if (host_num > DHCPS_MAX_LEASE) {
|
||||
host_num = DHCPS_MAX_LEASE;
|
||||
}
|
||||
dhcps_poll->start_ip.addr = server_ip | local_ip;
|
||||
dhcps_poll->end_ip.addr = server_ip | (temp_local_ip + host_num - 1);
|
||||
dhcps_poll->start_ip.addr = htonl(dhcps_poll->start_ip.addr);
|
||||
dhcps_poll->end_ip.addr = htonl(dhcps_poll->end_ip.addr);
|
||||
}
|
||||
@ -1229,6 +1272,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));
|
||||
dhcps_poll_set(dhcps, dhcps->server_address.addr);
|
||||
|
||||
dhcps->client_address_plus.addr = dhcps->dhcps_poll.start_ip.addr;
|
||||
|
@ -20,6 +20,7 @@
|
||||
#include "lwip/sockets.h"
|
||||
#include "ping/ping_sock.h"
|
||||
#include "dhcpserver/dhcpserver.h"
|
||||
#include "dhcpserver/dhcpserver_options.h"
|
||||
#include "esp_sntp.h"
|
||||
|
||||
#define ETH_PING_END_BIT BIT(1)
|
||||
@ -144,8 +145,9 @@ TEST(lwip, dhcp_server_init_deinit)
|
||||
TEST(lwip, dhcp_server_start_stop_localhost)
|
||||
{
|
||||
test_case_uses_tcpip();
|
||||
dhcps_t *dhcps = dhcps_new();
|
||||
struct netif *netif;
|
||||
dhcps_t *dhcps;
|
||||
ip4_addr_t netmask;
|
||||
|
||||
NETIF_FOREACH(netif) {
|
||||
if (netif->name[0] == 'l' && netif->name[1] == 'o') {
|
||||
@ -154,8 +156,39 @@ TEST(lwip, dhcp_server_start_stop_localhost)
|
||||
}
|
||||
TEST_ASSERT_NOT_NULL(netif);
|
||||
|
||||
ip4_addr_t ip = { .addr = 0x7f0001 };
|
||||
TEST_ASSERT(dhcps_start(dhcps, netif, ip) == ERR_OK);
|
||||
//Class A
|
||||
dhcps = dhcps_new();
|
||||
IP4_ADDR(&netmask, 255,0,0,0);
|
||||
dhcps_set_option_info(dhcps, SUBNET_MASK, (void*)&netmask, sizeof(netmask));
|
||||
ip4_addr_t a_ip = { .addr = 0x7f0001 };
|
||||
IP4_ADDR(&netmask, 255,0,0,0);
|
||||
TEST_ASSERT(dhcps_start(dhcps, netif, a_ip) == ERR_OK);
|
||||
TEST_ASSERT(dhcps_stop(dhcps, netif) == ERR_OK);
|
||||
dhcps_delete(dhcps);
|
||||
|
||||
//Class B
|
||||
dhcps = dhcps_new();
|
||||
IP4_ADDR(&netmask, 255,255,0,0);
|
||||
dhcps_set_option_info(dhcps, SUBNET_MASK, (void*)&netmask, sizeof(netmask));
|
||||
ip4_addr_t b_ip = { .addr = 0x1000080 };
|
||||
TEST_ASSERT(dhcps_start(dhcps, netif, b_ip) == ERR_OK);
|
||||
TEST_ASSERT(dhcps_stop(dhcps, netif) == ERR_OK);
|
||||
dhcps_delete(dhcps);
|
||||
|
||||
//Class C
|
||||
dhcps = dhcps_new();
|
||||
IP4_ADDR(&netmask, 255,255,255,0);
|
||||
dhcps_set_option_info(dhcps, SUBNET_MASK, (void*)&netmask, sizeof(netmask));
|
||||
ip4_addr_t c_ip = { .addr = 0x101A8C0 };
|
||||
TEST_ASSERT(dhcps_start(dhcps, netif, c_ip) == ERR_OK);
|
||||
TEST_ASSERT(dhcps_stop(dhcps, netif) == ERR_OK);
|
||||
dhcps_delete(dhcps);
|
||||
|
||||
//Class A Subnet C
|
||||
dhcps = dhcps_new();
|
||||
IP4_ADDR(&netmask, 255,255,255,0);
|
||||
dhcps_set_option_info(dhcps, SUBNET_MASK, (void*)&netmask, sizeof(netmask));
|
||||
TEST_ASSERT(dhcps_start(dhcps, netif, a_ip) == ERR_ARG);
|
||||
TEST_ASSERT(dhcps_stop(dhcps, netif) == ERR_OK);
|
||||
dhcps_delete(dhcps);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user