2013-07-29 07:13:03 -04:00
|
|
|
|
|
|
|
// Server.cpp
|
|
|
|
|
|
|
|
// Interfaces to the cServer class encapsulating the entire "server"
|
|
|
|
|
|
|
|
#include "Globals.h"
|
|
|
|
#include "Server.h"
|
|
|
|
#include "Connection.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cServer::cServer(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int cServer::Init(short a_ListenPort, short a_ConnectPort)
|
|
|
|
{
|
|
|
|
m_ConnectPort = a_ConnectPort;
|
2014-01-17 06:38:25 -05:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
WSAData wsa;
|
|
|
|
int res = WSAStartup(0x0202, &wsa);
|
|
|
|
if (res != 0)
|
|
|
|
{
|
|
|
|
printf("Cannot initialize WinSock: %d\n", res);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
#endif // _WIN32
|
2013-07-29 07:13:03 -04:00
|
|
|
|
2015-02-15 14:39:53 -05:00
|
|
|
LOGINFO("Generating protocol encryption keypair...");
|
2014-01-25 13:19:37 -05:00
|
|
|
m_PrivateKey.Generate();
|
|
|
|
m_PublicKeyDER = m_PrivateKey.GetPubKeyDER();
|
2013-07-29 07:13:03 -04:00
|
|
|
|
|
|
|
m_ListenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
|
2015-01-23 04:10:25 -05:00
|
|
|
if (m_ListenSocket < 0)
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
int err = WSAGetLastError();
|
|
|
|
#else
|
|
|
|
int err = errno;
|
|
|
|
#endif
|
|
|
|
printf("Failed to create listener socket: %d\n", err);
|
|
|
|
return err;
|
|
|
|
}
|
2013-07-29 07:13:03 -04:00
|
|
|
sockaddr_in local;
|
|
|
|
memset(&local, 0, sizeof(local));
|
|
|
|
local.sin_family = AF_INET;
|
2015-01-23 04:10:25 -05:00
|
|
|
local.sin_addr.s_addr = 130; // INADDR_ANY; // All interfaces
|
2013-07-29 07:13:03 -04:00
|
|
|
local.sin_port = htons(a_ListenPort);
|
2015-01-23 04:10:25 -05:00
|
|
|
if (!bind(m_ListenSocket, (sockaddr *)&local, sizeof(local)))
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
int err = WSAGetLastError();
|
|
|
|
#else
|
|
|
|
int err = errno;
|
|
|
|
#endif
|
|
|
|
printf("Failed to bind listener socket: %d\n", err);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
if (listen(m_ListenSocket, 1) != 0)
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
int err = WSAGetLastError();
|
|
|
|
#else
|
|
|
|
int err = errno;
|
|
|
|
#endif
|
|
|
|
printf("Failed to listen on socket: %d\n", err);
|
|
|
|
return err;
|
|
|
|
}
|
2013-07-29 07:13:03 -04:00
|
|
|
|
|
|
|
printf("Listening on port %d, connecting to localhost:%d\n", a_ListenPort, a_ConnectPort);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cServer::Run(void)
|
|
|
|
{
|
2015-02-16 03:27:52 -05:00
|
|
|
LOGINFO("Server running.");
|
2013-07-29 07:13:03 -04:00
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
sockaddr_in Addr;
|
2014-01-17 06:38:25 -05:00
|
|
|
memset(&Addr, 0, sizeof(Addr));
|
2014-01-17 07:15:54 -05:00
|
|
|
socklen_t AddrSize = sizeof(Addr);
|
2013-07-29 07:13:03 -04:00
|
|
|
SOCKET client = accept(m_ListenSocket, (sockaddr *)&Addr, &AddrSize);
|
|
|
|
if (client == INVALID_SOCKET)
|
|
|
|
{
|
2014-01-17 07:15:54 -05:00
|
|
|
printf("accept returned an error: %d; bailing out.\n", SocketError);
|
2013-07-29 07:13:03 -04:00
|
|
|
return;
|
|
|
|
}
|
2015-02-15 14:39:53 -05:00
|
|
|
LOGINFO("Client connected, proxying...");
|
2013-07-29 07:13:03 -04:00
|
|
|
cConnection Connection(client, *this);
|
|
|
|
Connection.Run();
|
2015-02-15 14:39:53 -05:00
|
|
|
LOGINFO("Client disconnected. Ready for another connection.");
|
2013-07-29 07:13:03 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|