@@ -82,7 +82,7 @@ esp_err_t set_esp_interface_hostname(esp_interface_t interface, const char * hos
82
82
return ESP_FAIL;
83
83
}
84
84
85
- esp_err_t set_esp_interface_ip (esp_interface_t interface, IPAddress local_ip=IPAddress(), IPAddress gateway=IPAddress(), IPAddress subnet=IPAddress()){
85
+ esp_err_t set_esp_interface_ip (esp_interface_t interface, IPAddress local_ip=IPAddress(), IPAddress gateway=IPAddress(), IPAddress subnet=IPAddress(), IPAddress dhcp_lease_start=INADDR_NONE ){
86
86
esp_netif_t *esp_netif = esp_netifs[interface];
87
87
esp_netif_dhcp_status_t status = ESP_NETIF_DHCP_INIT;
88
88
esp_netif_ip_info_t info;
@@ -138,19 +138,49 @@ esp_err_t set_esp_interface_ip(esp_interface_t interface, IPAddress local_ip=IPA
138
138
139
139
dhcps_lease_t lease;
140
140
lease.enable = true ;
141
- uint32_t dhcp_ipaddr = static_cast <uint32_t >(local_ip);
142
- // prevents DHCP lease range to overflow subnet/24 range
143
- // there will be 11 addresses for DHCP to lease
144
- uint8_t leaseStart = (uint8_t )(~subnet[3 ] - 12 );
145
- if ((local_ip[3 ]) < leaseStart) {
146
- lease.start_ip .addr = dhcp_ipaddr + (1 << 24 );
147
- lease.end_ip .addr = dhcp_ipaddr + (11 << 24 );
148
- } else {
149
- // make range stay in the begining of the netmask range
150
- dhcp_ipaddr = (dhcp_ipaddr & 0x00FFFFFF );
151
- lease.start_ip .addr = dhcp_ipaddr + (1 << 24 );
152
- lease.end_ip .addr = dhcp_ipaddr + (11 << 24 );
141
+ uint8_t CIDR = WiFiGenericClass::calculateSubnetCIDR (subnet);
142
+ // netmask must have room for at least 12 IP addresses (AP + GW + 10 DHCP Leasing addresses)
143
+ // netmask also must be limited to the last 8 bits of IPv4, otherwise this function won't work
144
+ if (CIDR > 28 || CIDR < 24 ) {
145
+ log_e (" Bad netmask. It must be from /24 to /28 (255.255.255. 0<->240)" );
146
+ return ESP_FAIL; // ESP_FAIL if initializing failed
147
+ }
148
+ uint32_t netmask = static_cast <uint32_t >(subnet);
149
+ uint32_t ap_ipaddr = static_cast <uint32_t >(local_ip);
150
+ uint32_t dhcp_ipaddr = static_cast <uint32_t >(dhcp_lease_start);
151
+ dhcp_ipaddr = dhcp_ipaddr == 0 ? ap_ipaddr + (1 << 24 ) : dhcp_ipaddr;
152
+ uint32_t leaseStartMax = ~netmask - (10 << 24 );
153
+ // there will be 10 addresses for DHCP to lease
154
+ lease.start_ip .addr = dhcp_ipaddr;
155
+ lease.end_ip .addr = lease.start_ip .addr + (10 << 24 );
156
+ // prevents DHCP lease range to overflow subnet range
157
+ if ((dhcp_ipaddr & ~netmask) >= leaseStartMax) {
158
+ // make first DHCP lease addr stay in the begining of the netmask range
159
+ lease.start_ip .addr = (dhcp_ipaddr & netmask) + (1 << 24 );
160
+ lease.end_ip .addr = lease.start_ip .addr + (10 << 24 );
161
+ log_w (" Changing DHCP leasing start address to %s" , IPAddress (lease.start_ip .addr ).toString ().c_str ());
162
+ }
163
+ // Check if local_ip is in the same subnet as the dhcp leasing range initial address
164
+ if ((ap_ipaddr & netmask) != (dhcp_ipaddr & netmask)) {
165
+ log_e (" The AP IP address (%s) and the DHCP start address (%s) must be in the same subnetwork" ,
166
+ local_ip.toString ().c_str (), IPAddress (dhcp_ipaddr).toString ().c_str ());
167
+ return ESP_FAIL; // ESP_FAIL if initializing failed
168
+ }
169
+ // Check if local_ip is within DHCP range
170
+ if (ap_ipaddr >= lease.start_ip .addr && ap_ipaddr <= lease.end_ip .addr ) {
171
+ log_e (" The AP IP address (%s) can't be within the DHCP range (%s -- %s)" ,
172
+ local_ip.toString ().c_str (), IPAddress (lease.start_ip .addr ).toString ().c_str (), IPAddress (lease.end_ip .addr ).toString ().c_str ());
173
+ return ESP_FAIL; // ESP_FAIL if initializing failed
174
+ }
175
+ // Check if gateway is within DHCP range
176
+ uint32_t gw_ipaddr = static_cast <uint32_t >(gateway);
177
+ bool gw_in_same_subnet = (gw_ipaddr & netmask) == (ap_ipaddr & netmask);
178
+ if (gw_in_same_subnet && gw_ipaddr >= lease.start_ip .addr && gw_ipaddr <= lease.end_ip .addr ) {
179
+ log_e (" The GatewayP address (%s) can't be within the DHCP range (%s -- %s)" ,
180
+ gateway.toString ().c_str (), IPAddress (lease.start_ip .addr ).toString ().c_str (), IPAddress (lease.end_ip .addr ).toString ().c_str ());
181
+ return ESP_FAIL; // ESP_FAIL if initializing failed
153
182
}
183
+ log_v (" SoftAP: %s | Gateway: %s | Netmask: %s" , local_ip.toString ().c_str (),gateway.toString ().c_str (),subnet.toString ().c_str ());
154
184
log_v (" DHCP Server Range: %s to %s" , IPAddress (lease.start_ip .addr ).toString ().c_str (), IPAddress (lease.end_ip .addr ).toString ().c_str ());
155
185
err = tcpip_adapter_dhcps_option (
156
186
(tcpip_adapter_dhcp_option_mode_t )TCPIP_ADAPTER_OP_SET,
0 commit comments