Changeset View
Changeset View
Standalone View
Standalone View
source/network/StunClient.cpp
Show All 13 Lines | |||||||||||||||||
* | * | ||||||||||||||||
* You should have received a copy of the GNU General Public License | * You should have received a copy of the GNU General Public License | ||||||||||||||||
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. | * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. | ||||||||||||||||
*/ | */ | ||||||||||||||||
#include "precompiled.h" | #include "precompiled.h" | ||||||||||||||||
#include "StunClient.h" | #include "StunClient.h" | ||||||||||||||||
#include "scriptinterface/ScriptInterface.h" | |||||||||||||||||
#include <chrono> | #include <chrono> | ||||||||||||||||
#include <cstdio> | #include <cstdio> | ||||||||||||||||
#include <stdlib.h> | #include <stdlib.h> | ||||||||||||||||
#include <string.h> | #include <string.h> | ||||||||||||||||
#include <sys/types.h> | #include <sys/types.h> | ||||||||||||||||
Show All 11 Lines | |||||||||||||||||
#if OS_WIN | #if OS_WIN | ||||||||||||||||
#include "lib/sysdep/os/win/wposix/wtime.h" | #include "lib/sysdep/os/win/wposix/wtime.h" | ||||||||||||||||
#endif | #endif | ||||||||||||||||
#include "scriptinterface/ScriptInterface.h" | #include "scriptinterface/ScriptInterface.h" | ||||||||||||||||
#include "ps/CLogger.h" | #include "ps/CLogger.h" | ||||||||||||||||
#include "ps/ConfigDB.h" | #include "ps/ConfigDB.h" | ||||||||||||||||
#include "ps/CStr.h" | |||||||||||||||||
unsigned int m_StunServerIP; | unsigned int m_StunServerIP; | ||||||||||||||||
int m_StunServerPort; | int m_StunServerPort; | ||||||||||||||||
/** | /** | ||||||||||||||||
* These constants are defined in Section 6 of RFC 5389. | * These constants are defined in Section 6 of RFC 5389. | ||||||||||||||||
*/ | */ | ||||||||||||||||
const u32 m_MagicCookie = 0x2112A442; | const u32 m_MagicCookie = 0x2112A442; | ||||||||||||||||
▲ Show 20 Lines • Show All 300 Lines • ▼ Show 20 Lines | bool ParseStunResponse(const std::vector<u8>& buffer) | ||||||||||||||||
} | } | ||||||||||||||||
return true; | return true; | ||||||||||||||||
} | } | ||||||||||||||||
bool STUNRequestAndResponse(ENetHost& transactionHost) | bool STUNRequestAndResponse(ENetHost& transactionHost) | ||||||||||||||||
{ | { | ||||||||||||||||
if (!CreateStunRequest(transactionHost)) | if (!CreateStunRequest(transactionHost)) | ||||||||||||||||
return false; | return false; | ||||||||||||||||
Silier: nuke | |||||||||||||||||
std::vector<u8> buffer; | std::vector<u8> buffer; | ||||||||||||||||
return ReceiveStunResponse(transactionHost, buffer) && | return ReceiveStunResponse(transactionHost, buffer) && | ||||||||||||||||
ParseStunResponse(buffer); | ParseStunResponse(buffer); | ||||||||||||||||
} | } | ||||||||||||||||
JS::Value StunClient::FindStunEndpointHost(const ScriptInterface& scriptInterface, int port) | bool StunClient::GetPublicIp(CStr8& ip, u16 port) | ||||||||||||||||
Done Inline Actionskind of wrong, we are interested just in ip Silier: kind of wrong, we are interested just in ip | |||||||||||||||||
Done Inline ActionsDo we need two methods? Stan: Do we need two methods? | |||||||||||||||||
{ | |||||||||||||||||
return FindStunEndpointHost(ip, port); | |||||||||||||||||
} | |||||||||||||||||
bool StunClient::FindStunEndpointHost(CStr8& ip, u16& port) | |||||||||||||||||
{ | { | ||||||||||||||||
Done Inline Actionsnuke Silier: nuke | |||||||||||||||||
ENetAddress hostAddr{ENET_HOST_ANY, static_cast<u16>(port)}; | ENetAddress hostAddr{ENET_HOST_ANY, static_cast<u16>(port)}; | ||||||||||||||||
ENetHost* transactionHost = enet_host_create(&hostAddr, 1, 1, 0, 0); | ENetHost* transactionHost = enet_host_create(&hostAddr, 1, 1, 0, 0); | ||||||||||||||||
if (!transactionHost) | if (!transactionHost) | ||||||||||||||||
{ | return false; | ||||||||||||||||
LOGERROR("FindStunEndpointHost: Failed to create enet host"); | |||||||||||||||||
return JS::UndefinedValue(); | |||||||||||||||||
} | |||||||||||||||||
bool success = STUNRequestAndResponse(*transactionHost); | bool success = STUNRequestAndResponse(*transactionHost); | ||||||||||||||||
enet_host_destroy(transactionHost); | enet_host_destroy(transactionHost); | ||||||||||||||||
if (!success) | if (!success) | ||||||||||||||||
return JS::UndefinedValue(); | return false; | ||||||||||||||||
Done Inline Actionsrevert Silier: revert | |||||||||||||||||
// Convert m_IP to string | // Convert m_IP to string | ||||||||||||||||
char ipStr[256] = "(error)"; | char ipStr[256] = "(error)"; | ||||||||||||||||
ENetAddress addr; | ENetAddress addr; | ||||||||||||||||
addr.host = ntohl(m_IP); | addr.host = ntohl(m_IP); | ||||||||||||||||
enet_address_get_host_ip(&addr, ipStr, ARRAY_SIZE(ipStr)); | enet_address_get_host_ip(&addr, ipStr, ARRAY_SIZE(ipStr)); | ||||||||||||||||
ScriptRequest rq(scriptInterface); | ip = ipStr; | ||||||||||||||||
port = m_Port; | |||||||||||||||||
JS::RootedValue stunEndpoint(rq.cx); | return ip != "(error)"; | ||||||||||||||||
vladislavbelovUnsubmitted Not Done Inline ActionsIt's better to check enet_address_get_host_ip for success. vladislavbelov: It's better to check `enet_address_get_host_ip` for success. | |||||||||||||||||
ScriptInterface::CreateObject(rq, &stunEndpoint, "ip", ipStr, "port", m_Port); | |||||||||||||||||
return stunEndpoint; | |||||||||||||||||
} | } | ||||||||||||||||
Not Done Inline Actions
/** Gives the printable form of the IP address specified in the address parameter. @param address address printed @param hostName destination for name, must not be NULL @param nameLength maximum length of hostName. @returns the null-terminated name of the host in hostName on success @retval 0 on success @retval < 0 on failure */ ENET_API int enet_address_get_host_ip (const ENetAddress * address, char * hostName, size_t nameLength); Stan: ```lang=cpp
/** Gives the printable form of the IP address specified in the address parameter. | |||||||||||||||||
bool StunClient::FindStunEndpointJoin(ENetHost& transactionHost, StunClient::StunEndpoint& stunEndpoint) | bool StunClient::FindStunEndpointJoin(ENetHost& transactionHost, StunClient::StunEndpoint& stunEndpoint) | ||||||||||||||||
{ | { | ||||||||||||||||
if (!STUNRequestAndResponse(transactionHost)) | if (!STUNRequestAndResponse(transactionHost)) | ||||||||||||||||
return false; | return false; | ||||||||||||||||
// Convert m_IP to string | // Convert m_IP to string | ||||||||||||||||
char ipStr[256] = "(error)"; | char ipStr[256] = "(error)"; | ||||||||||||||||
Show All 27 Lines |
Wildfire Games · Phabricator
nuke