Skip to content

Commit 91b9fae

Browse files
a7md0me-no-dev
authored andcommitted
Add generic IP utilities (#3038)
* Add generic IP calculations Add: calculateNetworkID(IPAddress ip, IPAddress subnet) => Calculate the network id using the ip and subnet (e.g. 192.168.0.0) calculateBroadcast(IPAddress ip, IPAddress subnet) => Calculate the broadcast ip using the ip and subnet (e.g. 192.168.0.255) calculateSubnetCIDR(IPAddress subnetMask) => Calculate the subnet CIDR using the subnet (e.g. 24) Add: broadcastIP() => Retrieve the network id (e.g. 192.168.0.0) networkID() => Retrieve the broadcast IP (e.g. 192.168.0.255) subnetCIDR() => Retrieve the subnet CIDR (e.g. 24) Add: broadcastIP() => Retrieve the network id (e.g. 192.168.0.0) networkID() => Retrieve the broadcast IP (e.g. 192.168.0.255) subnetCIDR() => Retrieve the subnet CIDR (e.g. 24) Add: softAPBroadcastIP() => Retrieve the network id (e.g. 192.168.0.0) softAPNetwrokID() => Retrieve the broadcast IP (e.g. 192.168.0.255) softAPSubnetCIDR() => Retrieve the subnet CIDR (e.g. 24)
1 parent 2a1fde7 commit 91b9fae

File tree

8 files changed

+168
-0
lines changed

8 files changed

+168
-0
lines changed

libraries/WiFi/src/ETH.cpp

+27
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,33 @@ IPAddress ETHClass::dnsIP(uint8_t dns_no)
197197
return IPAddress(dns_ip.u_addr.ip4.addr);
198198
}
199199

200+
IPAddress ETHClass::broadcastIP()
201+
{
202+
tcpip_adapter_ip_info_t ip;
203+
if(tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_ETH, &ip)){
204+
return IPAddress();
205+
}
206+
return WiFiGenericClass::calculateBroadcast(IPAddress(ip.gw.addr), IPAddress(ip.netmask.addr));
207+
}
208+
209+
IPAddress ETHClass::networkID()
210+
{
211+
tcpip_adapter_ip_info_t ip;
212+
if(tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_ETH, &ip)){
213+
return IPAddress();
214+
}
215+
return WiFiGenericClass::calculateNetworkID(IPAddress(ip.gw.addr), IPAddress(ip.netmask.addr));
216+
}
217+
218+
uint8_t ETHClass::subnetCIDR()
219+
{
220+
tcpip_adapter_ip_info_t ip;
221+
if(tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_ETH, &ip)){
222+
return (uint8_t)0;
223+
}
224+
return WiFiGenericClass::calculateSubnetCIDR(IPAddress(ip.netmask.addr));
225+
}
226+
200227
const char * ETHClass::getHostname()
201228
{
202229
const char * hostname;

libraries/WiFi/src/ETH.h

+4
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ class ETHClass {
7979
IPAddress gatewayIP();
8080
IPAddress dnsIP(uint8_t dns_no = 0);
8181

82+
IPAddress broadcastIP();
83+
IPAddress networkID();
84+
uint8_t subnetCIDR();
85+
8286
uint8_t * macAddress(uint8_t* mac);
8387
String macAddress();
8488

libraries/WiFi/src/WiFiAP.cpp

+41
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,47 @@ IPAddress WiFiAPClass::softAPIP()
235235
return IPAddress(ip.ip.addr);
236236
}
237237

238+
/**
239+
* Get the softAP broadcast IP address.
240+
* @return IPAddress softAP broadcastIP
241+
*/
242+
IPAddress WiFiAPClass::softAPBroadcastIP()
243+
{
244+
tcpip_adapter_ip_info_t ip;
245+
if(WiFiGenericClass::getMode() == WIFI_MODE_NULL){
246+
return IPAddress();
247+
}
248+
tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_AP, &ip);
249+
return WiFiGenericClass::calculateBroadcast(IPAddress(ip.gw.addr), IPAddress(ip.netmask.addr));
250+
}
251+
252+
/**
253+
* Get the softAP network ID.
254+
* @return IPAddress softAP networkID
255+
*/
256+
IPAddress WiFiAPClass::softAPNetworkID()
257+
{
258+
tcpip_adapter_ip_info_t ip;
259+
if(WiFiGenericClass::getMode() == WIFI_MODE_NULL){
260+
return IPAddress();
261+
}
262+
tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_AP, &ip);
263+
return WiFiGenericClass::calculateNetworkID(IPAddress(ip.gw.addr), IPAddress(ip.netmask.addr));
264+
}
265+
266+
/**
267+
* Get the softAP subnet CIDR.
268+
* @return uint8_t softAP subnetCIDR
269+
*/
270+
uint8_t WiFiAPClass::softAPSubnetCIDR()
271+
{
272+
tcpip_adapter_ip_info_t ip;
273+
if(WiFiGenericClass::getMode() == WIFI_MODE_NULL){
274+
return (uint8_t)0;
275+
}
276+
tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_AP, &ip);
277+
return WiFiGenericClass::calculateSubnetCIDR(IPAddress(ip.netmask.addr));
278+
}
238279

239280
/**
240281
* Get the softAP interface MAC address.

libraries/WiFi/src/WiFiAP.h

+4
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ class WiFiAPClass
4545

4646
IPAddress softAPIP();
4747

48+
IPAddress softAPBroadcastIP();
49+
IPAddress softAPNetworkID();
50+
uint8_t softAPSubnetCIDR();
51+
4852
bool softAPenableIpV6();
4953
IPv6Address softAPIPv6();
5054

libraries/WiFi/src/WiFiGeneric.cpp

+42
Original file line numberDiff line numberDiff line change
@@ -656,3 +656,45 @@ int WiFiGenericClass::hostByName(const char* aHostname, IPAddress& aResult)
656656
return (uint32_t)aResult != 0;
657657
}
658658

659+
IPAddress WiFiGenericClass::calculateNetworkID(IPAddress ip, IPAddress subnet) {
660+
IPAddress networkID;
661+
662+
for (size_t i = 0; i < 4; i++)
663+
networkID[i] = subnet[i] & ip[i];
664+
665+
return networkID;
666+
}
667+
668+
IPAddress WiFiGenericClass::calculateBroadcast(IPAddress ip, IPAddress subnet) {
669+
IPAddress broadcastIp;
670+
671+
for (int i = 0; i < 4; i++)
672+
broadcastIp[i] = ~subnet[i] | ip[i];
673+
674+
return broadcastIp;
675+
}
676+
677+
uint8_t WiFiGenericClass::calculateSubnetCIDR(IPAddress subnetMask) {
678+
uint8_t CIDR = 0;
679+
680+
for (uint8_t i = 0; i < 4; i++) {
681+
if (subnetMask[i] == 0x80) // 128
682+
CIDR += 1;
683+
else if (subnetMask[i] == 0xC0) // 192
684+
CIDR += 2;
685+
else if (subnetMask[i] == 0xE0) // 224
686+
CIDR += 3;
687+
else if (subnetMask[i] == 0xF0) // 242
688+
CIDR += 4;
689+
else if (subnetMask[i] == 0xF8) // 248
690+
CIDR += 5;
691+
else if (subnetMask[i] == 0xFC) // 252
692+
CIDR += 6;
693+
else if (subnetMask[i] == 0xFE) // 254
694+
CIDR += 7;
695+
else if (subnetMask[i] == 0xFF) // 255
696+
CIDR += 8;
697+
}
698+
699+
return CIDR;
700+
}

libraries/WiFi/src/WiFiGeneric.h

+4
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,10 @@ class WiFiGenericClass
108108
public:
109109
static int hostByName(const char *aHostname, IPAddress &aResult);
110110

111+
static IPAddress calculateNetworkID(IPAddress ip, IPAddress subnet);
112+
static IPAddress calculateBroadcast(IPAddress ip, IPAddress subnet);
113+
static uint8_t calculateSubnetCIDR(IPAddress subnetMask);
114+
111115
protected:
112116
friend class WiFiSTAClass;
113117
friend class WiFiScanClass;

libraries/WiFi/src/WiFiSTA.cpp

+42
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,48 @@ IPAddress WiFiSTAClass::dnsIP(uint8_t dns_no)
492492
return IPAddress(dns_ip.u_addr.ip4.addr);
493493
}
494494

495+
/**
496+
* Get the broadcast ip address.
497+
* @return IPAddress broadcastIP
498+
*/
499+
IPAddress WiFiSTAClass::broadcastIP()
500+
{
501+
if(WiFiGenericClass::getMode() == WIFI_MODE_NULL){
502+
return IPAddress();
503+
}
504+
tcpip_adapter_ip_info_t ip;
505+
tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_STA, &ip);
506+
return WiFiGenericClass::calculateBroadcast(IPAddress(ip.gw.addr), IPAddress(ip.netmask.addr));
507+
}
508+
509+
/**
510+
* Get the network id.
511+
* @return IPAddress networkID
512+
*/
513+
IPAddress WiFiSTAClass::networkID()
514+
{
515+
if(WiFiGenericClass::getMode() == WIFI_MODE_NULL){
516+
return IPAddress();
517+
}
518+
tcpip_adapter_ip_info_t ip;
519+
tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_STA, &ip);
520+
return WiFiGenericClass::calculateNetworkID(IPAddress(ip.gw.addr), IPAddress(ip.netmask.addr));
521+
}
522+
523+
/**
524+
* Get the subnet CIDR.
525+
* @return uint8_t subnetCIDR
526+
*/
527+
uint8_t WiFiSTAClass::subnetCIDR()
528+
{
529+
if(WiFiGenericClass::getMode() == WIFI_MODE_NULL){
530+
return (uint8_t)0;
531+
}
532+
tcpip_adapter_ip_info_t ip;
533+
tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_STA, &ip);
534+
return WiFiGenericClass::calculateSubnetCIDR(IPAddress(ip.netmask.addr));
535+
}
536+
495537
/**
496538
* Return the current SSID associated with the network
497539
* @return SSID

libraries/WiFi/src/WiFiSTA.h

+4
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ class WiFiSTAClass
6464
IPAddress subnetMask();
6565
IPAddress gatewayIP();
6666
IPAddress dnsIP(uint8_t dns_no = 0);
67+
68+
IPAddress broadcastIP();
69+
IPAddress networkID();
70+
uint8_t subnetCIDR();
6771

6872
bool enableIpV6();
6973
IPv6Address localIPv6();

0 commit comments

Comments
 (0)