Move git root from Client/ to src/ to track all source code: - Client: Game client source (moved to Client/Client/) - Server: Game server source - GameTools: Development tools - CryptoSource: Encryption utilities - database: Database scripts - Script: Game scripts - rylCoder_16.02.2008_src: Legacy coder tools - GMFont, Game: Additional resources 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
285 lines
5.9 KiB
C++
285 lines
5.9 KiB
C++
#include "Socket.h"
|
||
|
||
void Socket::InitWinsock(void)
|
||
{
|
||
WSADATA WsaData;
|
||
WSAStartup(MAKEWORD( 2, 2), &WsaData);
|
||
}
|
||
|
||
void Socket::ReleaseWinsock(void)
|
||
{
|
||
WSACleanup();
|
||
}
|
||
|
||
SOCKADDR_IN Socket::MakeSockAddr(char *IP_In, int Port_In)
|
||
{
|
||
SOCKADDR_IN Address = {0,};
|
||
Address.sin_family = AF_INET;
|
||
Address.sin_addr.s_addr = (NULL == IP_In) ? htonl(INADDR_ANY) : inet_addr(IP_In);
|
||
Address.sin_port = (0 == Port_In) ? 0 : htons(Port_In);
|
||
|
||
return Address;
|
||
}
|
||
|
||
SOCKADDR_IN Socket::MakeSockAddr(IN_ADDR Addr, int Port_In)
|
||
{
|
||
SOCKADDR_IN Address = {0,};
|
||
Address.sin_family = AF_INET;
|
||
Address.sin_addr = Addr;
|
||
Address.sin_port = (0 == Port_In) ? 0 : htons(Port_In);
|
||
|
||
return Address;
|
||
}
|
||
|
||
bool Socket::CreateTCPSocket(SOCKET *Socket_Out, HWND hWnd_In, unsigned int Msg_In)
|
||
{
|
||
BOOL KeepAlive = TRUE;
|
||
|
||
linger Linger;
|
||
Linger.l_onoff = 1;
|
||
Linger.l_linger = 0;
|
||
|
||
if(Socket_Out == NULL)
|
||
return false;
|
||
|
||
if((*Socket_Out = socket(AF_INET, SOCK_STREAM, IPPROTO_IP)) == SOCKET_ERROR)
|
||
return false;
|
||
|
||
if(setsockopt(*Socket_Out, SOL_SOCKET, SO_KEEPALIVE, (char *)&KeepAlive, sizeof(BOOL)) == SOCKET_ERROR)
|
||
return false;
|
||
|
||
if(setsockopt(*Socket_Out, SOL_SOCKET, SO_LINGER, (char *)&Linger, sizeof(linger)) == SOCKET_ERROR)
|
||
return false;
|
||
|
||
if(WSAAsyncSelect(*Socket_Out, hWnd_In, Msg_In, FD_READ | FD_CONNECT | FD_CLOSE) == SOCKET_ERROR)
|
||
return false;
|
||
|
||
return true;
|
||
}
|
||
|
||
bool Socket::CreateUDPSocket(SOCKET *Socket_Out, SOCKADDR_IN Address_In, HWND hWnd_In, unsigned int Msg_In)
|
||
{
|
||
BOOL ReUseAddr = TRUE;
|
||
if(Socket_Out == NULL)
|
||
return false;
|
||
|
||
if((*Socket_Out = socket(AF_INET, SOCK_DGRAM, 0)) == SOCKET_ERROR)
|
||
return false;
|
||
|
||
if(bind(*Socket_Out, (sockaddr *)&Address_In, sizeof(SOCKADDR_IN)) == SOCKET_ERROR)
|
||
return false;
|
||
|
||
if(setsockopt(*Socket_Out, SOL_SOCKET, SO_REUSEADDR, (char *)&ReUseAddr, sizeof(BOOL)) == SOCKET_ERROR)
|
||
return false;
|
||
|
||
if(WSAAsyncSelect(*Socket_Out, hWnd_In, Msg_In, FD_READ) == SOCKET_ERROR)
|
||
return false;
|
||
|
||
return true;
|
||
}
|
||
|
||
bool Socket::DeleteSocket(SOCKET *Socket_In, HWND hWnd_In)
|
||
{
|
||
if(Socket_In == NULL)
|
||
return false;
|
||
|
||
WSAAsyncSelect(*Socket_In, hWnd_In, 0, 0);
|
||
SAFE_CLOSESOCK(*Socket_In);
|
||
|
||
return true;
|
||
}
|
||
|
||
bool Socket::GetNATAddress(SOCKET Socket_In, SOCKADDR_IN *Address_Out, bool AllowVirtual)
|
||
{
|
||
int AddressSize = sizeof(SOCKADDR_IN);
|
||
if(getsockname(Socket_In, (struct sockaddr *)Address_Out, &AddressSize) != SOCKET_ERROR)
|
||
{
|
||
char HostName[256];
|
||
if(!gethostname(HostName, 255))
|
||
{
|
||
PHOSTENT pHost = gethostbyname(HostName);
|
||
if(NULL == pHost)
|
||
{
|
||
return false;
|
||
}
|
||
|
||
IN_ADDR& Addr = Address_Out->sin_addr;
|
||
for(int Count = 0;pHost->h_addr_list[Count]; ++Count)
|
||
{
|
||
memcpy(&(Addr.S_un.S_addr), pHost->h_addr_list[Count], sizeof(IN_ADDR));
|
||
|
||
if(!AllowVirtual)
|
||
{
|
||
// <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> Ȯ<><C8AE>
|
||
// 10.0.0.0 ~ 10.255.255.255
|
||
// 172.16.0.0 ~ 172.31.255.255
|
||
// 192.168.0.0 ~ 192.168.255.255
|
||
if((unsigned char)10 == Addr.S_un.S_un_b.s_b1)
|
||
{
|
||
continue;
|
||
}
|
||
else if((unsigned char)172 == Addr.S_un.S_un_b.s_b1)
|
||
{
|
||
if(Addr.S_un.S_un_b.s_b2 >= (unsigned char)16 && Addr.S_un.S_un_b.s_b2 <= (unsigned char)31)
|
||
{
|
||
continue;
|
||
}
|
||
}
|
||
else if((unsigned char)192 == Addr.S_un.S_un_b.s_b1)
|
||
{
|
||
if((unsigned char)168 == Addr.S_un.S_un_b.s_b2)
|
||
{
|
||
continue;
|
||
}
|
||
}
|
||
}
|
||
|
||
// <20>߸<EFBFBD><DFB8><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||
// 169.X.X.X <20>ڵ<EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20>Ҵ<EFBFBD> ( SyGate )
|
||
// 0.X.X.X
|
||
if((unsigned char)169 == Addr.S_un.S_un_b.s_b1)
|
||
{
|
||
continue;
|
||
}
|
||
else if((unsigned char)0 == Addr.S_un.S_un_b.s_b1)
|
||
{
|
||
continue;
|
||
}
|
||
|
||
return true;
|
||
}
|
||
|
||
if(0 != Count)
|
||
return true;
|
||
}
|
||
|
||
return false;
|
||
}
|
||
|
||
return false;
|
||
}
|
||
|
||
bool Socket::GetHostName(char *Name_Out, int Size)
|
||
{
|
||
if(!gethostname(Name_Out, Size))
|
||
return true;
|
||
|
||
return false;
|
||
}
|
||
|
||
bool Socket::GetHostIP(IN_ADDR &Addr, bool AllowVirtual)
|
||
{
|
||
char HostName[256];
|
||
if(!gethostname(HostName, 255))
|
||
{
|
||
PHOSTENT pHost = gethostbyname(HostName);
|
||
if(NULL == pHost)
|
||
{
|
||
return false;
|
||
}
|
||
|
||
for(int Count = 0;pHost->h_addr_list[Count]; ++Count)
|
||
{
|
||
memcpy(&(Addr.S_un.S_addr), pHost->h_addr_list[Count], sizeof(IN_ADDR));
|
||
|
||
if(!AllowVirtual)
|
||
{
|
||
// <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> Ȯ<><C8AE>
|
||
// 10.0.0.0 ~ 10.255.255.255
|
||
// 172.16.0.0 ~ 172.31.255.255
|
||
// 192.168.0.0 ~ 192.168.255.255
|
||
if((unsigned char)10 == Addr.S_un.S_un_b.s_b1)
|
||
{
|
||
continue;
|
||
}
|
||
else if((unsigned char)172 == Addr.S_un.S_un_b.s_b1)
|
||
{
|
||
if(Addr.S_un.S_un_b.s_b2 >= (unsigned char)16 && Addr.S_un.S_un_b.s_b2 <= (unsigned char)31)
|
||
{
|
||
continue;
|
||
}
|
||
}
|
||
else if((unsigned char)192 == Addr.S_un.S_un_b.s_b1)
|
||
{
|
||
if((unsigned char)168 == Addr.S_un.S_un_b.s_b2)
|
||
{
|
||
continue;
|
||
}
|
||
}
|
||
}
|
||
|
||
// <20>߸<EFBFBD><DFB8><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||
// 169.X.X.X <20>ڵ<EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20>Ҵ<EFBFBD> ( SyGate )
|
||
// 0.X.X.X
|
||
if((unsigned char)169 == Addr.S_un.S_un_b.s_b1)
|
||
{
|
||
continue;
|
||
}
|
||
else if((unsigned char)0 == Addr.S_un.S_un_b.s_b1)
|
||
{
|
||
continue;
|
||
}
|
||
|
||
return true;
|
||
}
|
||
|
||
if(0 != Count)
|
||
return true;
|
||
}
|
||
|
||
return false;
|
||
}
|
||
|
||
bool Socket::GetHostIP(IN_ADDR &Addr, char *Domain)
|
||
{
|
||
PHOSTENT pHost = gethostbyname(Domain);
|
||
if(NULL == pHost)
|
||
{
|
||
return false;
|
||
}
|
||
|
||
memcpy(&(Addr.S_un.S_addr), pHost->h_addr_list[0], sizeof(IN_ADDR));
|
||
|
||
return true;
|
||
}
|
||
|
||
bool Socket::IsWrongIP(IN_ADDR &Addr)
|
||
{
|
||
if((unsigned char)169 == Addr.S_un.S_un_b.s_b1)
|
||
{
|
||
if((unsigned char)254 == Addr.S_un.S_un_b.s_b2)
|
||
{
|
||
return true;
|
||
}
|
||
}
|
||
|
||
if((unsigned char)0 == Addr.S_un.S_un_b.s_b1)
|
||
return true;
|
||
|
||
return false;
|
||
}
|
||
|
||
bool Socket::IsEmptyAddr(IN_ADDR &Addr)
|
||
{
|
||
if((unsigned char)0 == Addr.S_un.S_un_b.s_b1)
|
||
{
|
||
return true;
|
||
}
|
||
|
||
return true;
|
||
}
|
||
|
||
bool Socket::Connect(SOCKET Socket_In, SOCKADDR_IN Address_In, WSABUF *WSABuf_In)
|
||
{
|
||
if(WSAConnect(Socket_In, (sockaddr *)&Address_In, sizeof(SOCKADDR_IN), 0, WSABuf_In, 0, 0) == SOCKET_ERROR)
|
||
{
|
||
DWORD dwError = GetLastError();
|
||
|
||
if(ERROR_IO_PENDING != dwError && WSAEWOULDBLOCK != (int)dwError)
|
||
return false;
|
||
}
|
||
|
||
return true;
|
||
}
|
||
|