Skip to content

Commit

Permalink
Correctly convert remote port bytecode to uint16 port number. (#321)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
enzobet authored Oct 22, 2021
1 parent 9bbd1f1 commit 3f1fc69
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
13 changes: 13 additions & 0 deletions ixwebsocket/IXNetSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -278,4 +278,17 @@ namespace ix
#endif
}

// Convert network bytes to host bytes. Copied from the ASIO library
unsigned short network_to_host_short(unsigned short value)
{
#if defined(_WIN32)
unsigned char* value_p = reinterpret_cast<unsigned char*>(&value);
unsigned short result = (static_cast<unsigned short>(value_p[0]) << 8)
| static_cast<unsigned short>(value_p[1]);
return result;
#else // defined(_WIN32)
return ntohs(value);
#endif // defined(_WIN32)
}

} // namespace ix
2 changes: 2 additions & 0 deletions ixwebsocket/IXNetSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,6 @@ namespace ix

const char* inet_ntop(int af, const void* src, char* dst, socklen_t size);
int inet_pton(int af, const char* src, void* dst);

unsigned short network_to_host_short(unsigned short value);
} // namespace ix
4 changes: 2 additions & 2 deletions ixwebsocket/IXSocketServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ namespace ix
continue;
}

remotePort = client.sin_port;
remotePort = ix::network_to_host_short(client.sin_port);
remoteIp = remoteIp4;
}
else // AF_INET6
Expand All @@ -371,7 +371,7 @@ namespace ix
continue;
}

remotePort = client.sin_port;
remotePort = ix::network_to_host_short(client.sin_port);
remoteIp = remoteIp6;
}

Expand Down

0 comments on commit 3f1fc69

Please sign in to comment.