summaryrefslogtreecommitdiffstats
path: root/Tools/ProtoProxy/Server.cpp
blob: 2db67e960bb419c26c24bfcc00de42c062d5192b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105

// Server.cpp

// Interfaces to the cServer class encapsulating the entire "server"

#include "Globals.h"
#include "Server.h"
#include "Connection.h"
#include "../../src/Logger.h"





cServer::cServer(void)
{
}





int cServer::Init(UInt16 a_ListenPort, UInt16 a_ConnectPort)
{
	m_ConnectPort = a_ConnectPort;

	#ifdef _WIN32
		WSAData wsa;
		int res = WSAStartup(0x0202, &wsa);
		if (res != 0)
		{
			LOGERROR("Cannot initialize WinSock: %d", res);
			return res;
		}
	#endif  // _WIN32

	m_ListenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	if (m_ListenSocket < 0)
	{
		#ifdef _WIN32
			int err = WSAGetLastError();
		#else
			int err = errno;
		#endif
		LOGERROR("Failed to create listener socket: %d", err);
		return err;
	}
	sockaddr_in local;
	memset(&local, 0, sizeof(local));
	local.sin_family = AF_INET;
	local.sin_addr.s_addr = INADDR_ANY;  // All interfaces
	local.sin_port = htons(a_ListenPort);
	if (bind(m_ListenSocket, reinterpret_cast<const sockaddr *>(&local), sizeof(local)) != 0)
	{
		#ifdef _WIN32
			int err = WSAGetLastError();
		#else
			int err = errno;
		#endif
		LOGERROR("Failed to bind listener socket: %d", 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;
	}
	LOGINFO("Listening for client connections on port %d, connecting to server at localhost:%d", a_ListenPort, a_ConnectPort);

	LOGINFO("Generating protocol encryption keypair...");
	m_PrivateKey.Generate();
	m_PublicKeyDER = m_PrivateKey.GetPubKeyDER();

	return 0;
}





void cServer::Run(void)
{
	LOGINFO("Server running.");
	while (true)
	{
		sockaddr_in Addr;
		memset(&Addr, 0, sizeof(Addr));
		socklen_t AddrSize = sizeof(Addr);
		SOCKET client = accept(m_ListenSocket, reinterpret_cast<sockaddr *>(&Addr), &AddrSize);
		if (client == INVALID_SOCKET)
		{
			printf("accept returned an error: %d; bailing out.\n", SocketError);
			return;
		}
		LOGINFO("Client connected, proxying...");
		cConnection Connection(client, *this);
		Connection.Run();
		LOGINFO("Client disconnected. Ready for another connection.");
	}
}