Store IpAddress::m_address in host byte order

This changes the ordering of `IpAddress` objects to be lexographical
which is more intuitive, and it fixes unit test failures on big-endian
systems.
This commit is contained in:
James Cowgill 2024-12-07 17:17:39 +00:00 committed by Lukas Dürrenberger
parent 34ec2795a1
commit ccda2659d4
3 changed files with 17 additions and 16 deletions

View File

@ -143,6 +143,7 @@ For a closer look at breaking changes and how to migrate from SFML 2, check out
- Removed invalid internal state from `sf::IpAddress` (#2145) - Removed invalid internal state from `sf::IpAddress` (#2145)
- Fixed sockets not closing before being moved into (#2758) - Fixed sockets not closing before being moved into (#2758)
- Fixed how `sf::IpAddress`'s internal representation is stored on big endian systems (#3339)
## SFML 2.6.2 ## SFML 2.6.2

View File

@ -93,13 +93,13 @@ std::optional<IpAddress> IpAddress::resolve(std::string_view address)
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
IpAddress::IpAddress(std::uint8_t byte0, std::uint8_t byte1, std::uint8_t byte2, std::uint8_t byte3) : IpAddress::IpAddress(std::uint8_t byte0, std::uint8_t byte1, std::uint8_t byte2, std::uint8_t byte3) :
m_address(htonl(static_cast<std::uint32_t>((byte0 << 24) | (byte1 << 16) | (byte2 << 8) | byte3))) m_address(static_cast<std::uint32_t>((byte0 << 24) | (byte1 << 16) | (byte2 << 8) | byte3))
{ {
} }
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
IpAddress::IpAddress(std::uint32_t address) : m_address(htonl(address)) IpAddress::IpAddress(std::uint32_t address) : m_address(address)
{ {
} }
@ -108,7 +108,7 @@ IpAddress::IpAddress(std::uint32_t address) : m_address(htonl(address))
std::string IpAddress::toString() const std::string IpAddress::toString() const
{ {
in_addr address{}; in_addr address{};
address.s_addr = m_address; address.s_addr = htonl(m_address);
return inet_ntoa(address); return inet_ntoa(address);
} }
@ -117,7 +117,7 @@ std::string IpAddress::toString() const
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
std::uint32_t IpAddress::toInteger() const std::uint32_t IpAddress::toInteger() const
{ {
return ntohl(m_address); return m_address;
} }

View File

@ -120,9 +120,9 @@ TEST_CASE("[Network] sf::IpAddress")
{ {
CHECK(sf::IpAddress(1) < sf::IpAddress(2)); CHECK(sf::IpAddress(1) < sf::IpAddress(2));
CHECK(sf::IpAddress(0, 0, 0, 0) < sf::IpAddress(1, 0, 0, 0)); CHECK(sf::IpAddress(0, 0, 0, 0) < sf::IpAddress(1, 0, 0, 0));
CHECK(sf::IpAddress(1, 0, 0, 0) < sf::IpAddress(0, 1, 0, 0)); CHECK(sf::IpAddress(0, 1, 0, 0) < sf::IpAddress(1, 0, 0, 0));
CHECK(sf::IpAddress(0, 1, 0, 0) < sf::IpAddress(0, 0, 1, 0)); CHECK(sf::IpAddress(0, 0, 1, 0) < sf::IpAddress(0, 1, 0, 0));
CHECK(sf::IpAddress(0, 0, 1, 0) < sf::IpAddress(0, 0, 0, 1)); CHECK(sf::IpAddress(0, 0, 0, 1) < sf::IpAddress(0, 0, 1, 0));
CHECK(sf::IpAddress(0, 0, 0, 1) < sf::IpAddress(1, 0, 0, 1)); CHECK(sf::IpAddress(0, 0, 0, 1) < sf::IpAddress(1, 0, 0, 1));
} }
@ -130,9 +130,9 @@ TEST_CASE("[Network] sf::IpAddress")
{ {
CHECK(sf::IpAddress(2) > sf::IpAddress(1)); CHECK(sf::IpAddress(2) > sf::IpAddress(1));
CHECK(sf::IpAddress(1, 0, 0, 0) > sf::IpAddress(0, 0, 0, 0)); CHECK(sf::IpAddress(1, 0, 0, 0) > sf::IpAddress(0, 0, 0, 0));
CHECK(sf::IpAddress(0, 1, 0, 0) > sf::IpAddress(1, 0, 0, 0)); CHECK(sf::IpAddress(1, 0, 0, 0) > sf::IpAddress(0, 1, 0, 0));
CHECK(sf::IpAddress(0, 0, 1, 0) > sf::IpAddress(0, 1, 0, 0)); CHECK(sf::IpAddress(0, 1, 0, 0) > sf::IpAddress(0, 0, 1, 0));
CHECK(sf::IpAddress(0, 0, 0, 1) > sf::IpAddress(0, 0, 1, 0)); CHECK(sf::IpAddress(0, 0, 1, 0) > sf::IpAddress(0, 0, 0, 1));
CHECK(sf::IpAddress(1, 0, 0, 1) > sf::IpAddress(0, 0, 0, 1)); CHECK(sf::IpAddress(1, 0, 0, 1) > sf::IpAddress(0, 0, 0, 1));
} }
@ -140,9 +140,9 @@ TEST_CASE("[Network] sf::IpAddress")
{ {
CHECK(sf::IpAddress(1) <= sf::IpAddress(2)); CHECK(sf::IpAddress(1) <= sf::IpAddress(2));
CHECK(sf::IpAddress(0, 0, 0, 0) <= sf::IpAddress(1, 0, 0, 0)); CHECK(sf::IpAddress(0, 0, 0, 0) <= sf::IpAddress(1, 0, 0, 0));
CHECK(sf::IpAddress(1, 0, 0, 0) <= sf::IpAddress(0, 1, 0, 0)); CHECK(sf::IpAddress(0, 1, 0, 0) <= sf::IpAddress(1, 0, 0, 0));
CHECK(sf::IpAddress(0, 1, 0, 0) <= sf::IpAddress(0, 0, 1, 0)); CHECK(sf::IpAddress(0, 0, 1, 0) <= sf::IpAddress(0, 1, 0, 0));
CHECK(sf::IpAddress(0, 0, 1, 0) <= sf::IpAddress(0, 0, 0, 1)); CHECK(sf::IpAddress(0, 0, 0, 1) <= sf::IpAddress(0, 0, 1, 0));
CHECK(sf::IpAddress(0, 0, 0, 1) <= sf::IpAddress(1, 0, 0, 1)); CHECK(sf::IpAddress(0, 0, 0, 1) <= sf::IpAddress(1, 0, 0, 1));
CHECK(sf::IpAddress(0xC6, 0x33, 0x64, 0x7B) <= sf::IpAddress(0xC633647B)); CHECK(sf::IpAddress(0xC6, 0x33, 0x64, 0x7B) <= sf::IpAddress(0xC633647B));
@ -153,9 +153,9 @@ TEST_CASE("[Network] sf::IpAddress")
{ {
CHECK(sf::IpAddress(2) >= sf::IpAddress(1)); CHECK(sf::IpAddress(2) >= sf::IpAddress(1));
CHECK(sf::IpAddress(1, 0, 0, 0) >= sf::IpAddress(0, 0, 0, 0)); CHECK(sf::IpAddress(1, 0, 0, 0) >= sf::IpAddress(0, 0, 0, 0));
CHECK(sf::IpAddress(0, 1, 0, 0) >= sf::IpAddress(1, 0, 0, 0)); CHECK(sf::IpAddress(1, 0, 0, 0) >= sf::IpAddress(0, 1, 0, 0));
CHECK(sf::IpAddress(0, 0, 1, 0) >= sf::IpAddress(0, 1, 0, 0)); CHECK(sf::IpAddress(0, 1, 0, 0) >= sf::IpAddress(0, 0, 1, 0));
CHECK(sf::IpAddress(0, 0, 0, 1) >= sf::IpAddress(0, 0, 1, 0)); CHECK(sf::IpAddress(0, 0, 1, 0) >= sf::IpAddress(0, 0, 0, 1));
CHECK(sf::IpAddress(1, 0, 0, 1) >= sf::IpAddress(0, 0, 0, 1)); CHECK(sf::IpAddress(1, 0, 0, 1) >= sf::IpAddress(0, 0, 0, 1));
CHECK(sf::IpAddress(0xC6, 0x33, 0x64, 0x7B) >= sf::IpAddress(0xC633647B)); CHECK(sf::IpAddress(0xC6, 0x33, 0x64, 0x7B) >= sf::IpAddress(0xC633647B));