Skip to content

Commit 3f1fc69

Browse files
authored
Correctly convert remote port bytecode to uint16 port number. (#321)
* Correctly convert remote port bytecode to uint16 port number. Copied the network_to_host_short function from the ASIO library to convert the remote port byte code to a uint16 number. * Switched from uint16_t to unsigned short to work in Windows * Updated missed uint16_t to unsigned short
1 parent 9bbd1f1 commit 3f1fc69

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

ixwebsocket/IXNetSystem.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -278,4 +278,17 @@ namespace ix
278278
#endif
279279
}
280280

281+
// Convert network bytes to host bytes. Copied from the ASIO library
282+
unsigned short network_to_host_short(unsigned short value)
283+
{
284+
#if defined(_WIN32)
285+
unsigned char* value_p = reinterpret_cast<unsigned char*>(&value);
286+
unsigned short result = (static_cast<unsigned short>(value_p[0]) << 8)
287+
| static_cast<unsigned short>(value_p[1]);
288+
return result;
289+
#else // defined(_WIN32)
290+
return ntohs(value);
291+
#endif // defined(_WIN32)
292+
}
293+
281294
} // namespace ix

ixwebsocket/IXNetSystem.h

+2
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,6 @@ namespace ix
8181

8282
const char* inet_ntop(int af, const void* src, char* dst, socklen_t size);
8383
int inet_pton(int af, const char* src, void* dst);
84+
85+
unsigned short network_to_host_short(unsigned short value);
8486
} // namespace ix

ixwebsocket/IXSocketServer.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ namespace ix
351351
continue;
352352
}
353353

354-
remotePort = client.sin_port;
354+
remotePort = ix::network_to_host_short(client.sin_port);
355355
remoteIp = remoteIp4;
356356
}
357357
else // AF_INET6
@@ -371,7 +371,7 @@ namespace ix
371371
continue;
372372
}
373373

374-
remotePort = client.sin_port;
374+
remotePort = ix::network_to_host_short(client.sin_port);
375375
remoteIp = remoteIp6;
376376
}
377377

0 commit comments

Comments
 (0)