| #include <cstring> |
| 6 |
| 7 | using namespace tgvoip; |
| 8 |
| 9 | IPv4Address::IPv4Address(std::string addr) |
| 10 | : addr(std::move(addr)) |
| 11 | { |
| 12 | } |
| 13 |
| 14 | IPv6Address::IPv6Address(std::string addr) |
| 15 | : addr(std::move(addr)) |
| 16 | { |
| 17 | } |
| 18 |
| 19 | Endpoint::Endpoint(std::int64_t id, std::uint16_t port, const IPv4Address& address, |
| 20 | const IPv6Address& v6address, Type type, const std::uint8_t peerTag[16]) |
| 21 | : id(id) |
| 22 | , address(NetworkAddress::IPv4(address.addr)) |
| 23 | , v6address(NetworkAddress::IPv6(v6address.addr)) |
| 24 | , type(type) |
| 25 | , port(port) |
| 26 | { |
| 27 | std::memcpy(this->peerTag, peerTag, 16); |
| 28 | if (type == Type::UDP_RELAY && ServerConfig::GetSharedInstance()->GetBoolean("force_tcp", false)) |
| 29 | this->type = Type::TCP_RELAY; |
| 30 | } |
| 31 |
| 32 | Endpoint::Endpoint(std::int64_t id, std::uint16_t port, const NetworkAddress& address, |
| 33 | const NetworkAddress& v6address, Type type, const std::uint8_t peerTag[16]) |
| 34 | : id(id) |
| 35 | , address(address) |
| 36 | , v6address(v6address) |
| 37 | , type(type) |
| 38 | , port(port) |
| 39 | { |
| 40 | std::memcpy(this->peerTag, peerTag, 16); |
| 41 | if (type == Type::UDP_RELAY && ServerConfig::GetSharedInstance()->GetBoolean("force_tcp", false)) |
| 42 | this->type = Type::TCP_RELAY; |
| 43 | } |
| 44 |
| 45 | Endpoint::Endpoint() |
| 46 | : address(NetworkAddress::Empty()) |
| 47 | , v6address(NetworkAddress::Empty()) |
| 48 | { |
| 49 | } |
| 50 |
| 51 | const NetworkAddress& Endpoint::GetAddress() const |
| 52 | { |
| 53 | return IsIPv6Only() ? v6address : address; |
| 54 | } |
| 55 |
| 56 | NetworkAddress& Endpoint::GetAddress() |
| 57 | { |
| 58 | return IsIPv6Only() ? v6address : address; |
| 59 | } |
| 60 |
| 61 | bool Endpoint::IsIPv6Only() const |
| 62 | { |
| 63 | return address.IsEmpty() && !v6address.IsEmpty(); |
| 64 | } |
| 65 |
| 66 | std::int64_t Endpoint::CleanID() const |
| 67 | { |
| 68 | std::int64_t _id = id; |
| 69 | if (type == Type::TCP_RELAY) |
| 70 | { |
| 71 | _id = _id ^ (static_cast<std::int64_t>(FOURCC('T', 'C', 'P', ' ')) << 32); |
| 72 | } |
| 73 | if (IsIPv6Only()) |
| 74 | { |
| 75 | _id = _id ^ (static_cast<std::int64_t>(FOURCC('I', 'P', 'v', '6')) << 32); |
| 76 | } |
| 77 | return _id; |
| 78 | } |
| 79 |
| 80 | Endpoint::~Endpoint() |
| 81 | { |
| 82 | if (m_socket) |
| 83 | { |
| 84 | m_socket->Close(); |
| 85 | } |
| 86 | } |
| 87 |