Skip to content

Commit ccda265

Browse files
jcowgilleXpl0it3r
authored andcommitted
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.
1 parent 34ec279 commit ccda265

File tree

3 files changed

+17
-16
lines changed

3 files changed

+17
-16
lines changed

changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ For a closer look at breaking changes and how to migrate from SFML 2, check out
143143

144144
- Removed invalid internal state from `sf::IpAddress` (#2145)
145145
- Fixed sockets not closing before being moved into (#2758)
146+
- Fixed how `sf::IpAddress`'s internal representation is stored on big endian systems (#3339)
146147

147148
## SFML 2.6.2
148149

src/SFML/Network/IpAddress.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,13 @@ std::optional<IpAddress> IpAddress::resolve(std::string_view address)
9393

9494
////////////////////////////////////////////////////////////
9595
IpAddress::IpAddress(std::uint8_t byte0, std::uint8_t byte1, std::uint8_t byte2, std::uint8_t byte3) :
96-
m_address(htonl(static_cast<std::uint32_t>((byte0 << 24) | (byte1 << 16) | (byte2 << 8) | byte3)))
96+
m_address(static_cast<std::uint32_t>((byte0 << 24) | (byte1 << 16) | (byte2 << 8) | byte3))
9797
{
9898
}
9999

100100

101101
////////////////////////////////////////////////////////////
102-
IpAddress::IpAddress(std::uint32_t address) : m_address(htonl(address))
102+
IpAddress::IpAddress(std::uint32_t address) : m_address(address)
103103
{
104104
}
105105

@@ -108,7 +108,7 @@ IpAddress::IpAddress(std::uint32_t address) : m_address(htonl(address))
108108
std::string IpAddress::toString() const
109109
{
110110
in_addr address{};
111-
address.s_addr = m_address;
111+
address.s_addr = htonl(m_address);
112112

113113
return inet_ntoa(address);
114114
}
@@ -117,7 +117,7 @@ std::string IpAddress::toString() const
117117
////////////////////////////////////////////////////////////
118118
std::uint32_t IpAddress::toInteger() const
119119
{
120-
return ntohl(m_address);
120+
return m_address;
121121
}
122122

123123

test/Network/IpAddress.test.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -120,29 +120,29 @@ TEST_CASE("[Network] sf::IpAddress")
120120
{
121121
CHECK(sf::IpAddress(1) < sf::IpAddress(2));
122122
CHECK(sf::IpAddress(0, 0, 0, 0) < sf::IpAddress(1, 0, 0, 0));
123-
CHECK(sf::IpAddress(1, 0, 0, 0) < sf::IpAddress(0, 1, 0, 0));
124-
CHECK(sf::IpAddress(0, 1, 0, 0) < sf::IpAddress(0, 0, 1, 0));
125-
CHECK(sf::IpAddress(0, 0, 1, 0) < sf::IpAddress(0, 0, 0, 1));
123+
CHECK(sf::IpAddress(0, 1, 0, 0) < sf::IpAddress(1, 0, 0, 0));
124+
CHECK(sf::IpAddress(0, 0, 1, 0) < sf::IpAddress(0, 1, 0, 0));
125+
CHECK(sf::IpAddress(0, 0, 0, 1) < sf::IpAddress(0, 0, 1, 0));
126126
CHECK(sf::IpAddress(0, 0, 0, 1) < sf::IpAddress(1, 0, 0, 1));
127127
}
128128

129129
SECTION("operator>")
130130
{
131131
CHECK(sf::IpAddress(2) > sf::IpAddress(1));
132132
CHECK(sf::IpAddress(1, 0, 0, 0) > sf::IpAddress(0, 0, 0, 0));
133-
CHECK(sf::IpAddress(0, 1, 0, 0) > sf::IpAddress(1, 0, 0, 0));
134-
CHECK(sf::IpAddress(0, 0, 1, 0) > sf::IpAddress(0, 1, 0, 0));
135-
CHECK(sf::IpAddress(0, 0, 0, 1) > sf::IpAddress(0, 0, 1, 0));
133+
CHECK(sf::IpAddress(1, 0, 0, 0) > sf::IpAddress(0, 1, 0, 0));
134+
CHECK(sf::IpAddress(0, 1, 0, 0) > sf::IpAddress(0, 0, 1, 0));
135+
CHECK(sf::IpAddress(0, 0, 1, 0) > sf::IpAddress(0, 0, 0, 1));
136136
CHECK(sf::IpAddress(1, 0, 0, 1) > sf::IpAddress(0, 0, 0, 1));
137137
}
138138

139139
SECTION("operator<=")
140140
{
141141
CHECK(sf::IpAddress(1) <= sf::IpAddress(2));
142142
CHECK(sf::IpAddress(0, 0, 0, 0) <= sf::IpAddress(1, 0, 0, 0));
143-
CHECK(sf::IpAddress(1, 0, 0, 0) <= sf::IpAddress(0, 1, 0, 0));
144-
CHECK(sf::IpAddress(0, 1, 0, 0) <= sf::IpAddress(0, 0, 1, 0));
145-
CHECK(sf::IpAddress(0, 0, 1, 0) <= sf::IpAddress(0, 0, 0, 1));
143+
CHECK(sf::IpAddress(0, 1, 0, 0) <= sf::IpAddress(1, 0, 0, 0));
144+
CHECK(sf::IpAddress(0, 0, 1, 0) <= sf::IpAddress(0, 1, 0, 0));
145+
CHECK(sf::IpAddress(0, 0, 0, 1) <= sf::IpAddress(0, 0, 1, 0));
146146
CHECK(sf::IpAddress(0, 0, 0, 1) <= sf::IpAddress(1, 0, 0, 1));
147147

148148
CHECK(sf::IpAddress(0xC6, 0x33, 0x64, 0x7B) <= sf::IpAddress(0xC633647B));
@@ -153,9 +153,9 @@ TEST_CASE("[Network] sf::IpAddress")
153153
{
154154
CHECK(sf::IpAddress(2) >= sf::IpAddress(1));
155155
CHECK(sf::IpAddress(1, 0, 0, 0) >= sf::IpAddress(0, 0, 0, 0));
156-
CHECK(sf::IpAddress(0, 1, 0, 0) >= sf::IpAddress(1, 0, 0, 0));
157-
CHECK(sf::IpAddress(0, 0, 1, 0) >= sf::IpAddress(0, 1, 0, 0));
158-
CHECK(sf::IpAddress(0, 0, 0, 1) >= sf::IpAddress(0, 0, 1, 0));
156+
CHECK(sf::IpAddress(1, 0, 0, 0) >= sf::IpAddress(0, 1, 0, 0));
157+
CHECK(sf::IpAddress(0, 1, 0, 0) >= sf::IpAddress(0, 0, 1, 0));
158+
CHECK(sf::IpAddress(0, 0, 1, 0) >= sf::IpAddress(0, 0, 0, 1));
159159
CHECK(sf::IpAddress(1, 0, 0, 1) >= sf::IpAddress(0, 0, 0, 1));
160160

161161
CHECK(sf::IpAddress(0xC6, 0x33, 0x64, 0x7B) >= sf::IpAddress(0xC633647B));

0 commit comments

Comments
 (0)