Adding protocols to publish/hide information in the SQL database. The protocol that allows a client to identify himself, retreive a server public IP:port and connect to it is finished. Remains the server-protocol for it. Protocol management improved (protocols can be paused/unpaused).

git-svn-id: svn+ssh://svn.code.sf.net/p/supertuxkart/code/main/branches/hilnius@12883 178a84e3-b1eb-0310-8ba1-8eac791a3b58
This commit is contained in:
hilnius
2013-06-18 22:25:03 +00:00
parent 918af26024
commit 714a85b507
15 changed files with 358 additions and 34 deletions

View File

@@ -31,6 +31,7 @@
<Compiler>
<Add option="-Wall" />
<Add option="-fexceptions" />
<Add directory="../../src" />
</Compiler>
<Linker>
<Add option="-lpthread" />
@@ -48,13 +49,16 @@
<Unit filename="network_manager.hpp" />
<Unit filename="protocol.cpp" />
<Unit filename="protocol.hpp" />
<Unit filename="protocol_listener.hpp" />
<Unit filename="protocol_manager.cpp" />
<Unit filename="protocol_manager.hpp" />
<Unit filename="protocols/connect_to_server.cpp" />
<Unit filename="protocols/connect_to_server.hpp" />
<Unit filename="protocols/get_public_address.cpp" />
<Unit filename="protocols/get_public_address.hpp" />
<Unit filename="protocols/hide_public_address.cpp" />
<Unit filename="protocols/hide_public_address.hpp" />
<Unit filename="protocols/show_public_address.cpp" />
<Unit filename="protocols/show_public_address.hpp" />
<Unit filename="server_network_manager.cpp" />
<Unit filename="server_network_manager.hpp" />
<Unit filename="singleton.hpp" />
@@ -62,6 +66,7 @@
<Unit filename="stk_host.hpp" />
<Unit filename="stk_peer.cpp" />
<Unit filename="stk_peer.hpp" />
<Unit filename="time.hpp" />
<Extensions>
<code_completion />
<debugger />

View File

@@ -37,5 +37,7 @@ void ClientNetworkManager::packetReceived(char* data)
}
void ClientNetworkManager::sendPacket(char* data)
{
if (m_peers.size() > 1)
printf("Ambiguous send of data\n");
m_peers[0]->sendPacket(data);
}

View File

@@ -7,6 +7,7 @@
#include "protocols/get_public_address.hpp"
#include "http_functions.hpp"
#include "protocols/connect_to_server.hpp"
#include "protocols/hide_public_address.hpp"
#include <stdio.h>
#include <string.h>
@@ -33,6 +34,10 @@ int main()
cin >> answer;
if (answer == "client")
{
protocolListener = new ProtocolManager();
ClientNetworkManager clt;
clt.run();
/// NICKNAME :
std::string nickname;
cout << "Nickname=";
@@ -43,18 +48,12 @@ int main()
std::cin >> password;
/// HOST NICKNAME :
std::string hostNickname;
cout << "Nickname=";
cout << "Host Nickname=";
std::cin >> hostNickname;
ConnectToServer* connectionProtocol = new ConnectToServer(NULL);
ConnectToServer* connectionProtocol = new ConnectToServer(&clt);
connectionProtocol->setPassword(password);
connectionProtocol->setUsername(nickname);
connectionProtocol->setHostName(hostNickname);
ClientNetworkManager clt;
clt.run();
protocolListener = new ProtocolManager();
pthread_t* thrd = (pthread_t*)(malloc(sizeof(pthread_t)));
pthread_create(thrd, NULL, foo, NULL);
@@ -64,31 +63,57 @@ int main()
prt->setListener(protocolListener);
connectionProtocol->setListener(protocolListener);
protocolListener->runProtocol(prt);
protocolListener->startProtocol(prt);
clt.connect(0x0100007f, 7000); // addr in little endian, real address is 7f 00 00 01 (127.0.0.1)
//clt.connect(0x0100007f, 7000); // addr in little endian, real address is 7f 00 00 01 (127.0.0.1)
bool* connected = &connectionProtocol->connected;
std::string buffer;
while (1)
{
cin >> buffer;
if (buffer == "protocolsCount")
if (buffer == "cmd=protocolsCount")
{
cout << protocolListener->runningProtocolsCount() << " protocols are running." << endl;
continue;
}
if (buffer == "cmd=hideAddress")
{
HidePublicAddress* hideipv4 = new HidePublicAddress(NULL);
hideipv4->setPassword(password);
hideipv4->setNickname(nickname);
protocolListener->startProtocol(hideipv4);
}
if (buffer == "cmd=login")
{
std::cout << "Username=";
std::cin >> nickname;
connectionProtocol->setUsername(nickname);
std::cout << "Password=";
std::cin >> password;
connectionProtocol->setPassword(password);
connectionProtocol->unpause();
}
if (buffer.size() == 0) { continue; }
char buffer2[256];
strcpy(buffer2, buffer.c_str());
clt.sendPacket(buffer2);
if (*connected)
{
clt.sendPacket(buffer2);
}
}
enet_deinitialize();
}
else if (answer == "host")
{
ServerNetworkManager srv;
srv.run();
srv.start();
//srv.protocolListener = new ProtocolManager();
srv.protocolListener = new ProtocolManager();
GetPublicAddress
while(1){}
}
return 0;

View File

@@ -6,8 +6,9 @@
#include <vector>
#include "protocol_manager.hpp"
#include "callback_object.hpp"
class NetworkManager
class NetworkManager : public CallbackObject
{
public:
NetworkManager();

View File

@@ -4,7 +4,9 @@
#include <assert.h>
#include <stdio.h>
#include <cstdlib>
#define RAND_MAX 65536
ProtocolManager::ProtocolManager()
{
@@ -20,16 +22,18 @@ void ProtocolManager::messageReceived(uint8_t* data)
m_messagesToProcess.push_back(data);
}
void ProtocolManager::runProtocol(Protocol* protocol)
int ProtocolManager::startProtocol(Protocol* protocol)
{
ProtocolInfo protocolInfo;
protocolInfo.paused = false;
protocolInfo.state = PROTOCOL_STATE_RUNNING;
assignProtocolId(protocolInfo);
protocolInfo.protocol = protocol;
m_protocols.push_back(protocolInfo);
protocol->setListener(this);
protocol->setup();
protocol->start();
printf("*** PROTOCOL MANAGER *** - A new protocol has been started. There are %ld protocols running.\n", m_protocols.size());
return protocolInfo.id;
}
void ProtocolManager::stopProtocol(Protocol* protocol)
{
@@ -39,9 +43,9 @@ void ProtocolManager::pauseProtocol(Protocol* protocol)
{
for (unsigned int i = 0; i < m_protocols.size(); i++)
{
if (m_protocols[i].protocol == protocol)
if (m_protocols[i].protocol == protocol && m_protocols[i].state == PROTOCOL_STATE_RUNNING)
{
m_protocols[i].paused = true;
m_protocols[i].state = PROTOCOL_STATE_PAUSED;
m_protocols[i].protocol->pause();
}
}
@@ -50,9 +54,9 @@ void ProtocolManager::unpauseProtocol(Protocol* protocol)
{
for (unsigned int i = 0; i < m_protocols.size(); i++)
{
if (m_protocols[i].protocol == protocol && m_protocols[i].paused == true)
if (m_protocols[i].protocol == protocol && m_protocols[i].state == PROTOCOL_STATE_PAUSED)
{
m_protocols[i].paused = false;
m_protocols[i].state = PROTOCOL_STATE_RUNNING;
m_protocols[i].protocol->unpause();
}
}
@@ -90,7 +94,7 @@ void ProtocolManager::update()
// now update all protocols
for (unsigned int i = 0; i < m_protocols.size(); i++)
{
if (m_protocols[i].paused == false)
if (m_protocols[i].state == PROTOCOL_STATE_RUNNING)
m_protocols[i].protocol->update();
}
}
@@ -100,3 +104,21 @@ int ProtocolManager::runningProtocolsCount()
return m_protocols.size();
}
void ProtocolManager::assignProtocolId(ProtocolInfo& protocolInfo)
{
uint32_t newId;
bool exists;
do
{
newId = (rand()<<16)+rand();
exists = false;
for (unsigned int i = 0; i < m_protocols.size(); i++)
{
if (m_protocols[i].id == newId)
exists = true;
}
} while (exists);
protocolInfo.id = newId;
}

View File

@@ -6,12 +6,20 @@
class Protocol;
enum PROTOCOL_STATE
{
PROTOCOL_STATE_RUNNING,
PROTOCOL_STATE_PAUSED,
PROTOCOL_STATE_TERMINATED
};
class ProtocolManager
{
typedef struct
{
bool paused;
PROTOCOL_STATE state;
Protocol* protocol;
uint32_t id;
} ProtocolInfo;
public:
ProtocolManager();
@@ -19,7 +27,7 @@ class ProtocolManager
virtual void messageReceived(uint8_t* data);
virtual void runProtocol(Protocol* protocol);
virtual int startProtocol(Protocol* protocol);
virtual void stopProtocol(Protocol* protocol);
virtual void pauseProtocol(Protocol* protocol);
virtual void unpauseProtocol(Protocol* protocol);
@@ -30,6 +38,8 @@ class ProtocolManager
virtual int runningProtocolsCount();
protected:
void assignProtocolId(ProtocolInfo& protocolInfo);
std::vector<ProtocolInfo> m_protocols;
std::vector<uint8_t*> m_messagesToProcess;
};

View File

@@ -1,14 +1,17 @@
#include "connect_to_server.hpp"
#include "../http_functions.hpp"
#include "../time.hpp"
#include "../client_network_manager.hpp"
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
ConnectToServer::ConnectToServer(CallbackObject* callbackObject) : Protocol(callbackObject)
{
m_ownPublicIp = 0;
m_ownPublicPort = 0;
connected = false;
}
ConnectToServer::~ConnectToServer()
@@ -35,10 +38,12 @@ void ConnectToServer::start()
void ConnectToServer::pause()
{
m_listener->pauseProtocol(this); // need to be sure that the protocol manager knows
}
void ConnectToServer::unpause()
{
m_listener->unpauseProtocol(this); // need to be sure that the protocol manager knows
}
void ConnectToServer::update()
@@ -48,7 +53,6 @@ void ConnectToServer::update()
char url[512];
sprintf(url, "http://stkconnect.freeserver.me/log.php?set&nick=%s&ip=%u&port=%u&pwd=%s", m_username.c_str(), m_ownPublicIp, m_ownPublicPort, m_password.c_str());
std::string result = HTTP::getPage(url);
std::cout << "size of answer : " << result.size() << std::endl;
if (result[0] == 's' && result[1] == 'u' && result[2] == 'c' && result[3] == 'c' && result[4] == 'e' && result[5] == 's' && result[6] == 's')
{
printf("Address set.\n");
@@ -56,17 +60,59 @@ void ConnectToServer::update()
}
if (result[0] == 'f' && result[1] == 'a' && result[2] == 'i' && result[3] == 'l')
{
printf("Login fail. Please re-set username:password and restart the protocol.\n");
printf("Login fail. Please re-set username:password and unpause the protocol.\n");
m_state = NOTHING;
m_listener->pauseProtocol(this);
pause();
}
}
else if (m_state == ADDRESS_KNOWN_ONLINE)
{
static double target = 0;
double currentTime = Time::getSeconds();
if (currentTime < target-1800) // sometimes the getSeconds method forgets 3600 seconds.
currentTime += 3600;
if (currentTime > target)
{
char url[512];
sprintf(url, "http://stkconnect.freeserver.me/log.php?get&nick=%s", m_hostName.c_str());
std::string result = HTTP::getPage(url);
if (result == "")
{
printf("The host you try to reach does not exist. Change the host name please.\n");
m_state = NOTHING;
pause();
return;
}
std::string ipAddr = result;
ipAddr.erase(ipAddr.find_first_of(':'));
std::string portNb = result;
portNb.erase(0, portNb.find_first_of(':')+1);
uint32_t dstIp = (uint32_t)(atoi(ipAddr.c_str()));
uint16_t dstPort = (uint32_t)(atoi(portNb.c_str()));
if (dstIp == 0 || dstPort == 0)
{
printf("The host you try to reach is not online. There will be a new try in 10 seconds.\n");
target = currentTime+10;
}
else
{
printf("Public ip of target is %i.%i.%i.%i:%i\n", (dstIp>>24)&0xff, (dstIp>>16)&0xff, (dstIp>>8)&0xff, dstIp&0xff, dstPort);
m_serverIp = ((dstIp&0x000000ff)<<24) // change the server IP to have a network-byte order
+ ((dstIp&0x0000ff00)<<8)
+ ((dstIp&0x00ff0000)>>8)
+ ((dstIp&0xff000000)>>24);
m_serverPort = dstPort;
m_state = PEER_ADDRESS_RETREIVED;
}
}
}
else if (m_state == PEER_ADDRESS_RETREIVED)
{
// we know the distant address:port, just need to connect.
ClientNetworkManager* networkManager = static_cast<ClientNetworkManager*>(m_callbackObject);
networkManager->connect(m_serverIp, m_serverPort);
m_state = CONNECTED;
connected = true;
}
else if (m_state == CONNECTED)
{

View File

@@ -22,9 +22,12 @@ class ConnectToServer : public Protocol, public CallbackObject
void setPassword(std::string password);
void setHostName(std::string hostName);
bool connected;
protected:
uint32_t m_ownPublicIp;
uint16_t m_ownPublicPort;
uint32_t m_serverIp;
uint16_t m_serverPort;
std::string m_hostName;
std::string m_username;
std::string m_password;

View File

@@ -92,14 +92,14 @@ void GetPublicAddress::update()
bytes[20] = '\0';
printf("Querrying STUN server 132.177.123.6\n");
unsigned int dst = 132*256*256*256+177*256*256+123*256+6;
unsigned int dst = (132<<24)+(177<<16)+(123<<8)+6;
NetworkManager::setManualSocketsMode(true);
NetworkManager::getHost()->sendRawPacket(bytes, 20, dst, 3478);
m_state = TEST_SENT;
}
if (m_state == TEST_SENT)
{
unsigned int dst = 132*256*256*256+177*256*256+123*256+6;
unsigned int dst = (132<<24)+(177<<16)+(123<<8)+6;
uint8_t* data = NetworkManager::getHost()->receiveRawPacket(dst, 3478);
assert(data);
@@ -181,7 +181,7 @@ void GetPublicAddress::update()
NetworkManager::setManualSocketsMode(false);
ConnectToServer* cbObj = static_cast<ConnectToServer*>(m_callbackObject);
cbObj->setSelfAddress(address, port);
m_listener->runProtocol(cbObj);
m_listener->startProtocol(cbObj);
}
else
m_state = NOTHING_DONE; // need to re-send the stun request

View File

@@ -0,0 +1,68 @@
#include "hide_public_address.hpp"
#include "../http_functions.hpp"
#include <stdio.h>
HidePublicAddress::HidePublicAddress(CallbackObject* callbackObject) : Protocol(callbackObject)
{
}
HidePublicAddress::~HidePublicAddress()
{
}
void HidePublicAddress::messageReceived(uint8_t* data)
{
}
void HidePublicAddress::setup()
{
m_state = NONE;
}
void HidePublicAddress::start()
{
}
void HidePublicAddress::pause()
{
}
void HidePublicAddress::unpause()
{
}
void HidePublicAddress::update()
{
if (m_state == NONE)
{
char url[512];
sprintf(url, "http://stkconnect.freeserver.me/log.php?logout&nick=%s&pwd=%s", m_nickname.c_str(), m_password.c_str());
std::string result = HTTP::getPage(url);
if (result[0] == 's' && result[1] == 'u' && result[2] == 'c' && result[3] == 'c' && result[4] == 'e' && result[5] == 's' && result[6] == 's')
{
printf("Public address hidden successfully.\n");
m_state = DONE;
}
if (result[0] == 'f' && result[1] == 'a' && result[2] == 'i' && result[3] == 'l')
{
printf("Public address still visible. Re-set nick:password and retry.\n");
m_state = NONE;
m_listener->pauseProtocol(this);
}
}
else if (m_state == DONE)
{
m_listener->protocolTerminated(this);
}
}
void HidePublicAddress::setNickname(std::string nickname)
{
m_nickname = nickname;
}
void HidePublicAddress::setPassword(std::string password)
{
m_password = password;
}

View File

@@ -0,0 +1,34 @@
#ifndef HIDE_PUBLIC_ADDRESS_HPP
#define HIDE_PUBLIC_ADDRESS_HPP
#include "../protocol.hpp"
#include <string>
class HidePublicAddress : public Protocol
{
public:
HidePublicAddress(CallbackObject* callbackObject);
virtual ~HidePublicAddress();
virtual void messageReceived(uint8_t* data);
virtual void setup();
virtual void start();
virtual void pause();
virtual void unpause();
virtual void update();
virtual void setNickname(std::string nickname);
virtual void setPassword(std::string password);
protected:
std::string m_nickname;
std::string m_password;
enum STATE
{
NONE,
DONE
};
STATE m_state;
};
#endif // HIDE_PUBLIC_ADDRESS_HPP

View File

@@ -0,0 +1,68 @@
#include "show_public_address.hpp"
#include "../http_functions.hpp"
#include <stdio.h>
ShowPublicAddress::ShowPublicAddress(CallbackObject* callbackObject) : Protocol(callbackObject)
{
}
ShowPublicAddress::~ShowPublicAddress()
{
}
void ShowPublicAddress::messageReceived(uint8_t* data)
{
}
void ShowPublicAddress::setup()
{
m_state = NONE;
}
void ShowPublicAddress::start()
{
}
void ShowPublicAddress::pause()
{
}
void ShowPublicAddress::unpause()
{
}
void ShowPublicAddress::update()
{
if (m_state == NONE)
{
char url[512];
sprintf(url, "http://stkconnect.freeserver.me/log.php?logout&nick=%s&pwd=%s", m_nickname.c_str(), m_password.c_str());
std::string result = HTTP::getPage(url);
if (result[0] == 's' && result[1] == 'u' && result[2] == 'c' && result[3] == 'c' && result[4] == 'e' && result[5] == 's' && result[6] == 's')
{
printf("Public address hidden successfully.\n");
m_state = DONE;
}
if (result[0] == 'f' && result[1] == 'a' && result[2] == 'i' && result[3] == 'l')
{
printf("Public address still visible. Re-set nick:password and retry.\n");
m_state = NONE;
m_listener->pauseProtocol(this);
}
}
else if (m_state == DONE)
{
m_listener->protocolTerminated(this);
}
}
void ShowPublicAddress::setNickname(std::string nickname)
{
m_nickname = nickname;
}
void ShowPublicAddress::setPassword(std::string password)
{
m_password = password;
}

View File

@@ -0,0 +1,34 @@
#ifndef SHOW_PUBLIC_ADDRESS_HPP
#define SHOW_PUBLIC_ADDRESS_HPP
#include "../protocol.hpp"
#include <string>
class ShowPublicAddress : public Protocol
{
public:
ShowPublicAddress(CallbackObject* callbackObject);
virtual ~ShowPublicAddress();
virtual void messageReceived(uint8_t* data);
virtual void setup();
virtual void start();
virtual void pause();
virtual void unpause();
virtual void update();
virtual void setNickname(std::string nickname);
virtual void setPassword(std::string password);
protected:
std::string m_nickname;
std::string m_password;
enum STATE
{
NONE,
DONE
};
STATE m_state;
};
#endif // HIDE_PUBLIC_ADDRESS_HPP

View File

@@ -28,7 +28,7 @@ void ServerNetworkManager::start()
m_localhost = new STKHost();
m_localhost->setupServer(STKHost::HOST_ANY, 7000, 32, 2, 0, 0);
m_localhost->startListening();
printf("Server now setup.\n");
printf("Server now setup, listening on port 7000.\n");
}
void ServerNetworkManager::packetReceived(char* data)

View File

@@ -24,10 +24,16 @@ void STKPeer::connectToServer(STKHost* host, uint32_t ip, uint16_t port, uint32_
printf("Could not connect to server.\n");
return;
}
else
{
printf("Connected.\n");
}
}
void STKPeer::sendPacket(char* data)
{
//printf("sending packet to %i.%i.%i.%i:%i", (m_peer->address.host>>24)&0xff,(m_peer->address.host>>16)&0xff,(m_peer->address.host>>8)&0xff,(m_peer->address.host>>0)&0xff,m_peer->address.port);
ENetPacket* packet = enet_packet_create(data, strlen(data)+1,ENET_PACKET_FLAG_RELIABLE);
enet_peer_send(m_peer, 0, packet);
}