stun working
git-svn-id: svn+ssh://svn.code.sf.net/p/supertuxkart/code/main/branches/hilnius@12867 178a84e3-b1eb-0310-8ba1-8eac791a3b58
This commit is contained in:
parent
2469902478
commit
27a7f1da6b
@ -24,7 +24,8 @@ int main()
|
||||
{
|
||||
std::string answer;
|
||||
cout << "host or client:";
|
||||
cin >> answer;
|
||||
answer = "client";
|
||||
//cin >> answer;
|
||||
if (answer == "client")
|
||||
{
|
||||
ClientNetworkManager clt;
|
||||
@ -33,19 +34,27 @@ int main()
|
||||
|
||||
protocolListener = new ProtocolManager();
|
||||
|
||||
|
||||
pthread_t* thrd = (pthread_t*)(malloc(sizeof(pthread_t)));
|
||||
pthread_create(thrd, NULL, foo, NULL);
|
||||
|
||||
// start a retreive stun addr protocol
|
||||
Protocol* prt = new GetPublicAddress();
|
||||
protocolListener->runProtocol(prt);
|
||||
|
||||
char buffer[256];
|
||||
|
||||
std::string buffer;
|
||||
while (1)
|
||||
{
|
||||
gets(buffer);
|
||||
if (strlen(buffer) == 0) { continue; }
|
||||
clt.sendPacket(buffer);
|
||||
cin >> buffer;
|
||||
if (buffer == "protocolsCount")
|
||||
{
|
||||
cout << protocolListener->runningProtocolsCount() << " protocols are running." << endl;
|
||||
continue;
|
||||
}
|
||||
if (buffer.size() == 0) { continue; }
|
||||
char buffer2[256];
|
||||
strcpy(buffer2, buffer.c_str());
|
||||
clt.sendPacket(buffer2);
|
||||
}
|
||||
}
|
||||
else if (answer == "host")
|
||||
|
@ -15,7 +15,7 @@ NetworkManager::NetworkManager()
|
||||
printf("New Network Manager created.\n");
|
||||
}
|
||||
|
||||
NetworkManager::~NetworkManager()
|
||||
NetworkManager::~NetworkManager()
|
||||
{
|
||||
}
|
||||
|
||||
@ -23,6 +23,13 @@ void NetworkManager::run()
|
||||
{
|
||||
}
|
||||
|
||||
void NetworkManager::setManualSocketsMode(bool manual)
|
||||
{
|
||||
if (manual)
|
||||
instance->getHost()->stopListening();
|
||||
else
|
||||
instance->getHost()->startListening();
|
||||
}
|
||||
|
||||
void NetworkManager::sendRawPacket(uint8_t* data, int length, unsigned int dstIp, unsigned short dstPort)
|
||||
{
|
||||
|
@ -13,10 +13,12 @@ class NetworkManager
|
||||
NetworkManager();
|
||||
virtual ~NetworkManager();
|
||||
|
||||
virtual void run() = 0;
|
||||
virtual void run() = 0;
|
||||
|
||||
static void setManualSocketsMode(bool manual);
|
||||
static void sendRawPacket(uint8_t* data, int length, unsigned int dstIp, unsigned short dstPort);
|
||||
static uint8_t* receiveRawPacket();
|
||||
|
||||
static void receptionCallback(char* data);
|
||||
virtual void packetReceived(char* data) = 0;
|
||||
|
||||
|
@ -3,7 +3,7 @@
|
||||
#include "protocol.hpp"
|
||||
#include <assert.h>
|
||||
|
||||
ProtocolManager::ProtocolManager() : ProtocolListener()
|
||||
ProtocolManager::ProtocolManager()
|
||||
{
|
||||
}
|
||||
|
||||
@ -14,12 +14,13 @@ ProtocolManager::~ProtocolManager()
|
||||
void ProtocolManager::messageReceived(uint8_t* data)
|
||||
{
|
||||
assert(data);
|
||||
m_messagesToProcess.push_back(data);
|
||||
m_messagesToProcess.push_back(data);
|
||||
}
|
||||
|
||||
void ProtocolManager::runProtocol(Protocol* protocol)
|
||||
{
|
||||
m_protocols.push_back(protocol);
|
||||
protocol->setListener(this);
|
||||
protocol->setup();
|
||||
protocol->start();
|
||||
}
|
||||
@ -27,6 +28,19 @@ void ProtocolManager::stopProtocol(Protocol* protocol)
|
||||
{
|
||||
|
||||
}
|
||||
void ProtocolManager::protocolTerminated(Protocol* protocol)
|
||||
{
|
||||
int offset = 0;
|
||||
for (unsigned int i = 0; i < m_protocols.size(); i++)
|
||||
{
|
||||
if (m_protocols[i-offset] == protocol)
|
||||
{
|
||||
delete m_protocols[i];
|
||||
m_protocols.erase(m_protocols.begin()+(i-offset), m_protocols.begin()+(i-offset)+1);
|
||||
offset++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ProtocolManager::update()
|
||||
{
|
||||
@ -49,3 +63,9 @@ void ProtocolManager::update()
|
||||
m_protocols[i]->update();
|
||||
}
|
||||
}
|
||||
|
||||
int ProtocolManager::runningProtocolsCount()
|
||||
{
|
||||
return m_protocols.size();
|
||||
}
|
||||
|
||||
|
@ -1,9 +1,12 @@
|
||||
#ifndef PROTOCOL_MANAGER_HPP
|
||||
#define PROTOCOL_MANAGER_HPP
|
||||
|
||||
#include "protocol_listener.hpp"
|
||||
#include <vector>
|
||||
#include <stdint.h>
|
||||
|
||||
class ProtocolManager : public ProtocolListener
|
||||
class Protocol;
|
||||
|
||||
class ProtocolManager
|
||||
{
|
||||
public:
|
||||
ProtocolManager();
|
||||
@ -13,9 +16,14 @@ class ProtocolManager : public ProtocolListener
|
||||
|
||||
virtual void runProtocol(Protocol* protocol);
|
||||
virtual void stopProtocol(Protocol* protocol);
|
||||
|
||||
virtual void protocolTerminated(Protocol* protocol);
|
||||
|
||||
virtual void update();
|
||||
|
||||
virtual int runningProtocolsCount();
|
||||
|
||||
protected:
|
||||
std::vector<Protocol*> m_protocols;
|
||||
std::vector<uint8_t*> m_messagesToProcess;
|
||||
};
|
||||
|
||||
|
@ -29,12 +29,7 @@ GetPublicAddress::~GetPublicAddress()
|
||||
|
||||
void GetPublicAddress::messageReceived(uint8_t* data)
|
||||
{
|
||||
assert(data);
|
||||
if (m_state == TEST_SENT && sizeof(data) >= 20)
|
||||
{
|
||||
|
||||
m_state = ADDRESS_KNOWN;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void GetPublicAddress::setup()
|
||||
@ -45,7 +40,7 @@ void GetPublicAddress::setup()
|
||||
void GetPublicAddress::start()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
void GetPublicAddress::update()
|
||||
{
|
||||
if (m_state == NOTHING_DONE)
|
||||
@ -85,14 +80,19 @@ void GetPublicAddress::update()
|
||||
bytes[17] = (uint8_t)(m_stunTransactionID[2]>>16);
|
||||
bytes[18] = (uint8_t)(m_stunTransactionID[2]>>8);
|
||||
bytes[19] = (uint8_t)(m_stunTransactionID[2]);
|
||||
bytes[20] = '\0';
|
||||
bytes[20] = '\0';
|
||||
|
||||
unsigned int dst = 132*256*256*256+177*256*256+123*256+6;
|
||||
NetworkManager::setManualSocketsMode(true);
|
||||
NetworkManager::sendRawPacket(bytes, 20, dst, 3478);
|
||||
m_state = TEST_SENT;
|
||||
|
||||
}
|
||||
if (m_state == TEST_SENT)
|
||||
{
|
||||
uint8_t* data = NetworkManager::receiveRawPacket();
|
||||
NetworkManager::setManualSocketsMode(false);
|
||||
assert(data);
|
||||
|
||||
// check that the stun response is a response, contains the magic cookie and the transaction ID
|
||||
if ( data[0] == 0b01 &&
|
||||
data[1] == 0b01 &&
|
||||
@ -113,9 +113,17 @@ void GetPublicAddress::update()
|
||||
data[18] == (uint8_t)(m_stunTransactionID[2]>>8 ) &&
|
||||
data[19] == (uint8_t)(m_stunTransactionID[2] ))
|
||||
{
|
||||
printf("the stun server reponded a valid answer\n");
|
||||
printf("the stun server responded with a valid answer\n");
|
||||
int messageSize = data[2]*256+data[3];
|
||||
printf("the answer is %i bytes long\n", messageSize);
|
||||
|
||||
// parse the stun message now:
|
||||
}
|
||||
m_state = ADDRESS_KNOWN;
|
||||
}
|
||||
if (m_state == ADDRESS_KNOWN)
|
||||
{
|
||||
// return the information and terminate the protocol
|
||||
m_listener->protocolTerminated(this);
|
||||
}
|
||||
}
|
||||
|
@ -10,7 +10,7 @@ class GetPublicAddress : public Protocol
|
||||
virtual ~GetPublicAddress();
|
||||
|
||||
virtual void messageReceived(uint8_t* data);
|
||||
|
||||
|
||||
virtual void setup();
|
||||
virtual void start();
|
||||
virtual void update();
|
||||
|
Loading…
Reference in New Issue
Block a user