I just made a mistake which others may make at some point. It was pretty silly but here goes:
IPAddress ip;
// -- other code --
ip = "255.0.0.0";
// Above will use the IPAddress(bool isIP6) constructor
// and will make and empty IPv6 address.
// What I meant to do
ip = IPAddress(String("255.0.0.0"));
Maybe something could be done to make this mistake harder to make in future? Explicit constructor?
This also works strangely:
IPAddress ip("255.255.255.0");
// Makes empty IPv6
I am using the new class quite extensively (to replace our internal ipv4 only equivalent). I now have a collection of util functions, of which maybe some could go upstream. An obvious choice would be extending == to cover > and <. Using something along the lines of the below perhaps?
namespace
{
int compare (const IPAddress& ip1, const IPAddress& ip2)
{
// Need to note in docs that comparing IPv4 address with IPv6 address
// may not give altogether sensible results.
for (int i = 0; i < (ip1.isIPv6 ? 16 : 4); i++)
{
if (ip1.address[i] > ip2.address[i]) return 1;
if (ip1.address[i] < ip2.address[i]) return -1;
}
return 0;
}
}
namespace juce
{
bool operator> (const IPAddress& ip1, const IPAddress& ip2) { return compare (ip1, ip2) > 0; }
bool operator< (const IPAddress& ip1, const IPAddress& ip2) { return compare (ip1, ip2) < 0; }
bool operator>= (const IPAddress& ip1, const IPAddress& ip2) { return compare (ip1, ip2) >= 0; }
bool operator<= (const IPAddress& ip1, const IPAddress& ip2) { return compare (ip1, ip2) <= 0; }
}
I also have stuff to cover prefixes and IPv4 masks and some other bits ‘n’ bobs.
OK, commit 8e463b4 will be on develop shortly and cleans a few things up in the IPAddress class. I’ve added more comparison operators and also support for IPv4-mapped IPv6 addresses with some handy static methods for converting to and from them. This makes the comparison between IPv6 and v4 addresses a bit more sensible as it will check whether the v6 address is a v4-mapped address before comparing them. If it isn’t, then the fallback is to always assume that v6 addresses are “greater” than v4 addresses.