From bf9e966e2d7f0edc00de1b52c5b1fa2ff794f945 Mon Sep 17 00:00:00 2001 From: Bram Date: Thu, 4 Apr 2024 12:59:54 +0200 Subject: [PATCH] Use correct addrlen when connecting --- darkice/trunk/src/TcpSocket.cpp | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/darkice/trunk/src/TcpSocket.cpp b/darkice/trunk/src/TcpSocket.cpp index 2f7ceab..78620ae 100644 --- a/darkice/trunk/src/TcpSocket.cpp +++ b/darkice/trunk/src/TcpSocket.cpp @@ -225,7 +225,7 @@ TcpSocket :: open ( void ) sockfd = 0; throw Exception( __FILE__, __LINE__, "gethostbyname error", errno); } - + memset( &addr, 0, sizeof(addr)); addr.sin_family = AF_INET; addr.sin_port = htons(port); @@ -240,12 +240,25 @@ TcpSocket :: open ( void ) // set TCP keep-alive optval = 1; optlen = sizeof(optval); - if (setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, &optval, optlen) == -1) { + if (setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, &optval, optlen) == -1) { reportEvent(5, "can't set TCP socket keep-alive mode", errno); } // connect - if ( connect( sockfd, (struct sockaddr*)&addr, sizeof(addr)) == -1 ) { + socklen_t addrlen; + switch (addr.ss_family) { + case AF_INET: + addrlen = sizeof(struct sockaddr_in); + break; + case AF_INET6: + addrlen = sizeof(struct sockaddr_in6); + break; + default: + throw Exception( __FILE__, __LINE__, "invalid address family", errno); + } + + + if ( connect( sockfd, (struct sockaddr*)&addr, addrlen) == -1 ) { ::close( sockfd); sockfd = 0; throw Exception( __FILE__, __LINE__, "connect error", errno);