forked from espressif/arduino-esp32
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)
This commit is contained in:
committed by
Me No Dev
parent
2a1fde7736
commit
91b9fae111
@ -656,3 +656,45 @@ int WiFiGenericClass::hostByName(const char* aHostname, IPAddress& aResult)
|
||||
return (uint32_t)aResult != 0;
|
||||
}
|
||||
|
||||
IPAddress WiFiGenericClass::calculateNetworkID(IPAddress ip, IPAddress subnet) {
|
||||
IPAddress networkID;
|
||||
|
||||
for (size_t i = 0; i < 4; i++)
|
||||
networkID[i] = subnet[i] & ip[i];
|
||||
|
||||
return networkID;
|
||||
}
|
||||
|
||||
IPAddress WiFiGenericClass::calculateBroadcast(IPAddress ip, IPAddress subnet) {
|
||||
IPAddress broadcastIp;
|
||||
|
||||
for (int i = 0; i < 4; i++)
|
||||
broadcastIp[i] = ~subnet[i] | ip[i];
|
||||
|
||||
return broadcastIp;
|
||||
}
|
||||
|
||||
uint8_t WiFiGenericClass::calculateSubnetCIDR(IPAddress subnetMask) {
|
||||
uint8_t CIDR = 0;
|
||||
|
||||
for (uint8_t i = 0; i < 4; i++) {
|
||||
if (subnetMask[i] == 0x80) // 128
|
||||
CIDR += 1;
|
||||
else if (subnetMask[i] == 0xC0) // 192
|
||||
CIDR += 2;
|
||||
else if (subnetMask[i] == 0xE0) // 224
|
||||
CIDR += 3;
|
||||
else if (subnetMask[i] == 0xF0) // 242
|
||||
CIDR += 4;
|
||||
else if (subnetMask[i] == 0xF8) // 248
|
||||
CIDR += 5;
|
||||
else if (subnetMask[i] == 0xFC) // 252
|
||||
CIDR += 6;
|
||||
else if (subnetMask[i] == 0xFE) // 254
|
||||
CIDR += 7;
|
||||
else if (subnetMask[i] == 0xFF) // 255
|
||||
CIDR += 8;
|
||||
}
|
||||
|
||||
return CIDR;
|
||||
}
|
||||
|
Reference in New Issue
Block a user