Merge pull request #101 from graydon/add-only-ipv4-flag

Add support for TRACY_ONLY_IPV4 macro to exclude listening on IPv6
This commit is contained in:
Bartosz Taudul 2020-08-27 21:23:42 +02:00 committed by GitHub
commit 118a00d536
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 18 deletions

View File

@ -386,33 +386,45 @@ ListenSocket::~ListenSocket()
if( m_sock != -1 ) Close(); if( m_sock != -1 ) Close();
} }
static int addrinfo_and_socket_for_family(int port, int ai_family, struct addrinfo** res)
{
struct addrinfo hints;
memset( &hints, 0, sizeof( hints ) );
hints.ai_family = ai_family;
hints.ai_socktype = SOCK_STREAM;
#ifndef TRACY_ONLY_LOCALHOST
const char* onlyLocalhost = getenv( "TRACY_ONLY_LOCALHOST" );
if( !onlyLocalhost || onlyLocalhost[0] != '1' )
{
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;
}
bool ListenSocket::Listen( int port, int backlog ) bool ListenSocket::Listen( int port, int backlog )
{ {
assert( m_sock == -1 ); assert( m_sock == -1 );
struct addrinfo* res; struct addrinfo* res = nullptr;
struct addrinfo hints;
memset( &hints, 0, sizeof( hints ) ); #ifndef TRACY_ONLY_IPV4
hints.ai_family = AF_INET6; const char* onlyIPv4 = getenv( "TRACY_ONLY_IPV4" );
hints.ai_socktype = SOCK_STREAM; if( !onlyIPv4 || onlyIPv4[0] != '1' )
#ifndef TRACY_ONLY_LOCALHOST {
hints.ai_flags = AI_PASSIVE; m_sock = addrinfo_and_socket_for_family(port, AF_INET6, &res);
}
#endif #endif
char portbuf[32];
sprintf( portbuf, "%i", port );
if( getaddrinfo( nullptr, portbuf, &hints, &res ) != 0 ) return false;
m_sock = socket( res->ai_family, res->ai_socktype, res->ai_protocol );
if (m_sock == -1) if (m_sock == -1)
{ {
// IPV6 protocol may not be available/is disabled. Try to create a socket // IPV6 protocol may not be available/is disabled. Try to create a socket
// with the IPV4 protocol // with the IPV4 protocol
hints.ai_family = AF_INET; m_sock = addrinfo_and_socket_for_family(port, AF_INET, &res);
if( getaddrinfo( nullptr, portbuf, &hints, &res ) != 0 ) return false;
m_sock = socket( res->ai_family, res->ai_socktype, res->ai_protocol );
if( m_sock == -1 ) return false; if( m_sock == -1 ) return false;
} }
#if defined _WIN32 || defined __CYGWIN__ #if defined _WIN32 || defined __CYGWIN__

View File

@ -425,7 +425,9 @@ By default Tracy client will announce its presence to the local network\footnote
\subsubsection{Client network interface} \subsubsection{Client network interface}
By default Tracy client will listen on all network interfaces. If you want to restrict it to only listening on the localhost interface, define the \texttt{TRACY\_ONLY\_LOCALHOST} macro. By default Tracy client will listen on all network interfaces. If you want to restrict it to only listening on the localhost interface, define the \texttt{TRACY\_ONLY\_LOCALHOST} macro at compile time, or set the \texttt{TRACY\_ONLY\_LOCALHOST} environment variable to $1$ at runtime.
By default Tracy client will listen on IPv6 interfaces, falling back to IPv4 only if IPv6 is not available. If you want to restrict it to only listening on IPv4 interfaces, define the \texttt{TRACY\_ONLY\_IPV4} macro at compile time, or set the \texttt{TRACY\_ONLY\_IPV4} environment variable to $1$ at runtime.
\subsubsection{Setup for multi-DLL projects} \subsubsection{Setup for multi-DLL projects}