1
0
mirror of https://github.com/OpenRCT2/OpenRCT2 synced 2026-01-18 20:43:04 +01:00

Merge pull request #4607 from wolfreak99/ip_logging

Store IP address in TcpClient
This commit is contained in:
Ted John
2016-10-13 21:52:02 +01:00
committed by GitHub
2 changed files with 24 additions and 1 deletions

View File

@@ -87,6 +87,8 @@ private:
uint16 _listeningPort = 0;
SOCKET _socket = INVALID_SOCKET;
std::string _hostName;
SDL_mutex * _connectMutex = nullptr;
std::string _error;
@@ -182,9 +184,11 @@ public:
{
throw Exception("Socket not listening.");
}
struct sockaddr_storage client_addr;
socklen_t client_len = sizeof(struct sockaddr_storage);
ITcpSocket * tcpSocket = nullptr;
SOCKET socket = accept(_socket, nullptr, nullptr);
SOCKET socket = accept(_socket, (struct sockaddr *)&client_addr, &client_len);
if (socket == INVALID_SOCKET)
{
if (LAST_SOCKET_ERROR() != EWOULDBLOCK)
@@ -201,8 +205,21 @@ public:
}
else
{
char hostName[NI_MAXHOST];
int rc = getnameinfo(
(struct sockaddr *)&client_addr,
client_len,
hostName,
sizeof(hostName),
nullptr,
0,
NI_NUMERICHOST | NI_NUMERICSERV);
SetTCPNoDelay(socket, true);
tcpSocket = new TcpSocket(socket);
if (rc == 0)
{
_hostName = std::string(hostName);
}
}
}
return tcpSocket;
@@ -403,6 +420,11 @@ public:
SDL_UnlockMutex(_connectMutex);
}
const char * GetHostName() const override
{
return _hostName.empty() ? nullptr : _hostName.c_str();
}
private:
TcpSocket(SOCKET socket)
{

View File

@@ -45,6 +45,7 @@ public:
virtual SOCKET_STATUS GetStatus() abstract;
virtual const char * GetError() abstract;
virtual const char * GetHostName() const abstract;
virtual void Listen(uint16 port) abstract;
virtual void Listen(const char * address, uint16 port) abstract;