diff --git a/changelog.md b/changelog.md index e3b653dc6..999a703ea 100644 --- a/changelog.md +++ b/changelog.md @@ -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) - 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 diff --git a/src/SFML/Network/IpAddress.cpp b/src/SFML/Network/IpAddress.cpp index 392b7d16c..4976c1cc2 100644 --- a/src/SFML/Network/IpAddress.cpp +++ b/src/SFML/Network/IpAddress.cpp @@ -93,13 +93,13 @@ std::optional IpAddress::resolve(std::string_view address) //////////////////////////////////////////////////////////// IpAddress::IpAddress(std::uint8_t byte0, std::uint8_t byte1, std::uint8_t byte2, std::uint8_t byte3) : -m_address(htonl(static_cast((byte0 << 24) | (byte1 << 16) | (byte2 << 8) | byte3))) +m_address(static_cast((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 { in_addr address{}; - address.s_addr = m_address; + address.s_addr = htonl(m_address); return inet_ntoa(address); } @@ -117,7 +117,7 @@ std::string IpAddress::toString() const //////////////////////////////////////////////////////////// std::uint32_t IpAddress::toInteger() const { - return ntohl(m_address); + return m_address; } diff --git a/test/Network/IpAddress.test.cpp b/test/Network/IpAddress.test.cpp index 3400ae32a..d62b72b75 100644 --- a/test/Network/IpAddress.test.cpp +++ b/test/Network/IpAddress.test.cpp @@ -120,9 +120,9 @@ TEST_CASE("[Network] sf::IpAddress") { CHECK(sf::IpAddress(1) < sf::IpAddress(2)); 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(0, 0, 1, 0)); - CHECK(sf::IpAddress(0, 0, 1, 0) < sf::IpAddress(0, 0, 0, 1)); + CHECK(sf::IpAddress(0, 1, 0, 0) < sf::IpAddress(1, 0, 0, 0)); + CHECK(sf::IpAddress(0, 0, 1, 0) < sf::IpAddress(0, 1, 0, 0)); + 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)); } @@ -130,9 +130,9 @@ TEST_CASE("[Network] sf::IpAddress") { CHECK(sf::IpAddress(2) > sf::IpAddress(1)); 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(0, 0, 1, 0) > sf::IpAddress(0, 1, 0, 0)); - CHECK(sf::IpAddress(0, 0, 0, 1) > sf::IpAddress(0, 0, 1, 0)); + CHECK(sf::IpAddress(1, 0, 0, 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, 1, 0) > 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(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(0, 0, 1, 0)); - CHECK(sf::IpAddress(0, 0, 1, 0) <= sf::IpAddress(0, 0, 0, 1)); + CHECK(sf::IpAddress(0, 1, 0, 0) <= sf::IpAddress(1, 0, 0, 0)); + CHECK(sf::IpAddress(0, 0, 1, 0) <= sf::IpAddress(0, 1, 0, 0)); + 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(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(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(0, 0, 1, 0) >= sf::IpAddress(0, 1, 0, 0)); - CHECK(sf::IpAddress(0, 0, 0, 1) >= sf::IpAddress(0, 0, 1, 0)); + CHECK(sf::IpAddress(1, 0, 0, 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, 1, 0) >= 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));