From 37ebdeca078fbfa7e43185ff5b4bdb9da629b08a Mon Sep 17 00:00:00 2001 From: Pierre Ossman Date: Mon, 8 Dec 2014 13:57:50 +0100 Subject: Revert IPv6 listening support There were too many problems found so drop this functionality on the stable branch. It will be revisited for the next release. This reverts commit 4561f7e9c63c6ca2859da9b9fa75f64a1a8c2a81. This reverts commit 6abf3f4c87a0309d5e7d436e4b72d26a08631ebf. This reverts commit 820c0ceb2ed92080e8d76d50b78ba01bd264baef. This reverts commit 698371a65002c8785c34481635044fddc217d3f1. This reverts commit 6ae42df65141ccb49c875bda45b8f5ae9fa60c28. This reverts commit f1665ac7fbff068ffc2c2f3f110645609b73806c. This reverts commit 14263e17e425d1bca7e99d4ddfca1ace3bae0ca7. This reverts commit 07cd2298dcbf7da8db22d630cb84c0216da30ea1. This reverts commit 8e101704c32c47654d7453f5bde42d8d0c8cc160. --- common/network/TcpSocket.cxx | 234 ++++++++----------------------------------- 1 file changed, 41 insertions(+), 193 deletions(-) (limited to 'common') diff --git a/common/network/TcpSocket.cxx b/common/network/TcpSocket.cxx index 0db6ff6b..d9e9376a 100644 --- a/common/network/TcpSocket.cxx +++ b/common/network/TcpSocket.cxx @@ -239,66 +239,27 @@ int TcpSocket::getMyPort() { } char* TcpSocket::getPeerAddress() { - vnc_sockaddr_t sa; - socklen_t sa_size = sizeof(sa); - - if (getpeername(getFd(), &sa.u.sa, &sa_size) != 0) { - vlog.error("unable to get peer name for socket"); - return rfb::strDup(""); - } - -#if defined(HAVE_GETADDRINFO) && defined(HAVE_INET_PTON) - if (sa.u.sa.sa_family == AF_INET6) { - char buffer[INET6_ADDRSTRLEN + 2]; - const char *name; - - buffer[0] = '['; + struct sockaddr_in info; + struct in_addr addr; + socklen_t info_size = sizeof(info); - name = inet_ntop(sa.u.sa.sa_family, &sa.u.sin6.sin6_addr, - buffer + 1, sizeof(buffer) - 2); - if (name == NULL) { - vlog.error("unable to convert peer name to a string"); - return rfb::strDup(""); - } - - strcat(buffer, "]"); - - return rfb::strDup(buffer); - } -#endif - - if (sa.u.sa.sa_family == AF_INET) { - char *name; - - name = inet_ntoa(sa.u.sin.sin_addr); - if (name == NULL) { - vlog.error("unable to convert peer name to a string"); - return rfb::strDup(""); - } + getpeername(getFd(), (struct sockaddr *)&info, &info_size); + memcpy(&addr, &info.sin_addr, sizeof(addr)); + char* name = inet_ntoa(addr); + if (name) { return rfb::strDup(name); + } else { + return rfb::strDup(""); } - - vlog.error("unknown address family for socket"); - return rfb::strDup(""); } int TcpSocket::getPeerPort() { - vnc_sockaddr_t sa; - socklen_t sa_size = sizeof(sa); - - getpeername(getFd(), &sa.u.sa, &sa_size); + struct sockaddr_in info; + socklen_t info_size = sizeof(info); - switch (sa.u.sa.sa_family) { -#ifdef HAVE_GETADDRINFO - case AF_INET6: - return ntohs(sa.u.sin6.sin6_port); -#endif /* HAVE_GETADDRINFO */ - case AF_INET: - return ntohs(sa.u.sin.sin_port); - default: - return 0; - } + getpeername(getFd(), (struct sockaddr *)&info, &info_size); + return ntohs(info.sin_port); } char* TcpSocket::getPeerEndpoint() { @@ -332,11 +293,7 @@ bool TcpSocket::sameMachine() { &myaddr.u.sin6.sin6_addr); #endif - if (peeraddr.u.sa.sa_family == AF_INET) - return (peeraddr.u.sin.sin_addr.s_addr == myaddr.u.sin.sin_addr.s_addr); - - // No idea what this is. Assume we're on different machines. - return false; + return (peeraddr.u.sin.sin_addr.s_addr == myaddr.u.sin.sin_addr.s_addr); } void TcpSocket::shutdown() @@ -369,34 +326,25 @@ bool TcpSocket::cork(int sock, bool enable) { bool TcpSocket::isSocket(int sock) { - vnc_sockaddr_t sa; - socklen_t sa_size = sizeof(sa); - return getsockname(sock, &sa.u.sa, &sa_size) >= 0; + struct sockaddr_in info; + socklen_t info_size = sizeof(info); + return getsockname(sock, (struct sockaddr *)&info, &info_size) >= 0; } bool TcpSocket::isConnected(int sock) { - vnc_sockaddr_t sa; - socklen_t sa_size = sizeof(sa); - return getpeername(sock, &sa.u.sa, &sa_size) >= 0; + struct sockaddr_in info; + socklen_t info_size = sizeof(info); + return getpeername(sock, (struct sockaddr *)&info, &info_size) >= 0; } int TcpSocket::getSockPort(int sock) { - vnc_sockaddr_t sa; - socklen_t sa_size = sizeof(sa); - if (getsockname(sock, &sa.u.sa, &sa_size) < 0) + struct sockaddr_in info; + socklen_t info_size = sizeof(info); + if (getsockname(sock, (struct sockaddr *)&info, &info_size) < 0) return 0; - - switch (sa.u.sa.sa_family) { -#ifdef HAVE_GETADDRINFO - case AF_INET6: - return ntohs(sa.u.sin6.sin6_port); -#endif /* HAVE_GETADDRINFO */ - - default: - return ntohs(sa.u.sin.sin_port); - } + return ntohs(info.sin_port); } @@ -408,40 +356,9 @@ TcpListener::TcpListener(const char *listenaddr, int port, bool localhostOnly, return; } - bool use_ipv6; - int af; -#ifdef HAVE_GETADDRINFO - use_ipv6 = true; - af = AF_INET6; -#else - use_ipv6 = false; - af = AF_INET; -#endif - initSockets(); - if ((fd = socket(af, SOCK_STREAM, 0)) < 0) { - // - Socket creation failed - if (use_ipv6) { - // - Trying to make an IPv6-capable socket failed - try again, IPv4-only - use_ipv6 = false; - af = AF_INET; - fd = socket(af, SOCK_STREAM, 0); - } - if (fd < 0) - throw SocketException("unable to create listening socket", errorNumber); - } else { - // - Socket creation succeeded - if (use_ipv6) { -#ifdef IPV6_V6ONLY - // - We made an IPv6-capable socket, and we need it to do IPv4 too - int opt = 0; - setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, (char *)&opt, sizeof(opt)); -#else - vlog.error("IPV6_V6ONLY support is missing. " - "IPv4 clients may not be able to connect."); -#endif - } - } + if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) + throw SocketException("unable to create listening socket", errorNumber); #ifndef WIN32 // - By default, close the socket on exec() @@ -458,62 +375,27 @@ TcpListener::TcpListener(const char *listenaddr, int port, bool localhostOnly, // - Bind it to the desired port struct sockaddr_in addr; -#ifdef HAVE_GETADDRINFO - struct sockaddr_in6 addr6; -#endif - struct sockaddr *sa; - int sa_len; - -#ifdef HAVE_GETADDRINFO - if (use_ipv6) { - memset(&addr6, 0, (sa_len = sizeof(addr6))); - addr6.sin6_family = af; - addr6.sin6_port = htons(port); - - if (localhostOnly) - addr6.sin6_addr = in6addr_loopback; - else if (listenaddr != NULL) { -#ifdef HAVE_INET_PTON - if (inet_pton(AF_INET6, listenaddr, &addr6.sin6_addr) != 1) - use_ipv6 = false; -#else - // Unable to parse without inet_pton - use_ipv6 = false; -#endif - } - - if (use_ipv6) - sa = (struct sockaddr *)&addr6; - } -#endif - - if (!use_ipv6) { - memset(&addr, 0, (sa_len = sizeof(addr))); - addr.sin_family = af; - addr.sin_port = htons(port); + memset(&addr, 0, sizeof(addr)); + addr.sin_family = AF_INET; - if (localhostOnly) { - addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); - } else if (listenaddr != NULL) { + if (localhostOnly) { + addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); + } else if (listenaddr != NULL) { #ifdef HAVE_INET_ATON - if (inet_aton(listenaddr, &addr.sin_addr) == 0) + if (inet_aton(listenaddr, &addr.sin_addr) == 0) #else - /* Some systems (e.g. Windows) do not have inet_aton, sigh */ - if ((addr.sin_addr.s_addr = inet_addr(listenaddr)) == INADDR_NONE) + /* Some systems (e.g. Windows) do not have inet_aton, sigh */ + if ((addr.sin_addr.s_addr = inet_addr(listenaddr)) == INADDR_NONE) #endif - { - closesocket(fd); - throw Exception("invalid network interface address: %s", listenaddr); - } - } else - /* Bind to 0.0.0.0 by default. */ - addr.sin_addr.s_addr = htonl(INADDR_ANY); - - sa = (struct sockaddr *)&addr; - } + { + closesocket(fd); + throw Exception("invalid network interface address: %s", listenaddr); + } + } else + addr.sin_addr.s_addr = htonl(INADDR_ANY); /* Bind to 0.0.0.0 by default. */ addr.sin_port = htons(port); - if (bind(fd, sa, sa_len) < 0) { + if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) { int e = errorNumber; closesocket(fd); throw SocketException("unable to bind listening socket", e); @@ -567,30 +449,6 @@ TcpListener::accept() { } void TcpListener::getMyAddresses(std::list* result) { -#if defined(HAVE_GETADDRINFO) && defined(HAVE_INET_PTON) - vnc_sockaddr_t sa; - struct addrinfo *ai, *current, hints; - - memset(&hints, 0, sizeof(struct addrinfo)); - hints.ai_family = AF_UNSPEC; - hints.ai_socktype = SOCK_STREAM; - hints.ai_canonname = NULL; - hints.ai_addr = NULL; - hints.ai_next = NULL; - - if ((getaddrinfo(NULL, NULL, &hints, &ai)) != 0) - return; - - for (current= ai; current != NULL; current = current->ai_next) { - if (current->ai_family != AF_INET && current->ai_family != AF_INET6) - continue; - - char *addr = new char[INET6_ADDRSTRLEN]; - inet_ntop(current->ai_family, current->ai_addr, addr, INET6_ADDRSTRLEN); - result->push_back(addr); - } - freeaddrinfo(ai); -#else const hostent* addrs = gethostbyname(0); if (addrs == 0) throw rdr::SystemException("gethostbyname", errorNumber); @@ -602,7 +460,6 @@ void TcpListener::getMyAddresses(std::list* result) { strcpy(addr, addrC); result->push_back(addr); } -#endif /* defined(HAVE_GETADDRINFO) && defined(HAVE_INET_PTON) */ } int TcpListener::getMyPort() { @@ -636,15 +493,6 @@ bool TcpFilter::verifyConnection(Socket* s) { rfb::CharArray name; -#ifdef HAVE_GETADDRINFO - vnc_sockaddr_t sa; - socklen_t sa_size = sizeof(sa); - if (getpeername(s->getFd(), &sa.u.sa, &sa_size) != 0 || - sa.u.sa.sa_family != AF_INET) - /* Matching only works for IPv4 */ - return false; -#endif /* HAVE_GETADDRINFO */ - name.buf = s->getPeerAddress(); std::list::iterator i; for (i=filter.begin(); i!=filter.end(); i++) { -- cgit v1.2.3