33static void zeroUnusedBytes (uint8* address)
noexcept
35 for (
int i = 4; i < 16; ++i)
41 for (
int i = 0; i < 16; ++i)
47 for (
int i = 0; i < (isIPv6 ? 16 : 4); ++i)
48 address[i] =
bytes[i];
51 zeroUnusedBytes (address);
56 IPAddressByteUnion temp;
58 for (
int i = 0; i < 8; ++i)
60 temp.combined = bytes[i];
62 address[i * 2] = temp.split[0];
63 address[i * 2 + 1] = temp.split[1];
69 address[0] =
a0; address[1] =
a1;
70 address[2] =
a2; address[3] =
a3;
72 zeroUnusedBytes (address);
83 for (
int i = 0; i < 8; ++i)
85 temp.combined = array[i];
86 address[i * 2] =
temp.split[0];
87 address[i * 2 + 1] =
temp.split[1];
93 address[0] = (
n >> 24);
94 address[1] = (
n >> 16) & 255;
95 address[2] = (
n >> 8) & 255;
96 address[3] = (
n & 255);
98 zeroUnusedBytes (address);
103 for (
int i = 0; i < 16; ++i)
112 if (
adr.containsAnyOf (
"[]"))
113 return adr.fromFirstOccurrenceOf (
"[",
false,
true).upToLastOccurrenceOf (
"]",
false,
true);
116 return adr.upToLastOccurrenceOf (
":",
false,
true);
131 for (
int i = 0; i < 4; ++i)
144 tokens.removeEmptyStrings();
154 for (
int i = 0; i < 8; ++i)
183 for (
int i = 1; i < 4; ++i)
196 for (
int i = 1; i < 8; ++i)
207bool IPAddress::operator== (
const IPAddress&
other)
const noexcept {
return compare (
other) == 0; }
208bool IPAddress::operator!= (
const IPAddress& other)
const noexcept {
return compare (other) != 0; }
209bool IPAddress::operator< (
const IPAddress& other)
const noexcept {
return compare (other) < 0; }
210bool IPAddress::operator<= (
const IPAddress& other)
const noexcept {
return compare (other) <= 0; }
211bool IPAddress::operator> (
const IPAddress& other)
const noexcept {
return compare (other) > 0; }
212bool IPAddress::operator>= (
const IPAddress& other)
const noexcept {
return compare (other) >= 0; }
216 if (isIPv6 !=
other.isIPv6)
220 if (isIPv4MappedAddress (*
this))
221 return convertIPv4MappedAddressToIPv4 (*this).compare (
other);
226 if (isIPv4MappedAddress (
other))
227 return compare (convertIPv4MappedAddressToIPv4 (
other));
232 for (
int i = 0; i < (isIPv6 ? 16 : 4); ++i)
234 if (address[i] >
other.address[i])
return 1;
235 if (address[i] <
other.address[i])
return -1;
264 if (
t.getHexValue32() == 0x0000)
325 for (
int i = 0; i < 10; ++i)
352 return { 0x0, 0x0, 0x0, 0x0, 0x0, 0xffff,
381 :
UnitTest (
"IPAddress",
"Networking")
385 void runTest()
override
394 beginTest (
"constructors");
401 expect (! local.isNull());
403 IPAddress
ipv4{1, 2, 3, 4};
404 expect (!
ipv4.isNull());
405 expect (!
ipv4.isIPv6);
406 expect (
ipv4.toString() ==
"1.2.3.4");
411 beginTest (
"find all addresses");
423 expect (!
a.isNull());
429 expect (!
a.isNull());
435 beginTest (
"broadcast addresses");
444 expect (!
a.isNull());
456 IPAddress address{1, 2, 3, 4};
461static IPAddressTests iPAddressTests;
Holds a resizable array of primitive or copy-by-value objects.
int size() const noexcept
Returns the current number of elements in the array.
void insert(int indexToInsertAt, ParameterType newElement)
Inserts a new element into the array at a given position.
Array()=default
Creates an empty array.
int indexOf(ParameterType elementToLookFor) const
Finds the index of the first element which matches the value passed in.
void add(const ElementType &newElement)
Appends a new element at the end of the array.
ElementType & getReference(int index) const noexcept
Returns a direct reference to one of the elements in the array, without checking the index passed in.
void set(int indexToChange, ParameterType newValue)
Replaces an element with a new value.
bool contains(ParameterType elementToLookFor) const
Returns true if the array contains at least one occurrence of an object.
Represents an IP address.
static Array< IPAddress > getAllAddresses(bool includeIPv6=false)
Populates a list of all the IP addresses that this machine is using.
static IPAddress getLocalAddress(bool includeIPv6=false)
Returns the first 'real' address for the local machine.
bool isNull() const
Returns whether the address contains the null address (e.g.
static IPAddress local(bool IPv6=false) noexcept
Returns an IPv4 or IPv6 address meaning "localhost", equivalent to 127.0.0.1 (IPv4) or ::1 (IPv6)
static bool isIPv4MappedAddress(const IPAddress &mappedAddress)
Returns true if the given IP address is an IPv4-mapped IPv6 address.
static IPAddress any() noexcept
Returns an IP address meaning "any", equivalent to 0.0.0.0 (IPv4) or ::, (IPv6)
static String getFormattedAddress(const String &unformattedAddress)
Returns a formatted version of the provided IPv6 address conforming to RFC 5952 with leading zeros su...
int compare(const IPAddress &) const noexcept
Compares this IPAddress with another.
String toString() const
Returns a dot- or colon-separated string in the form "1.2.3.4" (IPv4) or "1:2:3:4:5:6:7:8" (IPv6).
static IPAddress broadcast() noexcept
Returns an IPv4 address meaning "broadcast" (255.255.255.255)
static void findAllAddresses(Array< IPAddress > &results, bool includeIPv6=false)
Populates a list of all the IP addresses that this machine is using.
uint8 address[16]
The elements of the IP address.
static IPAddress convertIPv4AddressToIPv4Mapped(const IPAddress &addressToMap)
Converts an IPv4 address to an IPv4-mapped IPv6 address.
static IPAddress convertIPv4MappedAddressToIPv4(const IPAddress &mappedAddress)
Converts an IPv4-mapped IPv6 address to an IPv4 address.
IPAddress() noexcept
Creates a null address - 0.0.0.0 (IPv4) or ::, (IPv6)
static IPAddress getInterfaceBroadcastAddress(const IPAddress &interfaceAddress)
If the IPAdress is the address of an interface on the machine, returns the associated broadcast addre...
static StringArray fromTokens(StringRef stringToTokenise, bool preserveQuotedStrings)
Returns an array containing the tokens in a given string.
static String repeatedString(StringRef stringToRepeat, int numberOfTimesToRepeat)
Creates a string which is a version of a string repeated and joined together.
static String toHexString(IntegerType number)
Returns a string representing this numeric value in hexadecimal.
This is a base class for classes that perform a unit test.
Parses a character string, to read a hexadecimal value.
Union used to split a 16-bit unsigned integer into 2 8-bit unsigned integers or vice-versa.