From: Adam Tkac Date: Thu, 12 Nov 2009 10:32:43 +0000 (+0000) Subject: [Bugfix] Fix crash in TcpSocket::sameMachine for IPv6 connections (alan dot coopersmi... X-Git-Tag: v1.0.1~11 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=7f7e583620445a4bf00a7ac2b1e284151dfdc6e7;p=tigervnc.git [Bugfix] Fix crash in TcpSocket::sameMachine for IPv6 connections (alan dot coopersmith at sun dot com) git-svn-id: svn://svn.code.sf.net/p/tigervnc/code/branches/1_0@3918 3789f03b-4d11-0410-bbf8-ca57d06f2519 --- diff --git a/common/network/TcpSocket.cxx b/common/network/TcpSocket.cxx index 90431fd2..afdc7d2f 100644 --- a/common/network/TcpSocket.cxx +++ b/common/network/TcpSocket.cxx @@ -293,13 +293,27 @@ char* TcpSocket::getPeerEndpoint() { } bool TcpSocket::sameMachine() { - struct sockaddr_in peeraddr, myaddr; - socklen_t addrlen = sizeof(struct sockaddr_in); + vnc_sockaddr_t peeraddr, myaddr; + socklen_t addrlen; - getpeername(getFd(), (struct sockaddr *)&peeraddr, &addrlen); - getsockname(getFd(), (struct sockaddr *)&myaddr, &addrlen); + addrlen = sizeof(peeraddr); + if (getpeername(getFd(), &peeraddr.u.sa, &addrlen) < 0) + throw SocketException ("unable to get peer address", errorNumber); - return (peeraddr.sin_addr.s_addr == myaddr.sin_addr.s_addr); + addrlen = sizeof(myaddr); /* need to reset, since getpeername overwrote */ + if (getsockname(getFd(), &myaddr.u.sa, &addrlen) < 0) + throw SocketException ("unable to get my address", errorNumber); + + if (peeraddr.u.sa.sa_family != myaddr.u.sa.sa_family) + return false; + +#ifdef HAVE_GETADDRINFO + if (peeraddr.u.sa.sa_family == AF_INET6) + return IN6_ARE_ADDR_EQUAL(&peeraddr.u.sin6.sin6_addr, + &myaddr.u.sin6.sin6_addr); +#endif + + return (peeraddr.u.sin.sin_addr.s_addr == myaddr.u.sin.sin_addr.s_addr); } void TcpSocket::shutdown()