[IPv6] Fix a bug in the IPv6 listen behavior

The socket bind address should either be localhost or anyaddress. This bug in the listen behavior was preventing lldb-server from opening sockets for non-localhost connections.

The added test verifies that opening an anyaddress socket works and has a non-zero port assignment.

This should resolve PR34183.

llvm-svn: 312008
This commit is contained in:
Chris Bieneman 2017-08-29 16:13:41 +00:00
parent 734d8548ee
commit c6f6aa441b
2 changed files with 18 additions and 2 deletions

View File

@ -198,9 +198,14 @@ Status TCPSocket::Listen(llvm::StringRef name, int backlog) {
::setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, option_value_p,
sizeof(option_value));
address.SetPort(port);
SocketAddress listen_address = address;
if(!listen_address.IsLocalhost())
listen_address.SetToAnyAddress(address.GetFamily(), port);
else
listen_address.SetPort(port);
int err = ::bind(fd, &address.sockaddr(), address.GetLength());
int err =
::bind(fd, &listen_address.sockaddr(), listen_address.GetLength());
if (-1 != err)
err = ::listen(fd, backlog);

View File

@ -220,3 +220,14 @@ TEST_F(SocketTest, UDPConnect) {
EXPECT_TRUE(error.Success());
EXPECT_TRUE(socket_up->IsValid());
}
TEST_F(SocketTest, TCPListen0GetPort) {
Socket *server_socket;
Predicate<uint16_t> port_predicate;
port_predicate.SetValue(0, eBroadcastNever);
Status err =
Socket::TcpListen("10.10.12.3:0", false, server_socket, &port_predicate);
std::unique_ptr<TCPSocket> socket_up((TCPSocket*)server_socket);
EXPECT_TRUE(socket_up->IsValid());
EXPECT_NE(socket_up->GetLocalPortNumber(), 0);
}