2012-01-29 14:28:19 -05:00
|
|
|
|
|
|
|
#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
|
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
#include "cSocket.h"
|
2012-02-07 15:49:52 -05:00
|
|
|
#include "packets/cPacket.h"
|
2011-10-03 14:41:19 -04:00
|
|
|
|
2011-10-21 17:25:29 -04:00
|
|
|
#ifndef _WIN32
|
2012-01-29 14:28:19 -05:00
|
|
|
#include <netdb.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <arpa/inet.h> //inet_ntoa()
|
2011-10-22 20:18:44 -04:00
|
|
|
#else
|
2012-01-29 14:28:19 -05:00
|
|
|
#define socklen_t int
|
2011-10-21 17:25:29 -04:00
|
|
|
#endif
|
|
|
|
|
2012-01-29 14:28:19 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-02-05 10:41:56 -05:00
|
|
|
cSocket::cSocket(xSocket a_Socket)
|
|
|
|
: m_Socket(a_Socket)
|
2011-10-03 14:41:19 -04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-02-01 17:38:03 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
cSocket::~cSocket()
|
|
|
|
{
|
2012-02-08 05:02:46 -05:00
|
|
|
// Do NOT close the socket; this class is an API wrapper, not a RAII!
|
2011-10-03 14:41:19 -04:00
|
|
|
}
|
|
|
|
|
2012-02-01 17:38:03 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
cSocket::operator const cSocket::xSocket() const
|
|
|
|
{
|
|
|
|
return m_Socket;
|
|
|
|
}
|
|
|
|
|
2012-02-01 17:38:03 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
cSocket::xSocket cSocket::GetSocket() const
|
|
|
|
{
|
|
|
|
return m_Socket;
|
|
|
|
}
|
|
|
|
|
2012-02-01 17:38:03 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
|
2012-02-02 11:13:49 -05:00
|
|
|
bool cSocket::IsValid(void) const
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
return (m_Socket != INVALID_SOCKET);
|
|
|
|
#else // _WIN32
|
|
|
|
return (m_Socket >= 0);
|
|
|
|
#endif // else _WIN32
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-10-21 17:25:29 -04:00
|
|
|
void cSocket::CloseSocket()
|
|
|
|
{
|
2012-02-02 11:13:49 -05:00
|
|
|
#ifdef _WIN32
|
|
|
|
|
2011-10-21 17:25:29 -04:00
|
|
|
closesocket(m_Socket);
|
2012-02-02 11:13:49 -05:00
|
|
|
|
|
|
|
#else // _WIN32
|
|
|
|
|
2012-02-05 10:41:56 -05:00
|
|
|
if (shutdown(m_Socket, SHUT_RDWR) != 0)//SD_BOTH);
|
2012-02-02 11:13:49 -05:00
|
|
|
{
|
2012-02-05 10:41:56 -05:00
|
|
|
LOGWARN("Error on shutting down socket (%s)", m_IPString.c_str());
|
2012-02-02 11:13:49 -05:00
|
|
|
}
|
2012-02-05 10:41:56 -05:00
|
|
|
if (close(m_Socket) != 0)
|
2012-02-02 11:13:49 -05:00
|
|
|
{
|
2012-02-05 10:41:56 -05:00
|
|
|
LOGWARN("Error closing socket (%s)", m_IPString.c_str());
|
2012-02-02 11:13:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // else _WIN32
|
|
|
|
|
|
|
|
// Invalidate the socket so that this object can be re-used for another connection
|
|
|
|
m_Socket = INVALID_SOCKET;
|
2011-10-22 20:18:44 -04:00
|
|
|
}
|
|
|
|
|
2012-02-01 17:38:03 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-02-05 11:24:16 -05:00
|
|
|
AString cSocket::GetErrorString( int a_ErrNo )
|
2011-10-22 20:18:44 -04:00
|
|
|
{
|
2012-02-06 08:22:28 -05:00
|
|
|
char buffer[ 1024 ];
|
|
|
|
AString Out;
|
|
|
|
|
2012-02-06 06:53:01 -05:00
|
|
|
#ifdef _WIN32
|
|
|
|
|
2012-02-06 08:22:28 -05:00
|
|
|
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, a_ErrNo, 0, buffer, ARRAYCOUNT(buffer), NULL);
|
|
|
|
Printf(Out, "%d: %s", a_ErrNo, buffer);
|
2012-02-08 05:02:46 -05:00
|
|
|
if (!Out.empty() && (Out[Out.length() - 1] == '\n'))
|
|
|
|
{
|
|
|
|
Out.erase(Out.length() - 2);
|
|
|
|
}
|
2012-02-06 08:22:28 -05:00
|
|
|
return Out;
|
2012-02-06 06:53:01 -05:00
|
|
|
|
|
|
|
#else // _WIN32
|
2012-02-06 08:22:28 -05:00
|
|
|
|
|
|
|
// According to http://linux.die.net/man/3/strerror_r there are two versions of strerror_r():
|
|
|
|
|
|
|
|
#if (((_POSIX_C_SOURCE >= 200112L) || (_XOPEN_SOURCE >= 600)) && ! _GNU_SOURCE) // XSI version of strerror_r():
|
|
|
|
|
|
|
|
int res = strerror_r( errno, buffer, ARRAYCOUNT(buffer) );
|
|
|
|
if( res == 0 )
|
2012-02-05 11:24:16 -05:00
|
|
|
{
|
2012-02-06 08:22:28 -05:00
|
|
|
Printf(Out, "%d: %s", a_ErrNo, buffer);
|
|
|
|
return Out;
|
2012-02-05 11:24:16 -05:00
|
|
|
}
|
2012-02-06 08:22:28 -05:00
|
|
|
|
|
|
|
#else // GNU version of strerror_r()
|
|
|
|
|
|
|
|
char * res = strerror_r( errno, buffer, ARRAYCOUNT(buffer) );
|
|
|
|
if( res != NULL )
|
|
|
|
{
|
|
|
|
Printf(Out, "%d: %s", a_ErrNo, res);
|
|
|
|
return Out;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // strerror_r() version
|
|
|
|
|
2012-02-05 11:24:16 -05:00
|
|
|
else
|
|
|
|
{
|
2012-02-06 08:22:28 -05:00
|
|
|
Printf(Out, "Error %d while getting error string for error #%d!", errno, a_ErrNo);
|
|
|
|
return Out;
|
2012-02-05 11:24:16 -05:00
|
|
|
}
|
2012-02-06 06:53:01 -05:00
|
|
|
|
|
|
|
#endif // else _WIN32
|
2012-02-05 11:24:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int cSocket::GetLastError()
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
return WSAGetLastError();
|
|
|
|
#else
|
|
|
|
return errno;
|
2011-10-22 20:18:44 -04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2012-02-01 17:38:03 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-10-22 20:18:44 -04:00
|
|
|
int cSocket::SetReuseAddress()
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
char yes = 1;
|
|
|
|
#else
|
|
|
|
int yes = 1;
|
|
|
|
#endif
|
2012-02-05 10:41:56 -05:00
|
|
|
return setsockopt(m_Socket, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));
|
2011-10-22 20:18:44 -04:00
|
|
|
}
|
|
|
|
|
2012-02-01 17:38:03 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-10-22 20:18:44 -04:00
|
|
|
int cSocket::WSAStartup()
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
WSADATA wsaData;
|
2012-02-05 10:41:56 -05:00
|
|
|
memset(&wsaData, 0, sizeof(wsaData));
|
|
|
|
return ::WSAStartup(MAKEWORD(2, 2),&wsaData);
|
2011-10-22 20:18:44 -04:00
|
|
|
#else
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2012-02-01 17:38:03 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-10-22 20:18:44 -04:00
|
|
|
cSocket cSocket::CreateSocket()
|
|
|
|
{
|
|
|
|
return socket(AF_INET,SOCK_STREAM,0);
|
|
|
|
}
|
|
|
|
|
2012-02-01 17:38:03 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-02-07 16:16:03 -05:00
|
|
|
unsigned long cSocket::INTERNET_ADDRESS_LOCALHOST(void)
|
|
|
|
{
|
|
|
|
static unsigned long LocalHost = 0;
|
|
|
|
if (LocalHost == 0)
|
|
|
|
{
|
|
|
|
LocalHost = inet_addr("127.0.0.1"); // GCC won't accept this as a global var assignment
|
|
|
|
}
|
|
|
|
return LocalHost;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-02-05 10:41:56 -05:00
|
|
|
int cSocket::Bind(SockAddr_In& a_Address)
|
2011-10-22 20:18:44 -04:00
|
|
|
{
|
|
|
|
sockaddr_in local;
|
|
|
|
|
2012-02-07 02:44:00 -05:00
|
|
|
local.sin_family = a_Address.Family;
|
|
|
|
local.sin_addr.s_addr = a_Address.Address;
|
|
|
|
local.sin_port = htons((u_short)a_Address.Port);
|
2011-10-22 20:18:44 -04:00
|
|
|
|
2012-02-05 11:24:16 -05:00
|
|
|
return bind(m_Socket, (sockaddr*)&local, sizeof(local));
|
2011-10-22 20:18:44 -04:00
|
|
|
}
|
|
|
|
|
2012-02-01 17:38:03 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-02-05 10:41:56 -05:00
|
|
|
int cSocket::Listen(int a_Backlog)
|
2011-10-22 20:18:44 -04:00
|
|
|
{
|
2012-02-05 10:41:56 -05:00
|
|
|
return listen(m_Socket, a_Backlog);
|
2011-10-22 20:18:44 -04:00
|
|
|
}
|
|
|
|
|
2012-02-01 17:38:03 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-10-22 20:18:44 -04:00
|
|
|
cSocket cSocket::Accept()
|
|
|
|
{
|
|
|
|
sockaddr_in from;
|
|
|
|
socklen_t fromlen=sizeof(from);
|
|
|
|
|
2012-02-05 10:41:56 -05:00
|
|
|
cSocket SClient = accept(m_Socket, (sockaddr*)&from, &fromlen);
|
2011-10-22 20:18:44 -04:00
|
|
|
|
2012-02-05 10:41:56 -05:00
|
|
|
if (from.sin_addr.s_addr && SClient.IsValid()) // Get IP in string form
|
2011-10-22 20:18:44 -04:00
|
|
|
{
|
|
|
|
SClient.m_IPString = inet_ntoa(from.sin_addr);
|
2012-02-05 10:41:56 -05:00
|
|
|
//LOG("cSocket::Accept() %s", SClient.m_IPString);
|
2011-10-22 20:18:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return SClient;
|
|
|
|
}
|
|
|
|
|
2012-02-01 17:38:03 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-02-07 02:44:00 -05:00
|
|
|
int cSocket::Connect(SockAddr_In & a_Address)
|
|
|
|
{
|
|
|
|
sockaddr_in local;
|
|
|
|
|
|
|
|
local.sin_family = a_Address.Family;
|
|
|
|
local.sin_addr.s_addr = a_Address.Address;
|
|
|
|
local.sin_port = htons((u_short)a_Address.Port);
|
|
|
|
|
|
|
|
return connect(m_Socket, (sockaddr *)&local, sizeof(local));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-02-05 10:41:56 -05:00
|
|
|
int cSocket::Receive(char* a_Buffer, unsigned int a_Length, unsigned int a_Flags)
|
2011-10-22 20:18:44 -04:00
|
|
|
{
|
|
|
|
return recv(m_Socket, a_Buffer, a_Length, a_Flags);
|
2012-02-01 17:38:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-02-07 02:44:00 -05:00
|
|
|
|
|
|
|
int cSocket::Send(const char * a_Buffer, unsigned int a_Length)
|
|
|
|
{
|
|
|
|
return send(m_Socket, a_Buffer, a_Length, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-02-07 15:49:52 -05:00
|
|
|
int cSocket::Send(const cPacket * a_Packet)
|
|
|
|
{
|
|
|
|
AString Serialized;
|
|
|
|
a_Packet->Serialize(Serialized);
|
|
|
|
return Send(Serialized.data(), Serialized.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int cSocket::Send(const cPacket & a_Packet)
|
|
|
|
{
|
|
|
|
AString Serialized;
|
|
|
|
a_Packet.Serialize(Serialized);
|
|
|
|
return Send(Serialized.data(), Serialized.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-02-07 02:44:00 -05:00
|
|
|
unsigned short cSocket::GetPort(void) const
|
|
|
|
{
|
|
|
|
assert(IsValid());
|
|
|
|
|
|
|
|
sockaddr_in Addr;
|
|
|
|
socklen_t AddrSize = sizeof(Addr);
|
|
|
|
if (getsockname(m_Socket, (sockaddr *)&Addr, &AddrSize) != 0)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2012-02-08 05:02:46 -05:00
|
|
|
return ntohs(Addr.sin_port);
|
2012-02-07 02:44:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|