From 4201ebb28deb5042d84cd39dab9cba27e05ce56b Mon Sep 17 00:00:00 2001 From: Leander Beernaert Date: Thu, 9 Apr 2020 12:17:59 +0200 Subject: [PATCH 1/3] ListenSocket: Fallback to ipv4 If we can't create a listener socket with the ipv6 protocol, try to create one with the ipv4 protocol instead. This fixes the ListenSocket on machines where ipv6 is not available or has been completely disabled. This patch also exists ListenSocket::Listen() early if we fail to create the socket. --- common/TracySocket.cpp | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/common/TracySocket.cpp b/common/TracySocket.cpp index d51b3fea..a2461198 100644 --- a/common/TracySocket.cpp +++ b/common/TracySocket.cpp @@ -312,6 +312,16 @@ bool ListenSocket::Listen( int port, int backlog ) 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) { + // 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 ); + if (m_sock == -1) { + return false; + } #if defined _WIN32 || defined __CYGWIN__ unsigned long val = 0; setsockopt( m_sock, IPPROTO_IPV6, IPV6_V6ONLY, (const char*)&val, sizeof( val ) ); From 010376518fd635f02b97f722e680bef285f2b595 Mon Sep 17 00:00:00 2001 From: Leander Beernaert Date: Thu, 9 Apr 2020 12:27:54 +0200 Subject: [PATCH 2/3] Fix incorrect handling of ipv4 case --- common/TracySocket.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/common/TracySocket.cpp b/common/TracySocket.cpp index a2461198..a530102b 100644 --- a/common/TracySocket.cpp +++ b/common/TracySocket.cpp @@ -317,9 +317,11 @@ bool ListenSocket::Listen( int port, int backlog ) // 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 ); - if (m_sock == -1) { + m_sock = socket( res->ai_family, res->ai_socktype, res->ai_protocol ); + if (m_sock == -1) { + return false; + } + } else { return false; } #if defined _WIN32 || defined __CYGWIN__ From ac9f12a5f61a7cd38f6635c54873a2486113e178 Mon Sep 17 00:00:00 2001 From: Leander Beernaert Date: Thu, 9 Apr 2020 12:31:15 +0200 Subject: [PATCH 3/3] Review fixes Update bracket style. Remove erroneous else block. --- common/TracySocket.cpp | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/common/TracySocket.cpp b/common/TracySocket.cpp index a530102b..a6bcb275 100644 --- a/common/TracySocket.cpp +++ b/common/TracySocket.cpp @@ -312,17 +312,14 @@ bool ListenSocket::Listen( int port, int backlog ) 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 // 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 ); - if (m_sock == -1) { - return false; - } - } else { - return false; + if (m_sock == -1) return false; } #if defined _WIN32 || defined __CYGWIN__ unsigned long val = 0;