From fb63815a0410ca6ffbe6a45c3727a996c72b0c7b Mon Sep 17 00:00:00 2001 From: klzgrad Date: Sat, 13 Aug 2022 09:53:48 +0800 Subject: [PATCH] Support IPv6 in redir:// proxying --- src/net/tools/naive/naive_connection.cc | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/net/tools/naive/naive_connection.cc b/src/net/tools/naive/naive_connection.cc index e66988d500..00f612e163 100644 --- a/src/net/tools/naive/naive_connection.cc +++ b/src/net/tools/naive/naive_connection.cc @@ -220,10 +220,20 @@ int NaiveConnection::DoConnectServer() { #if defined(OS_LINUX) const auto* socket = static_cast(client_socket_.get()); + IPEndPoint local_address; + int rv; + rv = socket->GetLocalAddress(&local_address); + if (rv != OK) { + LOG(ERROR) << "Connection " << id_ << " cannot get local address"; + return rv; + } int sd = socket->SocketDescriptorForTesting(); SockaddrStorage dst; - int rv; - rv = getsockopt(sd, SOL_IP, SO_ORIGINAL_DST, dst.addr, &dst.addr_len); + if (local_address.GetFamily() == ADDRESS_FAMILY_IPV4) { + rv = getsockopt(sd, SOL_IP, SO_ORIGINAL_DST, dst.addr, &dst.addr_len); + } else { + rv = getsockopt(sd, SOL_IPV6, SO_ORIGINAL_DST, dst.addr, &dst.addr_len); + } if (rv == 0) { IPEndPoint ipe; if (ipe.FromSockAddr(dst.addr, dst.addr_len)) {