1
0
mirror of https://github.com/wolfpld/tracy.git synced 2025-03-20 07:40:02 +08:00

Add support for TRACY_ONLY_IPV4 macro to exclude listening on IPv6

This commit is contained in:
Graydon Hoare
2020-08-27 08:17:55 -07:00
parent b5f76f2cea
commit 30a6a5cdd1
2 changed files with 18 additions and 12 deletions
+16 -12
View File
@@ -386,33 +386,37 @@ ListenSocket::~ListenSocket()
if( m_sock != -1 ) Close();
}
bool ListenSocket::Listen( int port, int backlog )
static int addrinfo_and_socket_for_family(int port, int ai_family, struct addrinfo** res)
{
assert( m_sock == -1 );
struct addrinfo* res;
struct addrinfo hints;
memset( &hints, 0, sizeof( hints ) );
hints.ai_family = AF_INET6;
hints.ai_family = ai_family;
hints.ai_socktype = SOCK_STREAM;
#ifndef TRACY_ONLY_LOCALHOST
hints.ai_flags = AI_PASSIVE;
#endif
char portbuf[32];
sprintf( portbuf, "%i", port );
if( getaddrinfo( nullptr, portbuf, &hints, res ) != 0 ) return -1;
int sock = socket( (*res)->ai_family, (*res)->ai_socktype, (*res)->ai_protocol );
if (sock == -1) freeaddrinfo( *res );
return sock;
}
if( getaddrinfo( nullptr, portbuf, &hints, &res ) != 0 ) return false;
bool ListenSocket::Listen( int port, int backlog )
{
assert( m_sock == -1 );
m_sock = socket( res->ai_family, res->ai_socktype, res->ai_protocol );
struct addrinfo* res = nullptr;
#ifndef TRACY_ONLY_IPV4
m_sock = addrinfo_and_socket_for_family(port, AF_INET6, &res);
#endif
if (m_sock == -1)
{
// IPV6 protocol may not be available/is disabled. Try to create a socket
// with the IPV4 protocol
hints.ai_family = AF_INET;
if( getaddrinfo( nullptr, portbuf, &hints, &res ) != 0 ) return false;
m_sock = socket( res->ai_family, res->ai_socktype, res->ai_protocol );
m_sock = addrinfo_and_socket_for_family(port, AF_INET, &res);
if( m_sock == -1 ) return false;
}
#if defined _WIN32 || defined __CYGWIN__