21#ifndef HEADER_STUN_DETECTION
22#define HEADER_STUN_DETECTION
24#include "network/socket_address.hpp"
31# include <sys/socket.h>
48 std::atomic_bool m_connected;
51 std::atomic_bool m_socket_closed;
54 StunDetection(
const std::string& addr,
bool ipv4)
57 m_socket_closed =
true;
59 ipv4 ? AF_INET : AF_INET6);
60 if (sa.isUnset() || (ipv4 && sa.getFamily() != AF_INET) ||
61 (!ipv4 && sa.getFamily() != AF_INET6))
63 m_socket = socket(ipv4? AF_INET : AF_INET6, SOCK_STREAM, 0);
65 if (m_socket == INVALID_SOCKET)
71 m_socket_closed =
false;
72 m_thread = std::thread([addr, ipv4, sa,
this]()
75 if (connect(m_socket, sa.getSockaddr(), sa.getSocklen()) == -1)
76 m_connected.store(
false);
78 m_connected.store(
true);
79 shutdown(m_socket, 2);
81 closesocket(m_socket);
85 m_socket_closed.store(
true);
86 Log::debug(
"StunDetection",
"Took %dms for %s.",
88 (addr + (ipv4 ?
" (IPv4)" :
" (IPv6)")).c_str());
94 if (m_thread.joinable())
104 {
return m_thread.joinable() && m_socket_closed ==
false; }
106 bool connectionSucceeded() {
return m_connected ==
true; }
108 bool socketClosed() {
return m_socket_closed ==
true; }
Describes a IPv4 or IPv6 address in sockaddr_in(6) format, suitable in using with sendto.
Definition: socket_address.hpp:47
static uint64_t getMonoTimeMs()
Returns a time based since the starting of stk (monotonic clock).
Definition: time.hpp:106