Remove unused protocol

This commit is contained in:
Benau 2018-02-22 15:14:15 +08:00
parent 1458f3ef8e
commit 4559fd6a2c
7 changed files with 0 additions and 431 deletions

View File

@ -215,7 +215,6 @@
#include "network/rewind_queue.hpp"
#include "network/servers_manager.hpp"
#include "network/stk_host.hpp"
#include "network/protocols/get_public_address.hpp"
#include "online/profile_manager.hpp"
#include "online/request_manager.hpp"
#include "race/grand_prix_manager.hpp"

View File

@ -20,11 +20,9 @@
#include "network/event.hpp"
#include "network/network_config.hpp"
#include "network/protocols/get_public_address.hpp"
#include "network/protocols/get_peer_address.hpp"
#include "network/protocols/hide_public_address.hpp"
#include "network/protocols/request_connection.hpp"
#include "network/protocols/ping_protocol.hpp"
#include "network/protocol_manager.hpp"
#include "network/stk_host.hpp"
#include "utils/time.hpp"

View File

@ -1,258 +0,0 @@
//
// SuperTuxKart - a fun racing game with go-kart
// Copyright (C) 2013-2015 SuperTuxKart-Team
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 3
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "network/protocols/get_public_address.hpp"
#include "config/user_config.hpp"
#include "guiengine/message_queue.hpp"
#include "network/network.hpp"
#include "network/network_config.hpp"
#include "network/network_string.hpp"
#include "network/protocols/connect_to_server.hpp"
#include "network/stk_host.hpp"
#include "utils/log.hpp"
#include "utils/string_utils.hpp"
#include <assert.h>
#include <algorithm>
#include <random>
#include <string>
#ifdef __MINGW32__
# undef _WIN32_WINNT
# define _WIN32_WINNT 0x501
#endif
#ifdef WIN32
# include <winsock2.h>
# include <ws2tcpip.h>
#else
# include <netdb.h>
# include <sys/socket.h>
#endif
#include <sys/types.h>
// make the linker happy
const uint32_t GetPublicAddress::m_stun_magic_cookie = 0x2112A442;
TransportAddress GetPublicAddress::m_my_address(0, 0);
// ============================================================================
GetPublicAddress::GetPublicAddress()
: Protocol(PROTOCOL_SILENT, NULL)
{
m_untried_server = UserConfigParams::m_stun_servers;
// Generate random list of stun servers
std::random_device rd;
std::mt19937 g(rd());
std::shuffle(m_untried_server.begin(), m_untried_server.end(), g);
} // GetPublicAddress
// ----------------------------------------------------------------------------
/** Creates a STUN request and sends it to a random STUN server selected from
* the list stored in the config file. See
* https://tools.ietf.org/html/rfc5389#section-6
* for details on the message structure.
* The request is send through transaction_host, from which the answer
* will be retrieved by parseStunResponse()
*/
Network* GetPublicAddress::createStunRequest()
{
if (m_untried_server.empty())
{
// Notice: MessageQueue is thread safe to add
MessageQueue::add(MessageQueue::MT_ERROR,
_("Failed to get public address from stun server."));
requestTerminate();
return NULL;
}
// Pick last element in untried servers
const char* server_name = m_untried_server.back().c_str();
Log::debug("GetPublicAddress", "Using STUN server %s", server_name);
struct addrinfo hints, *res;
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC; // AF_INET or AF_INET6 to force version
hints.ai_socktype = SOCK_STREAM;
// Resolve the stun server name so we can send it a STUN request
int status = getaddrinfo(server_name, NULL, &hints, &res);
if (status != 0)
{
Log::error("GetPublicAddress", "Error in getaddrinfo for stun server"
" %s: %s", server_name, gai_strerror(status));
m_untried_server.pop_back();
return NULL;
}
m_untried_server.pop_back();
// documentation says it points to "one or more addrinfo structures"
assert(res != NULL);
struct sockaddr_in* current_interface = (struct sockaddr_in*)(res->ai_addr);
m_stun_server_ip = ntohl(current_interface->sin_addr.s_addr);
// Create a new socket for the stun server.
ENetAddress addr;
addr.host = STKHost::HOST_ANY;
addr.port = STKHost::PORT_ANY;
Network* transaction_host = new Network(1, 1, 0, 0, &addr);
// Assemble the message for the stun server
BareNetworkString s(20);
// bytes 0-1: the type of the message
// bytes 2-3: message length added to header (attributes)
uint16_t message_type = 0x0001; // binding request
uint16_t message_length = 0x0000;
s.addUInt16(message_type).addUInt16(message_length)
.addUInt32(0x2112A442);
// bytes 8-19: the transaction id
for (int i = 0; i < 12; i++)
{
uint8_t random_byte = rand() % 256;
s.addUInt8(random_byte);
m_stun_tansaction_id[i] = random_byte;
}
transaction_host->sendRawPacket(s,
TransportAddress(m_stun_server_ip,
m_stun_server_port) );
freeaddrinfo(res);
return transaction_host;
} // createStunRequest
// ----------------------------------------------------------------------------
/**
* Gets the response from the STUN server, checks it for its validity and
* then parses the answer into address and port
* \return "" if the address could be parsed or an error message
*/
std::string GetPublicAddress::parseStunResponse(Network* transaction_host)
{
TransportAddress sender;
const int LEN = 2048;
char buffer[LEN];
int len = transaction_host->receiveRawPacket(buffer, LEN, &sender, 2000);
delete transaction_host;
if(sender.getIP()!=m_stun_server_ip)
{
TransportAddress stun(m_stun_server_ip, m_stun_server_port);
Log::warn("GetPublicAddress",
"Received stun response from %s instead of %s.",
sender.toString().c_str(), stun.toString().c_str());
}
if (len<0)
return "STUN response contains no data at all";
// Convert to network string.
BareNetworkString datas(buffer, len);
// check that the stun response is a response, contains the magic cookie
// and the transaction ID
if (datas.getUInt16() != 0x0101)
return "STUN response doesn't contain the magic cookie";
int message_size = datas.getUInt16();
if (datas.getUInt32() != m_stun_magic_cookie)
{
return "STUN response doesn't contain the magic cookie";
}
for (int i = 0; i < 12; i++)
{
if (datas.getUInt8() != m_stun_tansaction_id[i])
return "STUN response doesn't contain the transaction ID";
}
Log::debug("GetPublicAddress",
"The STUN server responded with a valid answer");
// The stun message is valid, so we parse it now:
if (message_size == 0)
return "STUN response does not contain any information.";
if (message_size < 4) // cannot even read the size
return "STUN response is too short.";
// Those are the port and the address to be detected
while (true)
{
int type = datas.getUInt16();
int size = datas.getUInt16();
if (type == 0 || type == 1)
{
assert(size == 8);
datas.getUInt8(); // skip 1 byte
assert(datas.getUInt8() == 0x01); // Family IPv4 only
uint16_t port = datas.getUInt16();
uint32_t ip = datas.getUInt32();
TransportAddress address(ip, port);
// finished parsing, we know our public transport address
Log::debug("GetPublicAddress",
"The public address has been found: %s",
address.toString().c_str());
NetworkConfig::get()->setMyAddress(address);
break;
} // type = 0 or 1
datas.skip(4 + size);
message_size -= 4 + size;
if (message_size == 0)
return "STUN response is invalid.";
if (message_size < 4) // cannot even read the size
return "STUN response is invalid.";
} // while true
return "";
} // parseStunResponse
// ----------------------------------------------------------------------------
/** Detects public IP-address and port by first sending a request to a randomly
* selected STUN server and then parsing and validating the response */
void GetPublicAddress::asynchronousUpdate()
{
// If the user has specified an address, use it instead of the stun protocol.
if (m_my_address.getIP() != 0 && m_my_address.getPort() != 0)
{
NetworkConfig::get()->setMyAddress(m_my_address);
requestTerminate();
}
//#define LAN_TEST
#ifdef LAN_TEST
TransportAddress address(0x7f000001, 4);
NetworkConfig::get()->setMyAddress(address);
requestTerminate();
return;
#endif
Network* transaction_host = createStunRequest();
if (transaction_host)
{
std::string message = parseStunResponse(transaction_host);
if (message != "")
{
Log::warn("GetPublicAddress", "%s", message.c_str());
}
else
{
// The address and the port are known, so the connection can be closed
requestTerminate();
}
}
} // asynchronousUpdate

View File

@ -1,68 +0,0 @@
//
// SuperTuxKart - a fun racing game with go-kart
// Copyright (C) 2013-2015 SuperTuxKart-Team
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 3
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#ifndef GET_PUBLIC_ADDRESS_HPP
#define GET_PUBLIC_ADDRESS_HPP
#include "network/protocol.hpp"
#include "network/transport_address.hpp"
#include "utils/cpp2011.hpp"
#include <string>
class Network;
class GetPublicAddress : public Protocol
{
private:
Network* createStunRequest();
std::string parseStunResponse(Network* transaction_host);
std::vector<std::string> m_untried_server;
// Constants
static const uint32_t m_stun_magic_cookie;
static const int m_stun_server_port = 3478;
/** The user can specify its own IP address to make the use of stun
* unnecessary (though that means that the user has to take care of
* opening the firewall). */
static TransportAddress m_my_address;
uint8_t m_stun_tansaction_id[12];
uint32_t m_stun_server_ip;
Network* m_transaction_host;
public:
GetPublicAddress();
virtual ~GetPublicAddress() {}
virtual void asynchronousUpdate() OVERRIDE;
// ------------------------------------------------------------------------
virtual void update(float dt) OVERRIDE {}
// ------------------------------------------------------------------------
virtual bool notifyEvent(Event* event) OVERRIDE { return true; }
// ------------------------------------------------------------------------
virtual bool notifyEventAsynchronous(Event* event) OVERRIDE { return true; }
// ------------------------------------------------------------------------
virtual void setup() { }
// ------------------------------------------------------------------------
}; // class GetPublicAddress
#endif // GET_PUBLIC_ADDRESS_HPP

View File

@ -1,63 +0,0 @@
//
// SuperTuxKart - a fun racing game with go-kart
// Copyright (C) 2013-2015 SuperTuxKart-Team
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 3
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "network/protocols/ping_protocol.hpp"
#include "network/stk_host.hpp"
#include "utils/time.hpp"
/** Constructor. Stores the destination address and how often to ping.
* \param ping_dest: Destination of ping request.
* \param delay_between_pings: How often to ping.
*/
PingProtocol::PingProtocol(const TransportAddress& ping_dst,
double delay_between_pings, double timeout)
: Protocol(PROTOCOL_SILENT)
{
m_ping_dst.copy(ping_dst);
m_delay_between_pings = delay_between_pings;
m_timeout = timeout;
} // PingProtocol
// ----------------------------------------------------------------------------
PingProtocol::~PingProtocol()
{
} // ~PingProtocol
// ----------------------------------------------------------------------------
void PingProtocol::setup()
{
m_last_ping_time = 0.0;
} // setup
// ----------------------------------------------------------------------------
void PingProtocol::asynchronousUpdate()
{
if (StkTime::getRealTime() > m_last_ping_time+m_delay_between_pings)
{
if (m_last_ping_time == 0.0)
m_timeout = StkTime::getRealTime() + m_timeout;
else if (StkTime::getRealTime() > m_timeout)
requestTerminate();
m_last_ping_time = StkTime::getRealTime();
BareNetworkString data;
data.addUInt8(0);
STKHost::get()->sendRawPacket(data, m_ping_dst);
Log::info("PingProtocol", "Ping message sent");
}
} // asynchronousUpdate

View File

@ -1,38 +0,0 @@
#ifndef PING_PROTOCOL_HPP
#define PING_PROTOCOL_HPP
#include "network/protocol.hpp"
#include "network/transport_address.hpp"
#include "utils/cpp2011.hpp"
class PingProtocol : public Protocol
{
private:
/** The destination for the ping request. */
TransportAddress m_ping_dst;
/** How frequently to ping. */
double m_delay_between_pings;
/** Time of last ping. */
double m_last_ping_time;
/** If longer than this, terminate this protocol. */
double m_timeout;
public:
PingProtocol(const TransportAddress& ping_dst,
double delay_between_pings, double timeout = 60.0);
virtual ~PingProtocol();
virtual void asynchronousUpdate() OVERRIDE;
virtual void setup() OVERRIDE;
// ------------------------------------------------------------------------
virtual bool notifyEvent(Event* event) OVERRIDE { return true; }
// ------------------------------------------------------------------------
virtual bool notifyEventAsynchronous(Event* event) OVERRIDE { return true; }
// ------------------------------------------------------------------------
virtual void update(float dt) OVERRIDE {}
};
#endif // PING_PROTOCOL_HPP

View File

@ -25,7 +25,6 @@
#include "network/event.hpp"
#include "network/network_config.hpp"
#include "network/network_player_profile.hpp"
#include "network/protocols/get_public_address.hpp"
#include "network/protocols/connect_to_peer.hpp"
#include "network/protocols/latency_protocol.hpp"
#include "network/protocol_manager.hpp"