1
0
mirror of https://github.com/danog/libtgvoip.git synced 2024-12-11 16:49:52 +01:00
libtgvoip/controller/protocol/Endpoints.cpp

155 lines
4.6 KiB
C++
Raw Normal View History

2020-03-25 15:49:13 +01:00
#include "../../VoIPController.h"
2020-01-25 18:11:15 +01:00
using namespace tgvoip;
2020-03-25 15:49:13 +01:00
2020-01-25 18:11:15 +01:00
2020-01-25 20:45:43 +01:00
Endpoint &VoIPController::GetRemoteEndpoint()
2020-01-25 18:11:15 +01:00
{
2020-01-25 20:45:43 +01:00
return endpoints.at(currentEndpoint);
2020-01-25 18:11:15 +01:00
}
2020-03-24 12:15:04 +01:00
Endpoint *VoIPController::GetEndpointForPacket(const OutgoingPacket &pkt)
{
return GetEndpointById(pkt.endpoint);
}
2020-01-25 20:45:43 +01:00
Endpoint *VoIPController::GetEndpointForPacket(const PendingOutgoingPacket &pkt)
2020-01-25 18:11:15 +01:00
{
2020-01-29 15:52:43 +01:00
return GetEndpointById(pkt.endpoint);
}
Endpoint *VoIPController::GetEndpointById(const int64_t id)
{
if (id)
2020-01-25 18:11:15 +01:00
{
2020-01-25 20:45:43 +01:00
try
{
2020-01-29 15:52:43 +01:00
return &endpoints.at(id);
2020-01-25 20:45:43 +01:00
}
catch (out_of_range &x)
{
2020-01-29 15:52:43 +01:00
LOGW("Unable to send packet via nonexistent endpoint %" PRIu64, id);
2020-01-25 20:45:43 +01:00
return NULL;
}
2020-01-25 18:11:15 +01:00
}
2020-01-29 15:52:43 +01:00
return &endpoints.at(currentEndpoint);
2020-01-25 18:11:15 +01:00
}
2020-01-25 18:24:38 +01:00
int64_t VoIPController::GetPreferredRelayID()
2020-01-25 18:11:15 +01:00
{
2020-01-25 18:24:38 +01:00
return preferredRelay;
}
void VoIPController::SetRemoteEndpoints(vector<Endpoint> endpoints, bool allowP2p, int32_t connectionMaxLayer)
{
2020-03-26 17:42:34 +01:00
connectionMaxLayer = 110;
2020-01-25 18:24:38 +01:00
LOGW("Set remote endpoints, allowP2P=%d, connectionMaxLayer=%u", allowP2p ? 1 : 0, connectionMaxLayer);
assert(!runReceiver);
preferredRelay = 0;
this->endpoints.clear();
didAddTcpRelays = false;
useTCP = true;
for (auto it = endpoints.begin(); it != endpoints.end(); ++it)
{
if (this->endpoints.find(it->id) != this->endpoints.end())
LOGE("Endpoint IDs are not unique!");
this->endpoints[it->id] = *it;
if (currentEndpoint == 0)
currentEndpoint = it->id;
if (it->type == Endpoint::Type::UDP_RELAY)
useTCP = false;
else if (it->type == Endpoint::Type::TCP_RELAY)
didAddTcpRelays = true;
LOGV("Adding endpoint: %s:%d, %s", it->address.ToString().c_str(), it->port, it->type == Endpoint::Type::UDP_RELAY ? "UDP" : "TCP");
}
preferredRelay = currentEndpoint;
this->allowP2p = allowP2p;
2020-03-22 20:09:44 +01:00
ver.connectionMaxLayer = connectionMaxLayer;
2020-01-25 18:24:38 +01:00
if (connectionMaxLayer >= 74)
{
useMTProto2 = true;
}
AddIPv6Relays();
2020-01-25 18:11:15 +01:00
}
void VoIPController::AddIPv6Relays()
{
if (!myIPv6.IsEmpty() && !didAddIPv6Relays)
{
unordered_map<string, vector<Endpoint>> endpointsByAddress;
for (pair<const int64_t, Endpoint> &_e : endpoints)
{
Endpoint &e = _e.second;
if (e.IsReflector() && !e.v6address.IsEmpty() && !e.address.IsEmpty())
{
endpointsByAddress[e.v6address.ToString()].push_back(e);
}
}
MutexGuard m(endpointsMutex);
for (pair<const string, vector<Endpoint>> &addr : endpointsByAddress)
{
for (Endpoint &e : addr.second)
{
didAddIPv6Relays = true;
e.address = NetworkAddress::Empty();
e.id = e.id ^ (static_cast<int64_t>(FOURCC('I', 'P', 'v', '6')) << 32);
e.averageRTT = 0;
e.lastPingSeq = 0;
e.lastPingTime = 0;
e.rtts.Reset();
e.udpPongCount = 0;
endpoints[e.id] = e;
LOGD("Adding IPv6-only endpoint [%s]:%u", e.v6address.ToString().c_str(), e.port);
}
}
}
}
void VoIPController::AddTCPRelays()
{
if (!didAddTcpRelays)
{
bool wasSetCurrentToTCP = setCurrentEndpointToTCP;
LOGV("Adding TCP relays");
vector<Endpoint> relays;
for (pair<const int64_t, Endpoint> &_e : endpoints)
{
Endpoint &e = _e.second;
if (e.type != Endpoint::Type::UDP_RELAY)
continue;
if (wasSetCurrentToTCP && !useUDP)
{
e.rtts.Reset();
e.averageRTT = 0;
e.lastPingSeq = 0;
}
Endpoint tcpRelay(e);
tcpRelay.type = Endpoint::Type::TCP_RELAY;
tcpRelay.averageRTT = 0;
tcpRelay.lastPingSeq = 0;
tcpRelay.lastPingTime = 0;
tcpRelay.rtts.Reset();
tcpRelay.udpPongCount = 0;
tcpRelay.id = tcpRelay.id ^ (static_cast<int64_t>(FOURCC('T', 'C', 'P', 0)) << 32);
if (setCurrentEndpointToTCP && endpoints.at(currentEndpoint).type != Endpoint::Type::TCP_RELAY)
{
LOGV("Setting current endpoint to TCP");
setCurrentEndpointToTCP = false;
currentEndpoint = tcpRelay.id;
preferredRelay = tcpRelay.id;
}
relays.push_back(tcpRelay);
}
MutexGuard m(endpointsMutex);
for (Endpoint &e : relays)
{
endpoints[e.id] = e;
}
didAddTcpRelays = true;
}
}